-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwait4.c
51 lines (43 loc) · 1.2 KB
/
wait4.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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/errno.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/fail.h>
int caml_rev_convert_signal_number (int);
static value float_of_timeval(struct timeval t)
{
double d = t.tv_sec;
d *= 1000000;
d += t.tv_usec;
d /= 1000000;
return caml_copy_double(d);
}
value ml_wait4(value pid)
{
CAMLparam1(pid);
CAMLlocal2(st, usg);
int wstatus;
struct rusage usage;
if (wait4(Long_val(pid), &wstatus, 0, &usage) < 0) {
caml_failwith("wait4 failed");
}
if (WIFEXITED(wstatus)) {
st = caml_alloc_small(1, 0);
Field(st, 0) = Val_int(WEXITSTATUS(wstatus));
} else if (WIFSTOPPED(wstatus)) {
caml_failwith("WSTOPPED without WUNTRACED?");
} else {
st = caml_alloc_small(1, 1);
Field(st, 0) = Val_int(caml_rev_convert_signal_number(WTERMSIG(wstatus)));
}
usg = caml_alloc(4, 0);
caml_initialize(&Field(usg, 0), st);
caml_initialize(&Field(usg, 1), float_of_timeval(usage.ru_utime));
caml_initialize(&Field(usg, 2), float_of_timeval(usage.ru_stime));
caml_initialize(&Field(usg, 3), Val_long(usage.ru_maxrss));
CAMLreturn (usg);
}