blob: 3700506bfeee40d42fe0a15ac810d821d7637d1f [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;
27static char *cmdname = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000028static char *execname = NULL;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000029static char *pidfile = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000030
Eric Andersen63a1a7a2004-03-13 08:33:10 +000031struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000032 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000033 pid_t pid;
34};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000035
Eric Andersen63a1a7a2004-03-13 08:33:10 +000036static struct pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000037
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000038static inline void push(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000039{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000040 struct pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000041
42 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000043 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000044 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000045 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000046}
47
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000048static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000049{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000050 char buf[32];
51 struct stat sb, exec_stat;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000052
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000053 if (name)
54 xstat(name, &exec_stat);
Eric Andersen63a1a7a2004-03-13 08:33:10 +000055
56 sprintf(buf, "/proc/%d/exe", pid);
57 if (stat(buf, &sb) != 0)
58 return 0;
59 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000060}
61
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000062static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000063{
64 struct stat sb;
65 char buf[32];
66
67 sprintf(buf, "/proc/%d", pid);
68 if (stat(buf, &sb) != 0)
69 return 0;
70 return (sb.st_uid == uid);
71}
72
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000073static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000074{
75 char buf[32];
76 FILE *f;
77 int c;
78
79 sprintf(buf, "/proc/%d/stat", pid);
80 f = fopen(buf, "r");
81 if (!f)
82 return 0;
83 while ((c = getc(f)) != EOF && c != '(')
84 ;
85 if (c != '(') {
86 fclose(f);
87 return 0;
88 }
89 /* this hopefully handles command names containing ')' */
90 while ((c = getc(f)) != EOF && c == *name)
91 name++;
92 fclose(f);
93 return (c == ')' && *name == '\0');
94}
95
96
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000097static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000098{
99 if (execname && !pid_is_exec(pid, execname)) {
100 return;
101 }
102 if (userspec && !pid_is_user(pid, user_id)) {
103 return;
104 }
105 if (cmdname && !pid_is_cmd(pid, cmdname)) {
106 return;
107 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000108 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000109}
110
111
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000112static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000113{
114 FILE *f;
115 pid_t pid;
116
Eric Andersen625da9d2004-04-13 18:28:46 +0000117 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000118 if (f) {
119 if (fscanf(f, "%d", &pid) == 1)
120 check(pid);
121 fclose(f);
122 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000123 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000124
125}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000126
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000127static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000128{
129 DIR *procdir;
130 struct dirent *entry;
131 int foundany, pid;
132
Eric Andersen625da9d2004-04-13 18:28:46 +0000133 if (pidfile) {
134 do_pidfile();
135 return;
136 }
137
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000138 procdir = bb_xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000139
140 foundany = 0;
141 while ((entry = readdir(procdir)) != NULL) {
142 if (sscanf(entry->d_name, "%d", &pid) != 1)
143 continue;
144 foundany++;
145 check(pid);
146 }
147 closedir(procdir);
148 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000150}
151
152
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000153static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154{
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000155 RESERVE_CONFIG_BUFFER(what, 1024);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000156 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000157 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000158
Eric Andersen625da9d2004-04-13 18:28:46 +0000159 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000160
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000161 if (cmdname)
Bernhard Reutner-Fischer2f6a3172006-06-12 14:34:01 +0000162 strcpy(what, cmdname);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163 else if (execname)
164 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000165 else if (pidfile)
166 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000167 else if (userspec)
168 sprintf(what, "process(es) owned by `%s'", userspec);
169 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000171
172 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000173 if (!quiet)
174 printf("no %s found; none killed.\n", what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000175 if (ENABLE_FEATURE_CLEAN_UP)
176 RELEASE_CONFIG_BUFFER(what);
177 return -1;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000178 }
179 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000180 if (kill(p->pid, signal_nr) == 0) {
181 p->pid = -p->pid;
182 killed++;
183 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000184 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000185 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000186 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000187 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000188 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000189 for (p = found; p; p = p->next)
190 if(p->pid < 0)
191 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000192 printf(").\n");
193 }
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000194 if (ENABLE_FEATURE_CLEAN_UP)
195 RELEASE_CONFIG_BUFFER(what);
196 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000197}
198
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000199#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000200static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000201 { "stop", 0, NULL, 'K' },
202 { "start", 0, NULL, 'S' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000203 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000204 { "quiet", 0, NULL, 'q' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000205 { "make-pidfile", 0, NULL, 'm' },
206#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
207 { "oknodo", 0, NULL, 'o' },
208 { "verbose", 0, NULL, 'v' },
209#endif
210 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000211 { "name", 1, NULL, 'n' },
212 { "signal", 1, NULL, 's' },
213 { "user", 1, NULL, 'u' },
214 { "exec", 1, NULL, 'x' },
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000215 { "pidfile", 1, NULL, 'p' },
216#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
217 { "retry", 1, NULL, 'R' },
218#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000219 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000220};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000221#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000222
Eric Andersen625da9d2004-04-13 18:28:46 +0000223#define SSD_CTX_STOP 1
224#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000225#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000226#define SSD_OPT_QUIET 8
227#define SSD_OPT_MAKEPID 16
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000228#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
229#define SSD_OPT_OKNODO 32
230#define SSD_OPT_VERBOSE 64
Glenn L McGrath8d441782004-01-22 09:04:58 +0000231
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000232#endif
233
234int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000235{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000236 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000237 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000238 char *startas = NULL;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000239#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
240// char *retry_arg = NULL;
241// int retries = -1;
242#endif
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000243#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000244 bb_applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000245#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000246
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000247 /* Check required one context option was given */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000248 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
249 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
250// USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
251 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
252 "a:n:s:u:x:p:"
253// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
254 ,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000255
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000256 quiet = (opt & SSD_OPT_QUIET)
257 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000258
Eric Andersen08804ce2003-07-30 08:29:56 +0000259 if (signame) {
260 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
261 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000262
Eric Andersenaa820db2003-07-26 09:10:35 +0000263 if (!startas)
264 startas = execname;
265
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000266// USE_FEATURE_START_STOP_DAEMON_FANCY(
267// if (retry_arg)
268// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
269// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000270 argc -= optind;
271 argv += optind;
272
Eric Andersen950d8b42001-10-31 09:55:39 +0000273 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000274 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000275
Glenn L McGrath8d441782004-01-22 09:04:58 +0000276 if (opt & SSD_CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000277 int i = do_stop();
278 return
279 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
280 ? 0 :) !!(i<=0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000281 }
282
Eric Andersen625da9d2004-04-13 18:28:46 +0000283 do_procinit();
284
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000285 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000286 if (!quiet)
287 printf("%s already running.\n%d\n", execname ,found->pid);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000288 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
289 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000290 }
291 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000292 if (opt & SSD_OPT_BACKGROUND) {
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000293 bb_xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000294 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000295 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000296 if (opt & SSD_OPT_MAKEPID) {
297 /* user wants _us_ to make the pidfile */
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000298 FILE *pidf = bb_xfopen(pidfile, "w");
299
Eric Andersen625da9d2004-04-13 18:28:46 +0000300 pid_t pidt = getpid();
Eric Andersen625da9d2004-04-13 18:28:46 +0000301 fprintf(pidf, "%d\n", pidt);
302 fclose(pidf);
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}