blob: 6bc259fb36a4d884b87748c159d1405533ec89c9 [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>
Eric Andersenaa820db2003-07-26 09:10:35 +000019#include <getopt.h>
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
Eric Andersen63a1a7a2004-03-13 08:33:10 +000056 if (name && stat(name, &exec_stat))
57 bb_perror_msg_and_die("stat %s", name);
58
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
119do_pidfile(const char *name)
120{
121 FILE *f;
122 pid_t pid;
123
124 f = fopen(name, "r");
125 if (f) {
126 if (fscanf(f, "%d", &pid) == 1)
127 check(pid);
128 fclose(f);
129 } else if (errno != ENOENT)
130 bb_perror_msg_and_die("open pidfile %s", name);
131
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
141 procdir = opendir("/proc");
142 if (!procdir)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_perror_msg_and_die ("opendir /proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000144
145 foundany = 0;
146 while ((entry = readdir(procdir)) != NULL) {
147 if (sscanf(entry->d_name, "%d", &pid) != 1)
148 continue;
149 foundany++;
150 check(pid);
151 }
152 closedir(procdir);
153 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155}
156
157
158static void
159do_stop(void)
160{
161 char what[1024];
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000162 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000163 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000164
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000165 if (pidfile)
166 do_pidfile(pidfile);
167 else
168 do_procinit();
169
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000170 if (cmdname)
171 strcpy(what, cmdname);
172 else if (execname)
173 strcpy(what, execname);
174 else if (userspec)
175 sprintf(what, "process(es) owned by `%s'", userspec);
176 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000178
179 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000180 if (!quiet)
181 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000182 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000183 }
184 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000185 if (kill(p->pid, signal_nr) == 0) {
186 p->pid = -p->pid;
187 killed++;
188 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 bb_perror_msg("warning: failed to kill %d:", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000190 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000191 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000192 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000193 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000194 for (p = found; p; p = p->next)
195 if(p->pid < 0)
196 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000197 printf(").\n");
198 }
199}
200
201
Eric Andersenaa820db2003-07-26 09:10:35 +0000202static const struct option ssd_long_options[] = {
203 { "stop", 0, NULL, 'K' },
204 { "start", 0, NULL, 'S' },
205 { "background", 0, NULL, 'b' },
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000206 { "quiet", 0, NULL, 'q' },
Eric Andersenaa820db2003-07-26 09:10:35 +0000207 { "startas", 1, NULL, 'a' },
208 { "name", 1, NULL, 'n' },
209 { "signal", 1, NULL, 's' },
210 { "user", 1, NULL, 'u' },
211 { "exec", 1, NULL, 'x' },
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000212 { "pidfile", 1, NULL, 'p' },
Eric Andersenaa820db2003-07-26 09:10:35 +0000213 { 0, 0, 0, 0 }
214};
215
Glenn L McGrath8d441782004-01-22 09:04:58 +0000216#define SSD_CTX_STOP 1
217#define SSD_CTX_START 2
218#define SSD_OPT_BACKGROUND 4
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000219#define SSD_OPT_QUIET 8
Glenn L McGrath8d441782004-01-22 09:04:58 +0000220
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000221int
222start_stop_daemon_main(int argc, char **argv)
223{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000224 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000225 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000226 char *startas = NULL;
227
Eric Andersenaa820db2003-07-26 09:10:35 +0000228 bb_applet_long_options = ssd_long_options;
229
Glenn L McGrath2d016a32004-01-23 21:43:49 +0000230 bb_opt_complementaly = "K~S:S~K";
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000231 opt = bb_getopt_ulflags(argc, argv, "KSbqa:n:s:u:x:p:",
232 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
Eric Andersenaa820db2003-07-26 09:10:35 +0000233
Glenn L McGrath8d441782004-01-22 09:04:58 +0000234 /* Check one and only one context option was given */
Glenn L McGrath2d016a32004-01-23 21:43:49 +0000235 if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) {
Glenn L McGrath8d441782004-01-22 09:04:58 +0000236 bb_show_usage();
237 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000238
Eric Andersen08804ce2003-07-30 08:29:56 +0000239 if (signame) {
240 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
241 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000242
Eric Andersenaa820db2003-07-26 09:10:35 +0000243 if (!execname && !userspec)
244 bb_error_msg_and_die ("need at least one of -x or -u");
245
246 if (!startas)
247 startas = execname;
248
Glenn L McGrath8d441782004-01-22 09:04:58 +0000249 if ((opt & SSD_CTX_START) && !startas)
Eric Andersenaa820db2003-07-26 09:10:35 +0000250 bb_error_msg_and_die ("-S needs -x or -a");
251
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000252 argc -= optind;
253 argv += optind;
254
Eric Andersen950d8b42001-10-31 09:55:39 +0000255 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
256 user_id = my_getpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000257
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000258 do_procinit();
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000259
Glenn L McGrath8d441782004-01-22 09:04:58 +0000260 if (opt & SSD_CTX_STOP) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000261 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000262 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000263 }
264
265 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000266 if (!quiet)
267 printf("%s already running.\n%d\n", execname ,found->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000268 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000269 }
270 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000271 if (opt & SSD_OPT_BACKGROUND) {
Eric Andersen53a22992002-01-26 09:04:45 +0000272 if (daemon(0, 0) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000273 bb_perror_msg_and_die ("unable to fork");
Eric Andersen53a22992002-01-26 09:04:45 +0000274 }
275 setsid();
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000276 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000277 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000278}