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>
19 static int signal_nr = 15;
20 static int user_id = -1;
21 static char *userspec;
23 static char *execname;
25 static smallint quiet;
28 struct pid_list *next;
32 static struct pid_list *found;
34 static int pid_is_exec(pid_t pid, const char *name)
36 char buf[sizeof("/proc//exe") + sizeof(int)*3];
40 sprintf(buf, "/proc/%u/exe", pid);
42 execbuf = xzalloc(n + 1);
43 readlink(buf, execbuf, n);
45 /* if readlink fails, execbuf still contains "" */
46 n = strcmp(execbuf, name);
47 if (ENABLE_FEATURE_CLEAN_UP)
49 return ~n; /* nonzero (true) if execbuf == name */
52 static int pid_is_user(int pid, int uid)
55 char buf[sizeof("/proc/") + sizeof(int)*3];
57 sprintf(buf, "/proc/%u", pid);
58 if (stat(buf, &sb) != 0)
60 return (sb.st_uid == uid);
63 static int pid_is_cmd(pid_t pid, const char *name)
65 char fname[sizeof("/proc//stat") + sizeof(int)*3];
69 sprintf(fname, "/proc/%u/stat", pid);
70 buf = xmalloc_open_read_close(fname, NULL);
72 char *p = strchr(buf, '(');
74 char *pe = strrchr(++p, ')');
85 static void check(int pid)
89 if (execname && !pid_is_exec(pid, execname)) {
92 if (userspec && !pid_is_user(pid, user_id)) {
95 if (cmdname && !pid_is_cmd(pid, cmdname)) {
98 p = xmalloc(sizeof(*p));
104 static void do_pidfile(void)
109 f = fopen(pidfile, "r");
111 if (fscanf(f, "%u", &pid) == 1)
114 } else if (errno != ENOENT)
115 bb_perror_msg_and_die("open pidfile %s", pidfile);
118 static void do_procinit(void)
121 struct dirent *entry;
129 procdir = xopendir("/proc");
132 while ((entry = readdir(procdir)) != NULL) {
133 pid = bb_strtou(entry->d_name, NULL, 10);
141 bb_error_msg_and_die("nothing in /proc - not mounted?");
144 static int do_stop(void)
153 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
154 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
155 } else if (execname) {
156 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
157 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
159 what = xasprintf("process in pidfile '%s'", pidfile);
161 what = xasprintf("process(es) owned by '%s'", userspec);
163 bb_error_msg_and_die("internal error, please report");
167 printf("no %s found; none killed\n", what);
171 for (p = found; p; p = p->next) {
172 if (kill(p->pid, signal_nr) == 0) {
176 bb_perror_msg("warning: killing process %u", p->pid);
179 if (!quiet && killed) {
180 printf("stopped %s (pid", what);
181 for (p = found; p; p = p->next)
183 printf(" %u", - p->pid);
187 if (ENABLE_FEATURE_CLEAN_UP)
192 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
193 static const char start_stop_daemon_longopts[] =
194 "stop\0" No_argument "K"
195 "start\0" No_argument "S"
196 "background\0" No_argument "b"
197 "quiet\0" No_argument "q"
198 "make-pidfile\0" No_argument "m"
199 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
200 "oknodo\0" No_argument "o"
201 "verbose\0" No_argument "v"
202 "nicelevel\0" Required_argument "N"
204 "startas\0" Required_argument "a"
205 "name\0" Required_argument "n"
206 "signal\0" Required_argument "s"
207 "user\0" Required_argument "u"
208 "chuid\0" Required_argument "c"
209 "exec\0" Required_argument "x"
210 "pidfile\0" Required_argument "p"
211 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
212 "retry\0" Required_argument "R"
220 OPT_BACKGROUND = 0x4, // -b
221 OPT_QUIET = 0x8, // -q
222 OPT_MAKEPID = 0x10, // -m
230 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
231 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
232 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
235 int start_stop_daemon_main(int argc, char **argv);
236 int start_stop_daemon_main(int argc, char **argv)
242 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
243 // char *retry_arg = NULL;
247 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
248 applet_long_options = start_stop_daemon_longopts;
251 /* Check required one context option was given */
252 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
253 opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
254 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
255 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
256 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
257 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
258 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
261 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
264 signal_nr = get_signum(signame);
265 if (signal_nr < 0) bb_show_usage();
271 // USE_FEATURE_START_STOP_DAEMON_FANCY(
273 // retries = xatoi_u(retry_arg);
279 user_id = bb_strtou(userspec, NULL, 10);
281 user_id = xuname2uid(userspec);
284 if (opt & CTX_STOP) {
286 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
293 printf("%s already running\n%d\n", execname, found->pid);
294 return !(opt & OPT_OKNODO);
297 if (opt & OPT_BACKGROUND) {
302 if (pid < 0) /* error */
303 bb_perror_msg_and_die("vfork");
304 if (pid == 0) /* parent */
308 /* Redirect stdio to /dev/null, close extra FDs.
309 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
310 bb_daemonize_or_rexec(
312 + DAEMON_CLOSE_EXTRA_FDS
313 + DAEMON_ONLY_SANITIZE,
314 NULL /* argv, unused */ );
317 if (opt & OPT_MAKEPID) {
318 /* user wants _us_ to make the pidfile */
319 FILE *pidf = xfopen(pidfile, "w");
321 pid_t pidt = getpid();
322 fprintf(pidf, "%d\n", pidt);
326 struct bb_uidgid_t ugid;
327 parse_chown_usergroup_or_die(&ugid, chuid);
328 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
329 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
331 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
332 if (opt & OPT_NICELEVEL) {
333 /* Set process priority */
334 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
335 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
336 bb_perror_msg_and_die("setpriority(%d)", prio);
340 execv(startas, argv);
341 bb_perror_msg_and_die("cannot start %s", startas);