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;
38 static inline void push(pid_t pid)
42 p = xmalloc(sizeof(*p));
48 static int pid_is_exec(pid_t pid, const char *name)
51 struct stat sb, exec_stat;
54 xstat(name, &exec_stat);
56 sprintf(buf, "/proc/%d/exe", pid);
57 if (stat(buf, &sb) != 0)
59 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
62 static int pid_is_user(int pid, int uid)
67 sprintf(buf, "/proc/%d", pid);
68 if (stat(buf, &sb) != 0)
70 return (sb.st_uid == uid);
73 static int pid_is_cmd(pid_t pid, const char *name)
79 sprintf(buf, "/proc/%d/stat", pid);
83 while ((c = getc(f)) != EOF && c != '(')
89 /* this hopefully handles command names containing ')' */
90 while ((c = getc(f)) != EOF && c == *name)
93 return (c == ')' && *name == '\0');
97 static void check(int pid)
99 if (execname && !pid_is_exec(pid, execname)) {
102 if (userspec && !pid_is_user(pid, user_id)) {
105 if (cmdname && !pid_is_cmd(pid, cmdname)) {
112 static void do_pidfile(void)
117 f = fopen(pidfile, "r");
119 if (fscanf(f, "%d", &pid) == 1)
122 } else if (errno != ENOENT)
123 bb_perror_msg_and_die("open pidfile %s", pidfile);
127 static void do_procinit(void)
130 struct dirent *entry;
138 procdir = bb_xopendir("/proc");
141 while ((entry = readdir(procdir)) != NULL) {
142 if (sscanf(entry->d_name, "%d", &pid) != 1)
149 bb_error_msg_and_die ("nothing in /proc - not mounted?");
153 static int do_stop(void)
155 RESERVE_CONFIG_BUFFER(what, 1024);
162 strcpy(what, cmdname);
164 strcpy(what, execname);
166 sprintf(what, "process in pidfile `%.200s'", pidfile);
168 sprintf(what, "process(es) owned by `%s'", userspec);
170 bb_error_msg_and_die ("internal error, please report");
174 printf("no %s found; none killed.\n", what);
175 if (ENABLE_FEATURE_CLEAN_UP)
176 RELEASE_CONFIG_BUFFER(what);
179 for (p = found; p; p = p->next) {
180 if (kill(p->pid, signal_nr) == 0) {
184 bb_perror_msg("warning: failed to kill %d", p->pid);
187 if (!quiet && killed) {
188 printf("stopped %s (pid", what);
189 for (p = found; p; p = p->next)
191 printf(" %d", -p->pid);
194 if (ENABLE_FEATURE_CLEAN_UP)
195 RELEASE_CONFIG_BUFFER(what);
199 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
200 static const struct option ssd_long_options[] = {
201 { "stop", 0, NULL, 'K' },
202 { "start", 0, NULL, 'S' },
203 { "background", 0, NULL, 'b' },
204 { "quiet", 0, NULL, 'q' },
205 { "make-pidfile", 0, NULL, 'm' },
206 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
207 { "oknodo", 0, NULL, 'o' },
208 { "verbose", 0, NULL, 'v' },
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' },
216 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
217 { "retry", 1, NULL, 'R' },
223 #define SSD_CTX_STOP 1
224 #define SSD_CTX_START 2
225 #define SSD_OPT_BACKGROUND 4
226 #define SSD_OPT_QUIET 8
227 #define SSD_OPT_MAKEPID 16
228 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
229 #define SSD_OPT_OKNODO 32
230 #define SSD_OPT_VERBOSE 64
234 int start_stop_daemon_main(int argc, char **argv)
237 char *signame = NULL;
238 char *startas = NULL;
239 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
240 // char *retry_arg = NULL;
243 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
244 bb_applet_long_options = ssd_long_options;
247 /* Check required one context option was given */
248 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
249 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
250 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
251 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
253 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
254 ,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);
256 quiet = (opt & SSD_OPT_QUIET)
257 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
260 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
266 // USE_FEATURE_START_STOP_DAEMON_FANCY(
268 // retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
273 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
274 user_id = bb_xgetpwnam(userspec);
276 if (opt & SSD_CTX_STOP) {
279 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
287 printf("%s already running.\n%d\n", execname ,found->pid);
288 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
289 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
292 if (opt & SSD_OPT_BACKGROUND) {
296 if (opt & SSD_OPT_MAKEPID) {
297 /* user wants _us_ to make the pidfile */
298 FILE *pidf = bb_xfopen(pidfile, "w");
300 pid_t pidt = getpid();
301 fprintf(pidf, "%d\n", pidt);
304 execv(startas, argv);
305 bb_perror_msg_and_die ("unable to start %s", startas);