blob: d7c730f453500b7948333d26811c84a5633503ee [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
Denys Vlasenko17eedca2012-03-05 16:28:07 +010034 command in /proc/$PID/exe and /proc/$PID/cmdline
35 (/proc/$PID/cmdline is a bbox extension)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000036 Unlike -n, we match against the full path:
37 "ntpd" != "./ntpd" != "/path/to/ntpd"
Denis Vlasenko647c20c2008-05-07 14:52:01 +000038 -p,--pidfile PID_FILE Look for processes with PID from this file
39
40Options which are valid for --start only:
41 -x,--exec EXECUTABLE Program to run (1st arg of execvp). Mandatory.
42 -a,--startas NAME argv[0] (defaults to EXECUTABLE)
43 -b,--background Put process into background
44 -N,--nicelevel N Add N to process' nice level
45 -c,--chuid USER[:[GRP]] Change to specified user [and group]
46 -m,--make-pidfile Write PID to the pidfile
47 (both -m and -p must be given!)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000048
49Options which are valid for --stop only:
Denis Vlasenko647c20c2008-05-07 14:52:01 +000050 -s,--signal SIG Signal to send (default:TERM)
Denis Vlasenkoe125a682008-05-18 21:17:52 +000051 -t,--test Exit with status 0 if process is found
52 (we don't actually start or stop daemons)
53
54Misc options:
Denis Vlasenko647c20c2008-05-07 14:52:01 +000055 -o,--oknodo Exit with status 0 if nothing is done
56 -q,--quiet Quiet
57 -v,--verbose Verbose
58*/
Denys Vlasenko28826ac2015-10-19 00:52:26 +020059//config:config START_STOP_DAEMON
60//config: bool "start-stop-daemon"
61//config: default y
62//config: help
63//config: start-stop-daemon is used to control the creation and
64//config: termination of system-level processes, usually the ones
65//config: started during the startup of the system.
66//config:
67//config:config FEATURE_START_STOP_DAEMON_FANCY
68//config: bool "Support additional arguments"
69//config: default y
70//config: depends on START_STOP_DAEMON
71//config: help
72//config: Support additional arguments.
73//config: -o|--oknodo ignored since we exit with 0 anyway
74//config: -v|--verbose
75//config: -N|--nicelevel N
76//config:
77//config:config FEATURE_START_STOP_DAEMON_LONG_OPTIONS
78//config: bool "Enable long options"
79//config: default y
80//config: depends on START_STOP_DAEMON && LONG_OPTS
81//config: help
82//config: Support long options for the start-stop-daemon applet.
83
84//applet:IF_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, BB_DIR_SBIN, BB_SUID_DROP, start_stop_daemon))
85
86//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000087
Pere Orga6a3e01d2011-04-01 22:56:30 +020088//usage:#define start_stop_daemon_trivial_usage
89//usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]"
90//usage:#define start_stop_daemon_full_usage "\n\n"
91//usage: "Search for matching processes, and then\n"
92//usage: "-K: stop all matching processes.\n"
93//usage: "-S: start a process unless a matching process is found.\n"
94//usage: IF_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
95//usage: "\nProcess matching:"
96//usage: "\n -u,--user USERNAME|UID Match only this user's processes"
97//usage: "\n -n,--name NAME Match processes with NAME"
98//usage: "\n in comm field in /proc/PID/stat"
99//usage: "\n -x,--exec EXECUTABLE Match processes with this command"
Denys Vlasenko17eedca2012-03-05 16:28:07 +0100100//usage: "\n in /proc/PID/{exe,cmdline}"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200101//usage: "\n -p,--pidfile FILE Match a process with PID from the file"
102//usage: "\n All specified conditions must match"
103//usage: "\n-S only:"
104//usage: "\n -x,--exec EXECUTABLE Program to run"
105//usage: "\n -a,--startas NAME Zeroth argument"
106//usage: "\n -b,--background Background"
107//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
108//usage: "\n -N,--nicelevel N Change nice level"
109//usage: )
110//usage: "\n -c,--chuid USER[:[GRP]] Change to user/group"
111//usage: "\n -m,--make-pidfile Write PID to the pidfile specified by -p"
112//usage: "\n-K only:"
113//usage: "\n -s,--signal SIG Signal to send"
114//usage: "\n -t,--test Match only, exit with 0 if a process is found"
115//usage: "\nOther:"
116//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
117//usage: "\n -o,--oknodo Exit with status 0 if nothing is done"
118//usage: "\n -v,--verbose Verbose"
119//usage: )
120//usage: "\n -q,--quiet Quiet"
121//usage: )
122//usage: IF_NOT_FEATURE_START_STOP_DAEMON_LONG_OPTIONS(
123//usage: "\nProcess matching:"
124//usage: "\n -u USERNAME|UID Match only this user's processes"
125//usage: "\n -n NAME Match processes with NAME"
126//usage: "\n in comm field in /proc/PID/stat"
127//usage: "\n -x EXECUTABLE Match processes with this command"
128//usage: "\n command in /proc/PID/cmdline"
129//usage: "\n -p FILE Match a process with PID from the file"
130//usage: "\n All specified conditions must match"
131//usage: "\n-S only:"
132//usage: "\n -x EXECUTABLE Program to run"
133//usage: "\n -a NAME Zeroth argument"
134//usage: "\n -b Background"
135//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
136//usage: "\n -N N Change nice level"
137//usage: )
138//usage: "\n -c USER[:[GRP]] Change to user/group"
139//usage: "\n -m Write PID to the pidfile specified by -p"
140//usage: "\n-K only:"
141//usage: "\n -s SIG Signal to send"
142//usage: "\n -t Match only, exit with 0 if a process is found"
143//usage: "\nOther:"
144//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
145//usage: "\n -o Exit with status 0 if nothing is done"
146//usage: "\n -v Verbose"
147//usage: )
148//usage: "\n -q Quiet"
149//usage: )
150
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000151#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000152
Denis Vlasenko1caca342007-08-02 10:14:29 +0000153/* Override ENABLE_FEATURE_PIDFILE */
154#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000155#include "libbb.h"
156
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000157struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000158 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000159 pid_t pid;
160};
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000161
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000162enum {
163 CTX_STOP = (1 << 0),
164 CTX_START = (1 << 1),
165 OPT_BACKGROUND = (1 << 2), // -b
166 OPT_QUIET = (1 << 3), // -q
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000167 OPT_TEST = (1 << 4), // -t
168 OPT_MAKEPID = (1 << 5), // -m
169 OPT_a = (1 << 6), // -a
170 OPT_n = (1 << 7), // -n
171 OPT_s = (1 << 8), // -s
172 OPT_u = (1 << 9), // -u
173 OPT_c = (1 << 10), // -c
174 OPT_x = (1 << 11), // -x
175 OPT_p = (1 << 12), // -p
176 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
177 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
178 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000179};
180#define QUIET (option_mask32 & OPT_QUIET)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000181#define TEST (option_mask32 & OPT_TEST)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000182
183struct globals {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100184 struct pid_list *found_procs;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000185 char *userspec;
186 char *cmdname;
187 char *execname;
188 char *pidfile;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100189 char *execname_cmpbuf;
190 unsigned execname_sizeof;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000191 int user_id;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000192 smallint signal_nr;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100193} FIX_ALIASING;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000194#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000195#define userspec (G.userspec )
196#define cmdname (G.cmdname )
197#define execname (G.execname )
198#define pidfile (G.pidfile )
199#define user_id (G.user_id )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000200#define signal_nr (G.signal_nr )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000201#define INIT_G() do { \
202 user_id = -1; \
203 signal_nr = 15; \
204} while (0)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000205
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000206#ifdef OLDER_VERSION_OF_X
207/* -x,--exec EXECUTABLE
208 * Look for processes with matching /proc/$PID/exe.
209 * Match is performed using device+inode.
210 */
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000211static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000212{
Denis Vlasenkofe493472008-04-20 14:25:26 +0000213 struct stat st;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100214 char buf[sizeof("/proc/%u/exe") + sizeof(int)*3];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000215
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000216 sprintf(buf, "/proc/%u/exe", (unsigned)pid);
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000217 if (stat(buf, &st) < 0)
218 return 0;
219 if (st.st_dev == execstat.st_dev
220 && st.st_ino == execstat.st_ino)
221 return 1;
222 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000223}
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000224#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000225
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000226static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000227{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000228 ssize_t bytes;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100229 char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
Denys Vlasenko17eedca2012-03-05 16:28:07 +0100230 char *procname, *exelink;
231 int match;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000232
Denys Vlasenko17eedca2012-03-05 16:28:07 +0100233 procname = buf + sprintf(buf, "/proc/%u/exe", (unsigned)pid) - 3;
234
235 exelink = xmalloc_readlink(buf);
236 match = (exelink && strcmp(execname, exelink) == 0);
237 free(exelink);
238 if (match)
239 return match;
240
241 strcpy(procname, "cmdline");
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100242 bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000243 if (bytes > 0) {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100244 G.execname_cmpbuf[bytes] = '\0';
245 return strcmp(execname, G.execname_cmpbuf) == 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000246 }
247 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000248}
249
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000250static int pid_is_name(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000251{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000252 /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
253 char buf[32]; /* should be enough */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000254 char *p, *pe;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000255
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000256 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000257 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
258 return 0;
259 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
260 p = strchr(buf, '(');
261 if (!p)
262 return 0;
263 pe = strrchr(++p, ')');
264 if (!pe)
265 return 0;
266 *pe = '\0';
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000267 /* we require comm to match and to not be truncated */
268 /* in Linux, if comm is 15 chars, it may be a truncated
269 * name, so we don't allow that to match */
270 if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
271 return 0;
272 return strcmp(p, cmdname) == 0;
273}
274
275static int pid_is_user(int pid)
276{
277 struct stat sb;
278 char buf[sizeof("/proc/") + sizeof(int)*3];
279
280 sprintf(buf, "/proc/%u", (unsigned)pid);
281 if (stat(buf, &sb) != 0)
282 return 0;
283 return (sb.st_uid == (uid_t)user_id);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000284}
285
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000286static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000287{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000288 struct pid_list *p;
289
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000290 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000291 return;
292 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000293 if (cmdname && !pid_is_name(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000294 return;
295 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000296 if (userspec && !pid_is_user(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000297 return;
298 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000299 p = xmalloc(sizeof(*p));
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100300 p->next = G.found_procs;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000301 p->pid = pid;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100302 G.found_procs = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000303}
304
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000305static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000306{
307 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000308 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000309
Denis Vlasenko5415c852008-07-21 23:05:26 +0000310 f = fopen_for_read(pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000311 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000312 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000313 check(pid);
314 fclose(f);
315 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000316 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000317}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000318
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000319static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000320{
321 DIR *procdir;
322 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000323 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000324
Eric Andersen625da9d2004-04-13 18:28:46 +0000325 if (pidfile) {
326 do_pidfile();
327 return;
328 }
329
Rob Landleyd921b2e2006-08-03 15:41:12 +0000330 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000331
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000332 pid = 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000333 while (1) {
Denis Vlasenko1f228982008-04-22 00:16:29 +0000334 errno = 0; /* clear any previous error */
335 entry = readdir(procdir);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000336// TODO: this check is too generic, it's better
337// to check for exact errno(s) which mean that we got stale entry
Denis Vlasenko1f228982008-04-22 00:16:29 +0000338 if (errno) /* Stale entry, process has died after opendir */
339 continue;
340 if (!entry) /* EOF, no more entries */
341 break;
342 pid = bb_strtou(entry->d_name, NULL, 10);
343 if (errno) /* NaN */
344 continue;
345 check(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000346 }
347 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000348 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000349 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000350}
351
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000352static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000353{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000354 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000355 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000356 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000357
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000358 if (cmdname) {
359 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
360 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
361 } else if (execname) {
362 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
363 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000364 } else if (pidfile) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000365 what = xasprintf("process in pidfile '%s'", pidfile);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000366 } else if (userspec) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000367 what = xasprintf("process(es) owned by '%s'", userspec);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000368 } else {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000369 bb_error_msg_and_die("internal error, please report");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000370 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000371
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100372 if (!G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000373 if (!QUIET)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000374 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000375 killed = -1;
376 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000377 }
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100378 for (p = G.found_procs; p; p = p->next) {
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200379 if (kill(p->pid, TEST ? 0 : signal_nr) == 0) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000380 killed++;
381 } else {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000382 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200383 p->pid = 0;
384 if (TEST) {
385 /* Example: -K --test --pidfile PIDFILE detected
386 * that PIDFILE's pid doesn't exist */
387 killed = -1;
388 goto ret;
389 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000390 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000391 }
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000392 if (!QUIET && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000393 printf("stopped %s (pid", what);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100394 for (p = G.found_procs; p; p = p->next)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000395 if (p->pid)
396 printf(" %u", (unsigned)p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000397 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000398 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000399 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000400 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000401 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000402 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000403}
404
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000405#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000406static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000407 "stop\0" No_argument "K"
408 "start\0" No_argument "S"
409 "background\0" No_argument "b"
410 "quiet\0" No_argument "q"
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000411 "test\0" No_argument "t"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000412 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000413#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000414 "oknodo\0" No_argument "o"
415 "verbose\0" No_argument "v"
416 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000417#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000418 "startas\0" Required_argument "a"
419 "name\0" Required_argument "n"
420 "signal\0" Required_argument "s"
421 "user\0" Required_argument "u"
422 "chuid\0" Required_argument "c"
423 "exec\0" Required_argument "x"
424 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000425#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000426 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000427#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000428 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000429#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000430
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000431int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000432int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000433{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000434 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000435 char *signame;
436 char *startas;
437 char *chuid;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000438#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000439 struct stat execstat;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000440#endif
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000441#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
442// char *retry_arg = NULL;
443// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000444 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000445#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000446
447 INIT_G();
448
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000449#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000450 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000451#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000452
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000453 /* -K or -S is required; they are mutually exclusive */
454 /* -p is required if -m is given */
455 /* -xpun (at least one) is required if -K is given */
456 /* -xa (at least one) is required if -S is given */
457 /* -q turns off -v */
458 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000459 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000460 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000461 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
Denis Vlasenkocce38582007-02-26 22:47:42 +0000462 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000463 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000464 /* We accept and ignore -R <param> / --retry <param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000465 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000466 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000467
Denis Vlasenkocce38582007-02-26 22:47:42 +0000468 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000469 signal_nr = get_signum(signame);
470 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000471 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000472
Denis Vlasenkocce38582007-02-26 22:47:42 +0000473 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000474 startas = execname;
Denis Vlasenko7987a182008-07-01 10:00:46 +0000475 if (!execname) /* in case -a is given and -x is not */
476 execname = startas;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100477 if (execname) {
478 G.execname_sizeof = strlen(execname) + 1;
479 G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
480 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000481
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000482// IF_FEATURE_START_STOP_DAEMON_FANCY(
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000483// if (retry_arg)
Denys Vlasenko77832482010-08-12 14:14:45 +0200484// retries = xatoi_positive(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000485// )
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000486 //argc -= optind;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000487 argv += optind;
488
Denis Vlasenkob131b272006-12-17 17:30:01 +0000489 if (userspec) {
490 user_id = bb_strtou(userspec, NULL, 10);
491 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000492 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000493 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000494 /* Both start and stop need to know current processes */
495 do_procinit();
Denis Vlasenko85d788e2008-04-19 21:30:52 +0000496
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000497 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000498 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000499 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000500 }
501
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100502 if (G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000503 if (!QUIET)
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100504 printf("%s is already running\n%u\n", execname, (unsigned)G.found_procs->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000505 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000506 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000507
Denis Vlasenko7987a182008-07-01 10:00:46 +0000508#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000509 if (execname)
510 xstat(execname, &execstat);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000511#endif
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000512
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000513 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000514 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000515#if BB_MMU
Peter Korsgaard743edac2011-11-09 19:44:37 +0100516 bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS + DAEMON_DOUBLE_FORK);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000517 /* DAEMON_DEVNULL_STDIO is superfluous -
518 * it's always done by bb_daemonize() */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000519#else
Pascal Bellard926031b2010-07-04 15:32:38 +0200520 pid_t pid = xvfork();
Denis Vlasenko1caca342007-08-02 10:14:29 +0000521 if (pid != 0) {
522 /* parent */
523 /* why _exit? the child may have changed the stack,
524 * so "return 0" may do bad things */
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000525 _exit(EXIT_SUCCESS);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000526 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000527 /* Child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000528 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000529 /* Redirect stdio to /dev/null, close extra FDs.
530 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
Denis Vlasenko7987a182008-07-01 10:00:46 +0000531 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
Denis Vlasenko148f67a2008-07-01 10:05:12 +0000532 + DAEMON_CLOSE_EXTRA_FDS
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000533 + DAEMON_ONLY_SANITIZE,
534 NULL /* argv, unused */ );
535#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000536 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000537 if (opt & OPT_MAKEPID) {
Denis Vlasenko7987a182008-07-01 10:00:46 +0000538 /* User wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000539 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000540 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000541 if (opt & OPT_c) {
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200542 struct bb_uidgid_t ugid;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000543 parse_chown_usergroup_or_die(&ugid, chuid);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200544 if (ugid.uid != (uid_t) -1L) {
Denys Vlasenko585541e2011-09-15 18:27:05 +0200545 struct passwd *pw = xgetpwuid(ugid.uid);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200546 if (ugid.gid != (gid_t) -1L)
Denys Vlasenko585541e2011-09-15 18:27:05 +0200547 pw->pw_gid = ugid.gid;
548 /* initgroups, setgid, setuid: */
549 change_identity(pw);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200550 } else if (ugid.gid != (gid_t) -1L) {
Denys Vlasenko585541e2011-09-15 18:27:05 +0200551 xsetgid(ugid.gid);
552 setgroups(1, &ugid.gid);
553 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000554 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000555#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000556 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000557 /* Set process priority */
558 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
559 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
560 bb_perror_msg_and_die("setpriority(%d)", prio);
561 }
562 }
563#endif
Denis Vlasenko7987a182008-07-01 10:00:46 +0000564 execvp(startas, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200565 bb_perror_msg_and_die("can't execute '%s'", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000566}