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 char start_stop_daemon_longopts[] =
198 "stop\0" No_argument "K"
199 "start\0" No_argument "S"
200 "background\0" No_argument "b"
201 "quiet\0" No_argument "q"
202 "make-pidfile\0" No_argument "m"
203 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
204 "oknodo\0" No_argument "o"
205 "verbose\0" No_argument "v"
206 "nicelevel\0" Required_argument "N"
208 "startas\0" Required_argument "a"
209 "name\0" Required_argument "n"
210 "signal\0" Required_argument "s"
211 "user\0" Required_argument "u"
212 "chuid\0" Required_argument "c"
213 "exec\0" Required_argument "x"
214 "pidfile\0" Required_argument "p"
215 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
216 "retry\0" Required_argument "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 = start_stop_daemon_longopts;
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) {
304 if (opt & OPT_MAKEPID) {
305 /* user wants _us_ to make the pidfile */
306 FILE *pidf = xfopen(pidfile, "w");
308 pid_t pidt = getpid();
309 fprintf(pidf, "%d\n", pidt);
313 struct bb_uidgid_t ugid;
314 parse_chown_usergroup_or_die(&ugid, chuid);
315 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
316 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
318 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
319 if (opt & OPT_NICELEVEL) {
320 /* Set process priority */
321 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
322 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
323 bb_perror_msg_and_die("setpriority(%d)", prio);
327 execv(startas, argv);
328 bb_perror_msg_and_die("cannot start %s", startas);