[git commit] bb_daemonize_or_rexec(): add flag to double-fork; use it in start-stop-daemon

Denys Vlasenko vda.linux at googlemail.com
Wed Nov 9 18:44:37 UTC 2011


commit: http://git.busybox.net/busybox/commit/?id=743edac6c84ac207d0bb4c3124b0e8fe450a38b4
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Add a DAEMON_DOUBLE_FORK flag to make bb_daemonize double-fork so it isn't a
session leader, and hence doesn't get a controlling tty on Linux if a tty is
ever opened, similar to how libdaemon's daemon_fork or the big
start-stop-daemon does it - And use it in start-stop-daemon.

For details, see http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.3

Signed-off-by: Peter Korsgaard <jacmet at sunsite.dk>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 debianutils/start_stop_daemon.c |    2 +-
 include/libbb.h                 |    1 +
 libbb/vfork_daemon_rexec.c      |   10 +++++++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 02609c0..495ed0a 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -474,7 +474,7 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
 	*--argv = startas;
 	if (opt & OPT_BACKGROUND) {
 #if BB_MMU
-		bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
+		bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS + DAEMON_DOUBLE_FORK);
 		/* DAEMON_DEVNULL_STDIO is superfluous -
 		 * it's always done by bb_daemonize() */
 #else
diff --git a/include/libbb.h b/include/libbb.h
index 53224fa..09e8d28 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -966,6 +966,7 @@ enum {
 	DAEMON_DEVNULL_STDIO = 2,
 	DAEMON_CLOSE_EXTRA_FDS = 4,
 	DAEMON_ONLY_SANITIZE = 8, /* internal use */
+	DAEMON_DOUBLE_FORK = 16, /* double fork to avoid controlling tty */
 };
 #if BB_MMU
   enum { re_execed = 0 };
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index a75eafb..ed1f86f 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -253,11 +253,19 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
 	if (!(flags & DAEMON_ONLY_SANITIZE)) {
 		if (fork_or_rexec(argv))
 			exit(EXIT_SUCCESS); /* parent */
-		/* if daemonizing, make sure we detach from stdio & ctty */
+		/* if daemonizing, detach from stdio & ctty */
 		setsid();
 		dup2(fd, 0);
 		dup2(fd, 1);
 		dup2(fd, 2);
+		if (flags & DAEMON_DOUBLE_FORK) {
+			/* On Linux, session leader can acquire ctty
+			 * unknowingly, by opening a tty.
+			 * Prevent this: stop being a session leader.
+			 */
+			if (fork_or_rexec(argv))
+				exit(EXIT_SUCCESS); /* parent */
+		}
 	}
 	while (fd > 2) {
 		close(fd--);


More information about the busybox-cvs mailing list