1 /* vi: set sw=4 ts=4: */
3 * Mini start-stop-daemon implementation(s) for busybox
5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
7 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
19 #include <getopt.h> /* struct option */
24 static int signal_nr = 15;
25 static int user_id = -1;
27 static char *userspec = NULL;
28 static char *cmdname = NULL;
29 static char *execname = NULL;
30 static char *pidfile = NULL;
33 struct pid_list *next;
37 static struct pid_list *found = NULL;
44 p = xmalloc(sizeof(*p));
51 pid_is_exec(pid_t pid, const char *name)
54 struct stat sb, exec_stat;
57 xstat(name, &exec_stat);
59 sprintf(buf, "/proc/%d/exe", pid);
60 if (stat(buf, &sb) != 0)
62 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
66 pid_is_user(int pid, int uid)
71 sprintf(buf, "/proc/%d", pid);
72 if (stat(buf, &sb) != 0)
74 return (sb.st_uid == uid);
78 pid_is_cmd(pid_t pid, const char *name)
84 sprintf(buf, "/proc/%d/stat", pid);
88 while ((c = getc(f)) != EOF && c != '(')
94 /* this hopefully handles command names containing ')' */
95 while ((c = getc(f)) != EOF && c == *name)
98 return (c == ')' && *name == '\0');
105 if (execname && !pid_is_exec(pid, execname)) {
108 if (userspec && !pid_is_user(pid, user_id)) {
111 if (cmdname && !pid_is_cmd(pid, cmdname)) {
124 f = fopen(pidfile, "r");
126 if (fscanf(f, "%d", &pid) == 1)
129 } else if (errno != ENOENT)
130 bb_perror_msg_and_die("open pidfile %s", pidfile);
138 struct dirent *entry;
146 procdir = opendir("/proc");
148 bb_perror_msg_and_die ("opendir /proc");
151 while ((entry = readdir(procdir)) != NULL) {
152 if (sscanf(entry->d_name, "%d", &pid) != 1)
159 bb_error_msg_and_die ("nothing in /proc - not mounted?");
173 strcpy(what, cmdname);
175 strcpy(what, execname);
177 sprintf(what, "process in pidfile `%.200s'", pidfile);
179 sprintf(what, "process(es) owned by `%s'", userspec);
181 bb_error_msg_and_die ("internal error, please report");
185 printf("no %s found; none killed.\n", what);
188 for (p = found; p; p = p->next) {
189 if (kill(p->pid, signal_nr) == 0) {
193 bb_perror_msg("warning: failed to kill %d", p->pid);
196 if (!quiet && killed) {
197 printf("stopped %s (pid", what);
198 for (p = found; p; p = p->next)
200 printf(" %d", -p->pid);
206 static const struct option ssd_long_options[] = {
207 { "stop", 0, NULL, 'K' },
208 { "start", 0, NULL, 'S' },
209 { "background", 0, NULL, 'b' },
210 { "quiet", 0, NULL, 'q' },
211 { "make-pidfile", 0, NULL, 'm' },
212 { "startas", 1, NULL, 'a' },
213 { "name", 1, NULL, 'n' },
214 { "signal", 1, NULL, 's' },
215 { "user", 1, NULL, 'u' },
216 { "exec", 1, NULL, 'x' },
217 { "pidfile", 1, NULL, 'p' },
221 #define SSD_CTX_STOP 1
222 #define SSD_CTX_START 2
223 #define SSD_OPT_BACKGROUND 4
224 #define SSD_OPT_QUIET 8
225 #define SSD_OPT_MAKEPID 16
228 start_stop_daemon_main(int argc, char **argv)
231 char *signame = NULL;
232 char *startas = NULL;
234 bb_applet_long_options = ssd_long_options;
236 /* Check required one context option was given */
237 bb_opt_complementally = "K:S:?:K--S:S--K";
238 opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
239 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
242 quiet = opt & SSD_OPT_QUIET;
245 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
248 if (!execname && !pidfile && !userspec && !cmdname)
249 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
254 if ((opt & SSD_CTX_START) && !startas)
255 bb_error_msg_and_die ("-S needs -x or -a");
257 if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
258 bb_error_msg_and_die ("-m needs -p");
263 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
264 user_id = bb_xgetpwnam(userspec);
266 if (opt & SSD_CTX_STOP) {
275 printf("%s already running.\n%d\n", execname ,found->pid);
279 if (opt & SSD_OPT_BACKGROUND) {
280 if (daemon(0, 0) == -1)
281 bb_perror_msg_and_die ("unable to fork");
284 if (opt & SSD_OPT_MAKEPID) {
285 /* user wants _us_ to make the pidfile */
286 FILE *pidf = fopen(pidfile, "w");
287 pid_t pidt = getpid();
289 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
290 fprintf(pidf, "%d\n", pidt);
293 execv(startas, argv);
294 bb_perror_msg_and_die ("unable to start %s", startas);