blob: ab607bde0b55e15458948698be6c820e2e6e8476 [file] [log] [blame]
Eric Andersenc2af1ee2001-10-18 19:33:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini start-stop-daemon implementation(s) for busybox
4 *
5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
Eric Andersenc2af1ee2001-10-18 19:33:06 +00006 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
Rob Landleyd921b2e2006-08-03 15:41:12 +00007 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc2af1ee2001-10-18 19:33:06 +00009 */
10
Denis Vlasenko647c20c2008-05-07 14:52:01 +000011/*
12This is how it is supposed to work:
13
14start-stop-daemon [OPTIONS] [--start|--stop] [[--] arguments...]
15
16One (only) of these must be given:
17 -S,--start Start
18 -K,--stop Stop
19
20Search for matching processes.
21If --stop is given, stop all matching processes (by sending a signal).
Denis Vlasenkob67004b2008-06-20 18:24:14 +000022If --start is given, start a new process unless a matching process was found.
Denis Vlasenko647c20c2008-05-07 14:52:01 +000023
Denis Vlasenkob67004b2008-06-20 18:24:14 +000024Options controlling process matching
25(if multiple conditions are specified, all must match):
Denis Vlasenko647c20c2008-05-07 14:52:01 +000026 -u,--user USERNAME|UID Only consider this user's processes
Denis Vlasenkoe125a682008-05-18 21:17:52 +000027 -n,--name PROCESS_NAME Look for processes by matching PROCESS_NAME
28 with comm field in /proc/$PID/stat.
Denis Vlasenko647c20c2008-05-07 14:52:01 +000029 Only basename is compared:
30 "ntpd" == "./ntpd" == "/path/to/ntpd".
31[TODO: can PROCESS_NAME be a full pathname? Should we require full match then
32with /proc/$PID/exe or argv[0] (comm can't be matched, it never contains path)]
Denis Vlasenkoe125a682008-05-18 21:17:52 +000033 -x,--exec EXECUTABLE Look for processes that were started with this
34 command in /proc/$PID/cmdline.
35 Unlike -n, we match against the full path:
36 "ntpd" != "./ntpd" != "/path/to/ntpd"
Denis Vlasenko647c20c2008-05-07 14:52:01 +000037 -p,--pidfile PID_FILE Look for processes with PID from this file
38
39Options which are valid for --start only:
40 -x,--exec EXECUTABLE Program to run (1st arg of execvp). Mandatory.
41 -a,--startas NAME argv[0] (defaults to EXECUTABLE)
42 -b,--background Put process into background
43 -N,--nicelevel N Add N to process' nice level
44 -c,--chuid USER[:[GRP]] Change to specified user [and group]
45 -m,--make-pidfile Write PID to the pidfile
46 (both -m and -p must be given!)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000047
48Options which are valid for --stop only:
Denis Vlasenko647c20c2008-05-07 14:52:01 +000049 -s,--signal SIG Signal to send (default:TERM)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000050 -t,--test Exit with status 0 if process is found
51 (we don't actually start or stop daemons)
52
53Misc options:
Denis Vlasenko647c20c2008-05-07 14:52:01 +000054 -o,--oknodo Exit with status 0 if nothing is done
55 -q,--quiet Quiet
56 -v,--verbose Verbose
57*/
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000058
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000059#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000060
Denis Vlasenko1caca342007-08-02 10:14:29 +000061/* Override ENABLE_FEATURE_PIDFILE */
62#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000063#include "libbb.h"
64
Eric Andersen63a1a7a2004-03-13 08:33:10 +000065struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000066 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000067 pid_t pid;
68};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000069
Denis Vlasenkoe0612262008-04-30 13:58:31 +000070enum {
71 CTX_STOP = (1 << 0),
72 CTX_START = (1 << 1),
73 OPT_BACKGROUND = (1 << 2), // -b
74 OPT_QUIET = (1 << 3), // -q
Denis Vlasenkoe125a682008-05-18 21:17:52 +000075 OPT_TEST = (1 << 4), // -t
76 OPT_MAKEPID = (1 << 5), // -m
77 OPT_a = (1 << 6), // -a
78 OPT_n = (1 << 7), // -n
79 OPT_s = (1 << 8), // -s
80 OPT_u = (1 << 9), // -u
81 OPT_c = (1 << 10), // -c
82 OPT_x = (1 << 11), // -x
83 OPT_p = (1 << 12), // -p
84 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
85 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
86 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkoe0612262008-04-30 13:58:31 +000087};
88#define QUIET (option_mask32 & OPT_QUIET)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000089#define TEST (option_mask32 & OPT_TEST)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000090
91struct globals {
92 struct pid_list *found;
93 char *userspec;
94 char *cmdname;
95 char *execname;
96 char *pidfile;
97 int user_id;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000098 smallint signal_nr;
99};
100#define G (*(struct globals*)&bb_common_bufsiz1)
101#define found (G.found )
102#define userspec (G.userspec )
103#define cmdname (G.cmdname )
104#define execname (G.execname )
105#define pidfile (G.pidfile )
106#define user_id (G.user_id )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000107#define signal_nr (G.signal_nr )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000108#define INIT_G() do { \
109 user_id = -1; \
110 signal_nr = 15; \
111} while (0)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000112
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000113#ifdef OLDER_VERSION_OF_X
114/* -x,--exec EXECUTABLE
115 * Look for processes with matching /proc/$PID/exe.
116 * Match is performed using device+inode.
117 */
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000118static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000119{
Denis Vlasenkofe493472008-04-20 14:25:26 +0000120 struct stat st;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000121 char buf[sizeof("/proc//exe") + sizeof(int)*3];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000122
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000123 sprintf(buf, "/proc/%u/exe", (unsigned)pid);
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000124 if (stat(buf, &st) < 0)
125 return 0;
126 if (st.st_dev == execstat.st_dev
127 && st.st_ino == execstat.st_ino)
128 return 1;
129 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000130}
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000131#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000132
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000133static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000134{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000135 ssize_t bytes;
136 char buf[PATH_MAX];
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000137
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000138 sprintf(buf, "/proc/%u/cmdline", (unsigned)pid);
139 bytes = open_read_close(buf, buf, sizeof(buf) - 1);
140 if (bytes > 0) {
141 buf[bytes] = '\0';
142 return strcmp(buf, execname) == 0;
143 }
144 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000145}
146
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000147static int pid_is_name(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000148{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000149 /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
150 char buf[32]; /* should be enough */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000151 char *p, *pe;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000152
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000153 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000154 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
155 return 0;
156 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
157 p = strchr(buf, '(');
158 if (!p)
159 return 0;
160 pe = strrchr(++p, ')');
161 if (!pe)
162 return 0;
163 *pe = '\0';
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000164 /* we require comm to match and to not be truncated */
165 /* in Linux, if comm is 15 chars, it may be a truncated
166 * name, so we don't allow that to match */
167 if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
168 return 0;
169 return strcmp(p, cmdname) == 0;
170}
171
172static int pid_is_user(int pid)
173{
174 struct stat sb;
175 char buf[sizeof("/proc/") + sizeof(int)*3];
176
177 sprintf(buf, "/proc/%u", (unsigned)pid);
178 if (stat(buf, &sb) != 0)
179 return 0;
180 return (sb.st_uid == (uid_t)user_id);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000181}
182
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000183static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000184{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000185 struct pid_list *p;
186
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000187 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000188 return;
189 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000190 if (cmdname && !pid_is_name(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000191 return;
192 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000193 if (userspec && !pid_is_user(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000194 return;
195 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000196 p = xmalloc(sizeof(*p));
197 p->next = found;
198 p->pid = pid;
199 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000200}
201
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000202static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000203{
204 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000205 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000206
Denis Vlasenko5415c852008-07-21 23:05:26 +0000207 f = fopen_for_read(pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000208 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000209 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000210 check(pid);
211 fclose(f);
212 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000213 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000214}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000215
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000216static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000217{
218 DIR *procdir;
219 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000220 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000221
Eric Andersen625da9d2004-04-13 18:28:46 +0000222 if (pidfile) {
223 do_pidfile();
224 return;
225 }
226
Rob Landleyd921b2e2006-08-03 15:41:12 +0000227 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000228
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000229 pid = 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000230 while (1) {
Denis Vlasenko1f228982008-04-22 00:16:29 +0000231 errno = 0; /* clear any previous error */
232 entry = readdir(procdir);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000233// TODO: this check is too generic, it's better
234// to check for exact errno(s) which mean that we got stale entry
Denis Vlasenko1f228982008-04-22 00:16:29 +0000235 if (errno) /* Stale entry, process has died after opendir */
236 continue;
237 if (!entry) /* EOF, no more entries */
238 break;
239 pid = bb_strtou(entry->d_name, NULL, 10);
240 if (errno) /* NaN */
241 continue;
242 check(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000243 }
244 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000245 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000246 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000247}
248
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000249static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000250{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000251 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000252 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000253 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000254
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000255 if (cmdname) {
256 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
257 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
258 } else if (execname) {
259 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
260 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000261 } else if (pidfile) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000262 what = xasprintf("process in pidfile '%s'", pidfile);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000263 } else if (userspec) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000264 what = xasprintf("process(es) owned by '%s'", userspec);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000265 } else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000266 bb_error_msg_and_die("internal error, please report");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000267 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000268
269 if (!found) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000270 if (!QUIET)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000271 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000272 killed = -1;
273 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000274 }
275 for (p = found; p; p = p->next) {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000276 if (TEST || kill(p->pid, signal_nr) == 0) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000277 killed++;
278 } else {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000279 p->pid = 0;
280 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000281 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000282 }
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000283 if (!QUIET && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000284 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000285 for (p = found; p; p = p->next)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000286 if (p->pid)
287 printf(" %u", (unsigned)p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000288 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000289 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000290 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000291 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000292 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000293 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000294}
295
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000296#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000297static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000298 "stop\0" No_argument "K"
299 "start\0" No_argument "S"
300 "background\0" No_argument "b"
301 "quiet\0" No_argument "q"
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000302 "test\0" No_argument "t"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000303 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000304#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000305 "oknodo\0" No_argument "o"
306 "verbose\0" No_argument "v"
307 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000308#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000309 "startas\0" Required_argument "a"
310 "name\0" Required_argument "n"
311 "signal\0" Required_argument "s"
312 "user\0" Required_argument "u"
313 "chuid\0" Required_argument "c"
314 "exec\0" Required_argument "x"
315 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000316#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000317 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000318#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000319 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000320#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000321
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000322int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000323int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000324{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000325 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000326 char *signame;
327 char *startas;
328 char *chuid;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000329#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000330 struct stat execstat;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000331#endif
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000332#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
333// char *retry_arg = NULL;
334// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000335 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000336#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000337
338 INIT_G();
339
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000340#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000341 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000342#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000343
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000344 /* -K or -S is required; they are mutually exclusive */
345 /* -p is required if -m is given */
346 /* -xpun (at least one) is required if -K is given */
347 /* -xa (at least one) is required if -S is given */
348 /* -q turns off -v */
349 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
350 USE_FEATURE_START_STOP_DAEMON_FANCY("q-v");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000351 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000352 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
Denis Vlasenkocce38582007-02-26 22:47:42 +0000353 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000354 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000355 /* We accept and ignore -R <param> / --retry <param> */
356 USE_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000357 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000358
Denis Vlasenkocce38582007-02-26 22:47:42 +0000359 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000360 signal_nr = get_signum(signame);
361 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000362 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000363
Denis Vlasenkocce38582007-02-26 22:47:42 +0000364 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000365 startas = execname;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000366 if (!execname) /* in case -a is given and -x is not */
367 execname = startas;
Eric Andersenaa820db2003-07-26 09:10:35 +0000368
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000369// USE_FEATURE_START_STOP_DAEMON_FANCY(
370// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000371// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000372// )
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000373 //argc -= optind;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000374 argv += optind;
375
Denis Vlasenkob131b272006-12-17 17:30:01 +0000376 if (userspec) {
377 user_id = bb_strtou(userspec, NULL, 10);
378 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000379 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000380 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000381 /* Both start and stop need to know current processes */
382 do_procinit();
Denis Vlasenko85d788e2008-04-19 21:30:52 +0000383
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000384 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000385 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000386 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000387 }
388
389 if (found) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000390 if (!QUIET)
Denis Vlasenko7987a182008-07-01 10:00:46 +0000391 printf("%s is already running\n%u\n", execname, (unsigned)found->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000392 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000393 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000394
Denis Vlasenko7987a182008-07-01 10:00:46 +0000395#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000396 if (execname)
397 xstat(execname, &execstat);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000398#endif
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000399
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000400 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000401 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000402#if BB_MMU
Denis Vlasenko7987a182008-07-01 10:00:46 +0000403 bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
404 /* DAEMON_DEVNULL_STDIO is superfluous -
405 * it's always done by bb_daemonize() */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000406#else
Denis Vlasenko82604e92008-07-01 15:59:42 +0000407 pid_t pid = vfork();
408 if (pid < 0) /* error */
409 bb_perror_msg_and_die("vfork");
Denis Vlasenko1caca342007-08-02 10:14:29 +0000410 if (pid != 0) {
411 /* parent */
412 /* why _exit? the child may have changed the stack,
413 * so "return 0" may do bad things */
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000414 _exit(EXIT_SUCCESS);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000415 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000416 /* Child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000417 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000418 /* Redirect stdio to /dev/null, close extra FDs.
419 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
Denis Vlasenko7987a182008-07-01 10:00:46 +0000420 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
Denis Vlasenko148f67a2008-07-01 10:05:12 +0000421 + DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000422 + DAEMON_ONLY_SANITIZE,
423 NULL /* argv, unused */ );
424#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000425 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000426 if (opt & OPT_MAKEPID) {
Denis Vlasenko7987a182008-07-01 10:00:46 +0000427 /* User wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000428 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000429 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000430 if (opt & OPT_c) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000431 struct bb_uidgid_t ugid = { -1, -1 };
Denis Vlasenkocce38582007-02-26 22:47:42 +0000432 parse_chown_usergroup_or_die(&ugid, chuid);
433 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
434 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
Rob Landleyf0623a22006-07-17 00:35:07 +0000435 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000436#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000437 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000438 /* Set process priority */
439 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
440 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
441 bb_perror_msg_and_die("setpriority(%d)", prio);
442 }
443 }
444#endif
Denis Vlasenko7987a182008-07-01 10:00:46 +0000445 execvp(startas, argv);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000446 bb_perror_msg_and_die("cannot start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000447}