chiark / gitweb /
execute: when we can't get the requested rlimit, get the next closest
authorLennart Poettering <lennart@poettering.net>
Thu, 12 Apr 2012 01:38:52 +0000 (03:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 12 Apr 2012 10:58:19 +0000 (12:58 +0200)
src/core/execute.c
src/shared/util.c
src/shared/util.h

index 8ae3923c5d233bf6750d6e6e2ce02ce4f4fca2de..271c57f562186974fcde25e0c76b5afb99a8c5b8 100644 (file)
@@ -1367,7 +1367,7 @@ int exec_spawn(ExecCommand *command,
                                 if (!context->rlimit[i])
                                         continue;
 
-                                if (setrlimit(i, context->rlimit[i]) < 0) {
+                                if (setrlimit_closest(i, context->rlimit[i]) < 0) {
                                         err = -errno;
                                         r = EXIT_LIMITS;
                                         goto fail_child;
index a3873a931818ec4595e5e1f538c629b5768c0439..0b4d4d6746ecda587c6a55cd0f45db36ae075352 100644 (file)
@@ -6120,3 +6120,27 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
         execv(path, l);
         _exit(EXIT_FAILURE);
 }
+
+int setrlimit_closest(int resource, const struct rlimit *rlim) {
+        struct rlimit highest, fixed;
+
+        assert(rlim);
+
+        if (setrlimit(resource, rlim) >= 0)
+                return 0;
+
+        if (errno != EPERM)
+                return -errno;
+
+        /* So we failed to set the desired setrlimit, then let's try
+         * to get as close as we can */
+        assert_se(getrlimit(resource, &highest) == 0);
+
+        fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
+        fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
+
+        if (setrlimit(resource, &fixed) < 0)
+                return -errno;
+
+        return 0;
+}
index 3f4e49e6bcf6608bddf2b39fc0dab9c1511abda6..c487b702bf6d14c96131e3434f0d54a4b7ede9ed 100644 (file)
@@ -34,6 +34,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <sys/resource.h>
 
 #include "macro.h"
 
@@ -531,4 +532,6 @@ int fd_inc_rcvbuf(int fd, size_t n);
 
 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
 
+int setrlimit_closest(int resource, const struct rlimit *rlim);
+
 #endif