blob: 1dd2231dc93b354ad422d30bf417987510b31cf9 [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
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <stdarg.h>
14#include <signal.h>
15#include <errno.h>
16#include <sys/stat.h>
17#include <dirent.h>
18#include <unistd.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000019#include <getopt.h> /* struct option */
Eric Andersenc2af1ee2001-10-18 19:33:06 +000020
21#include "busybox.h"
Eric Andersen887ca792002-07-03 23:19:26 +000022#include "pwd_.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000023
Eric Andersenc2af1ee2001-10-18 19:33:06 +000024static int signal_nr = 15;
25static int user_id = -1;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000026static int quiet = 0;
Eric Andersenaa820db2003-07-26 09:10:35 +000027static char *userspec = NULL;
28static 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
Eric Andersen950d8b42001-10-31 09:55:39 +000039static inline void
Eric Andersen63a1a7a2004-03-13 08:33:10 +000040push(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000041{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000042 struct pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000043
44 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000045 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000046 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000047 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000048}
49
Eric Andersenc2af1ee2001-10-18 19:33:06 +000050static int
Eric Andersen63a1a7a2004-03-13 08:33:10 +000051pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000052{
Eric Andersen63a1a7a2004-03-13 08:33:10 +000053 char buf[32];
54 struct stat sb, exec_stat;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000055
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000056 if (name)
57 xstat(name, &exec_stat);
Eric Andersen63a1a7a2004-03-13 08:33:10 +000058
59 sprintf(buf, "/proc/%d/exe", pid);
60 if (stat(buf, &sb) != 0)
61 return 0;
62 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000063}
64
Eric Andersenc2af1ee2001-10-18 19:33:06 +000065static int
66pid_is_user(int pid, int uid)
67{
68 struct stat sb;
69 char buf[32];
70
71 sprintf(buf, "/proc/%d", pid);
72 if (stat(buf, &sb) != 0)
73 return 0;
74 return (sb.st_uid == uid);
75}
76
Eric Andersenc2af1ee2001-10-18 19:33:06 +000077static int
Eric Andersen63a1a7a2004-03-13 08:33:10 +000078pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000079{
80 char buf[32];
81 FILE *f;
82 int c;
83
84 sprintf(buf, "/proc/%d/stat", pid);
85 f = fopen(buf, "r");
86 if (!f)
87 return 0;
88 while ((c = getc(f)) != EOF && c != '(')
89 ;
90 if (c != '(') {
91 fclose(f);
92 return 0;
93 }
94 /* this hopefully handles command names containing ')' */
95 while ((c = getc(f)) != EOF && c == *name)
96 name++;
97 fclose(f);
98 return (c == ')' && *name == '\0');
99}
100
101
102static void
103check(int pid)
104{
105 if (execname && !pid_is_exec(pid, execname)) {
106 return;
107 }
108 if (userspec && !pid_is_user(pid, user_id)) {
109 return;
110 }
111 if (cmdname && !pid_is_cmd(pid, cmdname)) {
112 return;
113 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000114 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000115}
116
117
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000118static void
Eric Andersen625da9d2004-04-13 18:28:46 +0000119do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000120{
121 FILE *f;
122 pid_t pid;
123
Eric Andersen625da9d2004-04-13 18:28:46 +0000124 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000125 if (f) {
126 if (fscanf(f, "%d", &pid) == 1)
127 check(pid);
128 fclose(f);
129 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000130 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000131
132}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000133
134static void
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000135do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000136{
137 DIR *procdir;
138 struct dirent *entry;
139 int foundany, pid;
140
Eric Andersen625da9d2004-04-13 18:28:46 +0000141 if (pidfile) {
142 do_pidfile();
143 return;
144 }
145
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146 procdir = opendir("/proc");
147 if (!procdir)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 bb_perror_msg_and_die ("opendir /proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000149
150 foundany = 0;
151 while ((entry = readdir(procdir)) != NULL) {
152 if (sscanf(entry->d_name, "%d", &pid) != 1)
153 continue;
154 foundany++;
155 check(pid);
156 }
157 closedir(procdir);
158 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000160}
161
162
163static void
164do_stop(void)
165{
166 char what[1024];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000167 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000168 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000169
Eric Andersen625da9d2004-04-13 18:28:46 +0000170 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000171
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000172 if (cmdname)
173 strcpy(what, cmdname);
174 else if (execname)
175 strcpy(what, execname);
Eric Andersen625da9d2004-04-13 18:28:46 +0000176 else if (pidfile)
177 sprintf(what, "process in pidfile `%.200s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000178 else if (userspec)
179 sprintf(what, "process(es) owned by `%s'", userspec);
180 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000182
183 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000184 if (!quiet)
185 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000186 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000187 }
188 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000189 if (kill(p->pid, signal_nr) == 0) {
190 p->pid = -p->pid;
191 killed++;
192 } else {
Eric Andersen625da9d2004-04-13 18:28:46 +0000193 bb_perror_msg("warning: failed to kill %d", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000194 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000195 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000196 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000197 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000198 for (p = found; p; p = p->next)
199 if(p->pid < 0)
200 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000201 printf(").\n");
202 }
203}
204
205
Eric Andersenaa820db2003-07-26 09:10:35 +0000206static const struct option ssd_long_options[] = {
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000207 { "stop", 0, NULL, 'K' },
208 { "start", 0, NULL, 'S' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000209 { "background", 0, NULL, 'b' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000210 { "quiet", 0, NULL, 'q' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000211 { "make-pidfile", 0, NULL, 'm' },
212 { "startas", 1, NULL, 'a' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000213 { "name", 1, NULL, 'n' },
214 { "signal", 1, NULL, 's' },
215 { "user", 1, NULL, 'u' },
216 { "exec", 1, NULL, 'x' },
Eric Andersen625da9d2004-04-13 18:28:46 +0000217 { "pidfile", 1, NULL, 'p' },
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000218 { 0, 0, 0, 0 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000219};
220
Eric Andersen625da9d2004-04-13 18:28:46 +0000221#define SSD_CTX_STOP 1
222#define SSD_CTX_START 2
Glenn L McGrath8d441782004-01-22 09:04:58 +0000223#define SSD_OPT_BACKGROUND 4
Eric Andersen625da9d2004-04-13 18:28:46 +0000224#define SSD_OPT_QUIET 8
225#define SSD_OPT_MAKEPID 16
Glenn L McGrath8d441782004-01-22 09:04:58 +0000226
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000227int
228start_stop_daemon_main(int argc, char **argv)
229{
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;
233
Eric Andersenaa820db2003-07-26 09:10:35 +0000234 bb_applet_long_options = ssd_long_options;
235
"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) {
Eric Andersen53a22992002-01-26 09:04:45 +0000280 if (daemon(0, 0) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000281 bb_perror_msg_and_die ("unable to fork");
Eric Andersen625da9d2004-04-13 18:28:46 +0000282 setsid();
Eric Andersen53a22992002-01-26 09:04:45 +0000283 }
Eric Andersen625da9d2004-04-13 18:28:46 +0000284 if (opt & SSD_OPT_MAKEPID) {
285 /* user wants _us_ to make the pidfile */
286 FILE *pidf = fopen(pidfile, "w");
287 pid_t pidt = getpid();
288 if (pidf == NULL)
289 bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
290 fprintf(pidf, "%d\n", pidt);
291 fclose(pidf);
292 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000293 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000295}