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>
20 #include <getopt.h> /* struct option */
23 static int signal_nr = 15;
24 static int user_id = -1;
26 static char *userspec = NULL;
27 static char *cmdname = NULL;
28 static char *execname = NULL;
29 static char *pidfile = NULL;
32 struct pid_list *next;
36 static struct pid_list *found = NULL;
43 p = xmalloc(sizeof(*p));
50 pid_is_exec(pid_t pid, const char *name)
53 struct stat sb, exec_stat;
56 xstat(name, &exec_stat);
58 sprintf(buf, "/proc/%d/exe", pid);
59 if (stat(buf, &sb) != 0)
61 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
65 pid_is_user(int pid, int uid)
70 sprintf(buf, "/proc/%d", pid);
71 if (stat(buf, &sb) != 0)
73 return (sb.st_uid == uid);
77 pid_is_cmd(pid_t pid, const char *name)
83 sprintf(buf, "/proc/%d/stat", pid);
87 while ((c = getc(f)) != EOF && c != '(')
93 /* this hopefully handles command names containing ')' */
94 while ((c = getc(f)) != EOF && c == *name)
97 return (c == ')' && *name == '\0');
104 if (execname && !pid_is_exec(pid, execname)) {
107 if (userspec && !pid_is_user(pid, user_id)) {
110 if (cmdname && !pid_is_cmd(pid, cmdname)) {
123 f = fopen(pidfile, "r");
125 if (fscanf(f, "%d", &pid) == 1)
128 } else if (errno != ENOENT)
129 bb_perror_msg_and_die("open pidfile %s", pidfile);
137 struct dirent *entry;
145 procdir = bb_xopendir("/proc");
148 while ((entry = readdir(procdir)) != NULL) {
149 if (sscanf(entry->d_name, "%d", &pid) != 1)
156 bb_error_msg_and_die ("nothing in /proc - not mounted?");
170 strcpy(what, cmdname);
172 strcpy(what, execname);
174 sprintf(what, "process in pidfile `%.200s'", pidfile);
176 sprintf(what, "process(es) owned by `%s'", userspec);
178 bb_error_msg_and_die ("internal error, please report");
182 printf("no %s found; none killed.\n", what);
185 for (p = found; p; p = p->next) {
186 if (kill(p->pid, signal_nr) == 0) {
190 bb_perror_msg("warning: failed to kill %d", p->pid);
193 if (!quiet && killed) {
194 printf("stopped %s (pid", what);
195 for (p = found; p; p = p->next)
197 printf(" %d", -p->pid);
202 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
203 static const struct option ssd_long_options[] = {
204 { "stop", 0, NULL, 'K' },
205 { "start", 0, NULL, 'S' },
206 { "background", 0, NULL, 'b' },
207 { "quiet", 0, NULL, 'q' },
208 { "make-pidfile", 0, NULL, 'm' },
209 { "startas", 1, NULL, 'a' },
210 { "name", 1, NULL, 'n' },
211 { "signal", 1, NULL, 's' },
212 { "user", 1, NULL, 'u' },
213 { "exec", 1, NULL, 'x' },
214 { "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 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
233 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) {
283 if (opt & SSD_OPT_MAKEPID) {
284 /* user wants _us_ to make the pidfile */
285 FILE *pidf = fopen(pidfile, "w");
286 pid_t pidt = getpid();
288 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
289 fprintf(pidf, "%d\n", pidt);
292 execv(startas, argv);
293 bb_perror_msg_and_die ("unable to start %s", startas);