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 = bb_xopendir("/proc");
149 while ((entry = readdir(procdir)) != NULL) {
150 if (sscanf(entry->d_name, "%d", &pid) != 1)
157 bb_error_msg_and_die ("nothing in /proc - not mounted?");
171 strcpy(what, cmdname);
173 strcpy(what, execname);
175 sprintf(what, "process in pidfile `%.200s'", pidfile);
177 sprintf(what, "process(es) owned by `%s'", userspec);
179 bb_error_msg_and_die ("internal error, please report");
183 printf("no %s found; none killed.\n", what);
186 for (p = found; p; p = p->next) {
187 if (kill(p->pid, signal_nr) == 0) {
191 bb_perror_msg("warning: failed to kill %d", p->pid);
194 if (!quiet && killed) {
195 printf("stopped %s (pid", what);
196 for (p = found; p; p = p->next)
198 printf(" %d", -p->pid);
204 static const struct option ssd_long_options[] = {
205 { "stop", 0, NULL, 'K' },
206 { "start", 0, NULL, 'S' },
207 { "background", 0, NULL, 'b' },
208 { "quiet", 0, NULL, 'q' },
209 { "make-pidfile", 0, NULL, 'm' },
210 { "startas", 1, NULL, 'a' },
211 { "name", 1, NULL, 'n' },
212 { "signal", 1, NULL, 's' },
213 { "user", 1, NULL, 'u' },
214 { "exec", 1, NULL, 'x' },
215 { "pidfile", 1, NULL, 'p' },
219 #define SSD_CTX_STOP 1
220 #define SSD_CTX_START 2
221 #define SSD_OPT_BACKGROUND 4
222 #define SSD_OPT_QUIET 8
223 #define SSD_OPT_MAKEPID 16
226 start_stop_daemon_main(int argc, char **argv)
229 char *signame = NULL;
230 char *startas = NULL;
232 bb_applet_long_options = ssd_long_options;
234 /* Check required one context option was given */
235 bb_opt_complementally = "K:S:?:K--S:S--K";
236 opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
237 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
240 quiet = opt & SSD_OPT_QUIET;
243 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
246 if (!execname && !pidfile && !userspec && !cmdname)
247 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
252 if ((opt & SSD_CTX_START) && !startas)
253 bb_error_msg_and_die ("-S needs -x or -a");
255 if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
256 bb_error_msg_and_die ("-m needs -p");
261 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
262 user_id = bb_xgetpwnam(userspec);
264 if (opt & SSD_CTX_STOP) {
273 printf("%s already running.\n%d\n", execname ,found->pid);
277 if (opt & SSD_OPT_BACKGROUND) {
281 if (opt & SSD_OPT_MAKEPID) {
282 /* user wants _us_ to make the pidfile */
283 FILE *pidf = fopen(pidfile, "w");
284 pid_t pidt = getpid();
286 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
287 fprintf(pidf, "%d\n", pidt);
290 execv(startas, argv);
291 bb_perror_msg_and_die ("unable to start %s", startas);