aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorejaaskel <esa.jaaskela@suomi24.fi>2023-11-07 07:12:40 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2023-11-07 07:12:40 +0100
commit73f28134fc0987474384fde345559d4b34bf5385 (patch)
treed3439e5c69e79c940bfac38a773d24d9d1b78183
parent0c71497e654fb5e9a9272f81ddb5d994d2839df4 (diff)
downloadbusybox-w32-73f28134fc0987474384fde345559d4b34bf5385.tar.gz
busybox-w32-73f28134fc0987474384fde345559d4b34bf5385.tar.bz2
busybox-w32-73f28134fc0987474384fde345559d4b34bf5385.zip
start-stop-daemon: add -d DIR chdir option
Add option to change the running directory before starting the process. This can be done using -d or --chdir options. Add also test cases to start-stop-daemon to test out the directory change option. function old new delta packed_usage 34602 34648 +46 start_stop_daemon_main 1107 1130 +23 start_stop_daemon_longopts 156 164 +8 .rodata 105382 105384 +2 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 4/0 up/down: 79/0) Total: 79 bytes Signed-off-by: ejaaskel <esa.jaaskela@suomi24.fi> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--TODO2
-rw-r--r--debianutils/start_stop_daemon.c34
-rwxr-xr-xtestsuite/start-stop-daemon.tests20
3 files changed, 41 insertions, 15 deletions
diff --git a/TODO b/TODO
index 72ae0f88d..6c7415a81 100644
--- a/TODO
+++ b/TODO
@@ -222,8 +222,6 @@ Minor stuff:
222--- 222---
223 unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles. 223 unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles.
224--- 224---
225 support start-stop-daemon -d <chdir-path>
226---
227 225
228(TODO list after discussion 11.05.2009) 226(TODO list after discussion 11.05.2009)
229 227
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 16df67b3e..750167349 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -11,7 +11,7 @@
11/* 11/*
12This is how it is supposed to work: 12This is how it is supposed to work:
13 13
14start-stop-daemon [OPTIONS] [--start|--stop] [[--] ARGS...] 14start-stop-daemon [OPTIONS] [--start|--stop] [[--] ARGS]
15 15
16One (only) of these must be given: 16One (only) of these must be given:
17 -S,--start Start 17 -S,--start Start
@@ -58,13 +58,14 @@ Options which are valid for --start only:
58 priority can be optionally specified by appending a : 58 priority can be optionally specified by appending a :
59 followed by the value. The default priority is 0. The 59 followed by the value. The default priority is 0. The
60 currently supported policy values are other, fifo and rr. 60 currently supported policy values are other, fifo and rr.
61 -r,--chroot root Change directory and chroot to root before starting the 61 -r,--chroot DIR Change directory and chroot to DIR before starting the
62 process. Please note that the pidfile is also written after 62 process. Please note that the pidfile is also written after
63 the chroot. 63 the chroot.
64 -d,--chdir path Change directory to path before starting the process. This is 64 -d,--chdir DIR Change directory to DIR before starting the process. This is
65 done after the chroot if the -r|--chroot option is set. When 65 done after the chroot if the -r|--chroot option is set.
66 not specified, start-stop-daemon will change directory to the 66 When not specified, start-stop-daemon will change directory to the
67 root directory before starting the process. 67 root directory before starting the process.
68 ^^^^ Seems to be false, no default "/" chdir is done.
68 69
69Options which are valid for --stop only: 70Options which are valid for --stop only:
70 -s,--signal SIG Signal to send (default:TERM) 71 -s,--signal SIG Signal to send (default:TERM)
@@ -106,7 +107,7 @@ Misc options:
106//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o 107//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o
107 108
108//usage:#define start_stop_daemon_trivial_usage 109//usage:#define start_stop_daemon_trivial_usage
109//usage: "-S|-K [OPTIONS] [-- ARGS...]" 110//usage: "-S|-K [OPTIONS] [-- ARGS]"
110//usage:#define start_stop_daemon_full_usage "\n\n" 111//usage:#define start_stop_daemon_full_usage "\n\n"
111//usage: "Search for matching processes, and then\n" 112//usage: "Search for matching processes, and then\n"
112//usage: "-S: start a process unless a matching process is found\n" 113//usage: "-S: start a process unless a matching process is found\n"
@@ -127,6 +128,7 @@ Misc options:
127//usage: "\n -N N Change nice level" 128//usage: "\n -N N Change nice level"
128//usage: ) 129//usage: )
129//usage: "\n -c USER[:[GRP]] Change user/group" 130//usage: "\n -c USER[:[GRP]] Change user/group"
131//usage: "\n -d DIR Change to DIR"
130//usage: "\n -m Write PID to pidfile specified by -p" 132//usage: "\n -m Write PID to pidfile specified by -p"
131//usage: "\n-K only:" 133//usage: "\n-K only:"
132//usage: "\n -s SIG Signal to send" 134//usage: "\n -s SIG Signal to send"
@@ -160,11 +162,12 @@ enum {
160 OPT_s = (1 << 8), // -s 162 OPT_s = (1 << 8), // -s
161 OPT_u = (1 << 9), // -u 163 OPT_u = (1 << 9), // -u
162 OPT_c = (1 << 10), // -c 164 OPT_c = (1 << 10), // -c
163 OPT_x = (1 << 11), // -x 165 OPT_d = (1 << 11), // -d
164 OPT_p = (1 << 12), // -p 166 OPT_x = (1 << 12), // -x
165 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o 167 OPT_p = (1 << 13), // -p
166 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v 168 OPT_OKNODO = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
167 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N 169 OPT_VERBOSE = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
170 OPT_NICELEVEL = (1 << 16) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
168}; 171};
169#define QUIET (option_mask32 & OPT_QUIET) 172#define QUIET (option_mask32 & OPT_QUIET)
170#define TEST (option_mask32 & OPT_TEST) 173#define TEST (option_mask32 & OPT_TEST)
@@ -413,6 +416,7 @@ static const char start_stop_daemon_longopts[] ALIGN1 =
413 "signal\0" Required_argument "s" 416 "signal\0" Required_argument "s"
414 "user\0" Required_argument "u" 417 "user\0" Required_argument "u"
415 "chuid\0" Required_argument "c" 418 "chuid\0" Required_argument "c"
419 "chdir\0" Required_argument "d"
416 "exec\0" Required_argument "x" 420 "exec\0" Required_argument "x"
417 "pidfile\0" Required_argument "p" 421 "pidfile\0" Required_argument "p"
418# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 422# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
@@ -433,6 +437,7 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
433 char *signame; 437 char *signame;
434 char *startas = NULL; 438 char *startas = NULL;
435 char *chuid; 439 char *chuid;
440 char *chdir;
436#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 441#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
437// char *retry_arg = NULL; 442// char *retry_arg = NULL;
438// int retries = -1; 443// int retries = -1;
@@ -442,7 +447,7 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
442 INIT_G(); 447 INIT_G();
443 448
444 opt = GETOPT32(argv, "^" 449 opt = GETOPT32(argv, "^"
445 "KSbqtma:n:s:u:c:x:p:" 450 "KSbqtma:n:s:u:c:d:x:p:"
446 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:") 451 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
447 "\0" 452 "\0"
448 "K:S:K--S:S--K" 453 "K:S:K--S:S--K"
@@ -458,7 +463,7 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
458 IF_FEATURE_START_STOP_DAEMON_FANCY(":q-v") /* -q turns off -v */ 463 IF_FEATURE_START_STOP_DAEMON_FANCY(":q-v") /* -q turns off -v */
459 , 464 ,
460 LONGOPTS 465 LONGOPTS
461 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile 466 &startas, &cmdname, &signame, &userspec, &chuid, &chdir, &execname, &pidfile
462 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) 467 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
463 /* We accept and ignore -R <param> / --retry <param> */ 468 /* We accept and ignore -R <param> / --retry <param> */
464 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL) 469 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
@@ -586,6 +591,9 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
586 setgroups(1, &ugid.gid); 591 setgroups(1, &ugid.gid);
587 } 592 }
588 } 593 }
594 if (opt & OPT_d) {
595 xchdir(chdir);
596 }
589 /* Try: 597 /* Try:
590 * strace -oLOG start-stop-daemon -S -x /bin/usleep -a qwerty 500000 598 * strace -oLOG start-stop-daemon -S -x /bin/usleep -a qwerty 500000
591 * should exec "/bin/usleep", but argv[0] should be "qwerty": 599 * should exec "/bin/usleep", but argv[0] should be "qwerty":
diff --git a/testsuite/start-stop-daemon.tests b/testsuite/start-stop-daemon.tests
index 0757b1288..e1e49ab5f 100755
--- a/testsuite/start-stop-daemon.tests
+++ b/testsuite/start-stop-daemon.tests
@@ -11,6 +11,21 @@ testing "start-stop-daemon -x without -a" \
11 "0\n" \ 11 "0\n" \
12 "" "" 12 "" ""
13 13
14testing "start-stop-daemon -x with -d on existing directory" \
15 'start-stop-daemon -S -d /tmp -x true 2>&1; echo $?' \
16 "0\n" \
17 "" ""
18
19testing "start-stop-daemon -x with -d on existing and check dir" \
20 'output=$(start-stop-daemon -S -d /tmp -x pwd); echo $output' \
21 "/tmp\n" \
22 "" ""
23
24testing "start-stop-daemon -x with --chdir on existing and check dir" \
25 'output=$(start-stop-daemon -S --chdir /tmp -x pwd); echo $output' \
26 "/tmp\n" \
27 "" ""
28
14testing "start-stop-daemon -a without -x" \ 29testing "start-stop-daemon -a without -x" \
15 'start-stop-daemon -S -a false 2>&1; echo $?' \ 30 'start-stop-daemon -S -a false 2>&1; echo $?' \
16 "1\n" \ 31 "1\n" \
@@ -21,6 +36,11 @@ testing "start-stop-daemon without -x and -a" \
21 "1\n" \ 36 "1\n" \
22 "" "" 37 "" ""
23 38
39testing "start-stop-daemon -x with -d on non-existing directory" \
40 'start-stop-daemon -S -d /non-existent -x true > /dev/null 2>&1; echo $?' \
41 "1\n" \
42 "" ""
43
24# This runs /bin/false with argv[0..2] of { "qwerty", "false", NULL }. 44# This runs /bin/false with argv[0..2] of { "qwerty", "false", NULL }.
25# 45#
26# Unfortunately, this does not actually check argv[0] correctness, 46# Unfortunately, this does not actually check argv[0] correctness,