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;
17 static char *userspec;
19 static char *execname;
21 static smallint quiet;
24 struct pid_list *next;
28 static struct pid_list *found;
30 static inline void push(pid_t pid)
34 p = xmalloc(sizeof(*p));
40 static int pid_is_exec(pid_t pid, const char *name)
42 char buf[sizeof("/proc//exe") + sizeof(int)*3];
47 sprintf(buf, "/proc/%d/exe", pid);
48 sz = strlen(name) + 1;
49 execbuf = xzalloc(sz);
50 readlink(buf, execbuf, sz);
52 /* if readlink fails, execbuf still contains "" */
53 equal = !strcmp(execbuf, name);
54 if (ENABLE_FEATURE_CLEAN_UP)
59 static int pid_is_user(int pid, int uid)
62 char buf[sizeof("/proc/") + sizeof(int)*3];
64 sprintf(buf, "/proc/%u", pid);
65 if (stat(buf, &sb) != 0)
67 return (sb.st_uid == uid);
70 static int pid_is_cmd(pid_t pid, const char *name)
72 char fname[sizeof("/proc//stat") + sizeof(int)*3];
76 sprintf(fname, "/proc/%u/stat", pid);
77 buf = xmalloc_open_read_close(fname, NULL);
79 char *p = strchr(buf, '(');
81 char *pe = strrchr(++p, ')');
93 static void check(int pid)
95 if (execname && !pid_is_exec(pid, execname)) {
98 if (userspec && !pid_is_user(pid, user_id)) {
101 if (cmdname && !pid_is_cmd(pid, cmdname)) {
108 static void do_pidfile(void)
113 f = fopen(pidfile, "r");
115 if (fscanf(f, "%u", &pid) == 1)
118 } else if (errno != ENOENT)
119 bb_perror_msg_and_die("open pidfile %s", pidfile);
122 static void do_procinit(void)
125 struct dirent *entry;
133 procdir = xopendir("/proc");
136 while ((entry = readdir(procdir)) != NULL) {
137 pid = bb_strtou(entry->d_name, NULL, 10);
145 bb_error_msg_and_die ("nothing in /proc - not mounted?");
149 static int do_stop(void)
158 what = xstrdup(cmdname);
160 what = xstrdup(execname);
162 what = xasprintf("process in pidfile '%s'", pidfile);
164 what = xasprintf("process(es) owned by '%s'", userspec);
166 bb_error_msg_and_die("internal error, please report");
170 printf("no %s found; none killed\n", what);
171 if (ENABLE_FEATURE_CLEAN_UP)
175 for (p = found; p; p = p->next) {
176 if (kill(p->pid, signal_nr) == 0) {
180 bb_perror_msg("warning: failed to kill %d", p->pid);
183 if (!quiet && killed) {
184 printf("stopped %s (pid", what);
185 for (p = found; p; p = p->next)
187 printf(" %d", -p->pid);
190 if (ENABLE_FEATURE_CLEAN_UP)
195 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
196 static const struct option long_options[] = {
197 { "stop", 0, NULL, 'K' },
198 { "start", 0, NULL, 'S' },
199 { "background", 0, NULL, 'b' },
200 { "quiet", 0, NULL, 'q' },
201 { "make-pidfile", 0, NULL, 'm' },
202 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
203 { "oknodo", 0, NULL, 'o' },
204 { "verbose", 0, NULL, 'v' },
205 { "nicelevel", 1, NULL, 'N' },
207 { "startas", 1, NULL, 'a' },
208 { "name", 1, NULL, 'n' },
209 { "signal", 1, NULL, 's' },
210 { "user", 1, NULL, 'u' },
211 { "chuid", 1, NULL, 'c' },
212 { "exec", 1, NULL, 'x' },
213 { "pidfile", 1, NULL, 'p' },
214 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
215 { "retry", 1, NULL, 'R' },
224 OPT_BACKGROUND = 0x4, // -b
225 OPT_QUIET = 0x8, // -q
226 OPT_MAKEPID = 0x10, // -m
234 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
235 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
236 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
239 int start_stop_daemon_main(int argc, char **argv);
240 int start_stop_daemon_main(int argc, char **argv)
246 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
247 // char *retry_arg = NULL;
251 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
252 applet_long_options = long_options;
255 /* Check required one context option was given */
256 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
257 opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
258 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
259 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
260 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
261 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
262 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
265 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
268 signal_nr = get_signum(signame);
269 if (signal_nr < 0) bb_show_usage();
275 // USE_FEATURE_START_STOP_DAEMON_FANCY(
277 // retries = xatoi_u(retry_arg);
283 user_id = bb_strtou(userspec, NULL, 10);
285 user_id = xuname2uid(userspec);
288 if (opt & CTX_STOP) {
290 return (opt & OPT_OKNODO) ? 0 : (i<=0);
297 printf("%s already running\n%d\n", execname, found->pid);
298 return !(opt & OPT_OKNODO);
301 if (opt & OPT_BACKGROUND) {
305 if (opt & OPT_MAKEPID) {
306 /* user wants _us_ to make the pidfile */
307 FILE *pidf = xfopen(pidfile, "w");
309 pid_t pidt = getpid();
310 fprintf(pidf, "%d\n", pidt);
314 struct bb_uidgid_t ugid;
315 parse_chown_usergroup_or_die(&ugid, chuid);
316 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
317 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
319 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
320 if (opt & OPT_NICELEVEL) {
321 /* Set process priority */
322 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
323 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
324 bb_perror_msg_and_die("setpriority(%d)", prio);
328 execv(startas, argv);
329 bb_perror_msg_and_die("cannot start %s", startas);