chiark / gitweb /
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)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +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/test/test-fd-util.c

index ed82cd6283cf4463b86d5c19cdb3f253fcc5e2bb..efe974ee228ff6825122542a695d43dc765f90b4 100644 (file)
@@ -934,3 +934,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 5d52e3b19b048803837f764a888f1b2f42fc274e..7f7ac89011bdc248a2d77719de805f293377137c 100644 (file)
@@ -112,3 +112,5 @@ static inline int make_null_stdio(void) {
         })
 
 int fd_reopen(int fd, int flags);
+
+int read_nr_open(void);
index edcb8ab7510d5dd4a850bf38b707fa5b1869b548..d50810c01782c4a3b888ceafe32e8ce17abf4317 100644 (file)
@@ -317,7 +317,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();
 #if 0 /// UNNEEDED by elogind
@@ -328,6 +335,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;
 }