| 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 |
struct ctx { |
| 10 |
unsigned long lim[2]; |
| 11 |
int res; |
| 12 |
int err; |
| 13 |
}; |
| 14 |
|
| 15 |
#ifdef SYS_setrlimit |
| 16 |
static void do_setrlimit(void *p) |
| 17 |
{ |
| 18 |
struct ctx *c = p; |
| 19 |
if (c->err>0) return; |
| 20 |
c->err = -__syscall(SYS_setrlimit, c->res, c->lim); |
| 21 |
} |
| 22 |
#endif |
| 23 |
|
| 24 |
int setrlimit(int resource, const struct rlimit *rlim) |
| 25 |
{ |
| 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; |
| 32 |
} |
| 33 |
int ret = __syscall(SYS_prlimit64, 0, resource, rlim, 0); |
| 34 |
#ifdef SYS_setrlimit |
| 35 |
if (ret != -ENOSYS) return __syscall_ret(ret); |
| 36 |
|
| 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 |
| 41 |
}; |
| 42 |
__synccall(do_setrlimit, &c); |
| 43 |
if (c.err) { |
| 44 |
if (c.err>0) errno = c.err; |
| 45 |
return -1; |
| 46 |
} |
| 47 |
return 0; |
| 48 |
#else |
| 49 |
return __syscall_ret(ret); |
| 50 |
#endif |
| 51 |
} |
| 52 |
|
| 53 |
weak_alias(setrlimit, setrlimit64); |