1 #include <sys/resource.h>
2 #include <errno.h>
3 #include "syscall.h"
4 #include "libc.h"
5
6 #define MIN(a, b) ((a)<(b) ? (a) : (b))
7 #define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0)
8
__setrlimit(int resource,const struct rlimit * rlim)9 static int __setrlimit(int resource, const struct rlimit *rlim)
10 {
11 unsigned long long k_rlim[2];
12 struct rlimit tmp;
13 if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) {
14 tmp = *rlim;
15 FIX(tmp.rlim_cur);
16 FIX(tmp.rlim_max);
17 rlim = &tmp;
18 }
19
20 k_rlim[0] = rlim->rlim_cur;
21 k_rlim[1] = rlim->rlim_max;
22 return __syscall(SYS_setrlimit, resource, k_rlim);
23 }
24
25 struct ctx {
26 const struct rlimit *rlim;
27 int res;
28 int err;
29 };
30
do_setrlimit(void * p)31 static void do_setrlimit(void *p)
32 {
33 struct ctx *c = p;
34 if (c->err>0) return;
35 c->err = -__setrlimit(c->res, c->rlim);
36 }
37
setrlimit(int resource,const struct rlimit * rlim)38 int setrlimit(int resource, const struct rlimit *rlim)
39 {
40 struct ctx c = { .res = resource, .rlim = rlim, .err = -1 };
41 __synccall(do_setrlimit, &c);
42 if (c.err) {
43 if (c.err>0) errno = c.err;
44 return -1;
45 }
46 return 0;
47 }
48
49 weak_alias(setrlimit, setrlimit64);
50