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.
11 /* NB: we have a problem here with /proc/NN/exe usage, similar to
12 * one fixed in killall/pidof */
14 #include <sys/resource.h>
16 /* Override ENABLE_FEATURE_PIDFILE */
17 #define WANT_PIDFILE 1
21 struct pid_list *next;
28 OPT_BACKGROUND = (1 << 2), // -b
29 OPT_QUIET = (1 << 3), // -q
30 OPT_MAKEPID = (1 << 4), // -m
31 OPT_a = (1 << 5), // -a
32 OPT_n = (1 << 6), // -n
33 OPT_s = (1 << 7), // -s
34 OPT_u = (1 << 8), // -u
35 OPT_c = (1 << 9), // -c
36 OPT_x = (1 << 10), // -x
37 OPT_p = (1 << 11), // -p
38 OPT_OKNODO = (1 << 12) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
39 OPT_VERBOSE = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
40 OPT_NICELEVEL = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
42 #define QUIET (option_mask32 & OPT_QUIET)
45 struct pid_list *found;
54 #define G (*(struct globals*)&bb_common_bufsiz1)
55 #define found (G.found )
56 #define userspec (G.userspec )
57 #define cmdname (G.cmdname )
58 #define execname (G.execname )
59 #define pidfile (G.pidfile )
60 #define user_id (G.user_id )
61 #define signal_nr (G.signal_nr )
62 #define execstat (G.execstat )
70 static int pid_is_exec(pid_t pid)
73 char buf[sizeof("/proc//exe") + sizeof(int)*3];
75 sprintf(buf, "/proc/%u/exe", pid);
76 if (stat(buf, &st) < 0)
78 if (st.st_dev == execstat.st_dev
79 && st.st_ino == execstat.st_ino)
84 static int pid_is_user(int pid)
87 char buf[sizeof("/proc/") + sizeof(int)*3];
89 sprintf(buf, "/proc/%u", pid);
90 if (stat(buf, &sb) != 0)
92 return (sb.st_uid == user_id);
95 static int pid_is_cmd(pid_t pid)
97 char buf[256]; /* is it big enough? */
100 sprintf(buf, "/proc/%u/stat", pid);
101 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
103 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
104 p = strchr(buf, '(');
107 pe = strrchr(++p, ')');
111 return !strcmp(p, cmdname);
114 static void check(int pid)
118 if (execname && !pid_is_exec(pid)) {
121 if (userspec && !pid_is_user(pid)) {
124 if (cmdname && !pid_is_cmd(pid)) {
127 p = xmalloc(sizeof(*p));
133 static void do_pidfile(void)
138 f = fopen(pidfile, "r");
140 if (fscanf(f, "%u", &pid) == 1)
143 } else if (errno != ENOENT)
144 bb_perror_msg_and_die("open pidfile %s", pidfile);
147 static void do_procinit(void)
150 struct dirent *entry;
158 procdir = xopendir("/proc");
162 errno = 0; /* clear any previous error */
163 entry = readdir(procdir);
164 // TODO: check for exact errno(s) which mean that we got stale entry
165 if (errno) /* Stale entry, process has died after opendir */
167 if (!entry) /* EOF, no more entries */
169 pid = bb_strtou(entry->d_name, NULL, 10);
176 bb_error_msg_and_die("nothing in /proc - not mounted?");
179 static int do_stop(void)
186 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
187 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
188 } else if (execname) {
189 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
190 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
192 what = xasprintf("process in pidfile '%s'", pidfile);
194 what = xasprintf("process(es) owned by '%s'", userspec);
196 bb_error_msg_and_die("internal error, please report");
200 printf("no %s found; none killed\n", what);
204 for (p = found; p; p = p->next) {
205 if (kill(p->pid, signal_nr) == 0) {
209 bb_perror_msg("warning: killing process %u", p->pid);
212 if (!QUIET && killed) {
213 printf("stopped %s (pid", what);
214 for (p = found; p; p = p->next)
216 printf(" %u", - p->pid);
220 if (ENABLE_FEATURE_CLEAN_UP)
225 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
226 static const char start_stop_daemon_longopts[] ALIGN1 =
227 "stop\0" No_argument "K"
228 "start\0" No_argument "S"
229 "background\0" No_argument "b"
230 "quiet\0" No_argument "q"
231 "make-pidfile\0" No_argument "m"
232 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
233 "oknodo\0" No_argument "o"
234 "verbose\0" No_argument "v"
235 "nicelevel\0" Required_argument "N"
237 "startas\0" Required_argument "a"
238 "name\0" Required_argument "n"
239 "signal\0" Required_argument "s"
240 "user\0" Required_argument "u"
241 "chuid\0" Required_argument "c"
242 "exec\0" Required_argument "x"
243 "pidfile\0" Required_argument "p"
244 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
245 "retry\0" Required_argument "R"
250 int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
251 int start_stop_daemon_main(int argc ATTRIBUTE_UNUSED, char **argv)
257 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
258 // char *retry_arg = NULL;
265 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
266 applet_long_options = start_stop_daemon_longopts;
269 /* -K or -S is required; they are mutually exclusive */
270 /* -p is required if -m is given */
271 /* -xpun (at least one) is required if -K is given */
272 /* -xa (at least one) is required if -S is given */
273 /* -q turns off -v */
274 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
275 USE_FEATURE_START_STOP_DAEMON_FANCY("q-v");
276 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
277 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
278 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
279 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
280 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
281 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
285 signal_nr = get_signum(signame);
286 if (signal_nr < 0) bb_show_usage();
292 // USE_FEATURE_START_STOP_DAEMON_FANCY(
294 // retries = xatoi_u(retry_arg);
300 user_id = bb_strtou(userspec, NULL, 10);
302 user_id = xuname2uid(userspec);
305 xstat(execname, &execstat);
307 do_procinit(); /* Both start and stop needs to know current processes */
309 if (opt & CTX_STOP) {
311 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
316 printf("%s already running\n%d\n", execname, found->pid);
317 return !(opt & OPT_OKNODO);
320 if (opt & OPT_BACKGROUND) {
325 if (pid < 0) /* error */
326 bb_perror_msg_and_die("vfork");
329 /* why _exit? the child may have changed the stack,
330 * so "return 0" may do bad things */
334 setsid(); /* detach from controlling tty */
335 /* Redirect stdio to /dev/null, close extra FDs.
336 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
337 bb_daemonize_or_rexec(
339 + DAEMON_CLOSE_EXTRA_FDS
340 + DAEMON_ONLY_SANITIZE,
341 NULL /* argv, unused */ );
344 if (opt & OPT_MAKEPID) {
345 /* user wants _us_ to make the pidfile */
346 write_pidfile(pidfile);
349 struct bb_uidgid_t ugid;
350 parse_chown_usergroup_or_die(&ugid, chuid);
351 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
352 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
354 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
355 if (opt & OPT_NICELEVEL) {
356 /* Set process priority */
357 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
358 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
359 bb_perror_msg_and_die("setpriority(%d)", prio);
363 execv(startas, argv);
364 bb_perror_msg_and_die("cannot start %s", startas);