-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprocesses.c
More file actions
444 lines (399 loc) · 8.82 KB
/
processes.c
File metadata and controls
444 lines (399 loc) · 8.82 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017-2019 Giacomo Tesio <giacomo@tesio.it>
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <lib9.h>
#include <posix.h>
#include "internal.h"
extern char **environ;
WaitList **__libposix_wait_list;
ChildList **__libposix_child_list;
static PosixExitStatusTranslator __libposix_exit_status_translator;
static int __libposix_wnohang;
struct timeval {
unsigned int tv_sec;
unsigned int tv_usec;
};
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
struct timeval ru_etime; /* real elapsed time */
};
#define __POSIX_SIGNAL_PREFIX_LEN (sizeof(__POSIX_SIGNAL_PREFIX)-1)
static int
fork_without_sigchld(int *errnop)
{
int pid = fork();
if(pid == 0)
__libposix_setup_new_process();
return pid;
}
int (*__libposix_fork)(int *errnop) = fork_without_sigchld;
void
__libposix_free_wait_list(void)
{
WaitList *tail, *w;
/* free the wait list as the memory is NOT shared */
tail = *__libposix_wait_list;
while(w = tail){
tail = tail->next;
free(w);
}
*__libposix_wait_list = nil;
}
void
__libposix_free_child_list(void)
{
ChildList *tail, *c;
/* free the wait list as the memory is shared */
tail = *__libposix_child_list;
while(c = tail){
tail = tail->next;
free(c);
}
*__libposix_child_list = nil;
}
int
__libposix_is_child(int pid)
{
ChildList *l;
/* free the wait list as the memory is shared */
l = *__libposix_child_list;
while(l != nil){
if(l->pid == pid)
return 1;
l = l->next;
}
return 0;
}
void
__libposix_forget_child(int pid)
{
ChildList **l, *c;
/* free the wait list as the memory is shared */
l = __libposix_child_list;
while(c = *l){
if(c->pid == pid){
*l = c->next;
free(c);
return;
}
l = &c->next;
}
}
void
__libposix_setup_new_process(void)
{
extern PosixSignalMask *__libposix_signal_mask;
*__libposix_pid = getpid();
/* reset wait list for the child */
__libposix_free_wait_list();
__libposix_free_child_list();
/* clear pending signals; reload mask */
__libposix_reset_pending_signals();
__libposix_sighelper_open();
*__libposix_signal_mask = (PosixSignalMask)__libposix_sighelper_cmd(PHGetProcMask, 0);
}
void
POSIX_exit(int code)
{
char buf[64], *s;
__libposix_free_wait_list();
if(__libposix_exit_status_translator != nil
&&(s = __libposix_exit_status_translator(code)) != nil){
snprint(buf, sizeof(buf), "%s", s);
} else {
if(code == 0)
exits(nil);
snprint(buf, sizeof(buf), __POSIX_EXIT_PREFIX "%d", code);
}
exits(buf);
}
int
POSIX_getrusage(int *errnop, PosixRUsages who, void *r_usagep)
{
int32_t t[4];
struct rusage *r_usage = r_usagep;
times(&t[0]);
switch(who){
case PosixRUsageSelf:
r_usage->ru_utime.tv_sec = t[0]/1000;
r_usage->ru_utime.tv_sec = (t[0]%1000)*1000;
r_usage->ru_stime.tv_sec = t[1]/1000;
r_usage->ru_stime.tv_sec = (t[1]%1000)*1000;
return 0;
case PosixRUsageChildren:
r_usage->ru_utime.tv_sec = t[2]/1000;
r_usage->ru_utime.tv_sec = (t[2]%1000)*1000;
r_usage->ru_stime.tv_sec = t[3]/1000;
r_usage->ru_stime.tv_sec = (t[3]%1000)*1000;
return 0;
default:
*errnop = __libposix_get_errno(PosixEINVAL);
return -1;
}
}
int
POSIX_execve(int *errnop, const char *name, char * const*argv, char * const*env)
{
// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
if(env == environ){
/* just get a copy of the current environment */
sys_rfork(RFENVG);
} else {
sys_rfork(RFCENVG);
__libposix_setup_exec_environment(env);
}
__libposix_free_wait_list();
__libposix_close_on_exec();
__libposix_sighelper_cmd(PHCallingExec, 0);
sys_exec(name, (const char**)argv);
*errnop = __libposix_translate_errstr((uintptr_t)POSIX_execve);
return -1;
}
int
POSIX_getpid(int *errnop)
{
return *__libposix_pid;
}
int
POSIX_getppid(int *errnop)
{
return getppid();
}
int
POSIX_fork(int *errnop)
{
return __libposix_fork(errnop);
}
static int
__libposix_wait(int *errnop, int *status, long ms)
{
Waitmsg *w;
WaitList *l;
char *s, err[ERRMAX];
int ret = 0, sig = 0, pid;
long wakeup = 0;
l = *__libposix_wait_list;
if(l != nil){
*__libposix_wait_list = l->next;
if(status != nil)
*status = l->status;
pid = l->pid;
free(l);
return pid;
}
OnIgnoredSignalInterrupt:
if(ms)
wakeup = sys_awake(ms);
w = wait();
if(w == nil){
rerrstr(err, ERRMAX);
if(strstr(err, "no living children") != nil){
*errnop = __libposix_get_errno(PosixECHILD);
return -1;
}
if(__libposix_restart_syscall()){
if(wakeup)
forgivewkp(wakeup);
goto OnIgnoredSignalInterrupt;
}
if(wakeup){
if(awakened(wakeup))
return 0;
forgivewkp(wakeup);
}
*errnop = __libposix_get_errno(PosixECHILD);
return -1;
}
pid = w->pid;
__libposix_forget_child(pid);
if(w->msg[0] != 0){
s = strstr(w->msg, __POSIX_EXIT_PREFIX);
if(s){
s += (sizeof(__POSIX_EXIT_PREFIX)/sizeof(char) - 1);
ret = atoi(s);
} else {
s = strstr(w->msg, __POSIX_EXIT_SIGNAL_PREFIX);
if(s){
s += (sizeof(__POSIX_EXIT_SIGNAL_PREFIX)/sizeof(char) - 1);
sig = atoi(s);
} else {
/* TODO: setup configurable interpretations */
ret = 127;
}
}
}
if(status != nil){
if(sig == 0)
*status = ret << 8;
else
*status = sig;
}
free(w);
return pid;
}
int
POSIX_umask(int *errnop, int mask)
{
return 0;
}
int
POSIX_wait(int *errnop, int *status)
{
return __libposix_wait(errnop, status, 0);
}
int
POSIX_waitpid(int *errnop, int reqpid, int *status, int options)
{
Waitmsg *w;
WaitList *c, **nl;
char *s, err[ERRMAX];
int ret = 0, sig = 0, pid;
long nohang = 0;
if(options & __libposix_wnohang){
nohang = 1;
}
// else if(options != 0){
// /* WARNING: WCONTINUED and WUNTRACED are not supported */
// *errnop = __libposix_get_errno(PosixEINVAL);
// return -1;
// }
switch(reqpid){
case -1:
if(nohang){
return __libposix_wait(errnop, status, 100);
}
return __libposix_wait(errnop, status, 0);
case 0:
/* not yet implemented; requires changes to Waitmsg */
*errnop = __libposix_get_errno(PosixEINVAL);
return -1;
default:
if(reqpid < -1){
/* not yet implemented; requires changes to Waitmsg */
*errnop = __libposix_get_errno(PosixEINVAL);
return -1;
}
break;
}
nl = __libposix_wait_list;
c = *nl;
while(c != nil){
if(c->pid == reqpid){
if(status != nil)
*status = c->status;
*nl = c->next;
free(c);
return reqpid;
}
nl = &c->next;
c = *nl;
}
WaitAgain:
OnIgnoredSignalInterrupt:
if(nohang)
nohang = sys_awake(100);
w = wait();
if(w == nil){
rerrstr(err, ERRMAX);
if(strstr(err, "no living children") != nil){
*errnop = __libposix_get_errno(PosixECHILD);
return -1;
}
if(__libposix_restart_syscall()){
if(nohang)
forgivewkp(nohang);
goto OnIgnoredSignalInterrupt;
}
if(nohang){
if(awakened(nohang))
return 0;
forgivewkp(nohang);
}
*errnop = __libposix_get_errno(PosixECHILD);
return -1;
}
if(nohang)
forgivewkp(nohang);
pid = w->pid;
__libposix_forget_child(pid);
if(w->msg[0] != 0){
s = strstr(w->msg, __POSIX_EXIT_PREFIX);
if(s){
s += (sizeof(__POSIX_EXIT_PREFIX)/sizeof(char) - 1);
ret = atoi(s);
} else {
s = strstr(w->msg, __POSIX_EXIT_SIGNAL_PREFIX);
if(s){
s += (sizeof(__POSIX_EXIT_SIGNAL_PREFIX)/sizeof(char) - 1);
sig = atoi(s);
} else {
/* TODO: setup configurable interpretations */
ret = 127;
}
}
}
if(pid == reqpid){
if(status != nil){
if(sig == 0)
*status = ret << 8;
else
*status = sig;
}
return reqpid;
}
c = malloc(sizeof(WaitList));
c->next = nil;
c->pid = pid;
if(sig == 0)
c->status= ret << 8;
else
c->status = sig;
*nl = c;
if(!nohang)
goto WaitAgain;
*errnop = __libposix_get_errno(PosixECHILD);
return -1;
}
int
libposix_translate_exit_status(PosixExitStatusTranslator translator)
{
if(__libposix_initialized())
return 0;
if(translator == nil)
return 0;
__libposix_exit_status_translator = translator;
return 1;
}
int
libposix_set_wait_options(int wcontinued, int wnohang, int wuntraced)
{
if(wcontinued != 0)
sysfatal("libposix: unsupported WCONTINUED");
if(wuntraced != 0)
sysfatal("libposix: unsupported WUNTRACED");
__libposix_wnohang = wnohang;
return 1;
}
void
__libposix_processes_check_conf(void)
{
if(__libposix_wnohang == 0)
sysfatal("libposix: WNOHANG is undefined");
/* __libposix_exit_status_translator is optional */
}