fix setgroups behavior in multithreaded process
[musl.git] / src / linux / wait4.c
blob83650e34a860cccaf76c0b2aa102235b4114a9ba
1 #define _GNU_SOURCE
2 #include <sys/wait.h>
3 #include <sys/resource.h>
4 #include <string.h>
5 #include <errno.h>
6 #include "syscall.h"
8 pid_t wait4(pid_t pid, int *status, int options, struct rusage *ru)
10 int r;
11 #ifdef SYS_wait4_time64
12 if (ru) {
13 long long kru64[18];
14 r = __syscall(SYS_wait4_time64, pid, status, options, kru64);
15 if (!r) {
16 ru->ru_utime = (struct timeval)
17 { .tv_sec = kru64[0], .tv_usec = kru64[1] };
18 ru->ru_stime = (struct timeval)
19 { .tv_sec = kru64[2], .tv_usec = kru64[3] };
20 char *slots = (char *)&ru->ru_maxrss;
21 for (int i=0; i<14; i++)
22 *(long *)(slots + i*sizeof(long)) = kru64[4+i];
24 if (SYS_wait4_time64 == SYS_wait4 || r != -ENOSYS)
25 return __syscall_ret(r);
27 #endif
28 char *dest = ru ? (char *)&ru->ru_maxrss - 4*sizeof(long) : 0;
29 r = __syscall(SYS_wait4, pid, status, options, dest);
30 if (r>0 && ru && sizeof(time_t) > sizeof(long)) {
31 long kru[4];
32 memcpy(kru, dest, 4*sizeof(long));
33 ru->ru_utime = (struct timeval)
34 { .tv_sec = kru[0], .tv_usec = kru[1] };
35 ru->ru_stime = (struct timeval)
36 { .tv_sec = kru[2], .tv_usec = kru[3] };
38 return __syscall_ret(r);