mirror of
https://github.com/systemd/systemd.git
synced 2026-08-04 23:20:32 +00:00
Merge pull request #10244 from poettering/nofile-bump
bump RLIMIT_NOFILE
This commit is contained in:
@@ -75,3 +75,5 @@
|
||||
_CONF_PATHS_SPLIT_USR(n))
|
||||
|
||||
#define LONG_LINE_MAX (1U*1024U*1024U)
|
||||
|
||||
#define HIGH_RLIMIT_MEMLOCK (1024ULL*1024ULL*64ULL)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user