aboutsummaryrefslogtreecommitdiff
path: root/debianutils/start_stop_daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'debianutils/start_stop_daemon.c')
-rw-r--r--debianutils/start_stop_daemon.c94
1 files changed, 35 insertions, 59 deletions
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 9d60b2c7f..c8b7fa8f2 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -79,6 +79,7 @@ Misc options:
79//config: -N|--nicelevel N 79//config: -N|--nicelevel N
80 80
81//applet:IF_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, BB_DIR_SBIN, BB_SUID_DROP, start_stop_daemon)) 81//applet:IF_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, BB_DIR_SBIN, BB_SUID_DROP, start_stop_daemon))
82/* not NOEXEC: uses bb_common_bufsiz1 */
82 83
83//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o 84//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o
84 85
@@ -86,44 +87,15 @@ Misc options:
86//usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]" 87//usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]"
87//usage:#define start_stop_daemon_full_usage "\n\n" 88//usage:#define start_stop_daemon_full_usage "\n\n"
88//usage: "Search for matching processes, and then\n" 89//usage: "Search for matching processes, and then\n"
89//usage: "-K: stop all matching processes.\n" 90//usage: "-K: stop all matching processes\n"
90//usage: "-S: start a process unless a matching process is found.\n" 91//usage: "-S: start a process unless a matching process is found\n"
91//usage: IF_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
92//usage: "\nProcess matching:"
93//usage: "\n -u,--user USERNAME|UID Match only this user's processes"
94//usage: "\n -n,--name NAME Match processes with NAME"
95//usage: "\n in comm field in /proc/PID/stat"
96//usage: "\n -x,--exec EXECUTABLE Match processes with this command"
97//usage: "\n in /proc/PID/{exe,cmdline}"
98//usage: "\n -p,--pidfile FILE Match a process with PID from the file"
99//usage: "\n All specified conditions must match"
100//usage: "\n-S only:"
101//usage: "\n -x,--exec EXECUTABLE Program to run"
102//usage: "\n -a,--startas NAME Zeroth argument"
103//usage: "\n -b,--background Background"
104//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
105//usage: "\n -N,--nicelevel N Change nice level"
106//usage: )
107//usage: "\n -c,--chuid USER[:[GRP]] Change to user/group"
108//usage: "\n -m,--make-pidfile Write PID to the pidfile specified by -p"
109//usage: "\n-K only:"
110//usage: "\n -s,--signal SIG Signal to send"
111//usage: "\n -t,--test Match only, exit with 0 if a process is found"
112//usage: "\nOther:"
113//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
114//usage: "\n -o,--oknodo Exit with status 0 if nothing is done"
115//usage: "\n -v,--verbose Verbose"
116//usage: )
117//usage: "\n -q,--quiet Quiet"
118//usage: )
119//usage: IF_NOT_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
120//usage: "\nProcess matching:" 92//usage: "\nProcess matching:"
121//usage: "\n -u USERNAME|UID Match only this user's processes" 93//usage: "\n -u USERNAME|UID Match only this user's processes"
122//usage: "\n -n NAME Match processes with NAME" 94//usage: "\n -n NAME Match processes with NAME"
123//usage: "\n in comm field in /proc/PID/stat" 95//usage: "\n in comm field in /proc/PID/stat"
124//usage: "\n -x EXECUTABLE Match processes with this command" 96//usage: "\n -x EXECUTABLE Match processes with this command"
125//usage: "\n command in /proc/PID/cmdline" 97//usage: "\n command in /proc/PID/cmdline"
126//usage: "\n -p FILE Match a process with PID from the file" 98//usage: "\n -p FILE Match a process with PID from FILE"
127//usage: "\n All specified conditions must match" 99//usage: "\n All specified conditions must match"
128//usage: "\n-S only:" 100//usage: "\n-S only:"
129//usage: "\n -x EXECUTABLE Program to run" 101//usage: "\n -x EXECUTABLE Program to run"
@@ -132,18 +104,17 @@ Misc options:
132//usage: IF_FEATURE_START_STOP_DAEMON_FANCY( 104//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
133//usage: "\n -N N Change nice level" 105//usage: "\n -N N Change nice level"
134//usage: ) 106//usage: )
135//usage: "\n -c USER[:[GRP]] Change to user/group" 107//usage: "\n -c USER[:[GRP]] Change user/group"
136//usage: "\n -m Write PID to the pidfile specified by -p" 108//usage: "\n -m Write PID to pidfile specified by -p"
137//usage: "\n-K only:" 109//usage: "\n-K only:"
138//usage: "\n -s SIG Signal to send" 110//usage: "\n -s SIG Signal to send"
139//usage: "\n -t Match only, exit with 0 if a process is found" 111//usage: "\n -t Match only, exit with 0 if found"
140//usage: "\nOther:" 112//usage: "\nOther:"
141//usage: IF_FEATURE_START_STOP_DAEMON_FANCY( 113//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
142//usage: "\n -o Exit with status 0 if nothing is done" 114//usage: "\n -o Exit with status 0 if nothing is done"
143//usage: "\n -v Verbose" 115//usage: "\n -v Verbose"
144//usage: ) 116//usage: )
145//usage: "\n -q Quiet" 117//usage: "\n -q Quiet"
146//usage: )
147 118
148#include <sys/resource.h> 119#include <sys/resource.h>
149 120
@@ -409,11 +380,11 @@ static const char start_stop_daemon_longopts[] ALIGN1 =
409 "quiet\0" No_argument "q" 380 "quiet\0" No_argument "q"
410 "test\0" No_argument "t" 381 "test\0" No_argument "t"
411 "make-pidfile\0" No_argument "m" 382 "make-pidfile\0" No_argument "m"
412#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 383# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
413 "oknodo\0" No_argument "o" 384 "oknodo\0" No_argument "o"
414 "verbose\0" No_argument "v" 385 "verbose\0" No_argument "v"
415 "nicelevel\0" Required_argument "N" 386 "nicelevel\0" Required_argument "N"
416#endif 387# endif
417 "startas\0" Required_argument "a" 388 "startas\0" Required_argument "a"
418 "name\0" Required_argument "n" 389 "name\0" Required_argument "n"
419 "signal\0" Required_argument "s" 390 "signal\0" Required_argument "s"
@@ -421,10 +392,15 @@ static const char start_stop_daemon_longopts[] ALIGN1 =
421 "chuid\0" Required_argument "c" 392 "chuid\0" Required_argument "c"
422 "exec\0" Required_argument "x" 393 "exec\0" Required_argument "x"
423 "pidfile\0" Required_argument "p" 394 "pidfile\0" Required_argument "p"
424#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 395# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
425 "retry\0" Required_argument "R" 396 "retry\0" Required_argument "R"
426#endif 397# endif
427 ; 398 ;
399# define GETOPT32 getopt32long
400# define LONGOPTS start_stop_daemon_longopts,
401#else
402# define GETOPT32 getopt32
403# define LONGOPTS
428#endif 404#endif
429 405
430int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 406int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -445,19 +421,18 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
445 421
446 INIT_G(); 422 INIT_G();
447 423
448#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 424 opt = GETOPT32(argv, "^"
449 applet_long_options = start_stop_daemon_longopts; 425 "KSbqtma:n:s:u:c:x:p:"
450#endif 426 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
451 427 /* -K or -S is required; they are mutually exclusive */
452 /* -K or -S is required; they are mutually exclusive */ 428 /* -p is required if -m is given */
453 /* -p is required if -m is given */ 429 /* -xpun (at least one) is required if -K is given */
454 /* -xpun (at least one) is required if -K is given */ 430 /* -xa (at least one) is required if -S is given */
455 /* -xa (at least one) is required if -S is given */ 431 /* -q turns off -v */
456 /* -q turns off -v */ 432 "\0"
457 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa" 433 "K:S:K--S:S--K:m?p:K?xpun:S?xa"
458 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"); 434 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"),
459 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:" 435 LONGOPTS
460 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
461 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile 436 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
462 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) 437 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
463 /* We accept and ignore -R <param> / --retry <param> */ 438 /* We accept and ignore -R <param> / --retry <param> */
@@ -516,6 +491,11 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
516 /* DAEMON_DEVNULL_STDIO is superfluous - 491 /* DAEMON_DEVNULL_STDIO is superfluous -
517 * it's always done by bb_daemonize() */ 492 * it's always done by bb_daemonize() */
518#else 493#else
494 /* Daemons usually call bb_daemonize_or_rexec(), but SSD can do
495 * without: SSD is not itself a daemon, it _execs_ a daemon.
496 * The usual NOMMU problem of "child can't run indefinitely,
497 * it must exec" does not bite us: we exec anyway.
498 */
519 pid_t pid = xvfork(); 499 pid_t pid = xvfork();
520 if (pid != 0) { 500 if (pid != 0) {
521 /* parent */ 501 /* parent */
@@ -525,12 +505,8 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
525 } 505 }
526 /* Child */ 506 /* Child */
527 setsid(); /* detach from controlling tty */ 507 setsid(); /* detach from controlling tty */
528 /* Redirect stdio to /dev/null, close extra FDs. 508 /* Redirect stdio to /dev/null, close extra FDs */
529 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ 509 bb_daemon_helper(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
530 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
531 + DAEMON_CLOSE_EXTRA_FDS
532 + DAEMON_ONLY_SANITIZE,
533 NULL /* argv, unused */ );
534#endif 510#endif
535 } 511 }
536 if (opt & OPT_MAKEPID) { 512 if (opt & OPT_MAKEPID) {