From 53a2299230976b93d3bbe81ad92dad8c23d0b271 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 26 Jan 2002 09:04:45 +0000 Subject: Patch from Russ Dill . From the start-stop-daemon man page: -b|--background Typically used with programs that don't detach on their own. This option will force start-stop-daemon to fork before starting the process, and force it into the background. WARNING: start-stop-daemon cannot check the exit status if the process fails to execute for any reason. This is a last resort, and is only meant for programs that either make no sense forking on their own, or where it's not feasible to add the code for it to do this itself. This is usefull for applets like watchdog --- debianutils/start_stop_daemon.c | 11 ++++++++++- init/start_stop_daemon.c | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index c6b7043..ed4503c 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -22,6 +22,7 @@ static int start = 0; static int stop = 0; +static int fork_before_exec = 0; static int signal_nr = 15; static int user_id = -1; static const char *userspec = NULL; @@ -55,7 +56,7 @@ parse_options(int argc, char * const *argv) int c; for (;;) { - c = getopt (argc, argv, "a:n:s:u:x:KS"); + c = getopt (argc, argv, "a:n:s:u:x:KSb"); if (c == EOF) break; switch (c) { @@ -81,6 +82,9 @@ parse_options(int argc, char * const *argv) case 'x': execname = optarg; break; + case 'b': + fork_before_exec = 1; + break; default: show_usage(); } @@ -255,6 +259,11 @@ start_stop_daemon_main(int argc, char **argv) return EXIT_SUCCESS; } *--argv = startas; + if (fork_before_exec) { + if (daemon(0, 0) == -1) + perror_msg_and_die ("unable to fork"); + } + setsid(); execv(startas, argv); perror_msg_and_die ("unable to start %s", startas); } diff --git a/init/start_stop_daemon.c b/init/start_stop_daemon.c index c6b7043..ed4503c 100644 --- a/init/start_stop_daemon.c +++ b/init/start_stop_daemon.c @@ -22,6 +22,7 @@ static int start = 0; static int stop = 0; +static int fork_before_exec = 0; static int signal_nr = 15; static int user_id = -1; static const char *userspec = NULL; @@ -55,7 +56,7 @@ parse_options(int argc, char * const *argv) int c; for (;;) { - c = getopt (argc, argv, "a:n:s:u:x:KS"); + c = getopt (argc, argv, "a:n:s:u:x:KSb"); if (c == EOF) break; switch (c) { @@ -81,6 +82,9 @@ parse_options(int argc, char * const *argv) case 'x': execname = optarg; break; + case 'b': + fork_before_exec = 1; + break; default: show_usage(); } @@ -255,6 +259,11 @@ start_stop_daemon_main(int argc, char **argv) return EXIT_SUCCESS; } *--argv = startas; + if (fork_before_exec) { + if (daemon(0, 0) == -1) + perror_msg_and_die ("unable to fork"); + } + setsid(); execv(startas, argv); perror_msg_and_die ("unable to start %s", startas); } -- cgit v1.1