accidentally applied wrong (old) patch, fixing up...
[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  * 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 "busybox.h"
12 #include <getopt.h>
13 #include <sys/resource.h>
14
15 static int signal_nr = 15;
16 static int user_id = -1;
17 static int quiet;
18 static char *userspec;
19 static char *chuid;
20 static char *cmdname;
21 static char *execname;
22 static char *pidfile;
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,
226         OPT_QUIET      = 0x8,
227         OPT_MAKEPID    = 0x10,
228         OPT_OKNODO     = 0x20 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
229         OPT_VERBOSE    = 0x40 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
230         OPT_NICELEVEL  = 0x80 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
231 };
232
233 int start_stop_daemon_main(int argc, char **argv)
234 {
235         unsigned opt;
236         char *signame = NULL;
237         char *startas = NULL;
238 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
239 //      char *retry_arg = NULL;
240 //      int retries = -1;
241         char *opt_N;
242 #endif
243 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
244         applet_long_options = long_options;
245 #endif
246
247         /* Check required one context option was given */
248         opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
249         opt = getopt32(argc, argv, "KSbqm"
250 //              USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
251                 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
252                 "a:n:s:u:c:x:p:"
253                 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
254 //              USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
255                 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
256
257         quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
258
259         if (signame) {
260                 signal_nr = get_signum(signame);
261                 if (signal_nr < 0) bb_show_usage();
262         }
263
264         if (!startas)
265                 startas = execname;
266
267 //      USE_FEATURE_START_STOP_DAEMON_FANCY(
268 //              if (retry_arg)
269 //                      retries = xatoi_u(retry_arg);
270 //      )
271         argc -= optind;
272         argv += optind;
273
274         if (userspec) {
275                 user_id = bb_strtou(userspec, NULL, 10);
276                 if (errno)
277                         user_id = xuname2uid(userspec);
278         }
279
280         if (opt & CTX_STOP) {
281                 int i = do_stop();
282                 return (opt & OPT_OKNODO) ? 0 : (i<=0);
283         }
284
285         do_procinit();
286
287         if (found) {
288                 if (!quiet)
289                         printf("%s already running\n%d\n", execname, found->pid);
290                 return !(opt & OPT_OKNODO);
291         }
292         *--argv = startas;
293         if (opt & OPT_BACKGROUND) {
294                 setsid();
295                 bb_daemonize();
296         }
297         if (opt & OPT_MAKEPID) {
298                 /* user wants _us_ to make the pidfile */
299                 FILE *pidf = xfopen(pidfile, "w");
300
301                 pid_t pidt = getpid();
302                 fprintf(pidf, "%d\n", pidt);
303                 fclose(pidf);
304         }
305         if (chuid) {
306                 user_id = bb_strtou(chuid, NULL, 10);
307                 if (errno)
308                         user_id = xuname2uid(chuid);
309                 xsetuid(user_id);
310         }
311 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
312         if (opt & OPT_NICELEVEL) {
313                 /* Set process priority */
314                 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
315                 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
316                         bb_perror_msg_and_die("setpriority(%d)", prio);
317                 }
318         }
319 #endif
320         execv(startas, argv);
321         bb_perror_msg_and_die("cannot start %s", startas);
322 }