-
Notifications
You must be signed in to change notification settings - Fork 26
/
proc.c
175 lines (158 loc) · 3.91 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
/* proc.c -- process control system calls ($Revision: 1.2 $) */
#include "es.h"
/* TODO: the rusage code for the time builtin really needs to be cleaned up */
#if HAVE_GETRUSAGE
#include <sys/time.h>
#include <sys/resource.h>
#endif
Boolean hasforked = FALSE;
typedef struct Proc Proc;
struct Proc {
int pid;
Boolean background;
Proc *next, *prev;
};
static Proc *proclist = NULL;
/* 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 */
proclist = NULL;
hasforked = TRUE;
break;
case -1:
fail("es:efork", "fork: %s", esstrerror(errno));
}
}
closefds();
setsigdefaults();
newchildcatcher();
return 0;
}
#if HAVE_GETRUSAGE
/* This function is provided as timersub(3) on some systems, but it's simple enough
* to do ourselves. */
static void timesub(struct timeval *a, struct timeval *b, struct timeval *res) {
res->tv_sec = a->tv_sec - b->tv_sec;
res->tv_usec = a->tv_usec - b->tv_usec;
if (res->tv_usec < 0) {
res->tv_sec -= 1;
res->tv_usec += 1000000;
}
}
#endif
/* dowait -- a waitpid wrapper that gets rusage and interfaces with signals */
static int dowait(int pid, int *statusp, void UNUSED *rusagep) {
int n;
#if HAVE_GETRUSAGE
static struct rusage ru_saved;
struct rusage ru_new;
#endif
interrupted = FALSE;
if (!setjmp(slowlabel)) {
slow = TRUE;
n = interrupted ? -2 :
waitpid(pid, statusp, 0);
#if HAVE_GETRUSAGE
if (rusagep != NULL) {
struct rusage *rusage = (struct rusage *)rusagep;
if (getrusage(RUSAGE_CHILDREN, &ru_new) == -1)
fail("es:ewait", "getrusage: %s", esstrerror(errno));
timesub(&ru_new.ru_utime, &ru_saved.ru_utime, &rusage->ru_utime);
timesub(&ru_new.ru_stime, &ru_saved.ru_stime, &rusage->ru_stime);
ru_saved = ru_new;
}
#endif
} 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, void *rusage) {
int deadpid, status;
Proc *proc;
while ((deadpid = dowait(pidarg, &status, rusage)) == -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 (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))), NULL);
}
extern Dict *initprims_proc(Dict *primdict) {
X(apids);
X(wait);
return primdict;
}