From: Lennart Poettering Date: Mon, 26 Nov 2018 14:52:33 +0000 (+0100) Subject: rlimit: add new rlimit_nofile_safe() helper X-Git-Tag: v240~125^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1300f91149bf8477ec1b55ea0bd2ac6010f7b807;p=thirdparty%2Fsystemd.git rlimit: add new rlimit_nofile_safe() helper This helper sets RLIMIT_NOFILE's soft limit to 1024 (FD_SETSIZE) for compatibility with apps using select(). The idea is that we use this helper to reset the limit whenever we invoke foreign code from our own processes which have bumped RLIMIT_NOFILE high. --- diff --git a/src/shared/rlimit-util.c b/src/shared/rlimit-util.c index c133f84b7e9..74b3a023f18 100644 --- a/src/shared/rlimit-util.c +++ b/src/shared/rlimit-util.c @@ -389,3 +389,22 @@ int rlimit_nofile_bump(int limit) { return 0; } + +int rlimit_nofile_safe(void) { + struct rlimit rl; + + /* Resets RLIMIT_NOFILE's soft limit FD_SETSIZE (i.e. 1024), for compatibility with software still using + * select() */ + + if (getrlimit(RLIMIT_NOFILE, &rl) < 0) + return log_debug_errno(errno, "Failed to query RLIMIT_NOFILE: %m"); + + if (rl.rlim_cur <= FD_SETSIZE) + return 0; + + rl.rlim_cur = FD_SETSIZE; + if (setrlimit(RLIMIT_NOFILE, &rl) < 0) + return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur); + + return 1; +} diff --git a/src/shared/rlimit-util.h b/src/shared/rlimit-util.h index 6139af3ff50..d4fca2b8556 100644 --- a/src/shared/rlimit-util.h +++ b/src/shared/rlimit-util.h @@ -22,3 +22,4 @@ void rlimit_free_all(struct rlimit **rl); #define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim }) int rlimit_nofile_bump(int limit); +int rlimit_nofile_safe(void);