aboutsummaryrefslogtreecommitdiff
path: root/debianutils
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-06-11 17:24:01 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-06-11 17:24:01 +0000
commita926f8e50a3420648b526a85fcdefe66c9a7926f (patch)
treec6a139d9c660cf03cfa98721788c602500fe3ede /debianutils
parent081b1ac6b4cce2896e5a874c96b9b02e0edbcf8e (diff)
downloadbusybox-w32-a926f8e50a3420648b526a85fcdefe66c9a7926f.tar.gz
busybox-w32-a926f8e50a3420648b526a85fcdefe66c9a7926f.tar.bz2
busybox-w32-a926f8e50a3420648b526a85fcdefe66c9a7926f.zip
- add fancy mode to start-stop-daemon to support --oknodo and --verbose
Diffstat (limited to 'debianutils')
-rw-r--r--debianutils/Config.in9
-rw-r--r--debianutils/start_stop_daemon.c106
2 files changed, 68 insertions, 47 deletions
diff --git a/debianutils/Config.in b/debianutils/Config.in
index 626272474..3dd2ef61b 100644
--- a/debianutils/Config.in
+++ b/debianutils/Config.in
@@ -61,6 +61,15 @@ config CONFIG_START_STOP_DAEMON
61 termination of system-level processes, usually the ones 61 termination of system-level processes, usually the ones
62 started during the startup of the system. 62 started during the startup of the system.
63 63
64config CONFIG_FEATURE_START_STOP_DAEMON_FANCY
65 bool "Support additional arguments"
66 default y
67 depends on CONFIG_START_STOP_DAEMON
68 help
69 Support additional arguments.
70 -o|--oknodo ignored since we exit with 0 anyway
71 -v|--verbose
72
64config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 73config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
65 bool "Enable long options" 74 bool "Enable long options"
66 default n 75 default n
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index b83a75994..3a0fbd2c6 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -22,7 +22,7 @@
22 22
23static int signal_nr = 15; 23static int signal_nr = 15;
24static int user_id = -1; 24static int user_id = -1;
25static int quiet = 0; 25static int quiet;
26static char *userspec = NULL; 26static char *userspec = NULL;
27static char *cmdname = NULL; 27static char *cmdname = NULL;
28static char *execname = NULL; 28static char *execname = NULL;
@@ -35,8 +35,7 @@ struct pid_list {
35 35
36static struct pid_list *found = NULL; 36static struct pid_list *found = NULL;
37 37
38static inline void 38static inline void push(pid_t pid)
39push(pid_t pid)
40{ 39{
41 struct pid_list *p; 40 struct pid_list *p;
42 41
@@ -46,8 +45,7 @@ push(pid_t pid)
46 found = p; 45 found = p;
47} 46}
48 47
49static int 48static int pid_is_exec(pid_t pid, const char *name)
50pid_is_exec(pid_t pid, const char *name)
51{ 49{
52 char buf[32]; 50 char buf[32];
53 struct stat sb, exec_stat; 51 struct stat sb, exec_stat;
@@ -61,8 +59,7 @@ pid_is_exec(pid_t pid, const char *name)
61 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino); 59 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
62} 60}
63 61
64static int 62static int pid_is_user(int pid, int uid)
65pid_is_user(int pid, int uid)
66{ 63{
67 struct stat sb; 64 struct stat sb;
68 char buf[32]; 65 char buf[32];
@@ -73,8 +70,7 @@ pid_is_user(int pid, int uid)
73 return (sb.st_uid == uid); 70 return (sb.st_uid == uid);
74} 71}
75 72
76static int 73static int pid_is_cmd(pid_t pid, const char *name)
77pid_is_cmd(pid_t pid, const char *name)
78{ 74{
79 char buf[32]; 75 char buf[32];
80 FILE *f; 76 FILE *f;
@@ -98,8 +94,7 @@ pid_is_cmd(pid_t pid, const char *name)
98} 94}
99 95
100 96
101static void 97static void check(int pid)
102check(int pid)
103{ 98{
104 if (execname && !pid_is_exec(pid, execname)) { 99 if (execname && !pid_is_exec(pid, execname)) {
105 return; 100 return;
@@ -114,8 +109,7 @@ check(int pid)
114} 109}
115 110
116 111
117static void 112static void do_pidfile(void)
118do_pidfile(void)
119{ 113{
120 FILE *f; 114 FILE *f;
121 pid_t pid; 115 pid_t pid;
@@ -130,8 +124,7 @@ do_pidfile(void)
130 124
131} 125}
132 126
133static void 127static void do_procinit(void)
134do_procinit(void)
135{ 128{
136 DIR *procdir; 129 DIR *procdir;
137 struct dirent *entry; 130 struct dirent *entry;
@@ -157,17 +150,16 @@ do_procinit(void)
157} 150}
158 151
159 152
160static void 153static int do_stop(void)
161do_stop(void)
162{ 154{
163 char what[1024]; 155 RESERVE_CONFIG_BUFFER(what, 1024);
164 struct pid_list *p; 156 struct pid_list *p;
165 int killed = 0; 157 int killed = 0;
166 158
167 do_procinit(); 159 do_procinit();
168 160
169 if (cmdname) 161 if (cmdname)
170 strcpy(what, cmdname); 162 what=cmdname;//strcpy(what, cmdname);
171 else if (execname) 163 else if (execname)
172 strcpy(what, execname); 164 strcpy(what, execname);
173 else if (pidfile) 165 else if (pidfile)
@@ -180,7 +172,9 @@ do_stop(void)
180 if (!found) { 172 if (!found) {
181 if (!quiet) 173 if (!quiet)
182 printf("no %s found; none killed.\n", what); 174 printf("no %s found; none killed.\n", what);
183 return; 175 if (ENABLE_FEATURE_CLEAN_UP)
176 RELEASE_CONFIG_BUFFER(what);
177 return -1;
184 } 178 }
185 for (p = found; p; p = p->next) { 179 for (p = found; p; p = p->next) {
186 if (kill(p->pid, signal_nr) == 0) { 180 if (kill(p->pid, signal_nr) == 0) {
@@ -197,21 +191,31 @@ do_stop(void)
197 printf(" %d", -p->pid); 191 printf(" %d", -p->pid);
198 printf(").\n"); 192 printf(").\n");
199 } 193 }
194 if (ENABLE_FEATURE_CLEAN_UP)
195 RELEASE_CONFIG_BUFFER(what);
196 return killed;
200} 197}
201 198
202#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 199#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
203static const struct option ssd_long_options[] = { 200static const struct option ssd_long_options[] = {
204 { "stop", 0, NULL, 'K' }, 201 { "stop", 0, NULL, 'K' },
205 { "start", 0, NULL, 'S' }, 202 { "start", 0, NULL, 'S' },
206 { "background", 0, NULL, 'b' }, 203 { "background", 0, NULL, 'b' },
207 { "quiet", 0, NULL, 'q' }, 204 { "quiet", 0, NULL, 'q' },
208 { "make-pidfile", 0, NULL, 'm' }, 205 { "make-pidfile", 0, NULL, 'm' },
209 { "startas", 1, NULL, 'a' }, 206#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
207 { "oknodo", 0, NULL, 'o' },
208 { "verbose", 0, NULL, 'v' },
209#endif
210 { "startas", 1, NULL, 'a' },
210 { "name", 1, NULL, 'n' }, 211 { "name", 1, NULL, 'n' },
211 { "signal", 1, NULL, 's' }, 212 { "signal", 1, NULL, 's' },
212 { "user", 1, NULL, 'u' }, 213 { "user", 1, NULL, 'u' },
213 { "exec", 1, NULL, 'x' }, 214 { "exec", 1, NULL, 'x' },
214 { "pidfile", 1, NULL, 'p' }, 215 { "pidfile", 1, NULL, 'p' },
216#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
217 { "retry", 1, NULL, 'R' },
218#endif
215 { 0, 0, 0, 0 } 219 { 0, 0, 0, 0 }
216}; 220};
217#endif 221#endif
@@ -221,42 +225,48 @@ static const struct option ssd_long_options[] = {
221#define SSD_OPT_BACKGROUND 4 225#define SSD_OPT_BACKGROUND 4
222#define SSD_OPT_QUIET 8 226#define SSD_OPT_QUIET 8
223#define SSD_OPT_MAKEPID 16 227#define SSD_OPT_MAKEPID 16
228#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
229#define SSD_OPT_OKNODO 32
230#define SSD_OPT_VERBOSE 64
224 231
225int 232#endif
226start_stop_daemon_main(int argc, char **argv) 233
234int start_stop_daemon_main(int argc, char **argv)
227{ 235{
228 unsigned long opt; 236 unsigned long opt;
229 char *signame = NULL; 237 char *signame = NULL;
230 char *startas = NULL; 238 char *startas = NULL;
231 239#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
240// char *retry_arg = NULL;
241// int retries = -1;
242#endif
232#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 243#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
233 bb_applet_long_options = ssd_long_options; 244 bb_applet_long_options = ssd_long_options;
234#endif 245#endif
235 246
236 /* Check required one context option was given */ 247 /* Check required one context option was given */
237 bb_opt_complementally = "K:S:?:K--S:S--K"; 248 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
238 opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:", 249 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
239 &startas, &cmdname, &signame, &userspec, &execname, &pidfile); 250// USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
240 251 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
252 "a:n:s:u:x:p:"
253// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
254 ,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);
241 255
242 quiet = opt & SSD_OPT_QUIET; 256 quiet = (opt & SSD_OPT_QUIET)
257 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
243 258
244 if (signame) { 259 if (signame) {
245 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); 260 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
246 } 261 }
247 262
248 if (!execname && !pidfile && !userspec && !cmdname)
249 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
250
251 if (!startas) 263 if (!startas)
252 startas = execname; 264 startas = execname;
253 265
254 if ((opt & SSD_CTX_START) && !startas) 266// USE_FEATURE_START_STOP_DAEMON_FANCY(
255 bb_error_msg_and_die ("-S needs -x or -a"); 267// if (retry_arg)
256 268// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
257 if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL) 269// )
258 bb_error_msg_and_die ("-m needs -p");
259
260 argc -= optind; 270 argc -= optind;
261 argv += optind; 271 argv += optind;
262 272
@@ -264,8 +274,10 @@ start_stop_daemon_main(int argc, char **argv)
264 user_id = bb_xgetpwnam(userspec); 274 user_id = bb_xgetpwnam(userspec);
265 275
266 if (opt & SSD_CTX_STOP) { 276 if (opt & SSD_CTX_STOP) {
267 do_stop(); 277 int i = do_stop();
268 return EXIT_SUCCESS; 278 return
279 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
280 ? 0 :) !!(i<=0);
269 } 281 }
270 282
271 do_procinit(); 283 do_procinit();
@@ -273,7 +285,8 @@ start_stop_daemon_main(int argc, char **argv)
273 if (found) { 285 if (found) {
274 if (!quiet) 286 if (!quiet)
275 printf("%s already running.\n%d\n", execname ,found->pid); 287 printf("%s already running.\n%d\n", execname ,found->pid);
276 return EXIT_SUCCESS; 288 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
289 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
277 } 290 }
278 *--argv = startas; 291 *--argv = startas;
279 if (opt & SSD_OPT_BACKGROUND) { 292 if (opt & SSD_OPT_BACKGROUND) {
@@ -282,10 +295,9 @@ start_stop_daemon_main(int argc, char **argv)
282 } 295 }
283 if (opt & SSD_OPT_MAKEPID) { 296 if (opt & SSD_OPT_MAKEPID) {
284 /* user wants _us_ to make the pidfile */ 297 /* user wants _us_ to make the pidfile */
285 FILE *pidf = fopen(pidfile, "w"); 298 FILE *pidf = bb_xfopen(pidfile, "w");
299
286 pid_t pidt = getpid(); 300 pid_t pidt = getpid();
287 if (pidf == NULL)
288 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
289 fprintf(pidf, "%d\n", pidt); 301 fprintf(pidf, "%d\n", pidt);
290 fclose(pidf); 302 fclose(pidf);
291 } 303 }