blob: c31bba6dd9c99a2aae3924f7a7d13e5dbab2bd2b [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
24static int start = 0;
25static int stop = 0;
Eric Andersen53a22992002-01-26 09:04:45 +000026static int fork_before_exec = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000027static int signal_nr = 15;
28static int user_id = -1;
Eric Andersenaa820db2003-07-26 09:10:35 +000029static char *userspec = NULL;
30static char *cmdname = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000031static char *execname = NULL;
32static char *startas = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000033
Eric Andersen950d8b42001-10-31 09:55:39 +000034typedef struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000035 struct pid_list *next;
36 int pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000037} pid_list;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000038
Eric Andersen950d8b42001-10-31 09:55:39 +000039static pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000040
Eric Andersen950d8b42001-10-31 09:55:39 +000041static inline void
42push(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000043{
Eric Andersen950d8b42001-10-31 09:55:39 +000044 pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000045
46 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000047 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000048 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000049 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000050}
51
Eric Andersenc2af1ee2001-10-18 19:33:06 +000052static int
53pid_is_exec(int pid, const char *exec)
54{
55 char buf[PATH_MAX];
56 FILE *fp;
57
58 sprintf(buf, "/proc/%d/cmdline", pid);
59 fp = fopen(buf, "r");
60 if (fp && fgets (buf, sizeof (buf), fp) ) {
Eric Andersenff7661d2002-06-05 07:11:32 +000061 fclose(fp);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000062 if (strncmp (buf, exec, strlen(exec)) == 0)
63 return 1;
64 }
65 return 0;
66}
67
68
69static int
70pid_is_user(int pid, int uid)
71{
72 struct stat sb;
73 char buf[32];
74
75 sprintf(buf, "/proc/%d", pid);
76 if (stat(buf, &sb) != 0)
77 return 0;
78 return (sb.st_uid == uid);
79}
80
81
82static int
83pid_is_cmd(int pid, const char *name)
84{
85 char buf[32];
86 FILE *f;
87 int c;
88
89 sprintf(buf, "/proc/%d/stat", pid);
90 f = fopen(buf, "r");
91 if (!f)
92 return 0;
93 while ((c = getc(f)) != EOF && c != '(')
94 ;
95 if (c != '(') {
96 fclose(f);
97 return 0;
98 }
99 /* this hopefully handles command names containing ')' */
100 while ((c = getc(f)) != EOF && c == *name)
101 name++;
102 fclose(f);
103 return (c == ')' && *name == '\0');
104}
105
106
107static void
108check(int pid)
109{
110 if (execname && !pid_is_exec(pid, execname)) {
111 return;
112 }
113 if (userspec && !pid_is_user(pid, user_id)) {
114 return;
115 }
116 if (cmdname && !pid_is_cmd(pid, cmdname)) {
117 return;
118 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000119 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000120}
121
122
123
124static void
125do_procfs(void)
126{
127 DIR *procdir;
128 struct dirent *entry;
129 int foundany, pid;
130
131 procdir = opendir("/proc");
132 if (!procdir)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 bb_perror_msg_and_die ("opendir /proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000134
135 foundany = 0;
136 while ((entry = readdir(procdir)) != NULL) {
137 if (sscanf(entry->d_name, "%d", &pid) != 1)
138 continue;
139 foundany++;
140 check(pid);
141 }
142 closedir(procdir);
143 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000145}
146
147
148static void
149do_stop(void)
150{
151 char what[1024];
Eric Andersen950d8b42001-10-31 09:55:39 +0000152 pid_list *p;
153 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154
155 if (cmdname)
156 strcpy(what, cmdname);
157 else if (execname)
158 strcpy(what, execname);
159 else if (userspec)
160 sprintf(what, "process(es) owned by `%s'", userspec);
161 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163
164 if (!found) {
165 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000166 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000167 }
168 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000169 if (kill(p->pid, signal_nr) == 0) {
170 p->pid = -p->pid;
171 killed++;
172 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 bb_perror_msg("warning: failed to kill %d:", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000174 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000175 }
176 if (killed) {
177 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000178 for (p = found; p; p = p->next)
179 if(p->pid < 0)
180 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000181 printf(").\n");
182 }
183}
184
185
Eric Andersenaa820db2003-07-26 09:10:35 +0000186static const struct option ssd_long_options[] = {
187 { "stop", 0, NULL, 'K' },
188 { "start", 0, NULL, 'S' },
189 { "background", 0, NULL, 'b' },
190 { "startas", 1, NULL, 'a' },
191 { "name", 1, NULL, 'n' },
192 { "signal", 1, NULL, 's' },
193 { "user", 1, NULL, 'u' },
194 { "exec", 1, NULL, 'x' },
195 { 0, 0, 0, 0 }
196};
197
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198int
199start_stop_daemon_main(int argc, char **argv)
200{
Eric Andersenaa820db2003-07-26 09:10:35 +0000201 int flags;
Eric Andersen08804ce2003-07-30 08:29:56 +0000202 char *signame = NULL;
Eric Andersenaa820db2003-07-26 09:10:35 +0000203 bb_applet_long_options = ssd_long_options;
204
205 flags = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:",
206 &startas, &cmdname, &signame, &userspec, &execname);
207
208 /* Be sneaky and avoid branching */
209 stop = (flags & 1);
210 start = (flags & 2);
211 fork_before_exec = (flags & 4);
212
Eric Andersen08804ce2003-07-30 08:29:56 +0000213 if (signame) {
214 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
215 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000216
217 if (start == stop)
218 bb_error_msg_and_die ("need exactly one of -S or -K");
219
220 if (!execname && !userspec)
221 bb_error_msg_and_die ("need at least one of -x or -u");
222
223 if (!startas)
224 startas = execname;
225
226 if (start && !startas)
227 bb_error_msg_and_die ("-S needs -x or -a");
228
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000229 argc -= optind;
230 argv += optind;
231
Eric Andersen950d8b42001-10-31 09:55:39 +0000232 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
233 user_id = my_getpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000234
235 do_procfs();
236
237 if (stop) {
238 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000239 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000240 }
241
242 if (found) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000243 printf("%s already running.\n%d\n", execname ,found->pid);
244 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000245 }
246 *--argv = startas;
Eric Andersen53a22992002-01-26 09:04:45 +0000247 if (fork_before_exec) {
248 if (daemon(0, 0) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 bb_perror_msg_and_die ("unable to fork");
Eric Andersen53a22992002-01-26 09:04:45 +0000250 }
251 setsid();
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000252 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000253 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000254}
255