blob: 3e5dd9faa2a5d4050029a4e785b786a07fdde716 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020060//config: bool "start-stop-daemon (12 kb)"
Denys Vlasenko28826ac2015-10-19 00:52:26 +020061//config: default y
62//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020063//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.
Denys Vlasenko28826ac2015-10-19 00:52:26 +020066//config:
Denys Vlasenkof5604222017-01-10 14:58:54 +010067//config:config FEATURE_START_STOP_DAEMON_LONG_OPTIONS
68//config: bool "Enable long options"
69//config: default y
70//config: depends on START_STOP_DAEMON && LONG_OPTS
71//config:
Denys Vlasenko28826ac2015-10-19 00:52:26 +020072//config:config FEATURE_START_STOP_DAEMON_FANCY
73//config: bool "Support additional arguments"
74//config: default y
75//config: depends on START_STOP_DAEMON
76//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020077//config: -o|--oknodo ignored since we exit with 0 anyway
78//config: -v|--verbose
79//config: -N|--nicelevel N
Denys Vlasenko28826ac2015-10-19 00:52:26 +020080
81//applet:IF_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, BB_DIR_SBIN, BB_SUID_DROP, start_stop_daemon))
Denys Vlasenko184c7382017-08-06 20:55:56 +020082/* not NOEXEC: uses bb_common_bufsiz1 */
Denys Vlasenko28826ac2015-10-19 00:52:26 +020083
84//kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000085
Pere Orga6a3e01d2011-04-01 22:56:30 +020086//usage:#define start_stop_daemon_trivial_usage
87//usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]"
88//usage:#define start_stop_daemon_full_usage "\n\n"
89//usage: "Search for matching processes, and then\n"
Denys Vlasenko49749172017-08-08 23:18:05 +020090//usage: "-K: stop all matching processes\n"
91//usage: "-S: start a process unless a matching process is found\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020092//usage: "\nProcess matching:"
93//usage: "\n -u USERNAME|UID Match only this user's processes"
94//usage: "\n -n NAME Match processes with NAME"
95//usage: "\n in comm field in /proc/PID/stat"
96//usage: "\n -x EXECUTABLE Match processes with this command"
Alexander Vickberg100fa202019-04-27 15:42:41 +020097//usage: "\n in /proc/PID/cmdline"
Denys Vlasenko49749172017-08-08 23:18:05 +020098//usage: "\n -p FILE Match a process with PID from FILE"
Pere Orga6a3e01d2011-04-01 22:56:30 +020099//usage: "\n All specified conditions must match"
100//usage: "\n-S only:"
101//usage: "\n -x EXECUTABLE Program to run"
102//usage: "\n -a NAME Zeroth argument"
103//usage: "\n -b Background"
104//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
105//usage: "\n -N N Change nice level"
106//usage: )
Denys Vlasenko49749172017-08-08 23:18:05 +0200107//usage: "\n -c USER[:[GRP]] Change user/group"
108//usage: "\n -m Write PID to pidfile specified by -p"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200109//usage: "\n-K only:"
110//usage: "\n -s SIG Signal to send"
Denys Vlasenko49749172017-08-08 23:18:05 +0200111//usage: "\n -t Match only, exit with 0 if found"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200112//usage: "\nOther:"
113//usage: IF_FEATURE_START_STOP_DAEMON_FANCY(
114//usage: "\n -o Exit with status 0 if nothing is done"
115//usage: "\n -v Verbose"
116//usage: )
117//usage: "\n -q Quiet"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200118
Denis Vlasenko1caca342007-08-02 10:14:29 +0000119/* Override ENABLE_FEATURE_PIDFILE */
120#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000121#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200122#include "common_bufsiz.h"
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000123
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000124struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000125 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000126 pid_t pid;
127};
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000128
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000129enum {
130 CTX_STOP = (1 << 0),
131 CTX_START = (1 << 1),
132 OPT_BACKGROUND = (1 << 2), // -b
133 OPT_QUIET = (1 << 3), // -q
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000134 OPT_TEST = (1 << 4), // -t
135 OPT_MAKEPID = (1 << 5), // -m
136 OPT_a = (1 << 6), // -a
137 OPT_n = (1 << 7), // -n
138 OPT_s = (1 << 8), // -s
139 OPT_u = (1 << 9), // -u
140 OPT_c = (1 << 10), // -c
141 OPT_x = (1 << 11), // -x
142 OPT_p = (1 << 12), // -p
143 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
144 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
145 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000146};
147#define QUIET (option_mask32 & OPT_QUIET)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000148#define TEST (option_mask32 & OPT_TEST)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000149
150struct globals {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100151 struct pid_list *found_procs;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000152 char *userspec;
153 char *cmdname;
154 char *execname;
155 char *pidfile;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100156 char *execname_cmpbuf;
157 unsigned execname_sizeof;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000158 int user_id;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000159 smallint signal_nr;
Denys Vlasenko837913f2018-04-14 01:23:40 +0200160#ifdef OLDER_VERSION_OF_X
161 struct stat execstat;
162#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100163} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200164#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000165#define userspec (G.userspec )
166#define cmdname (G.cmdname )
167#define execname (G.execname )
168#define pidfile (G.pidfile )
169#define user_id (G.user_id )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000170#define signal_nr (G.signal_nr )
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000171#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200172 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000173 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;
Denys Vlasenko837913f2018-04-14 01:23:40 +0200190 if (st.st_dev == G.execstat.st_dev
191 && st.st_ino == G.execstat.st_ino)
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000192 return 1;
193 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000194}
Denys Vlasenkoc783cf72018-04-14 01:29:01 +0200195#else
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000196static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000197{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000198 ssize_t bytes;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100199 char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
Denys Vlasenko17eedca2012-03-05 16:28:07 +0100200 char *procname, *exelink;
201 int match;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000202
Denys Vlasenko17eedca2012-03-05 16:28:07 +0100203 procname = buf + sprintf(buf, "/proc/%u/exe", (unsigned)pid) - 3;
204
205 exelink = xmalloc_readlink(buf);
206 match = (exelink && strcmp(execname, exelink) == 0);
207 free(exelink);
208 if (match)
209 return match;
210
211 strcpy(procname, "cmdline");
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100212 bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000213 if (bytes > 0) {
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100214 G.execname_cmpbuf[bytes] = '\0';
215 return strcmp(execname, G.execname_cmpbuf) == 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000216 }
217 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000218}
Denys Vlasenkoc783cf72018-04-14 01:29:01 +0200219#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000220
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000221static int pid_is_name(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000222{
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000223 /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
224 char buf[32]; /* should be enough */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000225 char *p, *pe;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000226
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000227 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000228 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
229 return 0;
230 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
231 p = strchr(buf, '(');
232 if (!p)
233 return 0;
234 pe = strrchr(++p, ')');
235 if (!pe)
236 return 0;
237 *pe = '\0';
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000238 /* we require comm to match and to not be truncated */
239 /* in Linux, if comm is 15 chars, it may be a truncated
240 * name, so we don't allow that to match */
241 if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
242 return 0;
243 return strcmp(p, cmdname) == 0;
244}
245
246static int pid_is_user(int pid)
247{
248 struct stat sb;
249 char buf[sizeof("/proc/") + sizeof(int)*3];
250
251 sprintf(buf, "/proc/%u", (unsigned)pid);
252 if (stat(buf, &sb) != 0)
253 return 0;
254 return (sb.st_uid == (uid_t)user_id);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000255}
256
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000257static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000258{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000259 struct pid_list *p;
260
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000261 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000262 return;
263 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000264 if (cmdname && !pid_is_name(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000265 return;
266 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000267 if (userspec && !pid_is_user(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000268 return;
269 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000270 p = xmalloc(sizeof(*p));
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100271 p->next = G.found_procs;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000272 p->pid = pid;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100273 G.found_procs = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000274}
275
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000276static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000277{
278 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000279 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000280
Denis Vlasenko5415c852008-07-21 23:05:26 +0000281 f = fopen_for_read(pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000282 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000283 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000284 check(pid);
285 fclose(f);
286 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000287 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000288}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000289
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000290static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000291{
292 DIR *procdir;
293 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000294 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000295
Eric Andersen625da9d2004-04-13 18:28:46 +0000296 if (pidfile) {
297 do_pidfile();
298 return;
299 }
300
Rob Landleyd921b2e2006-08-03 15:41:12 +0000301 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000302
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000303 pid = 0;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000304 while (1) {
Denis Vlasenko1f228982008-04-22 00:16:29 +0000305 errno = 0; /* clear any previous error */
306 entry = readdir(procdir);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000307// TODO: this check is too generic, it's better
308// to check for exact errno(s) which mean that we got stale entry
Denis Vlasenko1f228982008-04-22 00:16:29 +0000309 if (errno) /* Stale entry, process has died after opendir */
310 continue;
311 if (!entry) /* EOF, no more entries */
312 break;
313 pid = bb_strtou(entry->d_name, NULL, 10);
314 if (errno) /* NaN */
315 continue;
316 check(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000317 }
318 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000319 if (!pid)
James Byrne69374872019-07-02 11:35:03 +0200320 bb_simple_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000321}
322
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000323static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000324{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000325 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000326 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000327 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000328
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000329 if (cmdname) {
330 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
331 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
332 } else if (execname) {
333 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
334 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000335 } else if (pidfile) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000336 what = xasprintf("process in pidfile '%s'", pidfile);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000337 } else if (userspec) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000338 what = xasprintf("process(es) owned by '%s'", userspec);
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000339 } else {
James Byrne69374872019-07-02 11:35:03 +0200340 bb_simple_error_msg_and_die("internal error, please report");
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000341 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000342
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100343 if (!G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000344 if (!QUIET)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000345 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000346 killed = -1;
347 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000348 }
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100349 for (p = G.found_procs; p; p = p->next) {
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200350 if (kill(p->pid, TEST ? 0 : signal_nr) == 0) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000351 killed++;
352 } else {
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000353 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
Denys Vlasenko929f63e2011-04-04 02:03:35 +0200354 p->pid = 0;
355 if (TEST) {
356 /* Example: -K --test --pidfile PIDFILE detected
357 * that PIDFILE's pid doesn't exist */
358 killed = -1;
359 goto ret;
360 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000361 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000362 }
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000363 if (!QUIET && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000364 printf("stopped %s (pid", what);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100365 for (p = G.found_procs; p; p = p->next)
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000366 if (p->pid)
367 printf(" %u", (unsigned)p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000368 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000369 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000370 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000371 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000372 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000373 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000374}
375
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000376#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000377static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000378 "stop\0" No_argument "K"
379 "start\0" No_argument "S"
380 "background\0" No_argument "b"
381 "quiet\0" No_argument "q"
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000382 "test\0" No_argument "t"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000383 "make-pidfile\0" No_argument "m"
Denys Vlasenko184c7382017-08-06 20:55:56 +0200384# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000385 "oknodo\0" No_argument "o"
386 "verbose\0" No_argument "v"
387 "nicelevel\0" Required_argument "N"
Denys Vlasenko184c7382017-08-06 20:55:56 +0200388# endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000389 "startas\0" Required_argument "a"
390 "name\0" Required_argument "n"
391 "signal\0" Required_argument "s"
392 "user\0" Required_argument "u"
393 "chuid\0" Required_argument "c"
394 "exec\0" Required_argument "x"
395 "pidfile\0" Required_argument "p"
Denys Vlasenko184c7382017-08-06 20:55:56 +0200396# if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000397 "retry\0" Required_argument "R"
Denys Vlasenko184c7382017-08-06 20:55:56 +0200398# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000399 ;
Denys Vlasenko036585a2017-08-08 16:38:18 +0200400# define GETOPT32 getopt32long
401# define LONGOPTS start_stop_daemon_longopts,
402#else
403# define GETOPT32 getopt32
404# define LONGOPTS
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000405#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000406
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000407int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000408int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000409{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000410 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000411 char *signame;
Denys Vlasenko088fec32019-01-14 14:45:18 +0100412 char *startas = NULL;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000413 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000414#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
415// char *retry_arg = NULL;
416// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000417 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000418#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000419
420 INIT_G();
421
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200422 opt = GETOPT32(argv, "^"
423 "KSbqtma:n:s:u:c:x:p:"
424 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
425 /* -K or -S is required; they are mutually exclusive */
426 /* -p is required if -m is given */
427 /* -xpun (at least one) is required if -K is given */
Denys Vlasenko088fec32019-01-14 14:45:18 +0100428// /* -xa (at least one) is required if -S is given */
429//WRONG: "start-stop-daemon -S -- sleep 5" is a valid invocation
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200430 /* -q turns off -v */
431 "\0"
Denys Vlasenko088fec32019-01-14 14:45:18 +0100432 "K:S:K--S:S--K:m?p:K?xpun"
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200433 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"),
Denys Vlasenko036585a2017-08-08 16:38:18 +0200434 LONGOPTS
Denis Vlasenkocce38582007-02-26 22:47:42 +0000435 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000436 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Denis Vlasenko75897ea2008-09-27 01:05:13 +0000437 /* We accept and ignore -R <param> / --retry <param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000438 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000439 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000440
Denis Vlasenkocce38582007-02-26 22:47:42 +0000441 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000442 signal_nr = get_signum(signame);
443 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000444 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000445
Denys Vlasenko088fec32019-01-14 14:45:18 +0100446 //argc -= optind;
447 argv += optind;
448// ARGS contains zeroth arg if -x/-a is not given, else it starts with 1st arg.
449// These will try to execute "[/bin/]sleep 5":
450// "start-stop-daemon -S -- sleep 5"
451// "start-stop-daemon -S -x /bin/sleep -- 5"
452// "start-stop-daemon -S -a sleep -- 5"
453// NB: -n option does _not_ behave in this way: this will try to execute "5":
454// "start-stop-daemon -S -n sleep -- 5"
Alexander Vickberg100fa202019-04-27 15:42:41 +0200455 if (opt & CTX_START) {
456 if (!execname) { /* -x is not given */
457 execname = startas;
458 if (!execname) { /* neither -x nor -a is given */
459 execname = argv[0];
460 if (!execname)
461 bb_show_usage();
462 argv++;
463 }
Denys Vlasenko088fec32019-01-14 14:45:18 +0100464 }
Alexander Vickberg100fa202019-04-27 15:42:41 +0200465 if (!startas) /* -a is not given: use -x EXECUTABLE or argv[0] */
466 startas = execname;
467 *--argv = startas;
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100468 }
Alexander Vickberg100fa202019-04-27 15:42:41 +0200469 if (execname) {
470 G.execname_sizeof = strlen(execname) + 1;
471 G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
472 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000473// IF_FEATURE_START_STOP_DAEMON_FANCY(
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000474// if (retry_arg)
Denys Vlasenko77832482010-08-12 14:14:45 +0200475// retries = xatoi_positive(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000476// )
Denis Vlasenkob131b272006-12-17 17:30:01 +0000477 if (userspec) {
478 user_id = bb_strtou(userspec, NULL, 10);
479 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000480 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000481 }
Alexander Vickberg100fa202019-04-27 15:42:41 +0200482
Denis Vlasenko7987a182008-07-01 10:00:46 +0000483 /* Both start and stop need to know current processes */
484 do_procinit();
Denis Vlasenko85d788e2008-04-19 21:30:52 +0000485
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000486 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000487 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000488 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000489 }
490
Alexander Vickberg100fa202019-04-27 15:42:41 +0200491 /* else: CTX_START (-S). execname can't be NULL. */
492
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100493 if (G.found_procs) {
Denis Vlasenkoe0612262008-04-30 13:58:31 +0000494 if (!QUIET)
Denys Vlasenko088fec32019-01-14 14:45:18 +0100495 printf("%s is already running\n", execname);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000496 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000497 }
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000498
Denis Vlasenko7987a182008-07-01 10:00:46 +0000499#ifdef OLDER_VERSION_OF_X
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000500 if (execname)
Denys Vlasenko837913f2018-04-14 01:23:40 +0200501 xstat(execname, &G.execstat);
Denis Vlasenko7987a182008-07-01 10:00:46 +0000502#endif
Denis Vlasenkoe125a682008-05-18 21:17:52 +0000503
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000504 if (opt & OPT_BACKGROUND) {
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200505 /* Daemons usually call bb_daemonize_or_rexec(), but SSD can do
506 * without: SSD is not itself a daemon, it _execs_ a daemon.
507 * The usual NOMMU problem of "child can't run indefinitely,
508 * it must exec" does not bite us: we exec anyway.
Denys Vlasenko088fec32019-01-14 14:45:18 +0100509 *
510 * bb_daemonize(DAEMON_DEVNULL_STDIO | DAEMON_CLOSE_EXTRA_FDS | DAEMON_DOUBLE_FORK)
511 * can be used on MMU systems, but use of vfork()
512 * is preferable since we want to create pidfile
513 * _before_ parent returns, and vfork() on Linux
514 * ensures that (by blocking parent until exec in the child).
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200515 */
Pascal Bellard926031b2010-07-04 15:32:38 +0200516 pid_t pid = xvfork();
Denis Vlasenko1caca342007-08-02 10:14:29 +0000517 if (pid != 0) {
Denys Vlasenko088fec32019-01-14 14:45:18 +0100518 /* Parent */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000519 /* why _exit? the child may have changed the stack,
Denys Vlasenko088fec32019-01-14 14:45:18 +0100520 * so "return 0" may do bad things
521 */
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100522 _exit_SUCCESS();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000523 }
Denis Vlasenko7987a182008-07-01 10:00:46 +0000524 /* Child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000525 setsid(); /* detach from controlling tty */
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200526 /* Redirect stdio to /dev/null, close extra FDs */
527 bb_daemon_helper(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
Denys Vlasenko088fec32019-01-14 14:45:18 +0100528 /* On Linux, session leader can acquire ctty
529 * unknowingly, by opening a tty.
530 * Prevent this: stop being a session leader.
531 */
532 pid = xvfork();
533 if (pid != 0)
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100534 _exit_SUCCESS(); /* Parent */
Eric Andersen53a22992002-01-26 09:04:45 +0000535 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000536 if (opt & OPT_MAKEPID) {
Denis Vlasenko7987a182008-07-01 10:00:46 +0000537 /* User wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000538 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000539 }
Aitor Esteve Alvarado7b6d4f52019-03-07 10:14:48 +0100540#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
541 if (opt & OPT_NICELEVEL) {
542 /* Set process priority (must be before OPT_c) */
543 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
544 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
545 bb_perror_msg_and_die("setpriority(%d)", prio);
546 }
547 }
548#endif
Denis Vlasenkocce38582007-02-26 22:47:42 +0000549 if (opt & OPT_c) {
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200550 struct bb_uidgid_t ugid;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000551 parse_chown_usergroup_or_die(&ugid, chuid);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200552 if (ugid.uid != (uid_t) -1L) {
Denys Vlasenko585541e2011-09-15 18:27:05 +0200553 struct passwd *pw = xgetpwuid(ugid.uid);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200554 if (ugid.gid != (gid_t) -1L)
Denys Vlasenko585541e2011-09-15 18:27:05 +0200555 pw->pw_gid = ugid.gid;
556 /* initgroups, setgid, setuid: */
557 change_identity(pw);
Denys Vlasenko3d0805e2015-10-19 04:37:19 +0200558 } else if (ugid.gid != (gid_t) -1L) {
Denys Vlasenko585541e2011-09-15 18:27:05 +0200559 xsetgid(ugid.gid);
560 setgroups(1, &ugid.gid);
561 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000562 }
Denys Vlasenko77524a32019-01-14 15:00:49 +0100563 /* Try:
564 * strace -oLOG start-stop-daemon -S -x /bin/usleep -a qwerty 500000
565 * should exec "/bin/usleep", but argv[0] should be "qwerty":
566 */
567 execvp(execname, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200568 bb_perror_msg_and_die("can't execute '%s'", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000569}