Notes on portability, and on when #include <linux/blah> is appropriate.
[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 <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>
19 #include <getopt.h> /* struct option */
20
21 #include "busybox.h"
22 #include "pwd_.h"
23
24 static int signal_nr = 15;
25 static int user_id = -1;
26 static int quiet = 0;
27 static char *userspec = NULL;
28 static char *cmdname = NULL;
29 static char *execname = NULL;
30 static char *pidfile = NULL;
31
32 struct pid_list {
33         struct pid_list *next;
34         pid_t pid;
35 };
36
37 static struct pid_list *found = NULL;
38
39 static inline void
40 push(pid_t pid)
41 {
42         struct pid_list *p;
43
44         p = xmalloc(sizeof(*p));
45         p->next = found;
46         p->pid = pid;
47         found = p;
48 }
49
50 static int
51 pid_is_exec(pid_t pid, const char *name)
52 {
53         char buf[32];
54         struct stat sb, exec_stat;
55
56         if (name)
57                 xstat(name, &exec_stat);
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);
63 }
64
65 static int
66 pid_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
77 static int
78 pid_is_cmd(pid_t pid, const char *name)
79 {
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
102 static void
103 check(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         }
114         push(pid);
115 }
116
117
118 static void
119 do_pidfile(void)
120 {
121         FILE *f;
122         pid_t pid;
123
124         f = fopen(pidfile, "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", pidfile);
131
132 }
133
134 static void
135 do_procinit(void)
136 {
137         DIR *procdir;
138         struct dirent *entry;
139         int foundany, pid;
140
141         if (pidfile) {
142                 do_pidfile();
143                 return;
144         }
145
146         procdir = bb_xopendir("/proc");
147
148         foundany = 0;
149         while ((entry = readdir(procdir)) != NULL) {
150                 if (sscanf(entry->d_name, "%d", &pid) != 1)
151                         continue;
152                 foundany++;
153                 check(pid);
154         }
155         closedir(procdir);
156         if (!foundany)
157                 bb_error_msg_and_die ("nothing in /proc - not mounted?");
158 }
159
160
161 static void
162 do_stop(void)
163 {
164         char what[1024];
165         struct pid_list *p;
166         int killed = 0;
167
168         do_procinit();
169
170         if (cmdname)
171                 strcpy(what, cmdname);
172         else if (execname)
173                 strcpy(what, execname);
174         else if (pidfile)
175                 sprintf(what, "process in pidfile `%.200s'", pidfile);
176         else if (userspec)
177                 sprintf(what, "process(es) owned by `%s'", userspec);
178         else
179                 bb_error_msg_and_die ("internal error, please report");
180
181         if (!found) {
182                 if (!quiet)
183                         printf("no %s found; none killed.\n", what);
184                 return;
185         }
186         for (p = found; p; p = p->next) {
187                 if (kill(p->pid, signal_nr) == 0) {
188                         p->pid = -p->pid;
189                         killed++;
190                 } else {
191                         bb_perror_msg("warning: failed to kill %d", p->pid);
192                 }
193         }
194         if (!quiet && killed) {
195                 printf("stopped %s (pid", what);
196                 for (p = found; p; p = p->next)
197                         if(p->pid < 0)
198                                 printf(" %d", -p->pid);
199                 printf(").\n");
200         }
201 }
202
203
204 static const struct option ssd_long_options[] = {
205         { "stop",                       0,              NULL,           'K' },
206         { "start",                      0,              NULL,           'S' },
207         { "background",                 0,              NULL,           'b' },
208         { "quiet",                      0,              NULL,           'q' },
209         { "make-pidfile",               0,              NULL,           'm' },
210         { "startas",                    1,              NULL,           'a' },
211         { "name",                       1,              NULL,           'n' },
212         { "signal",                     1,              NULL,           's' },
213         { "user",                       1,              NULL,           'u' },
214         { "exec",                       1,              NULL,           'x' },
215         { "pidfile",                    1,              NULL,           'p' },
216         { 0,                            0,              0,              0 }
217 };
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         bb_applet_long_options = ssd_long_options;
233
234         /* Check required one context option was given */
235         bb_opt_complementally = "K:S:?:K--S:S--K";
236         opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:",
237                 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
238
239
240         quiet = opt & SSD_OPT_QUIET;
241
242         if (signame) {
243                 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
244         }
245
246         if (!execname && !pidfile && !userspec && !cmdname)
247                 bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
248
249         if (!startas)
250                 startas = execname;
251
252         if ((opt & SSD_CTX_START) && !startas)
253                 bb_error_msg_and_die ("-S needs -x or -a");
254
255         if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL)
256                 bb_error_msg_and_die ("-m needs -p");
257
258         argc -= optind;
259         argv += optind;
260
261         if (userspec && sscanf(userspec, "%d", &user_id) != 1)
262                 user_id = bb_xgetpwnam(userspec);
263
264         if (opt & SSD_CTX_STOP) {
265                 do_stop();
266                 return EXIT_SUCCESS;
267         }
268
269         do_procinit();
270
271         if (found) {
272                 if (!quiet)
273                         printf("%s already running.\n%d\n", execname ,found->pid);
274                 return EXIT_SUCCESS;
275         }
276         *--argv = startas;
277         if (opt & SSD_OPT_BACKGROUND) {
278                 bb_xdaemon(0, 0);
279                 setsid();
280         }
281         if (opt & SSD_OPT_MAKEPID) {
282                 /* user wants _us_ to make the pidfile */
283                 FILE *pidf = fopen(pidfile, "w");
284                 pid_t pidt = getpid();
285                 if (pidf == NULL)
286                         bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
287                 fprintf(pidf, "%d\n", pidt);
288                 fclose(pidf);
289         }
290         execv(startas, argv);
291         bb_perror_msg_and_die ("unable to start %s", startas);
292 }