~emersion/mrsh

mrsh/builtin/wait.c -rw-r--r-- 2.6 KiB View raw
7105405aBenjamin Lowry Makefile: remove getopt.h from public_includes a day ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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[]) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	int npids = argc - 1;
	if (npids == 0) {
		npids = priv->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 < priv->processes.len; ++j) {
			struct mrsh_process *process = priv->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] == '%') {
				struct mrsh_job *job = job_by_id(state, argv[i], true);
				if (!job) {
					goto failure;
				}
				pids[i - 1].pid = job->pgid;
				pids[i - 1].status = -1;
			} else {
				char *endptr;
				pid_t pid = (pid_t)strtol(argv[i], &endptr, 10);
				if (*endptr != '\0' || argv[i][0] == '\0') {
					fprintf(stderr, "wait: error parsing pid '%s'", argv[i]);
					goto failure;
				}
				if (pid <= 0) {
					fprintf(stderr, "wait: invalid process ID\n");
					goto 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 < priv->processes.len; ++j) {
					struct mrsh_process *process = priv->processes.data[j];
					if (process->pid == pid) {
						if (process->terminated) {
							pids[i - 1].status = process->stat;
						}
						found = true;
						break;
					}
				}
				if (!found) {
					/* Unknown pids are assumed to have exited 127 */
					pids[i - 1].status = 127;
				}
			}
		}
	}

	for (int i = 0; i < npids; ++i) {
		int stat;
		pid_t waited = waitpid(pids[i].pid, &stat, 0);
		// TODO: update jobs internal state?
		if (waited == -1) {
			if (errno == ECHILD) {
				continue;
			}
			perror("wait");
			goto failure;
		}
		update_process(state, waited, stat);
		if (WIFEXITED(stat)) {
			pids[i].status = WEXITSTATUS(stat);
		} else {
			pids[i].status = 129;
		}
	}

	int status;
	if (argc == 1) {
		status = EXIT_SUCCESS;
	} else {
		status = pids[npids - 1].status;
	}

	free(pids);
	return status;

failure:
	free(pids);
	return EXIT_FAILURE;
}