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 */
15 #include <sys/resource.h>
17 /* Override ENABLE_FEATURE_PIDFILE */
18 #define WANT_PIDFILE 1
22 struct pid_list *next;
28 struct pid_list *found;
37 #define G (*(struct globals*)&bb_common_bufsiz1)
38 #define found (G.found )
39 #define userspec (G.userspec )
40 #define cmdname (G.cmdname )
41 #define execname (G.execname )
42 #define pidfile (G.pidfile )
43 #define user_id (G.user_id )
44 #define quiet (G.quiet )
45 #define signal_nr (G.signal_nr )
53 static int pid_is_exec(pid_t pid, const char *name)
55 char buf[sizeof("/proc//exe") + sizeof(int)*3];
59 sprintf(buf, "/proc/%u/exe", pid);
61 execbuf = xzalloc(n + 1);
62 readlink(buf, execbuf, n);
63 /* if readlink fails because link target is longer than strlen(name),
64 * execbuf still contains "", and strcmp will return !0. */
65 n = strcmp(execbuf, name);
66 if (ENABLE_FEATURE_CLEAN_UP)
68 return !n; /* nonzero (true) if execbuf == name */
71 static int pid_is_user(int pid, int uid)
74 char buf[sizeof("/proc/") + sizeof(int)*3];
76 sprintf(buf, "/proc/%u", pid);
77 if (stat(buf, &sb) != 0)
79 return (sb.st_uid == uid);
82 static int pid_is_cmd(pid_t pid, const char *name)
84 char fname[sizeof("/proc//stat") + sizeof(int)*3];
88 sprintf(fname, "/proc/%u/stat", pid);
89 buf = xmalloc_open_read_close(fname, NULL);
91 char *p = strchr(buf, '(');
93 char *pe = strrchr(++p, ')');
104 static void check(int pid)
108 if (execname && !pid_is_exec(pid, execname)) {
111 if (userspec && !pid_is_user(pid, user_id)) {
114 if (cmdname && !pid_is_cmd(pid, cmdname)) {
117 p = xmalloc(sizeof(*p));
123 static void do_pidfile(void)
128 f = fopen(pidfile, "r");
130 if (fscanf(f, "%u", &pid) == 1)
133 } else if (errno != ENOENT)
134 bb_perror_msg_and_die("open pidfile %s", pidfile);
137 static void do_procinit(void)
140 struct dirent *entry;
148 procdir = xopendir("/proc");
151 while ((entry = readdir(procdir)) != NULL) {
152 pid = bb_strtou(entry->d_name, NULL, 10);
159 bb_error_msg_and_die("nothing in /proc - not mounted?");
162 static int do_stop(void)
171 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
172 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
173 } else if (execname) {
174 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
175 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
177 what = xasprintf("process in pidfile '%s'", pidfile);
179 what = xasprintf("process(es) owned by '%s'", userspec);
181 bb_error_msg_and_die("internal error, please report");
185 printf("no %s found; none killed\n", what);
189 for (p = found; p; p = p->next) {
190 if (kill(p->pid, signal_nr) == 0) {
194 bb_perror_msg("warning: killing process %u", p->pid);
197 if (!quiet && killed) {
198 printf("stopped %s (pid", what);
199 for (p = found; p; p = p->next)
201 printf(" %u", - p->pid);
205 if (ENABLE_FEATURE_CLEAN_UP)
210 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
211 static const char start_stop_daemon_longopts[] ALIGN1 =
212 "stop\0" No_argument "K"
213 "start\0" No_argument "S"
214 "background\0" No_argument "b"
215 "quiet\0" No_argument "q"
216 "make-pidfile\0" No_argument "m"
217 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
218 "oknodo\0" No_argument "o"
219 "verbose\0" No_argument "v"
220 "nicelevel\0" Required_argument "N"
222 "startas\0" Required_argument "a"
223 "name\0" Required_argument "n"
224 "signal\0" Required_argument "s"
225 "user\0" Required_argument "u"
226 "chuid\0" Required_argument "c"
227 "exec\0" Required_argument "x"
228 "pidfile\0" Required_argument "p"
229 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
230 "retry\0" Required_argument "R"
238 OPT_BACKGROUND = 0x4, // -b
239 OPT_QUIET = 0x8, // -q
240 OPT_MAKEPID = 0x10, // -m
248 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
249 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
250 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
253 int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
254 int start_stop_daemon_main(int argc, char **argv)
260 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
261 // char *retry_arg = NULL;
268 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
269 applet_long_options = start_stop_daemon_longopts;
272 /* Check required one context option was given */
273 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
274 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
275 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
276 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
277 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
278 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
279 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
282 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
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 if (opt & CTX_STOP) {
307 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
314 printf("%s already running\n%d\n", execname, found->pid);
315 return !(opt & OPT_OKNODO);
318 if (opt & OPT_BACKGROUND) {
323 if (pid < 0) /* error */
324 bb_perror_msg_and_die("vfork");
327 /* why _exit? the child may have changed the stack,
328 * so "return 0" may do bad things */
332 setsid(); /* detach from controlling tty */
333 /* Redirect stdio to /dev/null, close extra FDs.
334 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
335 bb_daemonize_or_rexec(
337 + DAEMON_CLOSE_EXTRA_FDS
338 + DAEMON_ONLY_SANITIZE,
339 NULL /* argv, unused */ );
342 if (opt & OPT_MAKEPID) {
343 /* user wants _us_ to make the pidfile */
344 write_pidfile(pidfile);
347 struct bb_uidgid_t ugid;
348 parse_chown_usergroup_or_die(&ugid, chuid);
349 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
350 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
352 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
353 if (opt & OPT_NICELEVEL) {
354 /* Set process priority */
355 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
356 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
357 bb_perror_msg_and_die("setpriority(%d)", prio);
361 execv(startas, argv);
362 bb_perror_msg_and_die("cannot start %s", startas);