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;
27 struct pid_list *found;
36 #define G (*(struct globals*)&bb_common_bufsiz1)
37 #define found (G.found )
38 #define userspec (G.userspec )
39 #define cmdname (G.cmdname )
40 #define execname (G.execname )
41 #define pidfile (G.pidfile )
42 #define user_id (G.user_id )
43 #define quiet (G.quiet )
44 #define signal_nr (G.signal_nr )
52 static int pid_is_exec(pid_t pid, const char *name)
54 char buf[sizeof("/proc//exe") + sizeof(int)*3];
58 sprintf(buf, "/proc/%u/exe", pid);
60 execbuf = xzalloc(n + 1);
61 readlink(buf, execbuf, n);
62 /* if readlink fails because link target is longer than strlen(name),
63 * execbuf still contains "", and strcmp will return !0. */
64 n = strcmp(execbuf, name);
65 if (ENABLE_FEATURE_CLEAN_UP)
67 return !n; /* nonzero (true) if execbuf == name */
70 static int pid_is_user(int pid, int uid)
73 char buf[sizeof("/proc/") + sizeof(int)*3];
75 sprintf(buf, "/proc/%u", pid);
76 if (stat(buf, &sb) != 0)
78 return (sb.st_uid == uid);
81 static int pid_is_cmd(pid_t pid, const char *name)
83 char fname[sizeof("/proc//stat") + sizeof(int)*3];
87 sprintf(fname, "/proc/%u/stat", pid);
88 buf = xmalloc_open_read_close(fname, NULL);
90 char *p = strchr(buf, '(');
92 char *pe = strrchr(++p, ')');
103 static void check(int pid)
107 if (execname && !pid_is_exec(pid, execname)) {
110 if (userspec && !pid_is_user(pid, user_id)) {
113 if (cmdname && !pid_is_cmd(pid, cmdname)) {
116 p = xmalloc(sizeof(*p));
122 static void do_pidfile(void)
127 f = fopen(pidfile, "r");
129 if (fscanf(f, "%u", &pid) == 1)
132 } else if (errno != ENOENT)
133 bb_perror_msg_and_die("open pidfile %s", pidfile);
136 static void do_procinit(void)
139 struct dirent *entry;
147 procdir = xopendir("/proc");
150 while ((entry = readdir(procdir)) != NULL) {
151 pid = bb_strtou(entry->d_name, NULL, 10);
158 bb_error_msg_and_die("nothing in /proc - not mounted?");
161 static int do_stop(void)
170 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
171 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
172 } else if (execname) {
173 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
174 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
176 what = xasprintf("process in pidfile '%s'", pidfile);
178 what = xasprintf("process(es) owned by '%s'", userspec);
180 bb_error_msg_and_die("internal error, please report");
184 printf("no %s found; none killed\n", what);
188 for (p = found; p; p = p->next) {
189 if (kill(p->pid, signal_nr) == 0) {
193 bb_perror_msg("warning: killing process %u", p->pid);
196 if (!quiet && killed) {
197 printf("stopped %s (pid", what);
198 for (p = found; p; p = p->next)
200 printf(" %u", - p->pid);
204 if (ENABLE_FEATURE_CLEAN_UP)
209 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
210 static const char start_stop_daemon_longopts[] ALIGN1 =
211 "stop\0" No_argument "K"
212 "start\0" No_argument "S"
213 "background\0" No_argument "b"
214 "quiet\0" No_argument "q"
215 "make-pidfile\0" No_argument "m"
216 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
217 "oknodo\0" No_argument "o"
218 "verbose\0" No_argument "v"
219 "nicelevel\0" Required_argument "N"
221 "startas\0" Required_argument "a"
222 "name\0" Required_argument "n"
223 "signal\0" Required_argument "s"
224 "user\0" Required_argument "u"
225 "chuid\0" Required_argument "c"
226 "exec\0" Required_argument "x"
227 "pidfile\0" Required_argument "p"
228 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
229 "retry\0" Required_argument "R"
237 OPT_BACKGROUND = 0x4, // -b
238 OPT_QUIET = 0x8, // -q
239 OPT_MAKEPID = 0x10, // -m
247 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
248 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
249 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
252 int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
253 int start_stop_daemon_main(int argc ATTRIBUTE_UNUSED, char **argv)
259 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
260 // char *retry_arg = NULL;
267 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
268 applet_long_options = start_stop_daemon_longopts;
271 /* Check required one context option was given */
272 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
273 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
274 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
275 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
276 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
277 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
278 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
281 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
284 signal_nr = get_signum(signame);
285 if (signal_nr < 0) bb_show_usage();
291 // USE_FEATURE_START_STOP_DAEMON_FANCY(
293 // retries = xatoi_u(retry_arg);
299 user_id = bb_strtou(userspec, NULL, 10);
301 user_id = xuname2uid(userspec);
304 if (opt & CTX_STOP) {
306 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
313 printf("%s already running\n%d\n", execname, found->pid);
314 return !(opt & OPT_OKNODO);
317 if (opt & OPT_BACKGROUND) {
322 if (pid < 0) /* error */
323 bb_perror_msg_and_die("vfork");
326 /* why _exit? the child may have changed the stack,
327 * so "return 0" may do bad things */
331 setsid(); /* detach from controlling tty */
332 /* Redirect stdio to /dev/null, close extra FDs.
333 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
334 bb_daemonize_or_rexec(
336 + DAEMON_CLOSE_EXTRA_FDS
337 + DAEMON_ONLY_SANITIZE,
338 NULL /* argv, unused */ );
341 if (opt & OPT_MAKEPID) {
342 /* user wants _us_ to make the pidfile */
343 write_pidfile(pidfile);
346 struct bb_uidgid_t ugid;
347 parse_chown_usergroup_or_die(&ugid, chuid);
348 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
349 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
351 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
352 if (opt & OPT_NICELEVEL) {
353 /* Set process priority */
354 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
355 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
356 bb_perror_msg_and_die("setpriority(%d)", prio);
360 execv(startas, argv);
361 bb_perror_msg_and_die("cannot start %s", startas);