diff options
Diffstat (limited to '')
-rw-r--r-- | src/daemon.c | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/daemon.c b/src/daemon.c index b47e4d2..75e0f36 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -20,10 +20,9 @@ * DEALINGS IN THE SOFTWARE. */ #include "daemon.h" -#include <stdlib.h> #include <unistd.h> -#include <errno.h> -#include <string.h> +#include <stdio.h> +#include <fcntl.h> @@ -132,3 +131,29 @@ fail: return NULL; } + +/** + * Create a new open file descriptor for an already + * existing file descriptor. + * + * @param fd The file descriptor that shall be promoted + * to a new open file descriptor. + * @param oflag See open(3), `O_CREAT` is not allowed. + * @return 0 on success, -1 on error. + */ +int +reopen(int fd, int oflag) +{ + char path[sizeof("/dev/fd/") + 3 * sizeof(int)]; + int r, saved_errno; + + sprintf(path, "/dev/fd/%i", fd); + r = open(fd, oflag); + if (r < 0) + return -1; + if (dup2(r, fd) == -1) + return saved_errno = errno, close(r), errno = saved_errno, -1; + close(r); + return 0; +} + |