blob: 0681a085cd6b0a7055d0f549c4bd01ee999164a9 [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
Rob Landleyd921b2e2006-08-03 15:41:12 +000011#include <getopt.h>
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000012#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000013
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
15
Eric Andersenc2af1ee2001-10-18 19:33:06 +000016static int signal_nr = 15;
17static int user_id = -1;
Rob Landleyd921b2e2006-08-03 15:41:12 +000018static char *userspec;
Rob Landleyd921b2e2006-08-03 15:41:12 +000019static char *cmdname;
20static char *execname;
21static char *pidfile;
Denis Vlasenkocce38582007-02-26 22:47:42 +000022static smallint quiet;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000023
Eric Andersen63a1a7a2004-03-13 08:33:10 +000024struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000025 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000026 pid_t pid;
27};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000028
Denis Vlasenkob131b272006-12-17 17:30:01 +000029static struct pid_list *found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000030
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000031static inline void push(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000032{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000033 struct pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000034
35 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000036 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000037 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000038 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000039}
40
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000041static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000042{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000043 char buf[sizeof("/proc//exe") + sizeof(int)*3];
44 char *execbuf;
Denis Vlasenkob131b272006-12-17 17:30:01 +000045 int sz;
Rob Landley8cedaba2006-09-04 18:59:39 +000046 int equal;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000047
48 sprintf(buf, "/proc/%d/exe", pid);
Denis Vlasenkob131b272006-12-17 17:30:01 +000049 sz = strlen(name) + 1;
50 execbuf = xzalloc(sz);
51 readlink(buf, execbuf, sz);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000052
Denis Vlasenkob131b272006-12-17 17:30:01 +000053 /* if readlink fails, execbuf still contains "" */
54 equal = !strcmp(execbuf, name);
Rob Landley8cedaba2006-09-04 18:59:39 +000055 if (ENABLE_FEATURE_CLEAN_UP)
56 free(execbuf);
57 return equal;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000058}
59
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000060static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000061{
62 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000063 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000064
Denis Vlasenkob131b272006-12-17 17:30:01 +000065 sprintf(buf, "/proc/%u", pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000066 if (stat(buf, &sb) != 0)
67 return 0;
68 return (sb.st_uid == uid);
69}
70
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000071static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000072{
Denis Vlasenkob131b272006-12-17 17:30:01 +000073 char fname[sizeof("/proc//stat") + sizeof(int)*3];
74 char *buf;
75 int r = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000076
Denis Vlasenkob131b272006-12-17 17:30:01 +000077 sprintf(fname, "/proc/%u/stat", pid);
78 buf = xmalloc_open_read_close(fname, NULL);
79 if (buf) {
80 char *p = strchr(buf, '(');
81 if (p) {
82 char *pe = strrchr(++p, ')');
83 if (pe) {
84 *pe = '\0';
85 r = !strcmp(p, name);
86 }
87 }
88 free(buf);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000089 }
Denis Vlasenkob131b272006-12-17 17:30:01 +000090 return r;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000091}
92
93
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000094static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000095{
96 if (execname && !pid_is_exec(pid, execname)) {
97 return;
98 }
99 if (userspec && !pid_is_user(pid, user_id)) {
100 return;
101 }
102 if (cmdname && !pid_is_cmd(pid, cmdname)) {
103 return;
104 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000105 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000106}
107
108
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000109static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000110{
111 FILE *f;
112 pid_t pid;
113
Eric Andersen625da9d2004-04-13 18:28:46 +0000114 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000115 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000116 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000117 check(pid);
118 fclose(f);
119 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000120 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000121}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000122
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000123static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000124{
125 DIR *procdir;
126 struct dirent *entry;
127 int foundany, pid;
128
Eric Andersen625da9d2004-04-13 18:28:46 +0000129 if (pidfile) {
130 do_pidfile();
131 return;
132 }
133
Rob Landleyd921b2e2006-08-03 15:41:12 +0000134 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000135
136 foundany = 0;
137 while ((entry = readdir(procdir)) != NULL) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000138 pid = bb_strtou(entry->d_name, NULL, 10);
139 if (errno)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000140 continue;
141 foundany++;
142 check(pid);
143 }
144 closedir(procdir);
145 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000147}
148
149
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000150static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000151{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000152 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000153 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000154 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155
Eric Andersen625da9d2004-04-13 18:28:46 +0000156 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000157
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000158 if (cmdname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000159 what = xstrdup(cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000160 else if (execname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000161 what = xstrdup(execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000162 else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000163 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000164 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000165 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000166 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000167 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000168
169 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000170 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000171 printf("no %s found; none killed\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000172 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000173 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000174 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000175 }
176 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000177 if (kill(p->pid, signal_nr) == 0) {
178 p->pid = -p->pid;
179 killed++;
180 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000181 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000182 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000183 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000184 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000185 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000186 for (p = found; p; p = p->next)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000187 if (p->pid < 0)
Eric Andersen950d8b42001-10-31 09:55:39 +0000188 printf(" %d", -p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000189 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000190 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000191 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000192 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000193 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000194}
195
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000196#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000197static const struct option long_options[] = {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000198 { "stop", 0, NULL, 'K' },
199 { "start", 0, NULL, 'S' },
200 { "background", 0, NULL, 'b' },
201 { "quiet", 0, NULL, 'q' },
202 { "make-pidfile", 0, NULL, 'm' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000203#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000204 { "oknodo", 0, NULL, 'o' },
205 { "verbose", 0, NULL, 'v' },
206 { "nicelevel", 1, NULL, 'N' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000207#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000208 { "startas", 1, NULL, 'a' },
209 { "name", 1, NULL, 'n' },
210 { "signal", 1, NULL, 's' },
211 { "user", 1, NULL, 'u' },
212 { "chuid", 1, NULL, 'c' },
213 { "exec", 1, NULL, 'x' },
214 { "pidfile", 1, NULL, 'p' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000215#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000216 { "retry", 1, NULL, 'R' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000217#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000218 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000219};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000220#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000221
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000222enum {
223 CTX_STOP = 0x1,
224 CTX_START = 0x2,
Denis Vlasenkocce38582007-02-26 22:47:42 +0000225 OPT_BACKGROUND = 0x4, // -b
226 OPT_QUIET = 0x8, // -q
227 OPT_MAKEPID = 0x10, // -m
228 OPT_a = 0x20, // -a
229 OPT_n = 0x40, // -n
230 OPT_s = 0x80, // -s
231 OPT_u = 0x100, // -u
232 OPT_c = 0x200, // -c
233 OPT_x = 0x400, // -x
234 OPT_p = 0x800, // -p
235 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
236 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
237 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000238};
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000239
Denis Vlasenko06af2162007-02-03 17:28:39 +0000240int start_stop_daemon_main(int argc, char **argv);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000241int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000242{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000243 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000244 char *signame;
245 char *startas;
246 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000247#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
248// char *retry_arg = NULL;
249// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000250 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000251#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000252#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000253 applet_long_options = long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000254#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000255
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000256 /* Check required one context option was given */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000257 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
Denis Vlasenkocce38582007-02-26 22:47:42 +0000258 opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
259 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
260// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
261 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000262 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000263// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000264 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000265
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000266 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000267
Denis Vlasenkocce38582007-02-26 22:47:42 +0000268 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000269 signal_nr = get_signum(signame);
270 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000271 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000272
Denis Vlasenkocce38582007-02-26 22:47:42 +0000273 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000274 startas = execname;
275
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000276// USE_FEATURE_START_STOP_DAEMON_FANCY(
277// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000278// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000279// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000280 argc -= optind;
281 argv += optind;
282
Denis Vlasenkob131b272006-12-17 17:30:01 +0000283 if (userspec) {
284 user_id = bb_strtou(userspec, NULL, 10);
285 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000286 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000287 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000288
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000289 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000290 int i = do_stop();
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000291 return (opt & OPT_OKNODO) ? 0 : (i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000292 }
293
Eric Andersen625da9d2004-04-13 18:28:46 +0000294 do_procinit();
295
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000296 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000297 if (!quiet)
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000298 printf("%s already running\n%d\n", execname, found->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000299 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000300 }
301 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000302 if (opt & OPT_BACKGROUND) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000303 bb_daemonize(0);
Eric Andersen53a22992002-01-26 09:04:45 +0000304 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000305 if (opt & OPT_MAKEPID) {
Eric Andersen625da9d2004-04-13 18:28:46 +0000306 /* user wants _us_ to make the pidfile */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000307 FILE *pidf = xfopen(pidfile, "w");
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000308
Eric Andersen625da9d2004-04-13 18:28:46 +0000309 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000310 fprintf(pidf, "%d\n", pidt);
311 fclose(pidf);
312 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000313 if (opt & OPT_c) {
314 struct bb_uidgid_t ugid;
315 parse_chown_usergroup_or_die(&ugid, chuid);
316 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
317 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
Rob Landleyf0623a22006-07-17 00:35:07 +0000318 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000319#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000320 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000321 /* Set process priority */
322 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
323 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
324 bb_perror_msg_and_die("setpriority(%d)", prio);
325 }
326 }
327#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000328 execv(startas, argv);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000329 bb_perror_msg_and_die("cannot start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000330}