-
Notifications
You must be signed in to change notification settings - Fork 26
/
proc.c
210 lines (190 loc) · 4.11 KB
/
proc.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* proc.c -- process control system calls ($Revision: 1.2 $) */
#include "es.h"
Boolean hasforked = FALSE;
typedef struct Proc Proc;
struct Proc {
int pid;
Boolean background;
Proc *next, *prev;
};
static Proc *proclist = NULL;
static int ttyfd = -1;
static pid_t espgid;
#if JOB_PROTECT
static pid_t tcpgid0;
#endif
/* mkproc -- create a Proc structure */
extern Proc *mkproc(int pid, Boolean background) {
Proc *proc = ealloc(sizeof (Proc));
proc->next = proclist;
proc->pid = pid;
proc->background = background;
proc->prev = NULL;
return proc;
}
/* efork -- fork (if necessary) and clean up as appropriate */
extern int efork(Boolean parent, Boolean background) {
if (parent) {
int pid = fork();
switch (pid) {
default: { /* parent */
Proc *proc = mkproc(pid, background);
if (proclist != NULL)
proclist->prev = proc;
proclist = proc;
return pid;
}
case 0: /* child */
while (proclist != NULL) {
Proc *p = proclist;
proclist = proclist->next;
efree(p);
}
hasforked = TRUE;
#if JOB_PROTECT
tcpgid0 = 0;
#endif
break;
case -1:
fail("es:efork", "fork: %s", esstrerror(errno));
}
}
closefds();
setsigdefaults();
newchildcatcher();
return 0;
}
extern pid_t spgrp(pid_t pgid) {
pid_t old = getpgrp();
setpgid(0, pgid);
espgid = pgid;
return old;
}
static int tcspgrp(pid_t pgid) {
int e = 0;
Sigeffect tstp, ttin, ttou;
if (ttyfd < 0)
return ENOTTY;
tstp = esignal(SIGTSTP, sig_ignore);
ttin = esignal(SIGTTIN, sig_ignore);
ttou = esignal(SIGTTOU, sig_ignore);
if (tcsetpgrp(ttyfd, pgid) != 0)
e = errno;
esignal(SIGTSTP, tstp);
esignal(SIGTTIN, ttin);
esignal(SIGTTOU, ttou);
return e;
}
extern int tctakepgrp(void) {
pid_t tcpgid = 0;
if (ttyfd < 0)
return ENOTTY;
tcpgid = tcgetpgrp(ttyfd);
if (espgid == 0 || tcpgid == espgid)
return 0;
return tcspgrp(espgid);
}
extern void initpgrp(void) {
espgid = getpgrp();
ttyfd = opentty();
#if JOB_PROTECT
if (ttyfd >= 0)
tcpgid0 = tcgetpgrp(ttyfd);
#endif
}
#if JOB_PROTECT
extern void tcreturnpgrp(void) {
if (tcpgid0 != 0 && ttyfd >= 0 && tcpgid0 != tcgetpgrp(ttyfd))
tcspgrp(tcpgid0);
}
extern Noreturn esexit(int code) {
tcreturnpgrp();
exit(code);
}
#endif
/* dowait -- a waitpid wrapper that interfaces with signals */
static int dowait(int pid, int *statusp) {
int n;
interrupted = FALSE;
if (!setjmp(slowlabel)) {
slow = TRUE;
n = interrupted ? -2 :
waitpid(pid, statusp, 0);
} else
n = -2;
slow = FALSE;
if (n == -2) {
errno = EINTR;
n = -1;
}
return n;
}
/* reap -- mark a process as dead and return it */
static Proc *reap(int pid) {
Proc *proc;
for (proc = proclist; proc != NULL; proc = proc->next)
if (proc->pid == pid)
break;
assert(proc != NULL);
if (proc->next != NULL)
proc->next->prev = proc->prev;
if (proc->prev != NULL)
proc->prev->next = proc->next;
else
proclist = proc->next;
return proc;
}
/* ewait -- wait for a specific process to die, or any process if pid == -1 */
extern int ewait(int pidarg, Boolean interruptible) {
int deadpid, status;
Proc *proc;
while ((deadpid = dowait(pidarg, &status)) == -1) {
if (errno == ECHILD && pidarg > 0)
fail("es:ewait", "wait: %d is not a child of this shell", pidarg);
else if (errno != EINTR)
fail("es:ewait", "wait: %s", esstrerror(errno));
if (interruptible)
SIGCHK();
}
proc = reap(deadpid);
#if JOB_PROTECT
tctakepgrp();
#endif
if (proc->background)
printstatus(proc->pid, status);
efree(proc);
return status;
}
#include "prim.h"
PRIM(apids) {
Proc *p;
Ref(List *, lp, NULL);
for (p = proclist; p != NULL; p = p->next)
if (p->background) {
Term *t = mkstr(str("%d", p->pid));
lp = mklist(t, lp);
}
/* TODO: sort the return value, but by number? */
RefReturn(lp);
}
PRIM(wait) {
int pid;
if (list == NULL)
pid = -1;
else if (list->next == NULL) {
pid = atoi(getstr(list->term));
if (pid <= 0) {
fail("$&wait", "wait: %d: bad pid", pid);
NOTREACHED;
}
} else {
fail("$&wait", "usage: wait [pid]");
NOTREACHED;
}
return mklist(mkstr(mkstatus(ewait(pid, TRUE))), NULL);
}
extern Dict *initprims_proc(Dict *primdict) {
X(apids);
X(wait);
return primdict;
}