-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwait.c
178 lines (164 loc) · 2.97 KB
/
wait.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "rc.h"
#include <errno.h>
#include "wait.h"
bool forked = FALSE;
typedef struct Pid Pid;
static struct Pid {
pid_t pid;
int stat;
bool alive;
bool waiting;
Pid *n;
} *plist = NULL;
extern pid_t rc_fork() {
Pid *new;
struct Pid *p, *q;
pid_t pid = fork();
switch (pid) {
case -1:
uerror("fork");
rc_error(NULL);
/* NOTREACHED */
case 0:
forked = TRUE;
sigchk();
clearflow();
p = plist; q = 0;
while (p) {
if (q) efree(q);
q = p;
p = p->n;
}
if (q) efree(q);
plist = 0;
return 0;
default:
new = enew(Pid);
new->pid = pid;
new->alive = TRUE;
new->waiting = FALSE;
new->n = plist;
plist = new;
return pid;
}
}
static int markwaiting(pid_t pid, bool clear) {
Pid *p;
int n = 0;
for (p = plist; p != NULL; p = p->n)
if (pid == -1 || p->pid == pid) {
p->waiting = TRUE;
n++;
} else if (clear)
p->waiting = FALSE;
return n;
}
static pid_t dowait(int *stat, bool nointr) {
Pid **p, *q;
pid_t pid;
for (;;) {
for (p = &plist; *p != NULL; p = &(*p)->n) {
q = *p;
if (q->waiting && !q->alive) {
pid = q->pid;
*stat = q->stat;
*p = q->n; /* remove element from list */
efree(q);
return pid;
}
}
pid = rc_wait(stat);
if (pid < 0) {
if (errno == ECHILD)
panic("lost child");
if (nointr)
continue;
else
return pid;
}
for (q = plist; q != NULL; q = q->n)
if (q->pid == pid) {
q->alive = FALSE;
q->stat = *stat;
break;
}
}
/* never reached */
return -1;
}
extern pid_t rc_wait4(pid_t pid, int *stat, bool nointr) {
if (markwaiting(pid, TRUE) == 0) {
/* Uh-oh, not there. */
errno = ECHILD; /* no children */
uerror("wait");
*stat = 0x100; /* exit(1) */
return -1;
}
return dowait(stat, nointr);
}
extern List *sgetapids() {
List *r;
Pid *p;
for (r = NULL, p = plist; p != NULL; p = p->n) {
List *q;
if (!p->alive)
continue;
q = nnew(List);
q->w = nprint("%d", p->pid);
q->m = NULL;
q->n = r;
r = q;
}
return r;
}
extern void waitforall() {
int stat;
markwaiting(-1, TRUE);
while (plist != NULL) {
pid_t pid = dowait(&stat, FALSE);
if (pid > 0)
setstatus(pid, stat);
else {
set(FALSE);
if (errno == EINTR)
return;
}
sigchk();
}
}
extern void waitfor(char **av) {
int alive, count, i, stat;
pid_t pid;
for (i = 0; av[i] != NULL; i++)
if ((pid = a2u(av[i])) < 0) {
fprint(2, RC "`%s' is a bad number\n", av[i]);
set(FALSE);
return;
} else if (markwaiting(pid, i == 0) == 0) {
fprint(2, RC "`%s' is not a child\n", av[i]);
set(FALSE);
return;
}
alive = count = i;
if (count >= MAX_PIPELINE) {
fprint(2, RC "too many arguments to wait\n");
set(FALSE);
return;
}
setpipestatuslength(count);
while (alive > 0) {
pid = dowait(&stat, FALSE);
if (pid > 0) {
alive--;
for (i = 0; av[i] != NULL; i++)
if (a2u(av[i]) == pid) {
setpipestatus(count - i - 1, pid, stat);
break;
}
} else if (errno == EINTR) {
set(FALSE);
return;
}
sigchk();
}
}