aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-04-13 18:28:46 +0000
committerEric Andersen <andersen@codepoet.org>2004-04-13 18:28:46 +0000
commit625da9d61efd68a45bd5613450bcf96862ca8290 (patch)
treef07e653271f9aee4d03ffbc41d61efe7017d0170
parent3ddff210e65be3d1a1bfeefcfe6cf4a59ec07ef0 (diff)
downloadbusybox-w32-625da9d61efd68a45bd5613450bcf96862ca8290.tar.gz
busybox-w32-625da9d61efd68a45bd5613450bcf96862ca8290.tar.bz2
busybox-w32-625da9d61efd68a45bd5613450bcf96862ca8290.zip
Fix several problems with start-stop-daemon, add -m support
-rw-r--r--debianutils/start_stop_daemon.c72
-rw-r--r--include/usage.h1
2 files changed, 46 insertions, 27 deletions
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 6bc259fb3..e15944c59 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -116,18 +116,18 @@ check(int pid)
116 116
117 117
118static void 118static void
119do_pidfile(const char *name) 119do_pidfile(void)
120{ 120{
121 FILE *f; 121 FILE *f;
122 pid_t pid; 122 pid_t pid;
123 123
124 f = fopen(name, "r"); 124 f = fopen(pidfile, "r");
125 if (f) { 125 if (f) {
126 if (fscanf(f, "%d", &pid) == 1) 126 if (fscanf(f, "%d", &pid) == 1)
127 check(pid); 127 check(pid);
128 fclose(f); 128 fclose(f);
129 } else if (errno != ENOENT) 129 } else if (errno != ENOENT)
130 bb_perror_msg_and_die("open pidfile %s", name); 130 bb_perror_msg_and_die("open pidfile %s", pidfile);
131 131
132} 132}
133 133
@@ -138,6 +138,11 @@ do_procinit(void)
138 struct dirent *entry; 138 struct dirent *entry;
139 int foundany, pid; 139 int foundany, pid;
140 140
141 if (pidfile) {
142 do_pidfile();
143 return;
144 }
145
141 procdir = opendir("/proc"); 146 procdir = opendir("/proc");
142 if (!procdir) 147 if (!procdir)
143 bb_perror_msg_and_die ("opendir /proc"); 148 bb_perror_msg_and_die ("opendir /proc");
@@ -162,15 +167,14 @@ do_stop(void)
162 struct pid_list *p; 167 struct pid_list *p;
163 int killed = 0; 168 int killed = 0;
164 169
165 if (pidfile) 170 do_procinit();
166 do_pidfile(pidfile);
167 else
168 do_procinit();
169 171
170 if (cmdname) 172 if (cmdname)
171 strcpy(what, cmdname); 173 strcpy(what, cmdname);
172 else if (execname) 174 else if (execname)
173 strcpy(what, execname); 175 strcpy(what, execname);
176 else if (pidfile)
177 sprintf(what, "process in pidfile `%.200s'", pidfile);
174 else if (userspec) 178 else if (userspec)
175 sprintf(what, "process(es) owned by `%s'", userspec); 179 sprintf(what, "process(es) owned by `%s'", userspec);
176 else 180 else
@@ -186,7 +190,7 @@ do_stop(void)
186 p->pid = -p->pid; 190 p->pid = -p->pid;
187 killed++; 191 killed++;
188 } else { 192 } else {
189 bb_perror_msg("warning: failed to kill %d:", p->pid); 193 bb_perror_msg("warning: failed to kill %d", p->pid);
190 } 194 }
191 } 195 }
192 if (!quiet && killed) { 196 if (!quiet && killed) {
@@ -200,23 +204,25 @@ do_stop(void)
200 204
201 205
202static const struct option ssd_long_options[] = { 206static const struct option ssd_long_options[] = {
203 { "stop", 0, NULL, 'K' }, 207 { "stop", 0, NULL, 'K' },
204 { "start", 0, NULL, 'S' }, 208 { "start", 0, NULL, 'S' },
205 { "background", 0, NULL, 'b' }, 209 { "background", 0, NULL, 'b' },
206 { "quiet", 0, NULL, 'q' }, 210 { "quiet", 0, NULL, 'q' },
207 { "startas", 1, NULL, 'a' }, 211 { "make-pidfile", 0, NULL, 'm' },
208 { "name", 1, NULL, 'n' }, 212 { "startas", 1, NULL, 'a' },
209 { "signal", 1, NULL, 's' }, 213 { "name", 1, NULL, 'n' },
210 { "user", 1, NULL, 'u' }, 214 { "signal", 1, NULL, 's' },
211 { "exec", 1, NULL, 'x' }, 215 { "user", 1, NULL, 'u' },
212 { "pidfile", 1, NULL, 'p' }, 216 { "exec", 1, NULL, 'x' },
217 { "pidfile", 1, NULL, 'p' },
213 { 0, 0, 0, 0 } 218 { 0, 0, 0, 0 }
214}; 219};
215 220
216#define SSD_CTX_STOP 1 221#define SSD_CTX_STOP 1
217#define SSD_CTX_START 2 222#define SSD_CTX_START 2
218#define SSD_OPT_BACKGROUND 4 223#define SSD_OPT_BACKGROUND 4
219#define SSD_OPT_QUIET 8 224#define SSD_OPT_QUIET 8
225#define SSD_OPT_MAKEPID 16
220 226
221int 227int
222start_stop_daemon_main(int argc, char **argv) 228start_stop_daemon_main(int argc, char **argv)
@@ -228,7 +234,7 @@ start_stop_daemon_main(int argc, char **argv)
228 bb_applet_long_options = ssd_long_options; 234 bb_applet_long_options = ssd_long_options;
229 235
230 bb_opt_complementaly = "K~S:S~K"; 236 bb_opt_complementaly = "K~S:S~K";
231 opt = bb_getopt_ulflags(argc, argv, "KSbqa:n:s:u:x:p:", 237 opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
232 &startas, &cmdname, &signame, &userspec, &execname, &pidfile); 238 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
233 239
234 /* Check one and only one context option was given */ 240 /* Check one and only one context option was given */
@@ -240,8 +246,8 @@ start_stop_daemon_main(int argc, char **argv)
240 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); 246 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
241 } 247 }
242 248
243 if (!execname && !userspec) 249 if (!execname && !pidfile && !userspec && !cmdname)
244 bb_error_msg_and_die ("need at least one of -x or -u"); 250 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
245 251
246 if (!startas) 252 if (!startas)
247 startas = execname; 253 startas = execname;
@@ -249,19 +255,22 @@ start_stop_daemon_main(int argc, char **argv)
249 if ((opt & SSD_CTX_START) && !startas) 255 if ((opt & SSD_CTX_START) && !startas)
250 bb_error_msg_and_die ("-S needs -x or -a"); 256 bb_error_msg_and_die ("-S needs -x or -a");
251 257
258 if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
259 bb_error_msg_and_die ("-m needs -p");
260
252 argc -= optind; 261 argc -= optind;
253 argv += optind; 262 argv += optind;
254 263
255 if (userspec && sscanf(userspec, "%d", &user_id) != 1) 264 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
256 user_id = my_getpwnam(userspec); 265 user_id = my_getpwnam(userspec);
257 266
258 do_procinit();
259
260 if (opt & SSD_CTX_STOP) { 267 if (opt & SSD_CTX_STOP) {
261 do_stop(); 268 do_stop();
262 return EXIT_SUCCESS; 269 return EXIT_SUCCESS;
263 } 270 }
264 271
272 do_procinit();
273
265 if (found) { 274 if (found) {
266 if (!quiet) 275 if (!quiet)
267 printf("%s already running.\n%d\n", execname ,found->pid); 276 printf("%s already running.\n%d\n", execname ,found->pid);
@@ -271,8 +280,17 @@ start_stop_daemon_main(int argc, char **argv)
271 if (opt & SSD_OPT_BACKGROUND) { 280 if (opt & SSD_OPT_BACKGROUND) {
272 if (daemon(0, 0) == -1) 281 if (daemon(0, 0) == -1)
273 bb_perror_msg_and_die ("unable to fork"); 282 bb_perror_msg_and_die ("unable to fork");
283 setsid();
284 }
285 if (opt & SSD_OPT_MAKEPID) {
286 /* user wants _us_ to make the pidfile */
287 FILE *pidf = fopen(pidfile, "w");
288 pid_t pidt = getpid();
289 if (pidf == NULL)
290 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
291 fprintf(pidf, "%d\n", pidt);
292 fclose(pidf);
274 } 293 }
275 setsid();
276 execv(startas, argv); 294 execv(startas, argv);
277 bb_perror_msg_and_die ("unable to start %s", startas); 295 bb_perror_msg_and_die ("unable to start %s", startas);
278} 296}
diff --git a/include/usage.h b/include/usage.h
index 4401a763a..15e60d1d3 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2230,6 +2230,7 @@
2230 "\n\t-b|--background\t\t\tforce process into background"\ 2230 "\n\t-b|--background\t\t\tforce process into background"\
2231 "\n\t-u|--user <username>|<uid>\tstop this user's processes"\ 2231 "\n\t-u|--user <username>|<uid>\tstop this user's processes"\
2232 "\n\t-x|--exec <executable>\t\tprogram to either start or check"\ 2232 "\n\t-x|--exec <executable>\t\tprogram to either start or check"\
2233 "\n\t-m|--make-pidfile <filename>\tcreate the -p file and enter pid in it"\
2233 "\n\t-n|--name <process-name>\tstop processes with this name"\ 2234 "\n\t-n|--name <process-name>\tstop processes with this name"\
2234 "\n\t-p|--pidfile <pid-file>\t\tsave or load pid using a pid-file"\ 2235 "\n\t-p|--pidfile <pid-file>\t\tsave or load pid using a pid-file"\
2235 "\n\t-q|--quiet\t\t\tbe quiet" \ 2236 "\n\t-q|--quiet\t\t\tbe quiet" \