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.
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)
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,