X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbasic%2Ffd-util.c;h=13b025f4407ea6d8fe3df72273d2562b78bb42ae;hb=f082899f180431f04553e4ee3b0968020ef59188;hp=17cafa4bb8b46b5f7cf952e2ed96f8b1dfa9954a;hpb=c5729796b4ec5e5f4800bfe6bd9ceb5c2da81fbe;p=elogind.git diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 17cafa4bb..13b025f44 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - /*** This file is part of systemd. @@ -19,9 +17,18 @@ along with systemd; If not, see . ***/ -#include "dirent-util.h" +#include +#include +#include +#include +#include +#include + #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" @@ -179,6 +186,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; @@ -221,12 +234,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; @@ -352,4 +362,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