summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/misc/setrlimit.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libc-top-half/musl/src/misc/setrlimit.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/misc/setrlimit.c b/libc-top-half/musl/src/misc/setrlimit.c
new file mode 100644
index 0000000..8340aee
--- /dev/null
+++ b/libc-top-half/musl/src/misc/setrlimit.c
@@ -0,0 +1,47 @@
+#include <sys/resource.h>
+#include <errno.h>
+#include "syscall.h"
+#include "libc.h"
+
+#define MIN(a, b) ((a)<(b) ? (a) : (b))
+#define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0)
+
+struct ctx {
+ unsigned long lim[2];
+ int res;
+ int err;
+};
+
+static void do_setrlimit(void *p)
+{
+ struct ctx *c = p;
+ if (c->err>0) return;
+ c->err = -__syscall(SYS_setrlimit, c->res, c->lim);
+}
+
+int setrlimit(int resource, const struct rlimit *rlim)
+{
+ struct rlimit tmp;
+ if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) {
+ tmp = *rlim;
+ FIX(tmp.rlim_cur);
+ FIX(tmp.rlim_max);
+ rlim = &tmp;
+ }
+ int ret = __syscall(SYS_prlimit64, 0, resource, rlim, 0);
+ if (ret != -ENOSYS) return __syscall_ret(ret);
+
+ struct ctx c = {
+ .lim[0] = MIN(rlim->rlim_cur, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
+ .lim[1] = MIN(rlim->rlim_max, MIN(-1UL, SYSCALL_RLIM_INFINITY)),
+ .res = resource, .err = -1
+ };
+ __synccall(do_setrlimit, &c);
+ if (c.err) {
+ if (c.err>0) errno = c.err;
+ return -1;
+ }
+ return 0;
+}
+
+weak_alias(setrlimit, setrlimit64);