diff options
author | Lennart Poettering <lennart@poettering.net> | 2012-04-12 03:38:52 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2012-04-12 12:58:19 +0200 |
commit | 68faf98ca09314b61314ad2ac0cc133c400a83f9 (patch) | |
tree | 209d19aeced8bfdf54a425f1fe205f42fa455bd8 /src/shared/util.c | |
parent | d18f337c3f2fe14240598c18415f72d0cf15393f (diff) |
execute: when we can't get the requested rlimit, get the next closest
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index a3873a9318..0b4d4d6746 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -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; +} |