Merge pull request #10244 from poettering/nofile-bump

bump RLIMIT_NOFILE
This commit is contained in:
Lennart Poettering
2018-10-17 17:59:36 +02:00
committed by GitHub
22 changed files with 246 additions and 44 deletions

View File

@@ -75,3 +75,5 @@
_CONF_PATHS_SPLIT_USR(n))
#define LONG_LINE_MAX (1U*1024U*1024U)
#define HIGH_RLIMIT_MEMLOCK (1024ULL*1024ULL*64ULL)

View File

@@ -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"
@@ -33,8 +34,15 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
if (highest.rlim_max == RLIM_INFINITY)
return -EPERM;
fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
fixed = (struct rlimit) {
.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max),
.rlim_max = MIN(rlim->rlim_max, highest.rlim_max),
};
/* Shortcut things if we wouldn't change anything. */
if (fixed.rlim_cur == highest.rlim_cur &&
fixed.rlim_max == highest.rlim_max)
return 0;
if (setrlimit(resource, &fixed) < 0)
return -errno;
@@ -360,3 +368,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;
}

View File

@@ -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);