From b182e9ad6011909fdb76358431d23d195febaf54 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 4 Aug 2017 23:04:17 +0200 Subject: libbb: use _exit, not exit, in bb_daemonize_or_rexec() By the time we reach exit in parent, child already exited or execed. We should not re-run libc cleanup code. While at it, introduce bb_daemon_helper() and add a few comments. Signed-off-by: Denys Vlasenko --- libbb/vfork_daemon_rexec.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'libbb/vfork_daemon_rexec.c') diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index 98512bb00..f84e678b5 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c @@ -209,6 +209,9 @@ pid_t FAST_FUNC fork_or_rexec(char **argv) /* Maybe we are already re-execed and come here again? */ if (re_execed) return 0; + + /* fflush_all(); ? - so far all callers had no buffered output to flush */ + pid = xvfork(); if (pid) /* parent */ return pid; @@ -245,8 +248,11 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv) fd = dup(fd); /* have 0,1,2 open at least to /dev/null */ if (!(flags & DAEMON_ONLY_SANITIZE)) { + + /* fflush_all(); - add it in fork_or_rexec() if necessary */ + if (fork_or_rexec(argv)) - exit(EXIT_SUCCESS); /* parent */ + _exit(EXIT_SUCCESS); /* parent */ /* if daemonizing, detach from stdio & ctty */ setsid(); dup2(fd, 0); @@ -258,7 +264,7 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv) * Prevent this: stop being a session leader. */ if (fork_or_rexec(argv)) - exit(EXIT_SUCCESS); /* parent */ + _exit(EXIT_SUCCESS); /* parent */ } } while (fd > 2) { -- cgit v1.2.3