]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
main: split out reading of /proc/sys/fs/nr_open into its own function
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Jun 2018 13:21:47 +0000 (15:21 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 Jun 2018 12:39:15 +0000 (14:39 +0200)
This doesn't really reduce the code size over all, but it does make main.c
shorter and more readable, and that's always a good thing.

src/basic/fd-util.c
src/basic/fd-util.h
src/core/main.c
src/test/test-fd-util.c

index e54881ca3c4965c2d3da59aba8948209a42be6cc..e1bea94c90a31caf46086481b50c31c4da0bf752 100644 (file)
@@ -930,3 +930,27 @@ int fd_reopen(int fd, int flags) {
 
         return new_fd;
 }
+
+int read_nr_open(void) {
+        _cleanup_free_ char *nr_open = NULL;
+        int r;
+
+        /* Returns the kernel's current fd limit, either by reading it of /proc/sys if that works, or using the
+         * hard-coded default compiled-in value of current kernels (1M) if not. This call will never fail. */
+
+        r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
+        if (r < 0)
+                log_debug_errno(r, "Failed to read /proc/sys/fs/nr_open, ignoring: %m");
+        else {
+                int v;
+
+                r = safe_atoi(nr_open, &v);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open);
+                else
+                        return v;
+        }
+
+        /* If we fail, fallback to the hard-coded kernel limit of 1024 * 1024. */
+        return 1024 * 1024;
+}
index 2ae4e67ed082a43320bb56d79747ffb75dcf3f5e..b94944972ceeee9bf42037c3abf60e67961bf857 100644 (file)
@@ -108,3 +108,5 @@ static inline int make_null_stdio(void) {
         })
 
 int fd_reopen(int fd, int flags);
+
+int read_nr_open(void);
index 8c04d41c0c4537883e79a1b08b21e7b4a1d33acf..4158424dcf697fa1e5402bf03e1024960d30104c 100644 (file)
@@ -1136,10 +1136,7 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching
 }
 
 static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
-        struct rlimit nl;
-        int r;
-        int min_max;
-        _cleanup_free_ char *nr_open = NULL;
+        int r, nr;
 
         assert(saved_rlimit);
 
@@ -1160,17 +1157,9 @@ static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
                 arg_default_rlimit[RLIMIT_NOFILE] = rl;
         }
 
-        /* Get current RLIMIT_NOFILE maximum compiled into the kernel. */
-        r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
-        if (r >= 0)
-                r = safe_atoi(nr_open, &min_max);
-        /* If we fail, fallback to the hard-coded kernel limit of 1024 * 1024. */
-        if (r < 0)
-                min_max = 1024 * 1024;
-
-        /* Bump up the resource limit for ourselves substantially */
-        nl.rlim_cur = nl.rlim_max = min_max;
-        r = setrlimit_closest(RLIMIT_NOFILE, &nl);
+        /* Bump up the resource limit for ourselves substantially, all the way to the maximum the kernel allows */
+        nr = read_nr_open();
+        r = setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(nr));
         if (r < 0)
                 return log_warning_errno(r, "Setting RLIMIT_NOFILE failed, ignoring: %m");
 
index fc3c46338557423fa200115b218d2a49d6cfb7d7..b1de8aca5cfc297670ae13da2103606ab95e385c 100644 (file)
@@ -315,7 +315,14 @@ static void test_fd_duplicate_data_fd(void) {
         assert_se(read(fd2, &j, sizeof(j)) == 0);
 }
 
+static void test_read_nr_open(void) {
+        log_info("nr-open: %i", read_nr_open());
+}
+
 int main(int argc, char *argv[]) {
+
+        log_set_max_level(LOG_DEBUG);
+
         test_close_many();
         test_close_nointr();
         test_same_fd();
@@ -324,6 +331,7 @@ int main(int argc, char *argv[]) {
         test_fd_move_above_stdio();
         test_rearrange_stdio();
         test_fd_duplicate_data_fd();
+        test_read_nr_open();
 
         return 0;
 }