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
9 #ifdef __LITEOS_A__
__setrlimit(int resource,const struct rlimit * rlim)10 static int __setrlimit(int resource, const struct rlimit *rlim)
11 {
12 unsigned long long k_rlim[2];
13 struct rlimit tmp;
14 if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) {
15 tmp = *rlim;
16 FIX(tmp.rlim_cur);
17 FIX(tmp.rlim_max);
18 rlim = &tmp;
19 }
20
21 k_rlim[0] = rlim->rlim_cur;
22 k_rlim[1] = rlim->rlim_max;
23 return __syscall(SYS_setrlimit, resource, k_rlim);
24 }
25 #endif
26
27 struct ctx {
28 #ifdef __LITEOS_A__
29 const struct rlimit *rlim;
30 #else
31 unsigned long lim[2];
32 #endif
33 int res;
34 int err;
35 };
36
do_setrlimit(void * p)37 static void do_setrlimit(void *p)
38 {
39 struct ctx *c = p;
40 if (c->err>0) return;
41 #ifdef __LITEOS_A__
42 c->err = -__setrlimit(c->res, c->rlim);
43 #else
44 c->err = -__syscall(SYS_setrlimit, c->res, c->lim);
45 #endif
46 }
47
setrlimit(int resource,const struct rlimit * rlim)48 int setrlimit(int resource, const struct rlimit *rlim)
49 {
50 #ifdef __LITEOS_A__
51 struct ctx c = { .res = resource, .rlim = rlim, .err = -1 };
52 #else
53 struct rlimit tmp;
54 if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) {
55 tmp = *rlim;
56 FIX(tmp.rlim_cur);
57 FIX(tmp.rlim_max);
58 rlim = &tmp;
59 }
60 int ret = __syscall(SYS_prlimit64, 0, resource, rlim, 0);
61 if (ret != -ENOSYS) return __syscall_ret(ret);
62
63 struct ctx c = {
64 .lim[0] = MIN(rlim->rlim_cur, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
65 .lim[1] = MIN(rlim->rlim_max, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
66 .res = resource, .err = -1
67 };
68 #endif
69 __synccall(do_setrlimit, &c);
70 if (c.err) {
71 if (c.err>0) errno = c.err;
72 return -1;
73 }
74 return 0;
75 }
76
77 weak_alias(setrlimit, setrlimit64);
78