chiark / gitweb /
Prep v239: fd-util.[hc] - Masked fd_duplicate_data_fd() - Nowhere needed.
[elogind.git] / src / basic / fd-util.c
index 9b2fd332b484a0f84d4d6ba9ecaafe69f1ae0420..122b59a685ecd8a0c0d72d7bb0818ee736bfc726 100644 (file)
@@ -1,9 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2010 Lennart Poettering
-***/
 
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-//#include "alloc-util.h"
-//#include "copy.h"
+#include "alloc-util.h"
+#include "copy.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
-//#include "io-util.h"
+#include "io-util.h"
 #include "macro.h"
 #include "memfd-util.h"
 #include "missing.h"
@@ -575,11 +570,11 @@ try_dev_shm_without_o_tmpfile:
 /* If memfd/pipe didn't work out, then let's use a file in /tmp up to a size of 1M. If it's large than that use /var/tmp instead. */
 #define DATA_FD_TMP_LIMIT (1024U*1024U)
 
+#if 0 /// UNNEEDED by elogind
 int fd_duplicate_data_fd(int fd) {
 
         _cleanup_close_ int copy_fd = -1, tmp_fd = -1;
         _cleanup_free_ void *remains = NULL;
-        _cleanup_free_ char *t = NULL;
         size_t remains_size = 0;
         const char *td;
         struct stat st;
@@ -764,6 +759,7 @@ finish:
 
         return fd_reopen(tmp_fd, O_RDONLY|O_CLOEXEC);
 }
+#endif // 0
 
 int fd_move_above_stdio(int fd) {
         int flags, copy;
@@ -935,3 +931,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;
+}