]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add fd_close_maybe_stdio()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 16 Nov 2016 09:40:35 +0000 (11:40 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 16 Nov 2016 10:24:52 +0000 (12:24 +0200)
The idea is that this should be used whenever closing fds that may be 0 or
1. If they are closed normally, the following code may end up using 0/1 fd
for other purposes, which could cause problems.

src/lib/lib.c
src/lib/lib.h

index c1605cad8ce8bde98221409a27bfaa17b2957599..0b40fb1643bcfab0da0b81cac972c5cce07c6f27 100644 (file)
@@ -34,6 +34,25 @@ int close_keep_errno(int *fd)
        return ret;
 }
 
+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)
index 15819247599520d7cf58be0d4d7ff33a3313a047..fa386892ae89152c380725a42bdcc1b8fc787870 100644 (file)
@@ -54,6 +54,10 @@ typedef void lib_atexit_callback_t(void);
 extern int dev_null_fd;
 
 int close_keep_errno(int *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,