- add testcase for grep bug (http://busybox.net/bugs/view.php?id=887)
[oweals/busybox.git] / debianutils / start_stop_daemon.c
1 /* 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 "busybox.h"
11 #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>
20 #include <getopt.h> /* struct option */
21 #include "pwd_.h"
22
23 static int signal_nr = 15;
24 static int user_id = -1;
25 static int quiet = 0;
26 static char *userspec = NULL;
27 static char *cmdname = NULL;
28 static char *execname = NULL;
29 static char *pidfile = NULL;
30
31 struct pid_list {
32         struct pid_list *next;
33         pid_t pid;
34 };
35
36 static struct pid_list *found = NULL;
37
38 static inline void
39 push(pid_t pid)
40 {
41         struct pid_list *p;
42
43         p = xmalloc(sizeof(*p));
44         p->next = found;
45         p->pid = pid;
46         found = p;
47 }
48
49 static int
50 pid_is_exec(pid_t pid, const char *name)
51 {
52         char buf[32];
53         struct stat sb, exec_stat;
54
55         if (name)
56                 xstat(name, &exec_stat);
57
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);
62 }
63
64 static int
65 pid_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
76 static int
77 pid_is_cmd(pid_t pid, const char *name)
78 {
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
101 static void
102 check(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         }
113         push(pid);
114 }
115
116
117 static void
118 do_pidfile(void)
119 {
120         FILE *f;
121         pid_t pid;
122
123         f = fopen(pidfile, "r");
124         if (f) {
125                 if (fscanf(f, "%d", &pid) == 1)
126                         check(pid);
127                 fclose(f);
128         } else if (errno != ENOENT)
129                 bb_perror_msg_and_die("open pidfile %s", pidfile);
130
131 }
132
133 static void
134 do_procinit(void)
135 {
136         DIR *procdir;
137         struct dirent *entry;
138         int foundany, pid;
139
140         if (pidfile) {
141                 do_pidfile();
142                 return;
143         }
144
145         procdir = bb_xopendir("/proc");
146
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)
156                 bb_error_msg_and_die ("nothing in /proc - not mounted?");
157 }
158
159
160 static void
161 do_stop(void)
162 {
163         char what[1024];
164         struct pid_list *p;
165         int killed = 0;
166
167         do_procinit();
168
169         if (cmdname)
170                 strcpy(what, cmdname);
171         else if (execname)
172                 strcpy(what, execname);
173         else if (pidfile)
174                 sprintf(what, "process in pidfile `%.200s'", pidfile);
175         else if (userspec)
176                 sprintf(what, "process(es) owned by `%s'", userspec);
177         else
178                 bb_error_msg_and_die ("internal error, please report");
179
180         if (!found) {
181                 if (!quiet)
182                         printf("no %s found; none killed.\n", what);
183                 return;
184         }
185         for (p = found; p; p = p->next) {
186                 if (kill(p->pid, signal_nr) == 0) {
187                         p->pid = -p->pid;
188                         killed++;
189                 } else {
190                         bb_perror_msg("warning: failed to kill %d", p->pid);
191                 }
192         }
193         if (!quiet && killed) {
194                 printf("stopped %s (pid", what);
195                 for (p = found; p; p = p->next)
196                         if(p->pid < 0)
197                                 printf(" %d", -p->pid);
198                 printf(").\n");
199         }
200 }
201
202 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
203 static const struct option ssd_long_options[] = {
204         { "stop",                       0,              NULL,           'K' },
205         { "start",                      0,              NULL,           'S' },
206         { "background",                 0,              NULL,           'b' },
207         { "quiet",                      0,              NULL,           'q' },
208         { "make-pidfile",               0,              NULL,           'm' },
209         { "startas",                    1,              NULL,           'a' },
210         { "name",                       1,              NULL,           'n' },
211         { "signal",                     1,              NULL,           's' },
212         { "user",                       1,              NULL,           'u' },
213         { "exec",                       1,              NULL,           'x' },
214         { "pidfile",                    1,              NULL,           'p' },
215         { 0,                            0,              0,              0 }
216 };
217 #endif
218
219 #define SSD_CTX_STOP            1
220 #define SSD_CTX_START           2
221 #define SSD_OPT_BACKGROUND      4
222 #define SSD_OPT_QUIET           8
223 #define SSD_OPT_MAKEPID         16
224
225 int
226 start_stop_daemon_main(int argc, char **argv)
227 {
228         unsigned long opt;
229         char *signame = NULL;
230         char *startas = NULL;
231
232 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
233         bb_applet_long_options = ssd_long_options;
234 #endif
235
236         /* Check required one context option was given */
237         bb_opt_complementally = "K:S:?:K--S:S--K";
238         opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
239                 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
240
241
242         quiet = opt & SSD_OPT_QUIET;
243
244         if (signame) {
245                 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
246         }
247
248         if (!execname && !pidfile && !userspec && !cmdname)
249                 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
250
251         if (!startas)
252                 startas = execname;
253
254         if ((opt & SSD_CTX_START) && !startas)
255                 bb_error_msg_and_die ("-S needs -x or -a");
256
257         if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
258                 bb_error_msg_and_die ("-m needs -p");
259
260         argc -= optind;
261         argv += optind;
262
263         if (userspec && sscanf(userspec, "%d", &user_id) != 1)
264                 user_id = bb_xgetpwnam(userspec);
265
266         if (opt & SSD_CTX_STOP) {
267                 do_stop();
268                 return EXIT_SUCCESS;
269         }
270
271         do_procinit();
272
273         if (found) {
274                 if (!quiet)
275                         printf("%s already running.\n%d\n", execname ,found->pid);
276                 return EXIT_SUCCESS;
277         }
278         *--argv = startas;
279         if (opt & SSD_OPT_BACKGROUND) {
280                 bb_xdaemon(0, 0);
281                 setsid();
282         }
283         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         }
292         execv(startas, argv);
293         bb_perror_msg_and_die ("unable to start %s", startas);
294 }