~emersion/mrsh-dev

Implement 'wait' builtin v1 PROPOSED

Drew DeVault: 1
 Implement 'wait' builtin

 4 files changed, 112 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/patches/5969/mbox | git am -3
Learn more about email & git

[PATCH] Implement 'wait' builtin Export this patch

---
 builtin/builtin.c |   1 +
 builtin/wait.c    | 109 ++++++++++++++++++++++++++++++++++++++++++++++
 include/builtin.h |   1 +
 meson.build       |   1 +
 4 files changed, 112 insertions(+)
 create mode 100644 builtin/wait.c

diff --git a/builtin/builtin.c b/builtin/builtin.c
index aaeac2b..ce77af3 100644
--- a/builtin/builtin.c
@@ -38,6 +38,7 @@ static const struct builtin builtins[] = {
	{ "umask", builtin_umask, false },
	{ "unalias", builtin_unalias, false },
	{ "unset", builtin_unset, true },
	{ "wait", builtin_wait, false },
};

// The following commands are explicitly unspecified by POSIX
diff --git a/builtin/wait.c b/builtin/wait.c
new file mode 100644
index 0000000..1c610df
--- /dev/null
@@ -0,0 +1,109 @@
#define _POSIX_C_SOURCE 200112L
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include "builtin.h"
#include "shell/process.h"
#include "shell/shell.h"

struct wait_handle {
	pid_t pid;
	int status;
};

int builtin_wait(struct mrsh_state *state, int argc, char *argv[]) {
	int npids = argc - 1;
	if (npids == 0) {
		npids = state->processes.len;
	}
	struct wait_handle *pids = malloc(npids * sizeof(struct wait_handle));
	if (pids == NULL) {
		fprintf(stderr, "wait: unable to allocate pid list");
		return EXIT_FAILURE;
	}

	if (argc == 1) {
		/* All known processes */
		int _npids = 0;
		for (size_t j = 0; j < state->processes.len; ++j) {
			struct process *process = state->processes.data[j];
			if (process->terminated) {
				continue;
			}
			pids[_npids].pid = process->pid;
			pids[_npids].status = -1;
			++_npids;
		}
		npids = _npids;
	} else {
		for (int i = 1; i < argc; ++i) {
			if (argv[i][0] == '%') {
				// TODO
				fprintf(stderr, "wait: job control IDs are unimplemented\n");
				return EXIT_FAILURE;
			} else {
				char *endptr;
				pid_t pid = (pid_t)strtol(argv[i], &endptr, 10);
				if (*endptr != '\0') {
					fprintf(stderr, "wait: error parsing pid '%s: %s",
							argv[i], strerror(errno));
					return EXIT_FAILURE;
				}
				if (pid < 0) {
					fprintf(stderr, "wait: cannot wait on negative pid\n");
					return EXIT_FAILURE;
				}
				pids[i - 1].pid = pid;
				pids[i - 1].status = -1;
				/* Check if this pid is known */
				bool found = false;
				for (size_t j = 0; j < state->processes.len; ++j) {
					struct process *process = state->processes.data[j];
					if (process->pid == pid) {
						if (process->terminated) {
							pids[i].status = process->stat;
						}
						found = true;
						break;
					}
				}
				if (!found) {
					/* Unknown pids are assumed to have exited 127 */
					pids[i - 1].status = 127;
				}
			}
		}
	}

	bool loop = true;
	while (loop) {
		int stat;
		pid_t waited = waitpid((pid_t)-1, &stat, 0);
		if (waited == -1) {
			if (errno == ECHILD) {
				/* No remaining child processes */
				break;
			}
			fprintf(stderr, "wait: %s\n", strerror(errno));
			return EXIT_FAILURE;
		}
		loop = false;
		for (int i = 0; i < npids; ++i) {
			if (pids[i].pid == waited) {
				pids[i].status = WEXITSTATUS(stat);
			}
			if (pids[i].status == -1) {
				loop = true;
			}
		}
	}

	if (argc == 1) {
		return EXIT_SUCCESS;
	} else {
		return pids[npids - 1].status;
	}
}
diff --git a/include/builtin.h b/include/builtin.h
index fb1933a..1f60125 100644
--- a/include/builtin.h
+++ b/include/builtin.h
@@ -31,6 +31,7 @@ int builtin_ulimit(struct mrsh_state *state, int argc, char *argv[]);
int builtin_umask(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unalias(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unset(struct mrsh_state *state, int argc, char *argv[]);
int builtin_wait(struct mrsh_state *state, int argc, char *argv[]);

int builtin_unspecified(struct mrsh_state *state, int argc, char *argv[]);

diff --git a/meson.build b/meson.build
index e9b43e8..9c1f67a 100644
--- a/meson.build
+++ b/meson.build
@@ -97,6 +97,7 @@ lib_mrsh = library(
		'builtin/unalias.c',
		'builtin/unset.c',
		'builtin/unspecified.c',
		'builtin/wait.c',
		'getopt.c',
		'hashtable.c',
		'parser/arithm.c',
-- 
2.21.0
View this thread in the archives