]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
rlimit: add new rlimit_nofile_safe() helper
authorLennart Poettering <lennart@poettering.net>
Mon, 26 Nov 2018 14:52:33 +0000 (15:52 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 1 Dec 2018 11:50:45 +0000 (12:50 +0100)
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.

src/shared/rlimit-util.c
src/shared/rlimit-util.h

index c133f84b7e90e24c7dd10149d09188b11beeb371..74b3a023f18ab6c6fa218397a56f8447149bcd91 100644 (file)
@@ -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;
+}
index 6139af3ff50ff5fec8e5cf56bdbfc2719cd676e4..d4fca2b8556d9b00b3a080c02c11e64737dd8ccb 100644 (file)
@@ -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);