Patch from Russ Dill <Russ.Dill@asu.edu>. 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
This commit is contained in:
Eric Andersen 2002-01-26 09:04:45 +00:00
parent 467a18b1d9
commit 53a2299230
2 changed files with 20 additions and 2 deletions

View File

@ -22,6 +22,7 @@
static int start = 0; static int start = 0;
static int stop = 0; static int stop = 0;
static int fork_before_exec = 0;
static int signal_nr = 15; static int signal_nr = 15;
static int user_id = -1; static int user_id = -1;
static const char *userspec = NULL; static const char *userspec = NULL;
@ -55,7 +56,7 @@ parse_options(int argc, char * const *argv)
int c; int c;
for (;;) { for (;;) {
c = getopt (argc, argv, "a:n:s:u:x:KS"); c = getopt (argc, argv, "a:n:s:u:x:KSb");
if (c == EOF) if (c == EOF)
break; break;
switch (c) { switch (c) {
@ -81,6 +82,9 @@ parse_options(int argc, char * const *argv)
case 'x': case 'x':
execname = optarg; execname = optarg;
break; break;
case 'b':
fork_before_exec = 1;
break;
default: default:
show_usage(); show_usage();
} }
@ -255,6 +259,11 @@ start_stop_daemon_main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
*--argv = startas; *--argv = startas;
if (fork_before_exec) {
if (daemon(0, 0) == -1)
perror_msg_and_die ("unable to fork");
}
setsid();
execv(startas, argv); execv(startas, argv);
perror_msg_and_die ("unable to start %s", startas); perror_msg_and_die ("unable to start %s", startas);
} }

View File

@ -22,6 +22,7 @@
static int start = 0; static int start = 0;
static int stop = 0; static int stop = 0;
static int fork_before_exec = 0;
static int signal_nr = 15; static int signal_nr = 15;
static int user_id = -1; static int user_id = -1;
static const char *userspec = NULL; static const char *userspec = NULL;
@ -55,7 +56,7 @@ parse_options(int argc, char * const *argv)
int c; int c;
for (;;) { for (;;) {
c = getopt (argc, argv, "a:n:s:u:x:KS"); c = getopt (argc, argv, "a:n:s:u:x:KSb");
if (c == EOF) if (c == EOF)
break; break;
switch (c) { switch (c) {
@ -81,6 +82,9 @@ parse_options(int argc, char * const *argv)
case 'x': case 'x':
execname = optarg; execname = optarg;
break; break;
case 'b':
fork_before_exec = 1;
break;
default: default:
show_usage(); show_usage();
} }
@ -255,6 +259,11 @@ start_stop_daemon_main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
*--argv = startas; *--argv = startas;
if (fork_before_exec) {
if (daemon(0, 0) == -1)
perror_msg_and_die ("unable to fork");
}
setsid();
execv(startas, argv); execv(startas, argv);
perror_msg_and_die ("unable to start %s", startas); perror_msg_and_die ("unable to start %s", startas);
} }