blob: 5765261834369ad38311a19833cb5c7c993b75fe [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 Andersenc2af1ee2001-10-18 19:33:06 +000019
20#include "busybox.h"
Eric Andersen887ca792002-07-03 23:19:26 +000021#include "pwd_.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000022
23static int start = 0;
24static int stop = 0;
Eric Andersen53a22992002-01-26 09:04:45 +000025static int fork_before_exec = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000026static int signal_nr = 15;
27static int user_id = -1;
28static const char *userspec = NULL;
29static const char *cmdname = NULL;
30static char *execname = NULL;
31static char *startas = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000032
Eric Andersen950d8b42001-10-31 09:55:39 +000033typedef struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000034 struct pid_list *next;
35 int pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000036} pid_list;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000037
Eric Andersen950d8b42001-10-31 09:55:39 +000038static pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000039
Eric Andersen950d8b42001-10-31 09:55:39 +000040static inline void
41push(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000042{
Eric Andersen950d8b42001-10-31 09:55:39 +000043 pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000044
45 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000046 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000047 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000048 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000049}
50
51
52static void
53parse_options(int argc, char * const *argv)
54{
55
56 int c;
57
58 for (;;) {
Eric Andersen53a22992002-01-26 09:04:45 +000059 c = getopt (argc, argv, "a:n:s:u:x:KSb");
Eric Andersenc2af1ee2001-10-18 19:33:06 +000060 if (c == EOF)
61 break;
62 switch (c) {
63 case 'K':
64 stop = 1;
65 break;
66 case 'S':
67 start = 1;
68 break;
69 case 'a':
70 startas = optarg;
71 break;
72 case 'n':
73 cmdname = optarg;
74 break;
75 case 's':
76 if (sscanf(optarg, "%d", &signal_nr) != 1)
77 error_msg_and_die ("-s takes a numeric argument");
78 break;
79 case 'u':
80 userspec = optarg;
81 break;
82 case 'x':
83 execname = optarg;
84 break;
Eric Andersen53a22992002-01-26 09:04:45 +000085 case 'b':
86 fork_before_exec = 1;
87 break;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000088 default:
89 show_usage();
Eric Andersenc2af1ee2001-10-18 19:33:06 +000090 }
91 }
92
93 if (start == stop)
94 error_msg_and_die ("need one of -S or -K");
95
96 if (!execname && !userspec)
97 error_msg_and_die ("need at least one of -x or -u");
98
99 if (!startas)
100 startas = execname;
101
102 if (start && !startas)
103 error_msg_and_die ("-S needs -x or -a");
104}
105
106
107static int
108pid_is_exec(int pid, const char *exec)
109{
110 char buf[PATH_MAX];
111 FILE *fp;
112
113 sprintf(buf, "/proc/%d/cmdline", pid);
114 fp = fopen(buf, "r");
115 if (fp && fgets (buf, sizeof (buf), fp) ) {
Eric Andersenff7661d2002-06-05 07:11:32 +0000116 fclose(fp);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000117 if (strncmp (buf, exec, strlen(exec)) == 0)
118 return 1;
119 }
120 return 0;
121}
122
123
124static int
125pid_is_user(int pid, int uid)
126{
127 struct stat sb;
128 char buf[32];
129
130 sprintf(buf, "/proc/%d", pid);
131 if (stat(buf, &sb) != 0)
132 return 0;
133 return (sb.st_uid == uid);
134}
135
136
137static int
138pid_is_cmd(int pid, const char *name)
139{
140 char buf[32];
141 FILE *f;
142 int c;
143
144 sprintf(buf, "/proc/%d/stat", pid);
145 f = fopen(buf, "r");
146 if (!f)
147 return 0;
148 while ((c = getc(f)) != EOF && c != '(')
149 ;
150 if (c != '(') {
151 fclose(f);
152 return 0;
153 }
154 /* this hopefully handles command names containing ')' */
155 while ((c = getc(f)) != EOF && c == *name)
156 name++;
157 fclose(f);
158 return (c == ')' && *name == '\0');
159}
160
161
162static void
163check(int pid)
164{
165 if (execname && !pid_is_exec(pid, execname)) {
166 return;
167 }
168 if (userspec && !pid_is_user(pid, user_id)) {
169 return;
170 }
171 if (cmdname && !pid_is_cmd(pid, cmdname)) {
172 return;
173 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000174 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000175}
176
177
178
179static void
180do_procfs(void)
181{
182 DIR *procdir;
183 struct dirent *entry;
184 int foundany, pid;
185
186 procdir = opendir("/proc");
187 if (!procdir)
188 perror_msg_and_die ("opendir /proc");
189
190 foundany = 0;
191 while ((entry = readdir(procdir)) != NULL) {
192 if (sscanf(entry->d_name, "%d", &pid) != 1)
193 continue;
194 foundany++;
195 check(pid);
196 }
197 closedir(procdir);
198 if (!foundany)
199 error_msg_and_die ("nothing in /proc - not mounted?");
200}
201
202
203static void
204do_stop(void)
205{
206 char what[1024];
Eric Andersen950d8b42001-10-31 09:55:39 +0000207 pid_list *p;
208 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000209
210 if (cmdname)
211 strcpy(what, cmdname);
212 else if (execname)
213 strcpy(what, execname);
214 else if (userspec)
215 sprintf(what, "process(es) owned by `%s'", userspec);
216 else
217 error_msg_and_die ("internal error, please report");
218
219 if (!found) {
220 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000221 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000222 }
223 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000224 if (kill(p->pid, signal_nr) == 0) {
225 p->pid = -p->pid;
226 killed++;
227 } else {
228 perror_msg("warning: failed to kill %d:", p->pid);
229 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000230 }
231 if (killed) {
232 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000233 for (p = found; p; p = p->next)
234 if(p->pid < 0)
235 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000236 printf(").\n");
237 }
238}
239
240
241int
242start_stop_daemon_main(int argc, char **argv)
243{
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000244 parse_options(argc, argv);
245 argc -= optind;
246 argv += optind;
247
Eric Andersen950d8b42001-10-31 09:55:39 +0000248 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
249 user_id = my_getpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000250
251 do_procfs();
252
253 if (stop) {
254 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000255 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000256 }
257
258 if (found) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000259 printf("%s already running.\n%d\n", execname ,found->pid);
260 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000261 }
262 *--argv = startas;
Eric Andersen53a22992002-01-26 09:04:45 +0000263 if (fork_before_exec) {
264 if (daemon(0, 0) == -1)
265 perror_msg_and_die ("unable to fork");
266 }
267 setsid();
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000268 execv(startas, argv);
269 perror_msg_and_die ("unable to start %s", startas);
270}
271