Close the low 3 fd's and open dev/null in their place.
********************************************************************/
-_PUBLIC_ void close_low_fds(bool stderr_too)
+_PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too)
{
#ifndef VALGRIND
int fd;
int i;
- close(0);
- close(1);
+ if (stdin_too)
+ close(0);
+ if (stdout_too)
+ close(1);
if (stderr_too)
close(2);
/* try and use up these file descriptors, so silly
library routines writing to stdout etc won't cause havoc */
for (i=0;i<3;i++) {
+ if (i == 0 && !stdin_too)
+ continue;
+ if (i == 1 && !stdout_too)
+ continue;
if (i == 2 && !stderr_too)
continue;
}
#endif /* HAVE_SETSID */
- if (!log_stdout) {
- /* Close fd's 0,1,2. Needed if started by rsh */
- close_low_fds(false); /* Don't close stderr, let the debug system
- attach it to the logfile */
- }
+ /* Close fd's 0,1,2 as appropriate. Needed if started by rsh. */
+ /* stdin must be open if we do not fork, for monitoring for
+ * close. stdout must be open if we are logging there, and we
+ * never close stderr (but debug might dup it onto a log file) */
+ close_low_fds(do_fork, !log_stdout, false);
}
(void)umask(oldumask);
/* Take over stderr to catch output into logs */
- if (state.fd > 0 && dup2(state.fd, 2) == -1) {
- close_low_fds(true); /* Close stderr too, if dup2 can't point it
- at the logfile */
+ if (state.fd > 0) {
+ if (dup2(state.fd, 2) == -1) {
+ /* Close stderr too, if dup2 can't point it -
+ at the logfile. There really isn't much
+ that can be done on such a fundemental
+ failure... */
+ close_low_fds(false, false, true);
+ }
}
state.reopening_logs = false;
/**
Close the low 3 fd's and open dev/null in their place
**/
-_PUBLIC_ void close_low_fds(bool stderr_too);
+_PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too);
/**
Become a daemon, discarding the controlling terminal.
return pid;
}
- /* child */
- close_low_fds(false);
-
status = reinit_after_fork(msg_ctx,
ev_ctx,
true);
return;
}
- /* child */
- close_low_fds(false);
-
status = reinit_after_fork(msg_ctx,
ev_ctx,
true);
return;
}
- /* child */
- close_low_fds(false);
-
/* save the parent process id so the children can use it later */
parent_id = procid_self();
* them, counting worker smbds. */
CatchChild();
- /* close our standard file
- descriptors */
- if (!debug_get_output_is_stdout()) {
- close_low_fds(False); /* Don't close stderr */
- }
-
status = reinit_after_fork(msg_ctx,
ev,
true);
goes away */
smbd_server_conn->sock = dup(0);
- /* close our standard file descriptors */
- if (!debug_get_output_is_stdout()) {
- close_low_fds(False); /* Don't close stderr */
- }
+ /* close stdin, stdout (if not logging to it), but not stderr */
+ close_low_fds(true, !debug_get_output_is_stdout(), false);
#ifdef HAVE_ATEXIT
atexit(killkids);