blob: b83a75994932d3df1d4aecd6ed67800b567fb5ad [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;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000025static int quiet = 0;
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
Eric Andersen950d8b42001-10-31 09:55:39 +000038static inline void
Eric Andersen63a1a7a2004-03-13 08:33:10 +000039push(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
Eric Andersenc2af1ee2001-10-18 19:33:06 +000049static int
Eric Andersen63a1a7a2004-03-13 08:33:10 +000050pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000051{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000052 char buf[32];
53 struct stat sb, exec_stat;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000054
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000055 if (name)
56 xstat(name, &exec_stat);
Eric Andersen63a1a7a2004-03-13 08:33:10 +000057
58 sprintf(buf, "/proc/%d/exe", pid);
59 if (stat(buf, &sb) != 0)
60 return 0;
61 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000062}
63
Eric Andersenc2af1ee2001-10-18 19:33:06 +000064static int
65pid_is_user(int pid, int uid)
66{
67 struct stat sb;
68 char buf[32];
69
70 sprintf(buf, "/proc/%d", pid);
71 if (stat(buf, &sb) != 0)
72 return 0;
73 return (sb.st_uid == uid);
74}
75
Eric Andersenc2af1ee2001-10-18 19:33:06 +000076static int
Eric Andersen63a1a7a2004-03-13 08:33:10 +000077pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000078{
79 char buf[32];
80 FILE *f;
81 int c;
82
83 sprintf(buf, "/proc/%d/stat", pid);
84 f = fopen(buf, "r");
85 if (!f)
86 return 0;
87 while ((c = getc(f)) != EOF && c != '(')
88 ;
89 if (c != '(') {
90 fclose(f);
91 return 0;
92 }
93 /* this hopefully handles command names containing ')' */
94 while ((c = getc(f)) != EOF && c == *name)
95 name++;
96 fclose(f);
97 return (c == ')' && *name == '\0');
98}
99
100
101static void
102check(int pid)
103{
104 if (execname && !pid_is_exec(pid, execname)) {
105 return;
106 }
107 if (userspec && !pid_is_user(pid, user_id)) {
108 return;
109 }
110 if (cmdname && !pid_is_cmd(pid, cmdname)) {
111 return;
112 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000113 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000114}
115
116
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000117static void
Eric Andersen625da9d2004-04-13 18:28:46 +0000118do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000119{
120 FILE *f;
121 pid_t pid;
122
Eric Andersen625da9d2004-04-13 18:28:46 +0000123 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000124 if (f) {
125 if (fscanf(f, "%d", &pid) == 1)
126 check(pid);
127 fclose(f);
128 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000129 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000130
131}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000132
133static void
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000134do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000135{
136 DIR *procdir;
137 struct dirent *entry;
138 int foundany, pid;
139
Eric Andersen625da9d2004-04-13 18:28:46 +0000140 if (pidfile) {
141 do_pidfile();
142 return;
143 }
144
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000145 procdir = bb_xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146
147 foundany = 0;
148 while ((entry = readdir(procdir)) != NULL) {
149 if (sscanf(entry->d_name, "%d", &pid) != 1)
150 continue;
151 foundany++;
152 check(pid);
153 }
154 closedir(procdir);
155 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000157}
158
159
160static void
161do_stop(void)
162{
163 char what[1024];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000164 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000165 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000166
Eric Andersen625da9d2004-04-13 18:28:46 +0000167 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000168
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000169 if (cmdname)
170 strcpy(what, cmdname);
171 else if (execname)
172 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000173 else if (pidfile)
174 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000175 else if (userspec)
176 sprintf(what, "process(es) owned by `%s'", userspec);
177 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000179
180 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000181 if (!quiet)
182 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000183 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000184 }
185 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000186 if (kill(p->pid, signal_nr) == 0) {
187 p->pid = -p->pid;
188 killed++;
189 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000190 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000191 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000192 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000193 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000194 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000195 for (p = found; p; p = p->next)
196 if(p->pid < 0)
197 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198 printf(").\n");
199 }
200}
201
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000202#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000203static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000204 { "stop", 0, NULL, 'K' },
205 { "start", 0, NULL, 'S' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000206 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000207 { "quiet", 0, NULL, 'q' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000208 { "make-pidfile", 0, NULL, 'm' },
209 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000210 { "name", 1, NULL, 'n' },
211 { "signal", 1, NULL, 's' },
212 { "user", 1, NULL, 'u' },
213 { "exec", 1, NULL, 'x' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000214 { "pidfile", 1, NULL, 'p' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +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
Eric Andersen625da9d2004-04-13 18:28:46 +0000219#define SSD_CTX_STOP 1
220#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000221#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000222#define SSD_OPT_QUIET 8
223#define SSD_OPT_MAKEPID 16
Glenn L McGrath8d441782004-01-22 09:04:58 +0000224
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000225int
226start_stop_daemon_main(int argc, char **argv)
227{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000228 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000229 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000230 char *startas = NULL;
231
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000232#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Eric Andersenaa820db2003-07-26 09:10:35 +0000233 bb_applet_long_options = ssd_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000234#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000235
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000236 /* Check required one context option was given */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000237 bb_opt_complementally = "K:S:?:K--S:S--K";
Eric Andersen625da9d2004-04-13 18:28:46 +0000238 opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000239 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000240
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000241
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000242 quiet = opt & SSD_OPT_QUIET;
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000243
Eric Andersen08804ce2003-07-30 08:29:56 +0000244 if (signame) {
245 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
246 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000247
Eric Andersen625da9d2004-04-13 18:28:46 +0000248 if (!execname && !pidfile && !userspec && !cmdname)
249 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
Eric Andersenaa820db2003-07-26 09:10:35 +0000250
251 if (!startas)
252 startas = execname;
253
Glenn L McGrath8d441782004-01-22 09:04:58 +0000254 if ((opt & SSD_CTX_START) && !startas)
Eric Andersenaa820db2003-07-26 09:10:35 +0000255 bb_error_msg_and_die ("-S needs -x or -a");
256
Eric Andersen625da9d2004-04-13 18:28:46 +0000257 if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
258 bb_error_msg_and_die ("-m needs -p");
259
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000260 argc -= optind;
261 argv += optind;
262
Eric Andersen950d8b42001-10-31 09:55:39 +0000263 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000264 user_id = bb_xgetpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000265
Glenn L McGrath8d441782004-01-22 09:04:58 +0000266 if (opt & SSD_CTX_STOP) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000267 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000268 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000269 }
270
Eric Andersen625da9d2004-04-13 18:28:46 +0000271 do_procinit();
272
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000273 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000274 if (!quiet)
275 printf("%s already running.\n%d\n", execname ,found->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000276 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000277 }
278 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000279 if (opt & SSD_OPT_BACKGROUND) {
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000280 bb_xdaemon(0, 0);
Eric Andersen625da9d2004-04-13 18:28:46 +0000281 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000282 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000283 if (opt & SSD_OPT_MAKEPID) {
284 /* user wants _us_ to make the pidfile */
285 FILE *pidf = fopen(pidfile, "w");
286 pid_t pidt = getpid();
287 if (pidf == NULL)
288 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
289 fprintf(pidf, "%d\n", pidt);
290 fclose(pidf);
291 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000292 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000294}