From: Josef 'Jeff' Sipek Date: Wed, 4 Oct 2017 12:52:13 +0000 (-0400) Subject: lib: move fd_close_maybe_stdio() to fd-util.[ch] X-Git-Tag: 2.3.0.rc1~882 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27;p=thirdparty%2Fdovecot%2Fcore.git lib: move fd_close_maybe_stdio() to fd-util.[ch] --- diff --git a/src/lib/fd-util.c b/src/lib/fd-util.c index 80c3c95b78..16cbe81b4b 100644 --- a/src/lib/fd-util.c +++ b/src/lib/fd-util.c @@ -111,3 +111,22 @@ void fd_set_nonblock(int fd, bool nonblock) if (fcntl(fd, F_SETFL, flags) < 0) i_fatal("fcntl(%d, F_SETFL) failed: %m", fd); } + +void fd_close_maybe_stdio(int *fd_in, int *fd_out) +{ + int *fdp[2] = { fd_in, fd_out }; + + if (*fd_in == *fd_out) + *fd_in = -1; + + for (unsigned int i = 0; i < N_ELEMENTS(fdp); i++) { + if (*fdp[i] == -1) + ; + else if (*fdp[i] > 1) + i_close_fd(fdp[i]); + else if (dup2(dev_null_fd, *fdp[i]) == *fdp[i]) + *fdp[i] = -1; + else + i_fatal("dup2(/dev/null, %d) failed: %m", *fdp[i]); + } +} diff --git a/src/lib/fd-util.h b/src/lib/fd-util.h index bf81f69d79..0043eb9953 100644 --- a/src/lib/fd-util.h +++ b/src/lib/fd-util.h @@ -10,4 +10,9 @@ void fd_debug_verify_leaks(int first_fd, int last_fd); /* Set file descriptor to blocking/nonblocking state */ void fd_set_nonblock(int fd, bool nonblock); +/* Close fd_in and fd_out, unless they're already -1. They can point to the + same fd, in which case they're closed only once. If they point to stdin + or stdout, they're replaced with /dev/null. */ +void fd_close_maybe_stdio(int *fd_in, int *fd_out); + #endif diff --git a/src/lib/lib.c b/src/lib/lib.c index 3c5739ca1a..3225a17c11 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -50,25 +50,6 @@ void i_close_fd_path_real(int *fd, const char *path, const char *arg, *fd = -1; } -void fd_close_maybe_stdio(int *fd_in, int *fd_out) -{ - int *fdp[2] = { fd_in, fd_out }; - - if (*fd_in == *fd_out) - *fd_in = -1; - - for (unsigned int i = 0; i < N_ELEMENTS(fdp); i++) { - if (*fdp[i] == -1) - ; - else if (*fdp[i] > 1) - i_close_fd(fdp[i]); - else if (dup2(dev_null_fd, *fdp[i]) == *fdp[i]) - *fdp[i] = -1; - else - i_fatal("dup2(/dev/null, %d) failed: %m", *fdp[i]); - } -} - #undef i_unlink int i_unlink(const char *path, const char *source_fname, unsigned int source_linenum) diff --git a/src/lib/lib.h b/src/lib/lib.h index 6970997575..f6a5c2b04f 100644 --- a/src/lib/lib.h +++ b/src/lib/lib.h @@ -56,10 +56,6 @@ typedef void lib_atexit_callback_t(void); also inside chroots. */ extern int dev_null_fd; -/* Close fd_in and fd_out, unless they're already -1. They can point to the - same fd, in which case they're closed only once. If they point to stdin - or stdout, they're replaced with /dev/null. */ -void fd_close_maybe_stdio(int *fd_in, int *fd_out); /* Call unlink(). If it fails, log an error including the source filename and line number. */ int i_unlink(const char *path, const char *source_fname,