blob: 665f38fbd802c4e2b348409620c7b587d7112207 [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 {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010092 struct pid_list *found_procs;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000093 char *userspec;
94 char *cmdname;
95 char *execname;
96 char *pidfile;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010097 char *execname_cmpbuf;
98 unsigned execname_sizeof;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000099 int user_id;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000100 smallint signal_nr;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100101} FIX_ALIASING;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000102#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000103#define userspec (G.userspec )
104#define cmdname (G.cmdname )
105#define execname (G.execname )
106#define pidfile (G.pidfile )
107#define user_id (G.user_id )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000108#define signal_nr (G.signal_nr )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000109#define INIT_G() do { \
110 user_id = -1; \
111 signal_nr = 15; \
112} while (0)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000113
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000114#ifdef OLDER_VERSION_OF_X
115/* -x,--exec EXECUTABLE
116 * Look for processes with matching /proc/$PID/exe.
117 * Match is performed using device+inode.
118 */
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000119static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000120{
Denis Vlasenkofe493472008-04-20 14:25:26 +0000121 struct stat st;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100122 char buf[sizeof("/proc/%u/exe") + sizeof(int)*3];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000123
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000124 sprintf(buf, "/proc/%u/exe", (unsigned)pid);
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000125 if (stat(buf, &st) < 0)
126 return 0;
127 if (st.st_dev == execstat.st_dev
128 && st.st_ino == execstat.st_ino)
129 return 1;
130 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000131}
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000132#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000133
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000134static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000135{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000136 ssize_t bytes;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100137 char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000138
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000139 sprintf(buf, "/proc/%u/cmdline", (unsigned)pid);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100140 bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000141 if (bytes > 0) {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100142 G.execname_cmpbuf[bytes] = '\0';
143 return strcmp(execname, G.execname_cmpbuf) == 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000144 }
145 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146}
147
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000148static int pid_is_name(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000149{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000150 /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
151 char buf[32]; /* should be enough */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000152 char *p, *pe;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000153
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000154 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000155 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
156 return 0;
157 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
158 p = strchr(buf, '(');
159 if (!p)
160 return 0;
161 pe = strrchr(++p, ')');
162 if (!pe)
163 return 0;
164 *pe = '\0';
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000165 /* we require comm to match and to not be truncated */
166 /* in Linux, if comm is 15 chars, it may be a truncated
167 * name, so we don't allow that to match */
168 if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
169 return 0;
170 return strcmp(p, cmdname) == 0;
171}
172
173static int pid_is_user(int pid)
174{
175 struct stat sb;
176 char buf[sizeof("/proc/") + sizeof(int)*3];
177
178 sprintf(buf, "/proc/%u", (unsigned)pid);
179 if (stat(buf, &sb) != 0)
180 return 0;
181 return (sb.st_uid == (uid_t)user_id);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000182}
183
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000184static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000185{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000186 struct pid_list *p;
187
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000188 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000189 return;
190 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000191 if (cmdname && !pid_is_name(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000192 return;
193 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000194 if (userspec && !pid_is_user(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000195 return;
196 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000197 p = xmalloc(sizeof(*p));
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100198 p->next = G.found_procs;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000199 p->pid = pid;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100200 G.found_procs = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000201}
202
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000203static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000204{
205 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000206 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000207
Denis Vlasenko5415c852008-07-21 23:05:26 +0000208 f = fopen_for_read(pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000209 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000210 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000211 check(pid);
212 fclose(f);
213 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000214 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000215}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000216
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000217static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000218{
219 DIR *procdir;
220 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000221 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000222
Eric Andersen625da9d2004-04-13 18:28:46 +0000223 if (pidfile) {
224 do_pidfile();
225 return;
226 }
227
Rob Landleyd921b2e2006-08-03 15:41:12 +0000228 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000229
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000230 pid = 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000231 while (1) {
Denis Vlasenko1f228982008-04-22 00:16:29 +0000232 errno = 0; /* clear any previous error */
233 entry = readdir(procdir);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000234// TODO: this check is too generic, it's better
235// to check for exact errno(s) which mean that we got stale entry
Denis Vlasenko1f228982008-04-22 00:16:29 +0000236 if (errno) /* Stale entry, process has died after opendir */
237 continue;
238 if (!entry) /* EOF, no more entries */
239 break;
240 pid = bb_strtou(entry->d_name, NULL, 10);
241 if (errno) /* NaN */
242 continue;
243 check(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000244 }
245 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000246 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000247 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000248}
249
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000250static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000251{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000252 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000253 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000254 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000255
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000256 if (cmdname) {
257 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
258 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
259 } else if (execname) {
260 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
261 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000262 } else if (pidfile) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000263 what = xasprintf("process in pidfile '%s'", pidfile);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000264 } else if (userspec) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000265 what = xasprintf("process(es) owned by '%s'", userspec);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000266 } else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000267 bb_error_msg_and_die("internal error, please report");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000268 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000269
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100270 if (!G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000271 if (!QUIET)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000272 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000273 killed = -1;
274 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000275 }
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100276 for (p = G.found_procs; p; p = p->next) {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000277 if (TEST || kill(p->pid, signal_nr) == 0) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000278 killed++;
279 } else {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000280 p->pid = 0;
281 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000282 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000283 }
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000284 if (!QUIET && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000285 printf("stopped %s (pid", what);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100286 for (p = G.found_procs; p; p = p->next)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000287 if (p->pid)
288 printf(" %u", (unsigned)p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000289 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000290 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000291 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000292 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000293 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000294 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000295}
296
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000297#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000298static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000299 "stop\0" No_argument "K"
300 "start\0" No_argument "S"
301 "background\0" No_argument "b"
302 "quiet\0" No_argument "q"
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000303 "test\0" No_argument "t"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000304 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000305#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000306 "oknodo\0" No_argument "o"
307 "verbose\0" No_argument "v"
308 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000309#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000310 "startas\0" Required_argument "a"
311 "name\0" Required_argument "n"
312 "signal\0" Required_argument "s"
313 "user\0" Required_argument "u"
314 "chuid\0" Required_argument "c"
315 "exec\0" Required_argument "x"
316 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000317#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000318 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000319#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000320 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000321#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000322
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000323int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000324int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000325{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000326 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000327 char *signame;
328 char *startas;
329 char *chuid;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000330#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000331 struct stat execstat;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000332#endif
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000333#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
334// char *retry_arg = NULL;
335// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000336 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000337#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000338
339 INIT_G();
340
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000341#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000342 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000343#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000344
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000345 /* -K or -S is required; they are mutually exclusive */
346 /* -p is required if -m is given */
347 /* -xpun (at least one) is required if -K is given */
348 /* -xa (at least one) is required if -S is given */
349 /* -q turns off -v */
350 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000351 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000352 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000353 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
Denis Vlasenkocce38582007-02-26 22:47:42 +0000354 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000355 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000356 /* We accept and ignore -R <param> / --retry <param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000357 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000358 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000359
Denis Vlasenkocce38582007-02-26 22:47:42 +0000360 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000361 signal_nr = get_signum(signame);
362 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000363 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000364
Denis Vlasenkocce38582007-02-26 22:47:42 +0000365 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000366 startas = execname;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000367 if (!execname) /* in case -a is given and -x is not */
368 execname = startas;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100369 if (execname) {
370 G.execname_sizeof = strlen(execname) + 1;
371 G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
372 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000373
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000374// IF_FEATURE_START_STOP_DAEMON_FANCY(
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000375// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000376// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000377// )
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000378 //argc -= optind;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000379 argv += optind;
380
Denis Vlasenkob131b272006-12-17 17:30:01 +0000381 if (userspec) {
382 user_id = bb_strtou(userspec, NULL, 10);
383 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000384 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000385 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000386 /* Both start and stop need to know current processes */
387 do_procinit();
Denis Vlasenko85d788e2008-04-19 21:30:52 +0000388
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000389 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000390 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000391 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000392 }
393
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100394 if (G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000395 if (!QUIET)
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100396 printf("%s is already running\n%u\n", execname, (unsigned)G.found_procs->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000397 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000398 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000399
Denis Vlasenko7987a182008-07-01 10:00:46 +0000400#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000401 if (execname)
402 xstat(execname, &execstat);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000403#endif
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000404
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000405 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000406 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000407#if BB_MMU
Denis Vlasenko7987a182008-07-01 10:00:46 +0000408 bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
409 /* DAEMON_DEVNULL_STDIO is superfluous -
410 * it's always done by bb_daemonize() */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000411#else
Pascal Bellard926031b2010-07-04 15:32:38 +0200412 pid_t pid = xvfork();
Denis Vlasenko1caca342007-08-02 10:14:29 +0000413 if (pid != 0) {
414 /* parent */
415 /* why _exit? the child may have changed the stack,
416 * so "return 0" may do bad things */
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000417 _exit(EXIT_SUCCESS);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000418 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000419 /* Child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000420 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000421 /* Redirect stdio to /dev/null, close extra FDs.
422 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
Denis Vlasenko7987a182008-07-01 10:00:46 +0000423 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
Denis Vlasenko148f67a2008-07-01 10:05:12 +0000424 + DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000425 + DAEMON_ONLY_SANITIZE,
426 NULL /* argv, unused */ );
427#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000428 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000429 if (opt & OPT_MAKEPID) {
Denis Vlasenko7987a182008-07-01 10:00:46 +0000430 /* User wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000431 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000432 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000433 if (opt & OPT_c) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000434 struct bb_uidgid_t ugid = { -1, -1 };
Denis Vlasenkocce38582007-02-26 22:47:42 +0000435 parse_chown_usergroup_or_die(&ugid, chuid);
436 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
437 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
Rob Landleyf0623a22006-07-17 00:35:07 +0000438 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000439#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000440 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000441 /* Set process priority */
442 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
443 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
444 bb_perror_msg_and_die("setpriority(%d)", prio);
445 }
446 }
447#endif
Denis Vlasenko7987a182008-07-01 10:00:46 +0000448 execvp(startas, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200449 bb_perror_msg_and_die("can't execute '%s'", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000450}