chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/basic (1/6)
[elogind.git] / src / basic / fd-util.c
index 8b7065fd5d53b8ab1fb30fe11fc53bda2d6250b0..9afec93661e0e15f79475051c4de5b850d317fe6 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
 #include "dirent-util.h"
 #include "fd-util.h"
+#include "macro.h"
+#include "missing.h"
 #include "parse-util.h"
+#include "path-util.h"
 #include "socket-util.h"
 #include "util.h"
 
@@ -120,8 +128,7 @@ FILE* safe_fclose(FILE *f) {
         return NULL;
 }
 
-/// UNNEEDED by elogind
-#if 0
+#if 0 /// UNNEEDED by elogind
 DIR* safe_closedir(DIR *d) {
 
         if (d) {
@@ -180,6 +187,12 @@ int fd_cloexec(int fd, bool cloexec) {
         return 0;
 }
 
+void stdio_unset_cloexec(void) {
+        fd_cloexec(STDIN_FILENO, false);
+        fd_cloexec(STDOUT_FILENO, false);
+        fd_cloexec(STDERR_FILENO, false);
+}
+
 _pure_ static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) {
         unsigned i;
 
@@ -222,12 +235,9 @@ int close_all_fds(const int except[], unsigned n_except) {
                 return r;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 int fd = -1;
 
-                if (hidden_file(de->d_name))
-                        continue;
-
                 if (safe_atoi(de->d_name, &fd) < 0)
                         /* Let's better ignore this, just in case */
                         continue;
@@ -251,8 +261,7 @@ int close_all_fds(const int except[], unsigned n_except) {
         return r;
 }
 
-/// UNNEEDED by elogind
-#if 0
+#if 0 /// UNNEEDED by elogind
 int same_fd(int a, int b) {
         struct stat sta, stb;
         pid_t pid;
@@ -354,4 +363,18 @@ bool fdname_is_valid(const char *s) {
 
         return p - s < 256;
 }
+
+int fd_get_path(int fd, char **ret) {
+        char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
+        int r;
+
+        xsprintf(procfs_path, "/proc/self/fd/%i", fd);
+
+        r = readlink_malloc(procfs_path, ret);
+
+        if (r == -ENOENT) /* If the file doesn't exist the fd is invalid */
+                return -EBADF;
+
+        return r;
+}
 #endif // 0