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.
12 #include <sys/resource.h>
16 static int signal_nr = 15;
17 static int user_id = -1;
18 static char *userspec;
20 static char *execname;
22 static smallint quiet;
25 struct pid_list *next;
29 static struct pid_list *found;
31 static inline void push(pid_t pid)
35 p = xmalloc(sizeof(*p));
41 static int pid_is_exec(pid_t pid, const char *name)
43 char buf[sizeof("/proc//exe") + sizeof(int)*3];
48 sprintf(buf, "/proc/%d/exe", pid);
49 sz = strlen(name) + 1;
50 execbuf = xzalloc(sz);
51 readlink(buf, execbuf, sz);
53 /* if readlink fails, execbuf still contains "" */
54 equal = !strcmp(execbuf, name);
55 if (ENABLE_FEATURE_CLEAN_UP)
60 static int pid_is_user(int pid, int uid)
63 char buf[sizeof("/proc/") + sizeof(int)*3];
65 sprintf(buf, "/proc/%u", pid);
66 if (stat(buf, &sb) != 0)
68 return (sb.st_uid == uid);
71 static int pid_is_cmd(pid_t pid, const char *name)
73 char fname[sizeof("/proc//stat") + sizeof(int)*3];
77 sprintf(fname, "/proc/%u/stat", pid);
78 buf = xmalloc_open_read_close(fname, NULL);
80 char *p = strchr(buf, '(');
82 char *pe = strrchr(++p, ')');
94 static void check(int pid)
96 if (execname && !pid_is_exec(pid, execname)) {
99 if (userspec && !pid_is_user(pid, user_id)) {
102 if (cmdname && !pid_is_cmd(pid, cmdname)) {
109 static void do_pidfile(void)
114 f = fopen(pidfile, "r");
116 if (fscanf(f, "%u", &pid) == 1)
119 } else if (errno != ENOENT)
120 bb_perror_msg_and_die("open pidfile %s", pidfile);
123 static void do_procinit(void)
126 struct dirent *entry;
134 procdir = xopendir("/proc");
137 while ((entry = readdir(procdir)) != NULL) {
138 pid = bb_strtou(entry->d_name, NULL, 10);
146 bb_error_msg_and_die ("nothing in /proc - not mounted?");
150 static int do_stop(void)
159 what = xstrdup(cmdname);
161 what = xstrdup(execname);
163 what = xasprintf("process in pidfile '%s'", pidfile);
165 what = xasprintf("process(es) owned by '%s'", userspec);
167 bb_error_msg_and_die("internal error, please report");
171 printf("no %s found; none killed\n", what);
172 if (ENABLE_FEATURE_CLEAN_UP)
176 for (p = found; p; p = p->next) {
177 if (kill(p->pid, signal_nr) == 0) {
181 bb_perror_msg("warning: failed to kill %d", p->pid);
184 if (!quiet && killed) {
185 printf("stopped %s (pid", what);
186 for (p = found; p; p = p->next)
188 printf(" %d", -p->pid);
191 if (ENABLE_FEATURE_CLEAN_UP)
196 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
197 static const struct option long_options[] = {
198 { "stop", 0, NULL, 'K' },
199 { "start", 0, NULL, 'S' },
200 { "background", 0, NULL, 'b' },
201 { "quiet", 0, NULL, 'q' },
202 { "make-pidfile", 0, NULL, 'm' },
203 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
204 { "oknodo", 0, NULL, 'o' },
205 { "verbose", 0, NULL, 'v' },
206 { "nicelevel", 1, NULL, 'N' },
208 { "startas", 1, NULL, 'a' },
209 { "name", 1, NULL, 'n' },
210 { "signal", 1, NULL, 's' },
211 { "user", 1, NULL, 'u' },
212 { "chuid", 1, NULL, 'c' },
213 { "exec", 1, NULL, 'x' },
214 { "pidfile", 1, NULL, 'p' },
215 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
216 { "retry", 1, NULL, 'R' },
225 OPT_BACKGROUND = 0x4, // -b
226 OPT_QUIET = 0x8, // -q
227 OPT_MAKEPID = 0x10, // -m
235 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
236 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
237 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
240 int start_stop_daemon_main(int argc, char **argv);
241 int start_stop_daemon_main(int argc, char **argv)
247 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
248 // char *retry_arg = NULL;
252 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
253 applet_long_options = long_options;
256 /* Check required one context option was given */
257 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
258 opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
259 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
260 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
261 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
262 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
263 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
266 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
269 signal_nr = get_signum(signame);
270 if (signal_nr < 0) bb_show_usage();
276 // USE_FEATURE_START_STOP_DAEMON_FANCY(
278 // retries = xatoi_u(retry_arg);
284 user_id = bb_strtou(userspec, NULL, 10);
286 user_id = xuname2uid(userspec);
289 if (opt & CTX_STOP) {
291 return (opt & OPT_OKNODO) ? 0 : (i<=0);
298 printf("%s already running\n%d\n", execname, found->pid);
299 return !(opt & OPT_OKNODO);
302 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);