blob: 6f8fb2dc83c47afe25da059b052c6b00621305fd [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>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000013
Eric Andersenc2af1ee2001-10-18 19:33:06 +000014static int signal_nr = 15;
15static int user_id = -1;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000016static int quiet;
Rob Landleyd921b2e2006-08-03 15:41:12 +000017static char *userspec;
18static char *chuid;
19static char *cmdname;
20static char *execname;
21static char *pidfile;
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
Eric Andersen63a1a7a2004-03-13 08:33:10 +000028static struct pid_list *found = NULL;
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{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000042 char buf[32];
43 struct stat sb, exec_stat;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000044
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000045 if (name)
46 xstat(name, &exec_stat);
Eric Andersen63a1a7a2004-03-13 08:33:10 +000047
48 sprintf(buf, "/proc/%d/exe", pid);
49 if (stat(buf, &sb) != 0)
50 return 0;
51 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000052}
53
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000054static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000055{
56 struct stat sb;
57 char buf[32];
58
59 sprintf(buf, "/proc/%d", pid);
60 if (stat(buf, &sb) != 0)
61 return 0;
62 return (sb.st_uid == uid);
63}
64
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000065static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000066{
67 char buf[32];
68 FILE *f;
69 int c;
70
71 sprintf(buf, "/proc/%d/stat", pid);
72 f = fopen(buf, "r");
73 if (!f)
74 return 0;
75 while ((c = getc(f)) != EOF && c != '(')
76 ;
77 if (c != '(') {
78 fclose(f);
79 return 0;
80 }
81 /* this hopefully handles command names containing ')' */
82 while ((c = getc(f)) != EOF && c == *name)
83 name++;
84 fclose(f);
85 return (c == ')' && *name == '\0');
86}
87
88
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000089static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000090{
91 if (execname && !pid_is_exec(pid, execname)) {
92 return;
93 }
94 if (userspec && !pid_is_user(pid, user_id)) {
95 return;
96 }
97 if (cmdname && !pid_is_cmd(pid, cmdname)) {
98 return;
99 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000100 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000101}
102
103
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000104static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000105{
106 FILE *f;
107 pid_t pid;
108
Eric Andersen625da9d2004-04-13 18:28:46 +0000109 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000110 if (f) {
111 if (fscanf(f, "%d", &pid) == 1)
112 check(pid);
113 fclose(f);
114 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000115 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000116
117}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000118
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000119static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000120{
121 DIR *procdir;
122 struct dirent *entry;
123 int foundany, pid;
124
Eric Andersen625da9d2004-04-13 18:28:46 +0000125 if (pidfile) {
126 do_pidfile();
127 return;
128 }
129
Rob Landleyd921b2e2006-08-03 15:41:12 +0000130 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000131
132 foundany = 0;
133 while ((entry = readdir(procdir)) != NULL) {
134 if (sscanf(entry->d_name, "%d", &pid) != 1)
135 continue;
136 foundany++;
137 check(pid);
138 }
139 closedir(procdir);
140 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000142}
143
144
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000145static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146{
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000147 RESERVE_CONFIG_BUFFER(what, 1024);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000148 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000149 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000150
Eric Andersen625da9d2004-04-13 18:28:46 +0000151 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000152
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000153 if (cmdname)
Bernhard Reutner-Fischer2f6a3172006-06-12 14:34:01 +0000154 strcpy(what, cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155 else if (execname)
156 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000157 else if (pidfile)
158 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000159 else if (userspec)
160 sprintf(what, "process(es) owned by `%s'", userspec);
161 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163
164 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000165 if (!quiet)
166 printf("no %s found; none killed.\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000167 if (ENABLE_FEATURE_CLEAN_UP)
168 RELEASE_CONFIG_BUFFER(what);
169 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000170 }
171 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000172 if (kill(p->pid, signal_nr) == 0) {
173 p->pid = -p->pid;
174 killed++;
175 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000176 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000177 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000178 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000179 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000180 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000181 for (p = found; p; p = p->next)
182 if(p->pid < 0)
183 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000184 printf(").\n");
185 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000186 if (ENABLE_FEATURE_CLEAN_UP)
187 RELEASE_CONFIG_BUFFER(what);
188 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000189}
190
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000191#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000192static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000193 { "stop", 0, NULL, 'K' },
194 { "start", 0, NULL, 'S' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000195 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000196 { "quiet", 0, NULL, 'q' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000197 { "make-pidfile", 0, NULL, 'm' },
198#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
199 { "oknodo", 0, NULL, 'o' },
200 { "verbose", 0, NULL, 'v' },
201#endif
202 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000203 { "name", 1, NULL, 'n' },
204 { "signal", 1, NULL, 's' },
205 { "user", 1, NULL, 'u' },
Rob Landleyf0623a22006-07-17 00:35:07 +0000206 { "chuid", 1, NULL, 'c' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000207 { "exec", 1, NULL, 'x' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000208 { "pidfile", 1, NULL, 'p' },
209#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
210 { "retry", 1, NULL, 'R' },
211#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000212 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000213};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000214#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000215
Eric Andersen625da9d2004-04-13 18:28:46 +0000216#define SSD_CTX_STOP 1
217#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000218#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000219#define SSD_OPT_QUIET 8
220#define SSD_OPT_MAKEPID 16
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000221#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
222#define SSD_OPT_OKNODO 32
223#define SSD_OPT_VERBOSE 64
Glenn L McGrath8d441782004-01-22 09:04:58 +0000224
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000225#endif
226
227int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000228{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000229 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000230 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000231 char *startas = NULL;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000232#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
233// char *retry_arg = NULL;
234// int retries = -1;
235#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000236#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000237 bb_applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000238#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000239
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000240 /* Check required one context option was given */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000241 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
242 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
243// USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
244 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
Rob Landleyf0623a22006-07-17 00:35:07 +0000245 "a:n:s:u:c:x:p:"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000246// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Rob Landleyf0623a22006-07-17 00:35:07 +0000247 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000248
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000249 quiet = (opt & SSD_OPT_QUIET)
250 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000251
Eric Andersen08804ce2003-07-30 08:29:56 +0000252 if (signame) {
253 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
254 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000255
Eric Andersenaa820db2003-07-26 09:10:35 +0000256 if (!startas)
257 startas = execname;
258
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000259// USE_FEATURE_START_STOP_DAEMON_FANCY(
260// if (retry_arg)
261// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
262// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000263 argc -= optind;
264 argv += optind;
265
Eric Andersen950d8b42001-10-31 09:55:39 +0000266 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000267 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000268
Glenn L McGrath8d441782004-01-22 09:04:58 +0000269 if (opt & SSD_CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000270 int i = do_stop();
271 return
272 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
273 ? 0 :) !!(i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000274 }
275
Eric Andersen625da9d2004-04-13 18:28:46 +0000276 do_procinit();
277
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000278 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000279 if (!quiet)
280 printf("%s already running.\n%d\n", execname ,found->pid);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000281 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
282 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000283 }
284 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000285 if (opt & SSD_OPT_BACKGROUND) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000286 xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000287 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000288 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000289 if (opt & SSD_OPT_MAKEPID) {
290 /* user wants _us_ to make the pidfile */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000291 FILE *pidf = xfopen(pidfile, "w");
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000292
Eric Andersen625da9d2004-04-13 18:28:46 +0000293 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000294 fprintf(pidf, "%d\n", pidt);
295 fclose(pidf);
296 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000297 if(chuid) {
298 if(sscanf(chuid, "%d", &user_id) != 1)
299 user_id = bb_xgetpwnam(chuid);
300 setuid(user_id);
301 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000302 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000303 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000304}