summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-06-05 15:21:47 +0200
committerSven Eden <yamakuzure@gmx.net>2018-08-24 16:47:08 +0200
commit83e6d192cbe160dbea62427bab0f6f7d490ce215 (patch)
tree80c85bdcf9d4a8a10cbabdb0e82bf5108b213e76 /src/basic
parent9f900051e499dbedb1aae2a0f81c4a295d2069ae (diff)
main: split out reading of /proc/sys/fs/nr_open into its own function
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.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/fd-util.c24
-rw-r--r--src/basic/fd-util.h2
2 files changed, 26 insertions, 0 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
index ed82cd628..efe974ee2 100644
--- a/src/basic/fd-util.c
+++ b/src/basic/fd-util.c
@@ -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;
+}
diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h
index 5d52e3b19..7f7ac8901 100644
--- a/src/basic/fd-util.h
+++ b/src/basic/fd-util.h
@@ -112,3 +112,5 @@ static inline int make_null_stdio(void) {
})
int fd_reopen(int fd, int flags);
+
+int read_nr_open(void);