From 99ab6fdf8c68bbe05a127b59ae03d2156d6ea856 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 1 Oct 2018 17:38:48 +0200 Subject: [PATCH] core: add a new call for bumping RLIMIT_NOFILE to "high" values Following discussions with some kernel folks at All Systems Go! it appears that file descriptors are not really as expensive as they used to be (both memory and performance-wise) and it should thus be OK to allow programs (including unprivileged ones) to have more of them without ill effects. Unfortunately we can't just raise the RLIMIT_NOFILE soft limit globally for all processes, as select() and friends can't handle fds >= 1024, and thus unexpecting programs might fail if they accidently get an fd outside of that range. We can however raise the hard limit, so that programs that need a lot of fds can opt-in into getting fds beyond the 1024 boundary, simply by bumping the soft limit to the now higher hard limit. This is useful for all our client code that accesses the journal, as the journal merging logic might need a lot of fds. Let's add a unified function for bumping the limit in a robust way. --- src/basic/rlimit-util.c | 22 ++++++++++++++++++++++ src/basic/rlimit-util.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c index be1ba61..d63b858 100644 --- a/src/basic/rlimit-util.c +++ b/src/basic/rlimit-util.c @@ -5,6 +5,7 @@ #include "alloc-util.h" #include "extract-word.h" +#include "fd-util.h" #include "format-util.h" #include "macro.h" #include "missing.h" @@ -360,3 +361,24 @@ void rlimit_free_all(struct rlimit **rl) { for (i = 0; i < _RLIMIT_MAX; i++) rl[i] = mfree(rl[i]); } + +int rlimit_nofile_bump(int limit) { + int r; + + /* Bumps the (soft) RLIMIT_NOFILE resource limit as close as possible to the specified limit. If a negative + * limit is specified, bumps it to the maximum the kernel and the hard resource limit allows. This call should + * be used by all our programs that might need a lot of fds, and that know how to deal with high fd numbers + * (i.e. do not use select() — which chokes on fds >= 1024) */ + + if (limit < 0) + limit = read_nr_open(); + + if (limit < 3) + limit = 3; + + r = setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(limit)); + if (r < 0) + return log_debug_errno(r, "Failed to set RLIMIT_NOFILE: %m"); + + return 0; +} diff --git a/src/basic/rlimit-util.h b/src/basic/rlimit-util.h index c2ea6f8..6139af3 100644 --- a/src/basic/rlimit-util.h +++ b/src/basic/rlimit-util.h @@ -20,3 +20,5 @@ int rlimit_format(const struct rlimit *rl, char **ret); void rlimit_free_all(struct rlimit **rl); #define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim }) + +int rlimit_nofile_bump(int limit); -- 2.7.4