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>,
6 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #include <sys/resource.h>
15 static int signal_nr = 15;
16 static int user_id = -1;
18 static char *userspec;
21 static char *execname;
25 struct pid_list *next;
29 static struct pid_list *found;
31 static inline void push(pid_t pid)
35 p = xmalloc(sizeof(*p));
41 static int pid_is_exec(pid_t pid, const char *name)
43 char buf[sizeof("/proc//exe") + sizeof(int)*3];
48 sprintf(buf, "/proc/%d/exe", pid);
49 sz = strlen(name) + 1;
50 execbuf = xzalloc(sz);
51 readlink(buf, execbuf, sz);
53 /* if readlink fails, execbuf still contains "" */
54 equal = !strcmp(execbuf, name);
55 if (ENABLE_FEATURE_CLEAN_UP)
60 static int pid_is_user(int pid, int uid)
63 char buf[sizeof("/proc/") + sizeof(int)*3];
65 sprintf(buf, "/proc/%u", pid);
66 if (stat(buf, &sb) != 0)
68 return (sb.st_uid == uid);
71 static int pid_is_cmd(pid_t pid, const char *name)
73 char fname[sizeof("/proc//stat") + sizeof(int)*3];
77 sprintf(fname, "/proc/%u/stat", pid);
78 buf = xmalloc_open_read_close(fname, NULL);
80 char *p = strchr(buf, '(');
82 char *pe = strrchr(++p, ')');
94 static void check(int pid)
96 if (execname && !pid_is_exec(pid, execname)) {
99 if (userspec && !pid_is_user(pid, user_id)) {
102 if (cmdname && !pid_is_cmd(pid, cmdname)) {
109 static void do_pidfile(void)
114 f = fopen(pidfile, "r");
116 if (fscanf(f, "%u", &pid) == 1)
119 } else if (errno != ENOENT)
120 bb_perror_msg_and_die("open pidfile %s", pidfile);
123 static void do_procinit(void)
126 struct dirent *entry;
134 procdir = xopendir("/proc");
137 while ((entry = readdir(procdir)) != NULL) {
138 pid = bb_strtou(entry->d_name, NULL, 10);
146 bb_error_msg_and_die ("nothing in /proc - not mounted?");
150 static int do_stop(void)
159 what = xstrdup(cmdname);
161 what = xstrdup(execname);
163 what = xasprintf("process in pidfile '%s'", pidfile);
165 what = xasprintf("process(es) owned by '%s'", userspec);
167 bb_error_msg_and_die("internal error, please report");
171 printf("no %s found; none killed\n", what);
172 if (ENABLE_FEATURE_CLEAN_UP)
176 for (p = found; p; p = p->next) {
177 if (kill(p->pid, signal_nr) == 0) {
181 bb_perror_msg("warning: failed to kill %d", p->pid);
184 if (!quiet && killed) {
185 printf("stopped %s (pid", what);
186 for (p = found; p; p = p->next)
188 printf(" %d", -p->pid);
191 if (ENABLE_FEATURE_CLEAN_UP)
196 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
197 static const struct option long_options[] = {
198 { "stop", 0, NULL, 'K' },
199 { "start", 0, NULL, 'S' },
200 { "background", 0, NULL, 'b' },
201 { "quiet", 0, NULL, 'q' },
202 { "make-pidfile", 0, NULL, 'm' },
203 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
204 { "oknodo", 0, NULL, 'o' },
205 { "verbose", 0, NULL, 'v' },
206 { "nicelevel", 1, NULL, 'N' },
208 { "startas", 1, NULL, 'a' },
209 { "name", 1, NULL, 'n' },
210 { "signal", 1, NULL, 's' },
211 { "user", 1, NULL, 'u' },
212 { "chuid", 1, NULL, 'c' },
213 { "exec", 1, NULL, 'x' },
214 { "pidfile", 1, NULL, 'p' },
215 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
216 { "retry", 1, NULL, 'R' },
225 OPT_BACKGROUND = 0x4,
228 OPT_OKNODO = 0x20 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
229 OPT_VERBOSE = 0x40 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
230 OPT_NICELEVEL = 0x80 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
233 int start_stop_daemon_main(int argc, char **argv)
236 char *signame = NULL;
237 char *startas = NULL;
238 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
239 // char *retry_arg = NULL;
243 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
244 applet_long_options = long_options;
247 /* Check required one context option was given */
248 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
249 opt = getopt32(argc, argv, "KSbqm"
250 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
251 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
253 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
254 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
255 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
257 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
260 signal_nr = get_signum(signame);
261 if (signal_nr < 0) bb_show_usage();
267 // USE_FEATURE_START_STOP_DAEMON_FANCY(
269 // retries = xatoi_u(retry_arg);
275 user_id = bb_strtou(userspec, NULL, 10);
277 user_id = xuname2uid(userspec);
280 if (opt & CTX_STOP) {
282 return (opt & OPT_OKNODO) ? 0 : (i<=0);
289 printf("%s already running\n%d\n", execname, found->pid);
290 return !(opt & OPT_OKNODO);
293 if (opt & OPT_BACKGROUND) {
297 if (opt & OPT_MAKEPID) {
298 /* user wants _us_ to make the pidfile */
299 FILE *pidf = xfopen(pidfile, "w");
301 pid_t pidt = getpid();
302 fprintf(pidf, "%d\n", pidt);
306 user_id = bb_strtou(chuid, NULL, 10);
308 user_id = xuname2uid(chuid);
311 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
312 if (opt & OPT_NICELEVEL) {
313 /* Set process priority */
314 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
315 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
316 bb_perror_msg_and_die("setpriority(%d)", prio);
320 execv(startas, argv);
321 bb_perror_msg_and_die("cannot start %s", startas);