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>
26 static int fork_before_exec = 0;
27 static int signal_nr = 15;
28 static int user_id = -1;
29 static char *userspec = NULL;
30 static char *cmdname = NULL;
31 static char *execname = NULL;
32 static char *startas = NULL;
34 typedef struct pid_list {
35 struct pid_list *next;
39 static pid_list *found = NULL;
46 p = xmalloc(sizeof(*p));
53 pid_is_exec(int pid, const char *exec)
58 sprintf(buf, "/proc/%d/cmdline", pid);
60 if (fp && fgets (buf, sizeof (buf), fp) ) {
62 if (strncmp (buf, exec, strlen(exec)) == 0)
70 pid_is_user(int pid, int uid)
75 sprintf(buf, "/proc/%d", pid);
76 if (stat(buf, &sb) != 0)
78 return (sb.st_uid == uid);
83 pid_is_cmd(int pid, const char *name)
89 sprintf(buf, "/proc/%d/stat", pid);
93 while ((c = getc(f)) != EOF && c != '(')
99 /* this hopefully handles command names containing ')' */
100 while ((c = getc(f)) != EOF && c == *name)
103 return (c == ')' && *name == '\0');
110 if (execname && !pid_is_exec(pid, execname)) {
113 if (userspec && !pid_is_user(pid, user_id)) {
116 if (cmdname && !pid_is_cmd(pid, cmdname)) {
128 struct dirent *entry;
131 procdir = opendir("/proc");
133 bb_perror_msg_and_die ("opendir /proc");
136 while ((entry = readdir(procdir)) != NULL) {
137 if (sscanf(entry->d_name, "%d", &pid) != 1)
144 bb_error_msg_and_die ("nothing in /proc - not mounted?");
156 strcpy(what, cmdname);
158 strcpy(what, execname);
160 sprintf(what, "process(es) owned by `%s'", userspec);
162 bb_error_msg_and_die ("internal error, please report");
165 printf("no %s found; none killed.\n", what);
168 for (p = found; p; p = p->next) {
169 if (kill(p->pid, signal_nr) == 0) {
173 bb_perror_msg("warning: failed to kill %d:", p->pid);
177 printf("stopped %s (pid", what);
178 for (p = found; p; p = p->next)
180 printf(" %d", -p->pid);
186 static const struct option ssd_long_options[] = {
187 { "stop", 0, NULL, 'K' },
188 { "start", 0, NULL, 'S' },
189 { "background", 0, NULL, 'b' },
190 { "startas", 1, NULL, 'a' },
191 { "name", 1, NULL, 'n' },
192 { "signal", 1, NULL, 's' },
193 { "user", 1, NULL, 'u' },
194 { "exec", 1, NULL, 'x' },
199 start_stop_daemon_main(int argc, char **argv)
202 char *signame = NULL;
203 bb_applet_long_options = ssd_long_options;
205 flags = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:",
206 &startas, &cmdname, &signame, &userspec, &execname);
208 /* Be sneaky and avoid branching */
211 fork_before_exec = (flags & 4);
214 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
218 bb_error_msg_and_die ("need exactly one of -S or -K");
220 if (!execname && !userspec)
221 bb_error_msg_and_die ("need at least one of -x or -u");
226 if (start && !startas)
227 bb_error_msg_and_die ("-S needs -x or -a");
232 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
233 user_id = my_getpwnam(userspec);
243 printf("%s already running.\n%d\n", execname ,found->pid);
247 if (fork_before_exec) {
248 if (daemon(0, 0) == -1)
249 bb_perror_msg_and_die ("unable to fork");
252 execv(startas, argv);
253 bb_perror_msg_and_die ("unable to start %s", startas);