blob: f52352995ff2437a23d86ea549eb5151dedb62ba [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;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000017static int quiet;
Rob Landleyd921b2e2006-08-03 15:41:12 +000018static char *userspec;
19static char *chuid;
20static char *cmdname;
21static char *execname;
22static char *pidfile;
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
Eric Andersen63a1a7a2004-03-13 08:33:10 +000029static struct pid_list *found = NULL;
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;
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);
Rob Landley8cedaba2006-09-04 18:59:39 +000048 execbuf = xstrdup(name);
49 readlink(buf, execbuf, strlen(name)+1);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000050
Rob Landley8cedaba2006-09-04 18:59:39 +000051 equal = ! strcmp(execbuf, name);
52 if (ENABLE_FEATURE_CLEAN_UP)
53 free(execbuf);
54 return equal;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000055}
56
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000057static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000058{
59 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000060 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000061
62 sprintf(buf, "/proc/%d", pid);
63 if (stat(buf, &sb) != 0)
64 return 0;
65 return (sb.st_uid == uid);
66}
67
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000068static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000069{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000070 char buf[sizeof("/proc//stat") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000071 FILE *f;
72 int c;
73
74 sprintf(buf, "/proc/%d/stat", pid);
75 f = fopen(buf, "r");
76 if (!f)
77 return 0;
78 while ((c = getc(f)) != EOF && c != '(')
79 ;
80 if (c != '(') {
81 fclose(f);
82 return 0;
83 }
84 /* this hopefully handles command names containing ')' */
85 while ((c = getc(f)) != EOF && c == *name)
86 name++;
87 fclose(f);
88 return (c == ')' && *name == '\0');
89}
90
91
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000092static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000093{
94 if (execname && !pid_is_exec(pid, execname)) {
95 return;
96 }
97 if (userspec && !pid_is_user(pid, user_id)) {
98 return;
99 }
100 if (cmdname && !pid_is_cmd(pid, cmdname)) {
101 return;
102 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000103 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000104}
105
106
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000107static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000108{
109 FILE *f;
110 pid_t pid;
111
Eric Andersen625da9d2004-04-13 18:28:46 +0000112 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000113 if (f) {
114 if (fscanf(f, "%d", &pid) == 1)
115 check(pid);
116 fclose(f);
117 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000118 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000119}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000120
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000121static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000122{
123 DIR *procdir;
124 struct dirent *entry;
125 int foundany, pid;
126
Eric Andersen625da9d2004-04-13 18:28:46 +0000127 if (pidfile) {
128 do_pidfile();
129 return;
130 }
131
Rob Landleyd921b2e2006-08-03 15:41:12 +0000132 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000133
134 foundany = 0;
135 while ((entry = readdir(procdir)) != NULL) {
136 if (sscanf(entry->d_name, "%d", &pid) != 1)
137 continue;
138 foundany++;
139 check(pid);
140 }
141 closedir(procdir);
142 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000144}
145
146
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000147static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000148{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000149 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000150 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000151 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000152
Eric Andersen625da9d2004-04-13 18:28:46 +0000153 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000154
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155 if (cmdname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000156 what = xstrdup(cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000157 else if (execname)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000158 what = xstrdup(execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000159 else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000160 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000161 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000162 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000164 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000165
166 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000167 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000168 printf("no %s found; none killed\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000169 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000170 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000171 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000172 }
173 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000174 if (kill(p->pid, signal_nr) == 0) {
175 p->pid = -p->pid;
176 killed++;
177 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000178 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000179 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000180 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000181 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000182 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000183 for (p = found; p; p = p->next)
184 if(p->pid < 0)
185 printf(" %d", -p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000186 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000187 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000188 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000189 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000190 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000191}
192
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000193#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000194static const struct option ssd_long_options[] = {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000195 { "stop", 0, NULL, 'K' },
196 { "start", 0, NULL, 'S' },
197 { "background", 0, NULL, 'b' },
198 { "quiet", 0, NULL, 'q' },
199 { "make-pidfile", 0, NULL, 'm' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000200#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000201 { "oknodo", 0, NULL, 'o' },
202 { "verbose", 0, NULL, 'v' },
203 { "nicelevel", 1, NULL, 'N' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000204#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000205 { "startas", 1, NULL, 'a' },
206 { "name", 1, NULL, 'n' },
207 { "signal", 1, NULL, 's' },
208 { "user", 1, NULL, 'u' },
209 { "chuid", 1, NULL, 'c' },
210 { "exec", 1, NULL, 'x' },
211 { "pidfile", 1, NULL, 'p' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000212#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000213 { "retry", 1, NULL, 'R' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000214#endif
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000215 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000216};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000217#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000218
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000219#define SSD_CTX_STOP 0x1
220#define SSD_CTX_START 0x2
221#define SSD_OPT_BACKGROUND 0x4
222#define SSD_OPT_QUIET 0x8
223#define SSD_OPT_MAKEPID 0x10
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000224#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000225#define SSD_OPT_OKNODO 0x20
226#define SSD_OPT_VERBOSE 0x40
227#define SSD_OPT_NICELEVEL 0x80
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000228#endif
229
230int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000231{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000232 unsigned opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000233 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000234 char *startas = NULL;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000235#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
236// char *retry_arg = NULL;
237// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000238 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000239#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000240#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000241 applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000242#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000243
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000244 /* Check required one context option was given */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000245 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
246 opt = getopt32(argc, argv, "KSbqm"
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000247// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
248 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
Rob Landleyf0623a22006-07-17 00:35:07 +0000249 "a:n:s:u:c:x:p:"
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000250 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000251// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Rob Landleyf0623a22006-07-17 00:35:07 +0000252 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000253
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000254 quiet = (opt & SSD_OPT_QUIET)
255 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000256
Eric Andersen08804ce2003-07-30 08:29:56 +0000257 if (signame) {
Rob Landley84790632006-08-28 20:30:27 +0000258 signal_nr = get_signum(signame);
259 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000260 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000261
Eric Andersenaa820db2003-07-26 09:10:35 +0000262 if (!startas)
263 startas = execname;
264
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000265// USE_FEATURE_START_STOP_DAEMON_FANCY(
266// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000267// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000268// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000269 argc -= optind;
270 argv += optind;
271
Eric Andersen950d8b42001-10-31 09:55:39 +0000272 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000273 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000274
Glenn L McGrath8d441782004-01-22 09:04:58 +0000275 if (opt & SSD_CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000276 int i = do_stop();
277 return
278 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
279 ? 0 :) !!(i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000280 }
281
Eric Andersen625da9d2004-04-13 18:28:46 +0000282 do_procinit();
283
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000284 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000285 if (!quiet)
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000286 printf("%s already running\n%d\n", execname, found->pid);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000287 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
288 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000289 }
290 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000291 if (opt & SSD_OPT_BACKGROUND) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000292 xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000293 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000294 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000295 if (opt & SSD_OPT_MAKEPID) {
296 /* user wants _us_ to make the pidfile */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000297 FILE *pidf = xfopen(pidfile, "w");
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000298
Eric Andersen625da9d2004-04-13 18:28:46 +0000299 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000300 fprintf(pidf, "%d\n", pidt);
301 fclose(pidf);
302 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000303 if (chuid) {
304 if (sscanf(chuid, "%d", &user_id) != 1)
Rob Landleyf0623a22006-07-17 00:35:07 +0000305 user_id = bb_xgetpwnam(chuid);
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000306 xsetuid(user_id);
Rob Landleyf0623a22006-07-17 00:35:07 +0000307 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000308#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
309 if (opt & SSD_OPT_NICELEVEL) {
310 /* Set process priority */
311 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
312 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
313 bb_perror_msg_and_die("setpriority(%d)", prio);
314 }
315 }
316#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000317 execv(startas, argv);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000318 bb_perror_msg_and_die("cannot start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000319}