1 #include <sys/resource.h>
2 #include <string.h>
3 #include <errno.h>
4 #include "syscall.h"
5 
getrusage(int who,struct rusage * ru)6 int getrusage(int who, struct rusage *ru)
7 {
8 	int r;
9 #ifdef SYS_getrusage_time64
10 	long long kru64[18];
11 	r = __syscall(SYS_getrusage_time64, who, kru64);
12 	if (!r) {
13 		ru->ru_utime = (struct timeval)
14 			{ .tv_sec = kru64[0], .tv_usec = kru64[1] };
15 		ru->ru_stime = (struct timeval)
16 			{ .tv_sec = kru64[2], .tv_usec = kru64[3] };
17 		char *slots = (char *)&ru->ru_maxrss;
18 		for (int i=0; i<14; i++)
19 			*(long *)(slots + i*sizeof(long)) = kru64[4+i];
20 	}
21 	if (SYS_getrusage_time64 == SYS_getrusage || r != -ENOSYS)
22 		return __syscall_ret(r);
23 #endif
24 	char *dest = (char *)&ru->ru_maxrss - 4*sizeof(long);
25 	r = __syscall(SYS_getrusage, who, dest);
26 	if (!r && sizeof(time_t) > sizeof(long)) {
27 		long kru[4];
28 		memcpy(kru, dest, 4*sizeof(long));
29 		ru->ru_utime = (struct timeval)
30 			{ .tv_sec = kru[0], .tv_usec = kru[1] };
31 		ru->ru_stime = (struct timeval)
32 			{ .tv_sec = kru[2], .tv_usec = kru[3] };
33 	}
34 	return __syscall_ret(r);
35 }
36