loongarch64: add new syscall numbers
[musl.git] / src / misc / setrlimit.c
blobedb413fa78e47f158b816a99a30e84d22f9d9d03
1 #include <sys/resource.h>
2 #include <errno.h>
3 #include "syscall.h"
4 #include "libc.h"
6 #define MIN(a, b) ((a)<(b) ? (a) : (b))
7 #define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0)
9 struct ctx {
10 unsigned long lim[2];
11 int res;
12 int err;
15 #ifdef SYS_setrlimit
16 static void do_setrlimit(void *p)
18 struct ctx *c = p;
19 if (c->err>0) return;
20 c->err = -__syscall(SYS_setrlimit, c->res, c->lim);
22 #endif
24 int setrlimit(int resource, const struct rlimit *rlim)
26 struct rlimit tmp;
27 if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) {
28 tmp = *rlim;
29 FIX(tmp.rlim_cur);
30 FIX(tmp.rlim_max);
31 rlim = &tmp;
33 int ret = __syscall(SYS_prlimit64, 0, resource, rlim, 0);
34 #ifdef SYS_setrlimit
35 if (ret != -ENOSYS) return __syscall_ret(ret);
37 struct ctx c = {
38 .lim[0] = MIN(rlim->rlim_cur, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
39 .lim[1] = MIN(rlim->rlim_max, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
40 .res = resource, .err = -1
42 __synccall(do_setrlimit, &c);
43 if (c.err) {
44 if (c.err>0) errno = c.err;
45 return -1;
47 return 0;
48 #else
49 return __syscall_ret(ret);
50 #endif