blob: dcff1dd86c2f89e961496eb5bb57c4fa47dbc895 [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{
Rob Landley8cedaba2006-09-04 18:59:39 +000042 char buf[32], *execbuf;
43 int equal;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000044
45 sprintf(buf, "/proc/%d/exe", pid);
Rob Landley8cedaba2006-09-04 18:59:39 +000046 execbuf = xstrdup(name);
47 readlink(buf, execbuf, strlen(name)+1);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000048
Rob Landley8cedaba2006-09-04 18:59:39 +000049 equal = ! strcmp(execbuf, name);
50 if (ENABLE_FEATURE_CLEAN_UP)
51 free(execbuf);
52 return equal;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000053}
54
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000055static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000056{
57 struct stat sb;
58 char buf[32];
59
60 sprintf(buf, "/proc/%d", pid);
61 if (stat(buf, &sb) != 0)
62 return 0;
63 return (sb.st_uid == uid);
64}
65
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000066static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000067{
68 char buf[32];
69 FILE *f;
70 int c;
71
72 sprintf(buf, "/proc/%d/stat", pid);
73 f = fopen(buf, "r");
74 if (!f)
75 return 0;
76 while ((c = getc(f)) != EOF && c != '(')
77 ;
78 if (c != '(') {
79 fclose(f);
80 return 0;
81 }
82 /* this hopefully handles command names containing ')' */
83 while ((c = getc(f)) != EOF && c == *name)
84 name++;
85 fclose(f);
86 return (c == ')' && *name == '\0');
87}
88
89
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000090static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000091{
92 if (execname && !pid_is_exec(pid, execname)) {
93 return;
94 }
95 if (userspec && !pid_is_user(pid, user_id)) {
96 return;
97 }
98 if (cmdname && !pid_is_cmd(pid, cmdname)) {
99 return;
100 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000101 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000102}
103
104
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000105static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000106{
107 FILE *f;
108 pid_t pid;
109
Eric Andersen625da9d2004-04-13 18:28:46 +0000110 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000111 if (f) {
112 if (fscanf(f, "%d", &pid) == 1)
113 check(pid);
114 fclose(f);
115 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000116 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000117
118}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000119
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000120static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000121{
122 DIR *procdir;
123 struct dirent *entry;
124 int foundany, pid;
125
Eric Andersen625da9d2004-04-13 18:28:46 +0000126 if (pidfile) {
127 do_pidfile();
128 return;
129 }
130
Rob Landleyd921b2e2006-08-03 15:41:12 +0000131 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000132
133 foundany = 0;
134 while ((entry = readdir(procdir)) != NULL) {
135 if (sscanf(entry->d_name, "%d", &pid) != 1)
136 continue;
137 foundany++;
138 check(pid);
139 }
140 closedir(procdir);
141 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000143}
144
145
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000146static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000147{
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000148 RESERVE_CONFIG_BUFFER(what, 1024);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000149 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000150 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000151
Eric Andersen625da9d2004-04-13 18:28:46 +0000152 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000153
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154 if (cmdname)
Bernhard Reutner-Fischer2f6a3172006-06-12 14:34:01 +0000155 strcpy(what, cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000156 else if (execname)
157 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000158 else if (pidfile)
159 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000160 else if (userspec)
161 sprintf(what, "process(es) owned by `%s'", userspec);
162 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000164
165 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000166 if (!quiet)
167 printf("no %s found; none killed.\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000168 if (ENABLE_FEATURE_CLEAN_UP)
169 RELEASE_CONFIG_BUFFER(what);
170 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000171 }
172 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000173 if (kill(p->pid, signal_nr) == 0) {
174 p->pid = -p->pid;
175 killed++;
176 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000177 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000178 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000179 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000180 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000181 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000182 for (p = found; p; p = p->next)
183 if(p->pid < 0)
184 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000185 printf(").\n");
186 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000187 if (ENABLE_FEATURE_CLEAN_UP)
188 RELEASE_CONFIG_BUFFER(what);
189 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000190}
191
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000192#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000193static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000194 { "stop", 0, NULL, 'K' },
195 { "start", 0, NULL, 'S' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000196 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000197 { "quiet", 0, NULL, 'q' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000198 { "make-pidfile", 0, NULL, 'm' },
199#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
200 { "oknodo", 0, NULL, 'o' },
201 { "verbose", 0, NULL, 'v' },
202#endif
203 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000204 { "name", 1, NULL, 'n' },
205 { "signal", 1, NULL, 's' },
206 { "user", 1, NULL, 'u' },
Rob Landleyf0623a22006-07-17 00:35:07 +0000207 { "chuid", 1, NULL, 'c' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000208 { "exec", 1, NULL, 'x' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000209 { "pidfile", 1, NULL, 'p' },
210#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
211 { "retry", 1, NULL, 'R' },
212#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000213 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000214};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000215#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000216
Eric Andersen625da9d2004-04-13 18:28:46 +0000217#define SSD_CTX_STOP 1
218#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000219#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000220#define SSD_OPT_QUIET 8
221#define SSD_OPT_MAKEPID 16
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000222#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
223#define SSD_OPT_OKNODO 32
224#define SSD_OPT_VERBOSE 64
Glenn L McGrath8d441782004-01-22 09:04:58 +0000225
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000226#endif
227
228int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000229{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000230 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000231 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000232 char *startas = NULL;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000233#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
234// char *retry_arg = NULL;
235// int retries = -1;
236#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000237#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000238 bb_applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000239#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000240
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000241 /* Check required one context option was given */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000242 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
243 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
244// USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
245 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
Rob Landleyf0623a22006-07-17 00:35:07 +0000246 "a:n:s:u:c:x:p:"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000247// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Rob Landleyf0623a22006-07-17 00:35:07 +0000248 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000249
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000250 quiet = (opt & SSD_OPT_QUIET)
251 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000252
Eric Andersen08804ce2003-07-30 08:29:56 +0000253 if (signame) {
Rob Landley84790632006-08-28 20:30:27 +0000254 signal_nr = get_signum(signame);
255 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000256 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000257
Eric Andersenaa820db2003-07-26 09:10:35 +0000258 if (!startas)
259 startas = execname;
260
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000261// USE_FEATURE_START_STOP_DAEMON_FANCY(
262// if (retry_arg)
263// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
264// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000265 argc -= optind;
266 argv += optind;
267
Eric Andersen950d8b42001-10-31 09:55:39 +0000268 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000269 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000270
Glenn L McGrath8d441782004-01-22 09:04:58 +0000271 if (opt & SSD_CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000272 int i = do_stop();
273 return
274 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
275 ? 0 :) !!(i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000276 }
277
Eric Andersen625da9d2004-04-13 18:28:46 +0000278 do_procinit();
279
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000280 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000281 if (!quiet)
282 printf("%s already running.\n%d\n", execname ,found->pid);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000283 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
284 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000285 }
286 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000287 if (opt & SSD_OPT_BACKGROUND) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000288 xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000289 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000290 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000291 if (opt & SSD_OPT_MAKEPID) {
292 /* user wants _us_ to make the pidfile */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000293 FILE *pidf = xfopen(pidfile, "w");
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000294
Eric Andersen625da9d2004-04-13 18:28:46 +0000295 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000296 fprintf(pidf, "%d\n", pidt);
297 fclose(pidf);
298 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000299 if(chuid) {
300 if(sscanf(chuid, "%d", &user_id) != 1)
301 user_id = bb_xgetpwnam(chuid);
302 setuid(user_id);
303 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000304 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000305 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000306}