autofs-5.1.4 - move close stdio descriptors to become_daemon() From: Ian Kent Move the stdio file descriptor close to the become_daemon() function as closing these file descriptors, ie. detaching from ttys, is part of the preperation for becoming a system daemon. Signed-off-by: Ian Kent --- CHANGELOG | 1 + daemon/automount.c | 27 ++++++++++++++++++++++++++- include/log.h | 1 - lib/log.c | 30 ------------------------------ 4 files changed, 27 insertions(+), 32 deletions(-) --- autofs-5.0.7.orig/CHANGELOG +++ autofs-5.0.7/CHANGELOG @@ -314,6 +314,7 @@ - add NULL check in prepare_attempt_prefix(). - update build info with systemd. - use flags for startup boolean options. +- move close stdio descriptors to become_daemon(). 25/07/2012 autofs-5.0.7 ======================= --- autofs-5.0.7.orig/daemon/automount.c +++ autofs-5.0.7/daemon/automount.c @@ -1220,6 +1220,8 @@ static void become_daemon(unsigned int f } log_to_stderr(); } else { + int nullfd; + if (open_pipe(start_pipefd) < 0) { fprintf(stderr, "%s: failed to create start_pipefd.\n", program); @@ -1263,7 +1265,30 @@ static void become_daemon(unsigned int f close(start_pipefd[1]); exit(*pst_stat); } - log_to_syslog(); + + /* Redirect all our file descriptors to /dev/null */ + nullfd = open("/dev/null", O_RDWR); + if (nullfd < 0) { + char *estr = strerror_r(errno, buf, MAX_ERR_BUF); + fprintf(stderr, "cannot open /dev/null: %s", estr); + res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); + close(start_pipefd[1]); + exit(*pst_stat); + } + + if (dup2(nullfd, STDIN_FILENO) < 0 || + dup2(nullfd, STDOUT_FILENO) < 0 || + dup2(nullfd, STDERR_FILENO) < 0) { + char *estr = strerror_r(errno, buf, MAX_ERR_BUF); + fprintf(stderr, + "redirecting file descriptors failed: %s", estr); + res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); + close(start_pipefd[1]); + exit(*pst_stat); + } + + open_log(); + close(nullfd); } /* Write pid file if requested */ --- autofs-5.0.7.orig/include/log.h +++ autofs-5.0.7/include/log.h @@ -36,7 +36,6 @@ extern void set_log_debug_ap(struct auto extern void set_mnt_logging(unsigned global_logopt); extern void open_log(void); -extern void log_to_syslog(void); extern void log_to_stderr(void); extern void log_info(unsigned int, const char* msg, ...); --- autofs-5.0.7.orig/lib/log.c +++ autofs-5.0.7/lib/log.c @@ -314,36 +314,6 @@ void open_log(void) return; } -void log_to_syslog(void) -{ - char buf[MAX_ERR_BUF]; - int nullfd; - - open_log(); - - /* Redirect all our file descriptors to /dev/null */ - nullfd = open("/dev/null", O_RDWR); - if (nullfd < 0) { - char *estr = strerror_r(errno, buf, MAX_ERR_BUF); - fprintf(stderr, "cannot open /dev/null: %s", estr); - exit(1); - } - - if (dup2(nullfd, STDIN_FILENO) < 0 || - dup2(nullfd, STDOUT_FILENO) < 0 || - dup2(nullfd, STDERR_FILENO) < 0) { - char *estr = strerror_r(errno, buf, MAX_ERR_BUF); - fprintf(stderr, - "redirecting file descriptors failed: %s", estr); - exit(1); - } - - if (nullfd > 2) - close(nullfd); - - return; -} - void log_to_stderr(void) { if (syslog_open) {