blob: 495ed0a0930468245ce73ac2ff209368de444dc4 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga6a3e01d2011-04-01 22:56:30 +020059//usage:#define start_stop_daemon_trivial_usage
60//usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]"
61//usage:#define start_stop_daemon_full_usage "\n\n"
62//usage: "Search for matching processes, and then\n"
63//usage: "-K: stop all matching processes.\n"
64//usage: "-S: start a process unless a matching process is found.\n"
65//usage: IF_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
66//usage: "\nProcess matching:"
67//usage: "\n -u,--user USERNAME|UID Match only this user's processes"
68//usage: "\n -n,--name NAME Match processes with NAME"
69//usage: "\n in comm field in /proc/PID/stat"
70//usage: "\n -x,--exec EXECUTABLE Match processes with this command"
71//usage: "\n in /proc/PID/cmdline"
72//usage: "\n -p,--pidfile FILE Match a process with PID from the file"
73//usage: "\n All specified conditions must match"
74//usage: "\n-S only:"
75//usage: "\n -x,--exec EXECUTABLE Program to run"
76//usage: "\n -a,--startas NAME Zeroth argument"
77//usage: "\n -b,--background Background"
78//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
79//usage: "\n -N,--nicelevel N Change nice level"
80//usage: )
81//usage: "\n -c,--chuid USER[:[GRP]] Change to user/group"
82//usage: "\n -m,--make-pidfile Write PID to the pidfile specified by -p"
83//usage: "\n-K only:"
84//usage: "\n -s,--signal SIG Signal to send"
85//usage: "\n -t,--test Match only, exit with 0 if a process is found"
86//usage: "\nOther:"
87//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
88//usage: "\n -o,--oknodo Exit with status 0 if nothing is done"
89//usage: "\n -v,--verbose Verbose"
90//usage: )
91//usage: "\n -q,--quiet Quiet"
92//usage: )
93//usage: IF_NOT_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
94//usage: "\nProcess matching:"
95//usage: "\n -u USERNAME|UID Match only this user's processes"
96//usage: "\n -n NAME Match processes with NAME"
97//usage: "\n in comm field in /proc/PID/stat"
98//usage: "\n -x EXECUTABLE Match processes with this command"
99//usage: "\n command in /proc/PID/cmdline"
100//usage: "\n -p FILE Match a process with PID from the file"
101//usage: "\n All specified conditions must match"
102//usage: "\n-S only:"
103//usage: "\n -x EXECUTABLE Program to run"
104//usage: "\n -a NAME Zeroth argument"
105//usage: "\n -b Background"
106//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
107//usage: "\n -N N Change nice level"
108//usage: )
109//usage: "\n -c USER[:[GRP]] Change to user/group"
110//usage: "\n -m Write PID to the pidfile specified by -p"
111//usage: "\n-K only:"
112//usage: "\n -s SIG Signal to send"
113//usage: "\n -t Match only, exit with 0 if a process is found"
114//usage: "\nOther:"
115//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
116//usage: "\n -o Exit with status 0 if nothing is done"
117//usage: "\n -v Verbose"
118//usage: )
119//usage: "\n -q Quiet"
120//usage: )
121
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000122#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000123
Denis Vlasenko1caca342007-08-02 10:14:29 +0000124/* Override ENABLE_FEATURE_PIDFILE */
125#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000126#include "libbb.h"
127
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000128struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000129 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000130 pid_t pid;
131};
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000132
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000133enum {
134 CTX_STOP = (1 << 0),
135 CTX_START = (1 << 1),
136 OPT_BACKGROUND = (1 << 2), // -b
137 OPT_QUIET = (1 << 3), // -q
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000138 OPT_TEST = (1 << 4), // -t
139 OPT_MAKEPID = (1 << 5), // -m
140 OPT_a = (1 << 6), // -a
141 OPT_n = (1 << 7), // -n
142 OPT_s = (1 << 8), // -s
143 OPT_u = (1 << 9), // -u
144 OPT_c = (1 << 10), // -c
145 OPT_x = (1 << 11), // -x
146 OPT_p = (1 << 12), // -p
147 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
148 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
149 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000150};
151#define QUIET (option_mask32 & OPT_QUIET)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000152#define TEST (option_mask32 & OPT_TEST)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000153
154struct globals {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100155 struct pid_list *found_procs;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000156 char *userspec;
157 char *cmdname;
158 char *execname;
159 char *pidfile;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100160 char *execname_cmpbuf;
161 unsigned execname_sizeof;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000162 int user_id;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000163 smallint signal_nr;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100164} FIX_ALIASING;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000165#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000166#define userspec (G.userspec )
167#define cmdname (G.cmdname )
168#define execname (G.execname )
169#define pidfile (G.pidfile )
170#define user_id (G.user_id )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000171#define signal_nr (G.signal_nr )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000172#define INIT_G() do { \
173 user_id = -1; \
174 signal_nr = 15; \
175} while (0)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000176
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000177#ifdef OLDER_VERSION_OF_X
178/* -x,--exec EXECUTABLE
179 * Look for processes with matching /proc/$PID/exe.
180 * Match is performed using device+inode.
181 */
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000182static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000183{
Denis Vlasenkofe493472008-04-20 14:25:26 +0000184 struct stat st;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100185 char buf[sizeof("/proc/%u/exe") + sizeof(int)*3];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000186
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000187 sprintf(buf, "/proc/%u/exe", (unsigned)pid);
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000188 if (stat(buf, &st) < 0)
189 return 0;
190 if (st.st_dev == execstat.st_dev
191 && st.st_ino == execstat.st_ino)
192 return 1;
193 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000194}
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000195#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000196
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000197static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000199 ssize_t bytes;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100200 char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000201
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000202 sprintf(buf, "/proc/%u/cmdline", (unsigned)pid);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100203 bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000204 if (bytes > 0) {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100205 G.execname_cmpbuf[bytes] = '\0';
206 return strcmp(execname, G.execname_cmpbuf) == 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000207 }
208 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000209}
210
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000211static int pid_is_name(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000212{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000213 /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
214 char buf[32]; /* should be enough */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000215 char *p, *pe;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000216
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000217 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000218 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
219 return 0;
220 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
221 p = strchr(buf, '(');
222 if (!p)
223 return 0;
224 pe = strrchr(++p, ')');
225 if (!pe)
226 return 0;
227 *pe = '\0';
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000228 /* we require comm to match and to not be truncated */
229 /* in Linux, if comm is 15 chars, it may be a truncated
230 * name, so we don't allow that to match */
231 if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
232 return 0;
233 return strcmp(p, cmdname) == 0;
234}
235
236static int pid_is_user(int pid)
237{
238 struct stat sb;
239 char buf[sizeof("/proc/") + sizeof(int)*3];
240
241 sprintf(buf, "/proc/%u", (unsigned)pid);
242 if (stat(buf, &sb) != 0)
243 return 0;
244 return (sb.st_uid == (uid_t)user_id);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000245}
246
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000247static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000248{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000249 struct pid_list *p;
250
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000251 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000252 return;
253 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000254 if (cmdname && !pid_is_name(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000255 return;
256 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000257 if (userspec && !pid_is_user(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000258 return;
259 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000260 p = xmalloc(sizeof(*p));
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100261 p->next = G.found_procs;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000262 p->pid = pid;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100263 G.found_procs = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000264}
265
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000266static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000267{
268 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000269 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000270
Denis Vlasenko5415c852008-07-21 23:05:26 +0000271 f = fopen_for_read(pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000272 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000273 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000274 check(pid);
275 fclose(f);
276 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000277 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000278}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000279
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000280static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000281{
282 DIR *procdir;
283 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000284 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000285
Eric Andersen625da9d2004-04-13 18:28:46 +0000286 if (pidfile) {
287 do_pidfile();
288 return;
289 }
290
Rob Landleyd921b2e2006-08-03 15:41:12 +0000291 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000292
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000293 pid = 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000294 while (1) {
Denis Vlasenko1f228982008-04-22 00:16:29 +0000295 errno = 0; /* clear any previous error */
296 entry = readdir(procdir);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000297// TODO: this check is too generic, it's better
298// to check for exact errno(s) which mean that we got stale entry
Denis Vlasenko1f228982008-04-22 00:16:29 +0000299 if (errno) /* Stale entry, process has died after opendir */
300 continue;
301 if (!entry) /* EOF, no more entries */
302 break;
303 pid = bb_strtou(entry->d_name, NULL, 10);
304 if (errno) /* NaN */
305 continue;
306 check(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000307 }
308 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000309 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000310 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000311}
312
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000313static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000314{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000315 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000316 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000317 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000318
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000319 if (cmdname) {
320 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
321 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
322 } else if (execname) {
323 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
324 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000325 } else if (pidfile) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000326 what = xasprintf("process in pidfile '%s'", pidfile);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000327 } else if (userspec) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000328 what = xasprintf("process(es) owned by '%s'", userspec);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000329 } else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000330 bb_error_msg_and_die("internal error, please report");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000331 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000332
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100333 if (!G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000334 if (!QUIET)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000335 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000336 killed = -1;
337 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000338 }
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100339 for (p = G.found_procs; p; p = p->next) {
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200340 if (kill(p->pid, TEST ? 0 : signal_nr) == 0) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000341 killed++;
342 } else {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000343 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200344 p->pid = 0;
345 if (TEST) {
346 /* Example: -K --test --pidfile PIDFILE detected
347 * that PIDFILE's pid doesn't exist */
348 killed = -1;
349 goto ret;
350 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000351 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000352 }
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000353 if (!QUIET && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000354 printf("stopped %s (pid", what);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100355 for (p = G.found_procs; p; p = p->next)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000356 if (p->pid)
357 printf(" %u", (unsigned)p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000358 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000359 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000360 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000361 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000362 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000363 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000364}
365
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000366#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000367static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000368 "stop\0" No_argument "K"
369 "start\0" No_argument "S"
370 "background\0" No_argument "b"
371 "quiet\0" No_argument "q"
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000372 "test\0" No_argument "t"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000373 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000374#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000375 "oknodo\0" No_argument "o"
376 "verbose\0" No_argument "v"
377 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000378#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000379 "startas\0" Required_argument "a"
380 "name\0" Required_argument "n"
381 "signal\0" Required_argument "s"
382 "user\0" Required_argument "u"
383 "chuid\0" Required_argument "c"
384 "exec\0" Required_argument "x"
385 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000386#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000387 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000388#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000389 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000390#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000391
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000392int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000393int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000394{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000395 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000396 char *signame;
397 char *startas;
398 char *chuid;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000399#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000400 struct stat execstat;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000401#endif
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000402#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
403// char *retry_arg = NULL;
404// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000405 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000406#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000407
408 INIT_G();
409
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000410#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000411 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000412#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000413
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000414 /* -K or -S is required; they are mutually exclusive */
415 /* -p is required if -m is given */
416 /* -xpun (at least one) is required if -K is given */
417 /* -xa (at least one) is required if -S is given */
418 /* -q turns off -v */
419 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000420 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000421 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000422 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
Denis Vlasenkocce38582007-02-26 22:47:42 +0000423 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000424 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000425 /* We accept and ignore -R <param> / --retry <param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000426 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000427 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000428
Denis Vlasenkocce38582007-02-26 22:47:42 +0000429 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000430 signal_nr = get_signum(signame);
431 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000432 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000433
Denis Vlasenkocce38582007-02-26 22:47:42 +0000434 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000435 startas = execname;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000436 if (!execname) /* in case -a is given and -x is not */
437 execname = startas;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100438 if (execname) {
439 G.execname_sizeof = strlen(execname) + 1;
440 G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
441 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000442
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000443// IF_FEATURE_START_STOP_DAEMON_FANCY(
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000444// if (retry_arg)
Denys Vlasenko77832482010-08-12 14:14:45 +0200445// retries = xatoi_positive(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000446// )
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000447 //argc -= optind;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000448 argv += optind;
449
Denis Vlasenkob131b272006-12-17 17:30:01 +0000450 if (userspec) {
451 user_id = bb_strtou(userspec, NULL, 10);
452 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000453 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000454 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000455 /* Both start and stop need to know current processes */
456 do_procinit();
Denis Vlasenko85d788e2008-04-19 21:30:52 +0000457
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000458 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000459 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000460 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000461 }
462
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100463 if (G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000464 if (!QUIET)
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100465 printf("%s is already running\n%u\n", execname, (unsigned)G.found_procs->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000466 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000467 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000468
Denis Vlasenko7987a182008-07-01 10:00:46 +0000469#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000470 if (execname)
471 xstat(execname, &execstat);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000472#endif
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000473
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000474 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000475 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000476#if BB_MMU
Peter Korsgaard743edac2011-11-09 19:44:37 +0100477 bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS + DAEMON_DOUBLE_FORK);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000478 /* DAEMON_DEVNULL_STDIO is superfluous -
479 * it's always done by bb_daemonize() */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000480#else
Pascal Bellard926031b2010-07-04 15:32:38 +0200481 pid_t pid = xvfork();
Denis Vlasenko1caca342007-08-02 10:14:29 +0000482 if (pid != 0) {
483 /* parent */
484 /* why _exit? the child may have changed the stack,
485 * so "return 0" may do bad things */
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000486 _exit(EXIT_SUCCESS);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000487 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000488 /* Child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000489 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000490 /* Redirect stdio to /dev/null, close extra FDs.
491 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
Denis Vlasenko7987a182008-07-01 10:00:46 +0000492 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
Denis Vlasenko148f67a2008-07-01 10:05:12 +0000493 + DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000494 + DAEMON_ONLY_SANITIZE,
495 NULL /* argv, unused */ );
496#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000497 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000498 if (opt & OPT_MAKEPID) {
Denis Vlasenko7987a182008-07-01 10:00:46 +0000499 /* User wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000500 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000501 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000502 if (opt & OPT_c) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000503 struct bb_uidgid_t ugid = { -1, -1 };
Denis Vlasenkocce38582007-02-26 22:47:42 +0000504 parse_chown_usergroup_or_die(&ugid, chuid);
Denys Vlasenko585541e2011-09-15 18:27:05 +0200505 if (ugid.uid != (uid_t) -1) {
506 struct passwd *pw = xgetpwuid(ugid.uid);
507 if (ugid.gid != (gid_t) -1)
508 pw->pw_gid = ugid.gid;
509 /* initgroups, setgid, setuid: */
510 change_identity(pw);
511 } else if (ugid.gid != (gid_t) -1) {
512 xsetgid(ugid.gid);
513 setgroups(1, &ugid.gid);
514 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000515 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000516#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000517 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000518 /* Set process priority */
519 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
520 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
521 bb_perror_msg_and_die("setpriority(%d)", prio);
522 }
523 }
524#endif
Denis Vlasenko7987a182008-07-01 10:00:46 +0000525 execvp(startas, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200526 bb_perror_msg_and_die("can't execute '%s'", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000527}