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.
This commit is contained in:
Lennart Poettering
2018-12-01 12:50:45 +01:00
parent 09e5c07fbd
commit 1300f91149
2 changed files with 20 additions and 0 deletions
+19
View 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;
}
+1
View 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);