blob: 733fb4112588bc9cf4db8bc0c383395aa4a54482 [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>,
6 * public domain.
7 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
8 */
9
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <stdarg.h>
15#include <signal.h>
16#include <errno.h>
17#include <sys/stat.h>
18#include <dirent.h>
19#include <unistd.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000020#include <getopt.h> /* struct option */
Eric Andersen887ca792002-07-03 23:19:26 +000021#include "pwd_.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000022
Eric Andersenc2af1ee2001-10-18 19:33:06 +000023static int signal_nr = 15;
24static int user_id = -1;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000025static int quiet;
Eric Andersenaa820db2003-07-26 09:10:35 +000026static char *userspec = NULL;
Rob Landleyf0623a22006-07-17 00:35:07 +000027static char *chuid = NULL;
Eric Andersenaa820db2003-07-26 09:10:35 +000028static char *cmdname = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000029static char *execname = NULL;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000030static char *pidfile = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000031
Eric Andersen63a1a7a2004-03-13 08:33:10 +000032struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000033 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000034 pid_t pid;
35};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000036
Eric Andersen63a1a7a2004-03-13 08:33:10 +000037static struct pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000038
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000039static inline void push(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000040{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000041 struct pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000042
43 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000044 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000045 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000046 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000047}
48
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000049static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000050{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000051 char buf[32];
52 struct stat sb, exec_stat;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000053
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000054 if (name)
55 xstat(name, &exec_stat);
Eric Andersen63a1a7a2004-03-13 08:33:10 +000056
57 sprintf(buf, "/proc/%d/exe", pid);
58 if (stat(buf, &sb) != 0)
59 return 0;
60 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000061}
62
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000063static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000064{
65 struct stat sb;
66 char buf[32];
67
68 sprintf(buf, "/proc/%d", pid);
69 if (stat(buf, &sb) != 0)
70 return 0;
71 return (sb.st_uid == uid);
72}
73
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000074static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000075{
76 char buf[32];
77 FILE *f;
78 int c;
79
80 sprintf(buf, "/proc/%d/stat", pid);
81 f = fopen(buf, "r");
82 if (!f)
83 return 0;
84 while ((c = getc(f)) != EOF && c != '(')
85 ;
86 if (c != '(') {
87 fclose(f);
88 return 0;
89 }
90 /* this hopefully handles command names containing ')' */
91 while ((c = getc(f)) != EOF && c == *name)
92 name++;
93 fclose(f);
94 return (c == ')' && *name == '\0');
95}
96
97
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000098static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000099{
100 if (execname && !pid_is_exec(pid, execname)) {
101 return;
102 }
103 if (userspec && !pid_is_user(pid, user_id)) {
104 return;
105 }
106 if (cmdname && !pid_is_cmd(pid, cmdname)) {
107 return;
108 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000109 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000110}
111
112
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000113static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000114{
115 FILE *f;
116 pid_t pid;
117
Eric Andersen625da9d2004-04-13 18:28:46 +0000118 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000119 if (f) {
120 if (fscanf(f, "%d", &pid) == 1)
121 check(pid);
122 fclose(f);
123 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000124 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000125
126}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000127
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000128static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000129{
130 DIR *procdir;
131 struct dirent *entry;
132 int foundany, pid;
133
Eric Andersen625da9d2004-04-13 18:28:46 +0000134 if (pidfile) {
135 do_pidfile();
136 return;
137 }
138
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000139 procdir = bb_xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000140
141 foundany = 0;
142 while ((entry = readdir(procdir)) != NULL) {
143 if (sscanf(entry->d_name, "%d", &pid) != 1)
144 continue;
145 foundany++;
146 check(pid);
147 }
148 closedir(procdir);
149 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000151}
152
153
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000154static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155{
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000156 RESERVE_CONFIG_BUFFER(what, 1024);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000157 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000158 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000159
Eric Andersen625da9d2004-04-13 18:28:46 +0000160 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000161
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000162 if (cmdname)
Bernhard Reutner-Fischer2f6a3172006-06-12 14:34:01 +0000163 strcpy(what, cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000164 else if (execname)
165 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000166 else if (pidfile)
167 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000168 else if (userspec)
169 sprintf(what, "process(es) owned by `%s'", userspec);
170 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000172
173 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000174 if (!quiet)
175 printf("no %s found; none killed.\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000176 if (ENABLE_FEATURE_CLEAN_UP)
177 RELEASE_CONFIG_BUFFER(what);
178 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000179 }
180 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000181 if (kill(p->pid, signal_nr) == 0) {
182 p->pid = -p->pid;
183 killed++;
184 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000185 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000186 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000187 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000188 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000189 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000190 for (p = found; p; p = p->next)
191 if(p->pid < 0)
192 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000193 printf(").\n");
194 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000195 if (ENABLE_FEATURE_CLEAN_UP)
196 RELEASE_CONFIG_BUFFER(what);
197 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198}
199
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000200#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000201static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000202 { "stop", 0, NULL, 'K' },
203 { "start", 0, NULL, 'S' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000204 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000205 { "quiet", 0, NULL, 'q' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000206 { "make-pidfile", 0, NULL, 'm' },
207#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
208 { "oknodo", 0, NULL, 'o' },
209 { "verbose", 0, NULL, 'v' },
210#endif
211 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000212 { "name", 1, NULL, 'n' },
213 { "signal", 1, NULL, 's' },
214 { "user", 1, NULL, 'u' },
Rob Landleyf0623a22006-07-17 00:35:07 +0000215 { "chuid", 1, NULL, 'c' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000216 { "exec", 1, NULL, 'x' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000217 { "pidfile", 1, NULL, 'p' },
218#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
219 { "retry", 1, NULL, 'R' },
220#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000221 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000222};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000223#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000224
Eric Andersen625da9d2004-04-13 18:28:46 +0000225#define SSD_CTX_STOP 1
226#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000227#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000228#define SSD_OPT_QUIET 8
229#define SSD_OPT_MAKEPID 16
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000230#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
231#define SSD_OPT_OKNODO 32
232#define SSD_OPT_VERBOSE 64
Glenn L McGrath8d441782004-01-22 09:04:58 +0000233
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000234#endif
235
236int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000237{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000238 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000239 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000240 char *startas = NULL;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000241#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
242// char *retry_arg = NULL;
243// int retries = -1;
244#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000245#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000246 bb_applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000247#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000248
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000249 /* Check required one context option was given */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000250 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
251 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
252// USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
253 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
Rob Landleyf0623a22006-07-17 00:35:07 +0000254 "a:n:s:u:c:x:p:"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000255// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Rob Landleyf0623a22006-07-17 00:35:07 +0000256 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000257
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000258 quiet = (opt & SSD_OPT_QUIET)
259 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000260
Eric Andersen08804ce2003-07-30 08:29:56 +0000261 if (signame) {
262 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
263 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000264
Eric Andersenaa820db2003-07-26 09:10:35 +0000265 if (!startas)
266 startas = execname;
267
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000268// USE_FEATURE_START_STOP_DAEMON_FANCY(
269// if (retry_arg)
270// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
271// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000272 argc -= optind;
273 argv += optind;
274
Eric Andersen950d8b42001-10-31 09:55:39 +0000275 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000276 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000277
Glenn L McGrath8d441782004-01-22 09:04:58 +0000278 if (opt & SSD_CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000279 int i = do_stop();
280 return
281 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
282 ? 0 :) !!(i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000283 }
284
Eric Andersen625da9d2004-04-13 18:28:46 +0000285 do_procinit();
286
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000287 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000288 if (!quiet)
289 printf("%s already running.\n%d\n", execname ,found->pid);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000290 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
291 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000292 }
293 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000294 if (opt & SSD_OPT_BACKGROUND) {
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000295 bb_xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000296 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000297 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000298 if (opt & SSD_OPT_MAKEPID) {
299 /* user wants _us_ to make the pidfile */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000300 FILE *pidf = bb_xfopen(pidfile, "w");
301
Eric Andersen625da9d2004-04-13 18:28:46 +0000302 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000303 fprintf(pidf, "%d\n", pidt);
304 fclose(pidf);
305 }
Rob Landleyf0623a22006-07-17 00:35:07 +0000306 if(chuid) {
307 if(sscanf(chuid, "%d", &user_id) != 1)
308 user_id = bb_xgetpwnam(chuid);
309 setuid(user_id);
310 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000311 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000312 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000313}