~emersion/mrsh

ref: ac201b690420782d7e320c9060a875a2201a7ead mrsh/builtin/wait.c -rw-r--r-- 2.4 KiB
ac201b69Drew DeVault Implement 'wait' builtin 2 years 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
#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");
				goto failure;
			} 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 < state->processes.len; ++j) {
					struct process *process = state->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?
		update_process(state, waited, stat);
		if (waited == -1) {
			if (errno == ECHILD) {
				continue;
			}
			fprintf(stderr, "wait: %s\n", strerror(errno));
			goto failure;
		}
		if (WIFEXITED(stat)) {
			pids[i].status = WEXITSTATUS(stat);
		} else {
			pids[i].status = 129;
		}
	}

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

failure:
	free(pids);
	return EXIT_FAILURE;
}