usage.c: remove reference to busybox.h
[platform/upstream/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  * Adapted for busybox David Kimdon <dwhedon@gordian.com>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include <getopt.h>
12 #include <sys/resource.h>
13
14 #include "libbb.h"
15
16 static int signal_nr = 15;
17 static int user_id = -1;
18 static char *userspec;
19 static char *cmdname;
20 static char *execname;
21 static char *pidfile;
22 static smallint quiet;
23
24 struct pid_list {
25         struct pid_list *next;
26         pid_t pid;
27 };
28
29 static struct pid_list *found;
30
31 static inline void push(pid_t pid)
32 {
33         struct pid_list *p;
34
35         p = xmalloc(sizeof(*p));
36         p->next = found;
37         p->pid = pid;
38         found = p;
39 }
40
41 static int pid_is_exec(pid_t pid, const char *name)
42 {
43         char buf[sizeof("/proc//exe") + sizeof(int)*3];
44         char *execbuf;
45         int sz;
46         int equal;
47
48         sprintf(buf, "/proc/%d/exe", pid);
49         sz = strlen(name) + 1;
50         execbuf = xzalloc(sz);
51         readlink(buf, execbuf, sz);
52
53         /* if readlink fails, execbuf still contains "" */
54         equal = !strcmp(execbuf, name);
55         if (ENABLE_FEATURE_CLEAN_UP)
56                 free(execbuf);
57         return equal;
58 }
59
60 static int pid_is_user(int pid, int uid)
61 {
62         struct stat sb;
63         char buf[sizeof("/proc/") + sizeof(int)*3];
64
65         sprintf(buf, "/proc/%u", pid);
66         if (stat(buf, &sb) != 0)
67                 return 0;
68         return (sb.st_uid == uid);
69 }
70
71 static int pid_is_cmd(pid_t pid, const char *name)
72 {
73         char fname[sizeof("/proc//stat") + sizeof(int)*3];
74         char *buf;
75         int r = 0;
76
77         sprintf(fname, "/proc/%u/stat", pid);
78         buf = xmalloc_open_read_close(fname, NULL);
79         if (buf) {
80                 char *p = strchr(buf, '(');
81                 if (p) {
82                         char *pe = strrchr(++p, ')');
83                         if (pe) {
84                                 *pe = '\0';
85                                 r = !strcmp(p, name);
86                         }
87                 }
88                 free(buf);
89         }
90         return r;
91 }
92
93
94 static void check(int pid)
95 {
96         if (execname && !pid_is_exec(pid, execname)) {
97                 return;
98         }
99         if (userspec && !pid_is_user(pid, user_id)) {
100                 return;
101         }
102         if (cmdname && !pid_is_cmd(pid, cmdname)) {
103                 return;
104         }
105         push(pid);
106 }
107
108
109 static void do_pidfile(void)
110 {
111         FILE *f;
112         pid_t pid;
113
114         f = fopen(pidfile, "r");
115         if (f) {
116                 if (fscanf(f, "%u", &pid) == 1)
117                         check(pid);
118                 fclose(f);
119         } else if (errno != ENOENT)
120                 bb_perror_msg_and_die("open pidfile %s", pidfile);
121 }
122
123 static void do_procinit(void)
124 {
125         DIR *procdir;
126         struct dirent *entry;
127         int foundany, pid;
128
129         if (pidfile) {
130                 do_pidfile();
131                 return;
132         }
133
134         procdir = xopendir("/proc");
135
136         foundany = 0;
137         while ((entry = readdir(procdir)) != NULL) {
138                 pid = bb_strtou(entry->d_name, NULL, 10);
139                 if (errno)
140                         continue;
141                 foundany++;
142                 check(pid);
143         }
144         closedir(procdir);
145         if (!foundany)
146                 bb_error_msg_and_die ("nothing in /proc - not mounted?");
147 }
148
149
150 static int do_stop(void)
151 {
152         char *what;
153         struct pid_list *p;
154         int killed = 0;
155
156         do_procinit();
157
158         if (cmdname)
159                 what = xstrdup(cmdname);
160         else if (execname)
161                 what = xstrdup(execname);
162         else if (pidfile)
163                 what = xasprintf("process in pidfile '%s'", pidfile);
164         else if (userspec)
165                 what = xasprintf("process(es) owned by '%s'", userspec);
166         else
167                 bb_error_msg_and_die("internal error, please report");
168
169         if (!found) {
170                 if (!quiet)
171                         printf("no %s found; none killed\n", what);
172                 if (ENABLE_FEATURE_CLEAN_UP)
173                         free(what);
174                 return -1;
175         }
176         for (p = found; p; p = p->next) {
177                 if (kill(p->pid, signal_nr) == 0) {
178                         p->pid = -p->pid;
179                         killed++;
180                 } else {
181                         bb_perror_msg("warning: failed to kill %d", p->pid);
182                 }
183         }
184         if (!quiet && killed) {
185                 printf("stopped %s (pid", what);
186                 for (p = found; p; p = p->next)
187                         if (p->pid < 0)
188                                 printf(" %d", -p->pid);
189                 puts(")");
190         }
191         if (ENABLE_FEATURE_CLEAN_UP)
192                 free(what);
193         return killed;
194 }
195
196 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
197 static const struct option long_options[] = {
198         { "stop",               0,      NULL,   'K' },
199         { "start",              0,      NULL,   'S' },
200         { "background",         0,      NULL,   'b' },
201         { "quiet",              0,      NULL,   'q' },
202         { "make-pidfile",       0,      NULL,   'm' },
203 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
204         { "oknodo",             0,      NULL,   'o' },
205         { "verbose",            0,      NULL,   'v' },
206         { "nicelevel",          1,      NULL,   'N' },
207 #endif
208         { "startas",            1,      NULL,   'a' },
209         { "name",               1,      NULL,   'n' },
210         { "signal",             1,      NULL,   's' },
211         { "user",               1,      NULL,   'u' },
212         { "chuid",              1,      NULL,   'c' },
213         { "exec",               1,      NULL,   'x' },
214         { "pidfile",            1,      NULL,   'p' },
215 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
216         { "retry",              1,      NULL,   'R' },
217 #endif
218         { 0,                    0,      0,      0 }
219 };
220 #endif
221
222 enum {
223         CTX_STOP       = 0x1,
224         CTX_START      = 0x2,
225         OPT_BACKGROUND = 0x4, // -b
226         OPT_QUIET      = 0x8, // -q
227         OPT_MAKEPID    = 0x10, // -m
228         OPT_a          = 0x20, // -a
229         OPT_n          = 0x40, // -n
230         OPT_s          = 0x80, // -s
231         OPT_u          = 0x100, // -u
232         OPT_c          = 0x200, // -c
233         OPT_x          = 0x400, // -x
234         OPT_p          = 0x800, // -p
235         OPT_OKNODO     = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
236         OPT_VERBOSE    = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
237         OPT_NICELEVEL  = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
238 };
239
240 int start_stop_daemon_main(int argc, char **argv);
241 int start_stop_daemon_main(int argc, char **argv)
242 {
243         unsigned opt;
244         char *signame;
245         char *startas;
246         char *chuid;
247 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
248 //      char *retry_arg = NULL;
249 //      int retries = -1;
250         char *opt_N;
251 #endif
252 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
253         applet_long_options = long_options;
254 #endif
255
256         /* Check required one context option was given */
257         opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
258         opt = getopt32(argc, argv, "KSbqma:n:s:u:c:x:p:"
259                 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
260 //              USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
261                 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
262                 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
263 //              USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
264         );
265
266         quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
267
268         if (opt & OPT_s) {
269                 signal_nr = get_signum(signame);
270                 if (signal_nr < 0) bb_show_usage();
271         }
272
273         if (!(opt & OPT_a))
274                 startas = execname;
275
276 //      USE_FEATURE_START_STOP_DAEMON_FANCY(
277 //              if (retry_arg)
278 //                      retries = xatoi_u(retry_arg);
279 //      )
280         argc -= optind;
281         argv += optind;
282
283         if (userspec) {
284                 user_id = bb_strtou(userspec, NULL, 10);
285                 if (errno)
286                         user_id = xuname2uid(userspec);
287         }
288
289         if (opt & CTX_STOP) {
290                 int i = do_stop();
291                 return (opt & OPT_OKNODO) ? 0 : (i<=0);
292         }
293
294         do_procinit();
295
296         if (found) {
297                 if (!quiet)
298                         printf("%s already running\n%d\n", execname, found->pid);
299                 return !(opt & OPT_OKNODO);
300         }
301         *--argv = startas;
302         if (opt & OPT_BACKGROUND) {
303                 bb_daemonize(0);
304         }
305         if (opt & OPT_MAKEPID) {
306                 /* user wants _us_ to make the pidfile */
307                 FILE *pidf = xfopen(pidfile, "w");
308
309                 pid_t pidt = getpid();
310                 fprintf(pidf, "%d\n", pidt);
311                 fclose(pidf);
312         }
313         if (opt & OPT_c) {
314                 struct bb_uidgid_t ugid;
315                 parse_chown_usergroup_or_die(&ugid, chuid);
316                 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
317                 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
318         }
319 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
320         if (opt & OPT_NICELEVEL) {
321                 /* Set process priority */
322                 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
323                 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
324                         bb_perror_msg_and_die("setpriority(%d)", prio);
325                 }
326         }
327 #endif
328         execv(startas, argv);
329         bb_perror_msg_and_die("cannot start %s", startas);
330 }