blob: 1862f113db8b013bebba379703b25aa26eab34a0 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000011#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000012#include <getopt.h>
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000013#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000014
Eric Andersenc2af1ee2001-10-18 19:33:06 +000015static int signal_nr = 15;
16static int user_id = -1;
Rob Landleyd921b2e2006-08-03 15:41:12 +000017static char *userspec;
Rob Landleyd921b2e2006-08-03 15:41:12 +000018static char *cmdname;
19static char *execname;
20static char *pidfile;
Denis Vlasenkocce38582007-02-26 22:47:42 +000021static smallint quiet;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000022
Eric Andersen63a1a7a2004-03-13 08:33:10 +000023struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000024 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000025 pid_t pid;
26};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000027
Denis Vlasenkob131b272006-12-17 17:30:01 +000028static struct pid_list *found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000029
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000030static inline void push(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000031{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000032 struct pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000033
34 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000035 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000036 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000037 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000038}
39
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000040static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000041{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000042 char buf[sizeof("/proc//exe") + sizeof(int)*3];
43 char *execbuf;
Denis Vlasenkob131b272006-12-17 17:30:01 +000044 int sz;
Rob Landley8cedaba2006-09-04 18:59:39 +000045 int equal;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000046
47 sprintf(buf, "/proc/%d/exe", pid);
Denis Vlasenkob131b272006-12-17 17:30:01 +000048 sz = strlen(name) + 1;
49 execbuf = xzalloc(sz);
50 readlink(buf, execbuf, sz);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000051
Denis Vlasenkob131b272006-12-17 17:30:01 +000052 /* if readlink fails, execbuf still contains "" */
53 equal = !strcmp(execbuf, name);
Rob Landley8cedaba2006-09-04 18:59:39 +000054 if (ENABLE_FEATURE_CLEAN_UP)
55 free(execbuf);
56 return equal;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000057}
58
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000059static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000060{
61 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000062 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000063
Denis Vlasenkob131b272006-12-17 17:30:01 +000064 sprintf(buf, "/proc/%u", pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000065 if (stat(buf, &sb) != 0)
66 return 0;
67 return (sb.st_uid == uid);
68}
69
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000070static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000071{
Denis Vlasenkob131b272006-12-17 17:30:01 +000072 char fname[sizeof("/proc//stat") + sizeof(int)*3];
73 char *buf;
74 int r = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000075
Denis Vlasenkob131b272006-12-17 17:30:01 +000076 sprintf(fname, "/proc/%u/stat", pid);
77 buf = xmalloc_open_read_close(fname, NULL);
78 if (buf) {
79 char *p = strchr(buf, '(');
80 if (p) {
81 char *pe = strrchr(++p, ')');
82 if (pe) {
83 *pe = '\0';
84 r = !strcmp(p, name);
85 }
86 }
87 free(buf);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000088 }
Denis Vlasenkob131b272006-12-17 17:30:01 +000089 return r;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000090}
91
92
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000093static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000094{
95 if (execname && !pid_is_exec(pid, execname)) {
96 return;
97 }
98 if (userspec && !pid_is_user(pid, user_id)) {
99 return;
100 }
101 if (cmdname && !pid_is_cmd(pid, cmdname)) {
102 return;
103 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000104 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000105}
106
107
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000108static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000109{
110 FILE *f;
111 pid_t pid;
112
Eric Andersen625da9d2004-04-13 18:28:46 +0000113 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000114 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000115 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000116 check(pid);
117 fclose(f);
118 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000119 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000120}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000121
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000122static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000123{
124 DIR *procdir;
125 struct dirent *entry;
126 int foundany, pid;
127
Eric Andersen625da9d2004-04-13 18:28:46 +0000128 if (pidfile) {
129 do_pidfile();
130 return;
131 }
132
Rob Landleyd921b2e2006-08-03 15:41:12 +0000133 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000134
135 foundany = 0;
136 while ((entry = readdir(procdir)) != NULL) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000137 pid = bb_strtou(entry->d_name, NULL, 10);
138 if (errno)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000139 continue;
140 foundany++;
141 check(pid);
142 }
143 closedir(procdir);
144 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146}
147
148
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000149static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000150{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000151 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000152 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000153 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154
Eric Andersen625da9d2004-04-13 18:28:46 +0000155 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000156
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000157 if (cmdname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000158 what = xstrdup(cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000159 else if (execname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000160 what = xstrdup(execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000161 else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000162 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000164 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000165 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000166 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000167
168 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000169 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000170 printf("no %s found; none killed\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000171 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000172 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000173 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000174 }
175 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000176 if (kill(p->pid, signal_nr) == 0) {
177 p->pid = -p->pid;
178 killed++;
179 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000180 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000181 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000182 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000183 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000184 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000185 for (p = found; p; p = p->next)
186 if(p->pid < 0)
187 printf(" %d", -p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000188 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000189 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000190 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000191 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000192 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000193}
194
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000195#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000196static const struct option long_options[] = {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000197 { "stop", 0, NULL, 'K' },
198 { "start", 0, NULL, 'S' },
199 { "background", 0, NULL, 'b' },
200 { "quiet", 0, NULL, 'q' },
201 { "make-pidfile", 0, NULL, 'm' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000202#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000203 { "oknodo", 0, NULL, 'o' },
204 { "verbose", 0, NULL, 'v' },
205 { "nicelevel", 1, NULL, 'N' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000206#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000207 { "startas", 1, NULL, 'a' },
208 { "name", 1, NULL, 'n' },
209 { "signal", 1, NULL, 's' },
210 { "user", 1, NULL, 'u' },
211 { "chuid", 1, NULL, 'c' },
212 { "exec", 1, NULL, 'x' },
213 { "pidfile", 1, NULL, 'p' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000214#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000215 { "retry", 1, NULL, 'R' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000216#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000217 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000218};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000219#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000220
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000221enum {
222 CTX_STOP = 0x1,
223 CTX_START = 0x2,
Denis Vlasenkocce38582007-02-26 22:47:42 +0000224 OPT_BACKGROUND = 0x4, // -b
225 OPT_QUIET = 0x8, // -q
226 OPT_MAKEPID = 0x10, // -m
227 OPT_a = 0x20, // -a
228 OPT_n = 0x40, // -n
229 OPT_s = 0x80, // -s
230 OPT_u = 0x100, // -u
231 OPT_c = 0x200, // -c
232 OPT_x = 0x400, // -x
233 OPT_p = 0x800, // -p
234 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
235 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
236 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000237};
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000238
Denis Vlasenko06af2162007-02-03 17:28:39 +0000239int start_stop_daemon_main(int argc, char **argv);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000240int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000241{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000242 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000243 char *signame;
244 char *startas;
245 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000246#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
247// char *retry_arg = NULL;
248// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000249 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000250#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000251#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000252 applet_long_options = long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000253#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000254
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000255 /* Check required one context option was given */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000256 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
Denis Vlasenkocce38582007-02-26 22:47:42 +0000257 opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
258 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
259// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
260 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000261 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000262// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000263 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000264
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000265 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000266
Denis Vlasenkocce38582007-02-26 22:47:42 +0000267 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000268 signal_nr = get_signum(signame);
269 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000270 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000271
Denis Vlasenkocce38582007-02-26 22:47:42 +0000272 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000273 startas = execname;
274
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000275// USE_FEATURE_START_STOP_DAEMON_FANCY(
276// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000277// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000278// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000279 argc -= optind;
280 argv += optind;
281
Denis Vlasenkob131b272006-12-17 17:30:01 +0000282 if (userspec) {
283 user_id = bb_strtou(userspec, NULL, 10);
284 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000285 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000286 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000287
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000288 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000289 int i = do_stop();
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000290 return (opt & OPT_OKNODO) ? 0 : (i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000291 }
292
Eric Andersen625da9d2004-04-13 18:28:46 +0000293 do_procinit();
294
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000295 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000296 if (!quiet)
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000297 printf("%s already running\n%d\n", execname, found->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000298 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000299 }
300 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000301 if (opt & OPT_BACKGROUND) {
Eric Andersen625da9d2004-04-13 18:28:46 +0000302 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000303 bb_daemonize();
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}