]>
Commit | Line | Data |
---|---|---|
183843cd DS |
1 | /* |
2 | * A rewrite of the original Debian's start-stop-daemon Perl script | |
3 | * in C (faster - it is executed many times during system startup). | |
4 | * | |
5 | * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, | |
6 | * public domain. Based conceptually on start-stop-daemon.pl, by Ian | |
7 | * Jackson <ijackson@gnu.ai.mit.edu>. May be used and distributed | |
8 | * freely for any purpose. Changes by Christian Schwarz | |
9 | * <schwarz@monet.m.isar.de>, to make output conform to the Debian | |
10 | * Console Message Standard, also placed in public domain. Minor | |
11 | * changes by Klee Dienes <klee@debian.org>, also placed in the Public | |
12 | * Domain. | |
13 | * | |
14 | * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background | |
15 | * and --make-pidfile options, placed in public domain aswell. | |
16 | * | |
17 | * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu> | |
18 | * and Andreas Schuldei <andreas@schuldei.org> | |
19 | * | |
20 | * Changes by Ian Jackson: added --retry (and associated rearrangements). | |
21 | * | |
22 | * Modified for Gentoo rc-scripts by Donny Davies <woodchip@gentoo.org>: | |
23 | * I removed the BSD/Hurd/OtherOS stuff, added #include <stddef.h> | |
24 | * and stuck in a #define VERSION "1.9.18". Now it compiles without | |
25 | * the whole automake/config.h dance. | |
26 | */ | |
27 | ||
b45ac5f5 DL |
28 | #ifdef HAVE_CONFIG_H |
29 | #include "config.h" | |
30 | #endif | |
31 | ||
183843cd DS |
32 | #ifdef HAVE_LXC |
33 | #define _GNU_SOURCE | |
34 | #include <sched.h> | |
35 | #endif /* HAVE_LXC */ | |
36 | ||
37 | #include <stddef.h> | |
b45ac5f5 | 38 | #undef VERSION |
183843cd DS |
39 | #define VERSION "1.9.18" |
40 | ||
41 | #define MIN_POLL_INTERVAL 20000 /*us*/ | |
42 | ||
43 | #include <errno.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <string.h> | |
47 | #include <stdarg.h> | |
48 | #include <signal.h> | |
49 | #include <sys/stat.h> | |
50 | #include <dirent.h> | |
51 | #include <sys/time.h> | |
52 | #include <sys/queue.h> | |
53 | #include <unistd.h> | |
54 | #include <getopt.h> | |
55 | #include <pwd.h> | |
56 | #include <grp.h> | |
57 | #include <sys/ioctl.h> | |
58 | #include <sys/types.h> | |
f89d8115 | 59 | #include <termios.h> |
183843cd DS |
60 | #include <fcntl.h> |
61 | #include <limits.h> | |
62 | #include <assert.h> | |
63 | #include <ctype.h> | |
8f500a1c | 64 | #ifdef linux |
183843cd | 65 | #include <linux/sched.h> |
8f500a1c | 66 | #endif |
183843cd DS |
67 | |
68 | static int testmode = 0; | |
69 | static int quietmode = 0; | |
70 | static int exitnodo = 1; | |
71 | static int start = 0; | |
72 | static int stop = 0; | |
73 | static int background = 0; | |
74 | static int mpidfile = 0; | |
75 | static int signal_nr = 15; | |
76 | static const char *signal_str = NULL; | |
77 | static int user_id = -1; | |
78 | static int runas_uid = -1; | |
79 | static int runas_gid = -1; | |
80 | static const char *userspec = NULL; | |
81 | static char *changeuser = NULL; | |
82 | static const char *changegroup = NULL; | |
83 | static char *changeroot = NULL; | |
84 | static const char *cmdname = NULL; | |
85 | static char *execname = NULL; | |
86 | static char *startas = NULL; | |
87 | static const char *pidfile = NULL; | |
88 | static char what_stop[1024]; | |
89 | static const char *schedule_str = NULL; | |
90 | static const char *progname = ""; | |
91 | static int nicelevel = 0; | |
92 | ||
93 | static struct stat exec_stat; | |
94 | ||
95 | struct pid_list { | |
96 | struct pid_list *next; | |
97 | pid_t pid; | |
98 | }; | |
99 | ||
100 | static struct pid_list *found = NULL; | |
101 | static struct pid_list *killed = NULL; | |
102 | ||
103 | struct schedule_item { | |
104 | enum { sched_timeout, sched_signal, sched_goto, sched_forever } type; | |
105 | int value; /* seconds, signal no., or index into array */ | |
106 | /* sched_forever is only seen within parse_schedule and callees */ | |
107 | }; | |
108 | ||
109 | static int schedule_length; | |
110 | static struct schedule_item *schedule = NULL; | |
111 | ||
112 | LIST_HEAD(namespace_head, namespace); | |
113 | ||
d62a17ae | 114 | struct namespace |
115 | { | |
183843cd | 116 | LIST_ENTRY(namespace) list; |
c9deb8fd | 117 | const char *path; |
183843cd DS |
118 | int nstype; |
119 | }; | |
120 | ||
121 | static struct namespace_head namespace_head; | |
122 | ||
123 | static void *xmalloc(int size); | |
124 | static void push(struct pid_list **list, pid_t pid); | |
125 | static void do_help(void); | |
d62a17ae | 126 | static void parse_options(int argc, char *const *argv); |
183843cd DS |
127 | static int pid_is_user(pid_t pid, uid_t uid); |
128 | static int pid_is_cmd(pid_t pid, const char *name); | |
129 | static void check(pid_t pid); | |
130 | static void do_pidfile(const char *name); | |
d62a17ae | 131 | static void do_stop(int signal_nr, int quietmode, int *n_killed, |
132 | int *n_notkilled, int retry_nr); | |
183843cd DS |
133 | static int pid_is_exec(pid_t pid, const struct stat *esb); |
134 | ||
135 | #ifdef __GNUC__ | |
136 | static void fatal(const char *format, ...) | |
137 | __attribute__((noreturn, format(printf, 1, 2))); | |
d62a17ae | 138 | static void badusage(const char *msg) __attribute__((noreturn)); |
183843cd DS |
139 | #else |
140 | static void fatal(const char *format, ...); | |
141 | static void badusage(const char *msg); | |
142 | #endif | |
143 | ||
144 | /* This next part serves only to construct the TVCALC macro, which | |
145 | * is used for doing arithmetic on struct timeval's. It works like this: | |
146 | * TVCALC(result, expression); | |
147 | * where result is a struct timeval (and must be an lvalue) and | |
148 | * expression is the single expression for both components. In this | |
149 | * expression you can use the special values TVELEM, which when fed a | |
150 | * const struct timeval* gives you the relevant component, and | |
151 | * TVADJUST. TVADJUST is necessary when subtracting timevals, to make | |
152 | * it easier to renormalise. Whenver you subtract timeval elements, | |
153 | * you must make sure that TVADJUST is added to the result of the | |
154 | * subtraction (before any resulting multiplication or what have you). | |
155 | * TVELEM must be linear in TVADJUST. | |
156 | */ | |
d62a17ae | 157 | typedef long tvselector(const struct timeval *); |
158 | static long tvselector_sec(const struct timeval *tv) | |
159 | { | |
160 | return tv->tv_sec; | |
183843cd | 161 | } |
d62a17ae | 162 | static long tvselector_usec(const struct timeval *tv) |
163 | { | |
164 | return tv->tv_usec; | |
165 | } | |
166 | #define TVCALC_ELEM(result, expr, sec, adj) \ | |
167 | { \ | |
168 | const long TVADJUST = adj; \ | |
169 | long (*const TVELEM)(const struct timeval *) = \ | |
170 | tvselector_##sec; \ | |
171 | (result).tv_##sec = (expr); \ | |
172 | } | |
173 | #define TVCALC(result, expr) \ | |
174 | do { \ | |
175 | TVCALC_ELEM(result, expr, sec, (-1)); \ | |
176 | TVCALC_ELEM(result, expr, usec, (+1000000)); \ | |
177 | (result).tv_sec += (result).tv_usec / 1000000; \ | |
178 | (result).tv_usec %= 1000000; \ | |
179 | } while (0) | |
183843cd DS |
180 | |
181 | ||
d62a17ae | 182 | static void fatal(const char *format, ...) |
183843cd DS |
183 | { |
184 | va_list arglist; | |
185 | ||
186 | fprintf(stderr, "%s: ", progname); | |
187 | va_start(arglist, format); | |
188 | vfprintf(stderr, format, arglist); | |
189 | va_end(arglist); | |
190 | putc('\n', stderr); | |
191 | exit(2); | |
192 | } | |
193 | ||
194 | ||
d62a17ae | 195 | static void *xmalloc(int size) |
183843cd DS |
196 | { |
197 | void *ptr; | |
198 | ||
199 | ptr = malloc(size); | |
200 | if (ptr) | |
201 | return ptr; | |
202 | fatal("malloc(%d) failed", size); | |
203 | } | |
204 | ||
d62a17ae | 205 | static void xgettimeofday(struct timeval *tv) |
183843cd | 206 | { |
d62a17ae | 207 | if (gettimeofday(tv, 0) != 0) |
183843cd DS |
208 | fatal("gettimeofday failed: %s", strerror(errno)); |
209 | } | |
210 | ||
d62a17ae | 211 | static void push(struct pid_list **list, pid_t pid) |
183843cd DS |
212 | { |
213 | struct pid_list *p; | |
214 | ||
215 | p = xmalloc(sizeof(*p)); | |
216 | p->next = *list; | |
217 | p->pid = pid; | |
218 | *list = p; | |
219 | } | |
220 | ||
d62a17ae | 221 | static void clear(struct pid_list **list) |
183843cd DS |
222 | { |
223 | struct pid_list *here, *next; | |
224 | ||
225 | for (here = *list; here != NULL; here = next) { | |
226 | next = here->next; | |
227 | free(here); | |
228 | } | |
229 | ||
230 | *list = NULL; | |
231 | } | |
232 | ||
7936827a | 233 | #ifdef linux |
d62a17ae | 234 | static const char *next_dirname(const char *s) |
183843cd | 235 | { |
c9deb8fd | 236 | const char *cur; |
183843cd | 237 | |
c4efd0f4 | 238 | cur = s; |
183843cd DS |
239 | |
240 | if (*cur != '\0') { | |
241 | for (; *cur != '/'; ++cur) | |
242 | if (*cur == '\0') | |
243 | return cur; | |
244 | ||
245 | for (; *cur == '/'; ++cur) | |
246 | ; | |
247 | } | |
248 | ||
249 | return cur; | |
250 | } | |
251 | ||
d62a17ae | 252 | static void add_namespace(const char *path) |
183843cd DS |
253 | { |
254 | int nstype; | |
255 | const char *nsdirname, *nsname, *cur; | |
256 | struct namespace *namespace; | |
257 | ||
c4efd0f4 | 258 | cur = path; |
183843cd DS |
259 | nsdirname = nsname = ""; |
260 | ||
261 | while ((cur = next_dirname(cur))[0] != '\0') { | |
262 | nsdirname = nsname; | |
263 | nsname = cur; | |
264 | } | |
265 | ||
e1be9119 | 266 | if (!strncmp(nsdirname, "ipcns/", strlen("ipcns/"))) |
183843cd | 267 | nstype = CLONE_NEWIPC; |
e1be9119 | 268 | else if (!strncmp(nsdirname, "netns/", strlen("netns/"))) |
183843cd | 269 | nstype = CLONE_NEWNET; |
e1be9119 | 270 | else if (!strncmp(nsdirname, "utcns/", strlen("utcns/"))) |
183843cd DS |
271 | nstype = CLONE_NEWUTS; |
272 | else | |
273 | badusage("invalid namepspace path"); | |
274 | ||
275 | namespace = xmalloc(sizeof(*namespace)); | |
c4efd0f4 | 276 | namespace->path = path; |
183843cd DS |
277 | namespace->nstype = nstype; |
278 | LIST_INSERT_HEAD(&namespace_head, namespace, list); | |
279 | } | |
8f500a1c | 280 | #endif |
183843cd DS |
281 | |
282 | #ifdef HAVE_LXC | |
4d762f26 | 283 | static void set_namespaces(void) |
183843cd DS |
284 | { |
285 | struct namespace *namespace; | |
286 | int fd; | |
287 | ||
a2addae8 | 288 | LIST_FOREACH (namespace, &namespace_head, list) { |
183843cd | 289 | if ((fd = open(namespace->path, O_RDONLY)) == -1) |
d62a17ae | 290 | fatal("open namespace %s: %s", namespace->path, |
291 | strerror(errno)); | |
183843cd DS |
292 | if (setns(fd, namespace->nstype) == -1) |
293 | fatal("setns %s: %s", namespace->path, strerror(errno)); | |
294 | } | |
295 | } | |
296 | #else | |
4d762f26 | 297 | static void set_namespaces(void) |
183843cd DS |
298 | { |
299 | if (!LIST_EMPTY(&namespace_head)) | |
300 | fatal("LCX namespaces not supported"); | |
301 | } | |
302 | #endif | |
303 | ||
d62a17ae | 304 | static void do_help(void) |
183843cd | 305 | { |
d62a17ae | 306 | printf("start-stop-daemon " VERSION |
307 | " for Debian - small and fast C version written by\n" | |
308 | "Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n" | |
309 | "\n" | |
310 | "Usage:\n" | |
311 | " start-stop-daemon -S|--start options ... -- arguments ...\n" | |
312 | " start-stop-daemon -K|--stop options ...\n" | |
313 | " start-stop-daemon -H|--help\n" | |
314 | " start-stop-daemon -V|--version\n" | |
315 | "\n" | |
316 | "Options (at least one of --exec|--pidfile|--user is required):\n" | |
317 | " -x|--exec <executable> program to start/check if it is running\n" | |
318 | " -p|--pidfile <pid-file> pid file to check\n" | |
319 | " -c|--chuid <name|uid[:group|gid]>\n" | |
320 | " change to this user/group before starting process\n" | |
321 | " -u|--user <username>|<uid> stop processes owned by this user\n" | |
322 | " -n|--name <process-name> stop processes with this name\n" | |
323 | " -s|--signal <signal> signal to send (default TERM)\n" | |
324 | " -a|--startas <pathname> program to start (default is <executable>)\n" | |
325 | " -N|--nicelevel <incr> add incr to the process's nice level\n" | |
326 | " -b|--background force the process to detach\n" | |
327 | " -m|--make-pidfile create the pidfile before starting\n" | |
328 | " -R|--retry <schedule> check whether processes die, and retry\n" | |
329 | " -t|--test test mode, don't do anything\n" | |
330 | " -o|--oknodo exit status 0 (not 1) if nothing done\n" | |
331 | " -q|--quiet be more quiet\n" | |
332 | " -v|--verbose be more verbose\n" | |
333 | "Retry <schedule> is <item>|/<item>/... where <item> is one of\n" | |
334 | " -<signal-num>|[-]<signal-name> send that signal\n" | |
335 | " <timeout> wait that many seconds\n" | |
336 | " forever repeat remainder forever\n" | |
337 | "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n" | |
338 | "\n" | |
339 | "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n" | |
340 | " 3 = trouble 2 = with --retry, processes wouldn't die\n"); | |
183843cd DS |
341 | } |
342 | ||
343 | ||
d62a17ae | 344 | static void badusage(const char *msg) |
183843cd DS |
345 | { |
346 | if (msg) | |
347 | fprintf(stderr, "%s: %s\n", progname, msg); | |
348 | fprintf(stderr, "Try `%s --help' for more information.\n", progname); | |
349 | exit(3); | |
350 | } | |
351 | ||
352 | struct sigpair { | |
353 | const char *name; | |
354 | int signal; | |
355 | }; | |
356 | ||
357 | const struct sigpair siglist[] = { | |
d62a17ae | 358 | {"ABRT", SIGABRT}, {"ALRM", SIGALRM}, {"FPE", SIGFPE}, |
359 | {"HUP", SIGHUP}, {"ILL", SIGILL}, {"INT", SIGINT}, | |
360 | {"KILL", SIGKILL}, {"PIPE", SIGPIPE}, {"QUIT", SIGQUIT}, | |
361 | {"SEGV", SIGSEGV}, {"TERM", SIGTERM}, {"USR1", SIGUSR1}, | |
362 | {"USR2", SIGUSR2}, {"CHLD", SIGCHLD}, {"CONT", SIGCONT}, | |
363 | {"STOP", SIGSTOP}, {"TSTP", SIGTSTP}, {"TTIN", SIGTTIN}, | |
364 | {"TTOU", SIGTTOU}}; | |
365 | ||
366 | static int parse_integer(const char *string, int *value_r) | |
367 | { | |
183843cd DS |
368 | unsigned long ul; |
369 | char *ep; | |
370 | ||
371 | if (!string[0]) | |
372 | return -1; | |
373 | ||
d62a17ae | 374 | ul = strtoul(string, &ep, 10); |
183843cd DS |
375 | if (ul > INT_MAX || *ep != '\0') |
376 | return -1; | |
377 | ||
d62a17ae | 378 | *value_r = ul; |
183843cd DS |
379 | return 0; |
380 | } | |
381 | ||
d62a17ae | 382 | static int parse_signal(const char *signal_str, int *signal_nr) |
183843cd DS |
383 | { |
384 | unsigned int i; | |
385 | ||
386 | if (parse_integer(signal_str, signal_nr) == 0) | |
387 | return 0; | |
388 | ||
d62a17ae | 389 | for (i = 0; i < sizeof(siglist) / sizeof(siglist[0]); i++) { |
390 | if (strcmp(signal_str, siglist[i].name) == 0) { | |
183843cd DS |
391 | *signal_nr = siglist[i].signal; |
392 | return 0; | |
393 | } | |
394 | } | |
395 | return -1; | |
396 | } | |
397 | ||
d62a17ae | 398 | static void parse_schedule_item(const char *string, struct schedule_item *item) |
399 | { | |
183843cd DS |
400 | const char *after_hyph; |
401 | ||
d62a17ae | 402 | if (!strcmp(string, "forever")) { |
183843cd | 403 | item->type = sched_forever; |
fefa5e0f | 404 | } else if (isdigit((unsigned char)string[0])) { |
183843cd DS |
405 | item->type = sched_timeout; |
406 | if (parse_integer(string, &item->value) != 0) | |
407 | badusage("invalid timeout value in schedule"); | |
d62a17ae | 408 | } else if ((after_hyph = string + (string[0] == '-')) |
409 | && parse_signal(after_hyph, &item->value) == 0) { | |
183843cd DS |
410 | item->type = sched_signal; |
411 | } else { | |
d62a17ae | 412 | badusage( |
413 | "invalid schedule item (must be [-]<signal-name>, " | |
414 | "-<signal-number>, <timeout> or `forever'"); | |
183843cd DS |
415 | } |
416 | } | |
417 | ||
d62a17ae | 418 | static void parse_schedule(const char *schedule_str) |
419 | { | |
183843cd DS |
420 | char item_buf[20]; |
421 | const char *slash; | |
422 | int count, repeatat; | |
423 | ptrdiff_t str_len; | |
424 | ||
425 | count = 0; | |
426 | for (slash = schedule_str; *slash; slash++) | |
427 | if (*slash == '/') | |
428 | count++; | |
429 | ||
d62a17ae | 430 | schedule_length = (count == 0) ? 4 : count + 1; |
183843cd DS |
431 | schedule = xmalloc(sizeof(*schedule) * schedule_length); |
432 | ||
433 | if (count == 0) { | |
434 | schedule[0].type = sched_signal; | |
435 | schedule[0].value = signal_nr; | |
436 | parse_schedule_item(schedule_str, &schedule[1]); | |
437 | if (schedule[1].type != sched_timeout) { | |
d62a17ae | 438 | badusage( |
439 | "--retry takes timeout, or schedule list" | |
440 | " of at least two items"); | |
183843cd DS |
441 | } |
442 | schedule[2].type = sched_signal; | |
443 | schedule[2].value = SIGKILL; | |
d62a17ae | 444 | schedule[3] = schedule[1]; |
183843cd DS |
445 | } else { |
446 | count = 0; | |
447 | repeatat = -1; | |
448 | while (schedule_str != NULL) { | |
d62a17ae | 449 | slash = strchr(schedule_str, '/'); |
450 | str_len = slash ? slash - schedule_str | |
451 | : (ptrdiff_t)strlen(schedule_str); | |
183843cd | 452 | if (str_len >= (ptrdiff_t)sizeof(item_buf)) |
d62a17ae | 453 | badusage( |
454 | "invalid schedule item: far too long" | |
455 | " (you must delimit items with slashes)"); | |
183843cd DS |
456 | memcpy(item_buf, schedule_str, str_len); |
457 | item_buf[str_len] = 0; | |
d62a17ae | 458 | schedule_str = slash ? slash + 1 : NULL; |
183843cd DS |
459 | |
460 | parse_schedule_item(item_buf, &schedule[count]); | |
461 | if (schedule[count].type == sched_forever) { | |
462 | if (repeatat >= 0) | |
d62a17ae | 463 | badusage( |
464 | "invalid schedule: `forever'" | |
465 | " appears more than once"); | |
183843cd DS |
466 | repeatat = count; |
467 | continue; | |
468 | } | |
469 | count++; | |
470 | } | |
471 | if (repeatat >= 0) { | |
472 | schedule[count].type = sched_goto; | |
473 | schedule[count].value = repeatat; | |
474 | count++; | |
475 | } | |
476 | assert(count == schedule_length); | |
477 | } | |
478 | } | |
479 | ||
d62a17ae | 480 | static void parse_options(int argc, char *const *argv) |
183843cd DS |
481 | { |
482 | static struct option longopts[] = { | |
d62a17ae | 483 | {"help", 0, NULL, 'H'}, {"stop", 0, NULL, 'K'}, |
484 | {"start", 0, NULL, 'S'}, {"version", 0, NULL, 'V'}, | |
485 | {"startas", 1, NULL, 'a'}, {"name", 1, NULL, 'n'}, | |
486 | {"oknodo", 0, NULL, 'o'}, {"pidfile", 1, NULL, 'p'}, | |
487 | {"quiet", 0, NULL, 'q'}, {"signal", 1, NULL, 's'}, | |
488 | {"test", 0, NULL, 't'}, {"user", 1, NULL, 'u'}, | |
489 | {"chroot", 1, NULL, 'r'}, {"namespace", 1, NULL, 'd'}, | |
490 | {"verbose", 0, NULL, 'v'}, {"exec", 1, NULL, 'x'}, | |
491 | {"chuid", 1, NULL, 'c'}, {"nicelevel", 1, NULL, 'N'}, | |
492 | {"background", 0, NULL, 'b'}, {"make-pidfile", 0, NULL, 'm'}, | |
493 | {"retry", 1, NULL, 'R'}, {NULL, 0, NULL, 0}}; | |
183843cd DS |
494 | int c; |
495 | ||
496 | for (;;) { | |
d62a17ae | 497 | c = getopt_long(argc, argv, |
498 | "HKSVa:n:op:qr:d:s:tu:vx:c:N:bmR:", longopts, | |
499 | (int *)0); | |
183843cd DS |
500 | if (c == -1) |
501 | break; | |
502 | switch (c) { | |
d62a17ae | 503 | case 'H': /* --help */ |
183843cd DS |
504 | do_help(); |
505 | exit(0); | |
d62a17ae | 506 | case 'K': /* --stop */ |
183843cd DS |
507 | stop = 1; |
508 | break; | |
d62a17ae | 509 | case 'S': /* --start */ |
183843cd DS |
510 | start = 1; |
511 | break; | |
d62a17ae | 512 | case 'V': /* --version */ |
183843cd DS |
513 | printf("start-stop-daemon " VERSION "\n"); |
514 | exit(0); | |
d62a17ae | 515 | case 'a': /* --startas <pathname> */ |
183843cd DS |
516 | startas = optarg; |
517 | break; | |
d62a17ae | 518 | case 'n': /* --name <process-name> */ |
183843cd DS |
519 | cmdname = optarg; |
520 | break; | |
d62a17ae | 521 | case 'o': /* --oknodo */ |
183843cd DS |
522 | exitnodo = 0; |
523 | break; | |
d62a17ae | 524 | case 'p': /* --pidfile <pid-file> */ |
183843cd DS |
525 | pidfile = optarg; |
526 | break; | |
d62a17ae | 527 | case 'q': /* --quiet */ |
183843cd DS |
528 | quietmode = 1; |
529 | break; | |
d62a17ae | 530 | case 's': /* --signal <signal> */ |
183843cd DS |
531 | signal_str = optarg; |
532 | break; | |
d62a17ae | 533 | case 't': /* --test */ |
183843cd DS |
534 | testmode = 1; |
535 | break; | |
d62a17ae | 536 | case 'u': /* --user <username>|<uid> */ |
183843cd DS |
537 | userspec = optarg; |
538 | break; | |
d62a17ae | 539 | case 'v': /* --verbose */ |
183843cd DS |
540 | quietmode = -1; |
541 | break; | |
d62a17ae | 542 | case 'x': /* --exec <executable> */ |
183843cd DS |
543 | execname = optarg; |
544 | break; | |
d62a17ae | 545 | case 'c': /* --chuid <username>|<uid> */ |
44f12f20 | 546 | changeuser = strtok(optarg, ":"); |
183843cd DS |
547 | changegroup = strtok(NULL, ":"); |
548 | break; | |
d62a17ae | 549 | case 'r': /* --chroot /new/root */ |
183843cd DS |
550 | changeroot = optarg; |
551 | break; | |
552 | case 'd': /* --namespace /.../<ipcns>|<netns>|<utsns>/name */ | |
8f500a1c | 553 | #ifdef linux |
183843cd | 554 | add_namespace(optarg); |
8f500a1c | 555 | #endif |
183843cd | 556 | break; |
d62a17ae | 557 | case 'N': /* --nice */ |
183843cd DS |
558 | nicelevel = atoi(optarg); |
559 | break; | |
d62a17ae | 560 | case 'b': /* --background */ |
183843cd DS |
561 | background = 1; |
562 | break; | |
d62a17ae | 563 | case 'm': /* --make-pidfile */ |
183843cd DS |
564 | mpidfile = 1; |
565 | break; | |
d62a17ae | 566 | case 'R': /* --retry <schedule>|<timeout> */ |
183843cd DS |
567 | schedule_str = optarg; |
568 | break; | |
569 | default: | |
d62a17ae | 570 | badusage(NULL); /* message printed by getopt */ |
183843cd DS |
571 | } |
572 | } | |
573 | ||
574 | if (signal_str != NULL) { | |
d62a17ae | 575 | if (parse_signal(signal_str, &signal_nr) != 0) |
576 | badusage( | |
577 | "signal value must be numeric or name" | |
578 | " of signal (KILL, INTR, ...)"); | |
183843cd DS |
579 | } |
580 | ||
581 | if (schedule_str != NULL) { | |
582 | parse_schedule(schedule_str); | |
583 | } | |
584 | ||
585 | if (start == stop) | |
586 | badusage("need one of --start or --stop"); | |
587 | ||
588 | if (!execname && !pidfile && !userspec && !cmdname) | |
d62a17ae | 589 | badusage( |
590 | "need at least one of --exec, --pidfile, --user or --name"); | |
183843cd DS |
591 | |
592 | if (!startas) | |
593 | startas = execname; | |
594 | ||
595 | if (start && !startas) | |
596 | badusage("--start needs --exec or --startas"); | |
597 | ||
598 | if (mpidfile && pidfile == NULL) | |
599 | badusage("--make-pidfile is only relevant with --pidfile"); | |
600 | ||
601 | if (background && !start) | |
602 | badusage("--background is only relevant with --start"); | |
183843cd DS |
603 | } |
604 | ||
d62a17ae | 605 | static int pid_is_exec(pid_t pid, const struct stat *esb) |
183843cd DS |
606 | { |
607 | struct stat sb; | |
608 | char buf[32]; | |
609 | ||
ae9eebca | 610 | sprintf(buf, "/proc/%ld/exe", (long)pid); |
183843cd DS |
611 | if (stat(buf, &sb) != 0) |
612 | return 0; | |
613 | return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino); | |
614 | } | |
615 | ||
616 | ||
d62a17ae | 617 | static int pid_is_user(pid_t pid, uid_t uid) |
183843cd DS |
618 | { |
619 | struct stat sb; | |
620 | char buf[32]; | |
621 | ||
ae9eebca | 622 | sprintf(buf, "/proc/%ld", (long)pid); |
183843cd DS |
623 | if (stat(buf, &sb) != 0) |
624 | return 0; | |
625 | return (sb.st_uid == uid); | |
626 | } | |
627 | ||
628 | ||
d62a17ae | 629 | static int pid_is_cmd(pid_t pid, const char *name) |
183843cd DS |
630 | { |
631 | char buf[32]; | |
632 | FILE *f; | |
633 | int c; | |
634 | ||
ae9eebca | 635 | sprintf(buf, "/proc/%ld/stat", (long)pid); |
183843cd DS |
636 | f = fopen(buf, "r"); |
637 | if (!f) | |
638 | return 0; | |
639 | while ((c = getc(f)) != EOF && c != '(') | |
640 | ; | |
641 | if (c != '(') { | |
642 | fclose(f); | |
643 | return 0; | |
644 | } | |
645 | /* this hopefully handles command names containing ')' */ | |
646 | while ((c = getc(f)) != EOF && c == *name) | |
647 | name++; | |
648 | fclose(f); | |
649 | return (c == ')' && *name == '\0'); | |
650 | } | |
651 | ||
652 | ||
d62a17ae | 653 | static void check(pid_t pid) |
183843cd DS |
654 | { |
655 | if (execname && !pid_is_exec(pid, &exec_stat)) | |
656 | return; | |
657 | if (userspec && !pid_is_user(pid, user_id)) | |
658 | return; | |
659 | if (cmdname && !pid_is_cmd(pid, cmdname)) | |
660 | return; | |
661 | push(&found, pid); | |
662 | } | |
663 | ||
d62a17ae | 664 | static void do_pidfile(const char *name) |
183843cd DS |
665 | { |
666 | FILE *f; | |
ae9eebca | 667 | long pid; |
183843cd DS |
668 | |
669 | f = fopen(name, "r"); | |
670 | if (f) { | |
ae9eebca DL |
671 | if (fscanf(f, "%ld", &pid) == 1) |
672 | check((pid_t)pid); | |
183843cd DS |
673 | fclose(f); |
674 | } else if (errno != ENOENT) | |
675 | fatal("open pidfile %s: %s", name, strerror(errno)); | |
183843cd DS |
676 | } |
677 | ||
678 | /* WTA: this needs to be an autoconf check for /proc/pid existance. | |
679 | */ | |
d62a17ae | 680 | static void do_procinit(void) |
183843cd DS |
681 | { |
682 | DIR *procdir; | |
683 | struct dirent *entry; | |
684 | int foundany; | |
ae9eebca | 685 | long pid; |
183843cd DS |
686 | |
687 | procdir = opendir("/proc"); | |
688 | if (!procdir) | |
689 | fatal("opendir /proc: %s", strerror(errno)); | |
690 | ||
691 | foundany = 0; | |
692 | while ((entry = readdir(procdir)) != NULL) { | |
ae9eebca | 693 | if (sscanf(entry->d_name, "%ld", &pid) != 1) |
183843cd DS |
694 | continue; |
695 | foundany++; | |
ae9eebca | 696 | check((pid_t)pid); |
183843cd DS |
697 | } |
698 | closedir(procdir); | |
699 | if (!foundany) | |
700 | fatal("nothing in /proc - not mounted?"); | |
701 | } | |
702 | ||
d62a17ae | 703 | static void do_findprocs(void) |
183843cd DS |
704 | { |
705 | clear(&found); | |
d62a17ae | 706 | |
183843cd DS |
707 | if (pidfile) |
708 | do_pidfile(pidfile); | |
709 | else | |
710 | do_procinit(); | |
711 | } | |
712 | ||
713 | /* return 1 on failure */ | |
d62a17ae | 714 | static void do_stop(int signal_nr, int quietmode, int *n_killed, |
715 | int *n_notkilled, int retry_nr) | |
183843cd DS |
716 | { |
717 | struct pid_list *p; | |
718 | ||
d62a17ae | 719 | do_findprocs(); |
720 | ||
721 | *n_killed = 0; | |
722 | *n_notkilled = 0; | |
723 | ||
724 | if (!found) | |
725 | return; | |
726 | ||
727 | clear(&killed); | |
183843cd DS |
728 | |
729 | for (p = found; p; p = p->next) { | |
730 | if (testmode) | |
ae9eebca DL |
731 | printf("Would send signal %d to %ld.\n", signal_nr, |
732 | (long)p->pid); | |
d62a17ae | 733 | else if (kill(p->pid, signal_nr) == 0) { |
183843cd | 734 | push(&killed, p->pid); |
d62a17ae | 735 | (*n_killed)++; |
183843cd | 736 | } else { |
ae9eebca DL |
737 | printf("%s: warning: failed to kill %ld: %s\n", |
738 | progname, (long)p->pid, strerror(errno)); | |
d62a17ae | 739 | (*n_notkilled)++; |
183843cd DS |
740 | } |
741 | } | |
742 | if (quietmode < 0 && killed) { | |
d62a17ae | 743 | printf("Stopped %s (pid", what_stop); |
183843cd | 744 | for (p = killed; p; p = p->next) |
ae9eebca | 745 | printf(" %ld", (long)p->pid); |
d62a17ae | 746 | putchar(')'); |
747 | if (retry_nr > 0) | |
748 | printf(", retry #%d", retry_nr); | |
749 | printf(".\n"); | |
183843cd DS |
750 | } |
751 | } | |
752 | ||
753 | ||
d62a17ae | 754 | static void set_what_stop(const char *str) |
183843cd DS |
755 | { |
756 | strncpy(what_stop, str, sizeof(what_stop)); | |
d62a17ae | 757 | what_stop[sizeof(what_stop) - 1] = '\0'; |
183843cd DS |
758 | } |
759 | ||
d62a17ae | 760 | static int run_stop_schedule(void) |
183843cd | 761 | { |
d62a17ae | 762 | int r, position, n_killed, n_notkilled, value, ratio, anykilled, |
763 | retry_nr; | |
183843cd DS |
764 | struct timeval stopat, before, after, interval, maxinterval; |
765 | ||
766 | if (testmode) { | |
767 | if (schedule != NULL) { | |
768 | printf("Ignoring --retry in test mode\n"); | |
769 | schedule = NULL; | |
770 | } | |
771 | } | |
772 | ||
773 | if (cmdname) | |
774 | set_what_stop(cmdname); | |
775 | else if (execname) | |
776 | set_what_stop(execname); | |
777 | else if (pidfile) | |
778 | sprintf(what_stop, "process in pidfile `%.200s'", pidfile); | |
779 | else if (userspec) | |
780 | sprintf(what_stop, "process(es) owned by `%.200s'", userspec); | |
781 | else | |
782 | fatal("internal error, please report"); | |
783 | ||
784 | anykilled = 0; | |
785 | retry_nr = 0; | |
c604467a | 786 | n_killed = 0; |
183843cd DS |
787 | |
788 | if (schedule == NULL) { | |
789 | do_stop(signal_nr, quietmode, &n_killed, &n_notkilled, 0); | |
790 | if (n_notkilled > 0 && quietmode <= 0) | |
791 | printf("%d pids were not killed\n", n_notkilled); | |
792 | if (n_killed) | |
793 | anykilled = 1; | |
794 | goto x_finished; | |
795 | } | |
796 | ||
d62a17ae | 797 | for (position = 0; position < schedule_length;) { |
798 | value = schedule[position].value; | |
183843cd DS |
799 | n_notkilled = 0; |
800 | ||
801 | switch (schedule[position].type) { | |
802 | ||
803 | case sched_goto: | |
804 | position = value; | |
805 | continue; | |
806 | ||
807 | case sched_signal: | |
d62a17ae | 808 | do_stop(value, quietmode, &n_killed, &n_notkilled, |
809 | retry_nr++); | |
183843cd DS |
810 | if (!n_killed) |
811 | goto x_finished; | |
812 | else | |
813 | anykilled = 1; | |
814 | goto next_item; | |
815 | ||
816 | case sched_timeout: | |
d62a17ae | 817 | /* We want to keep polling for the processes, to see if |
818 | * they've exited, | |
819 | * or until the timeout expires. | |
820 | * | |
821 | * This is a somewhat complicated algorithm to try to | |
822 | * ensure that we | |
823 | * notice reasonably quickly when all the processes have | |
824 | * exited, but | |
825 | * don't spend too much CPU time polling. In | |
826 | * particular, on a fast | |
827 | * machine with quick-exiting daemons we don't want to | |
828 | * delay system | |
829 | * shutdown too much, whereas on a slow one, or where | |
830 | * processes are | |
831 | * taking some time to exit, we want to increase the | |
832 | * polling | |
833 | * interval. | |
834 | * | |
835 | * The algorithm is as follows: we measure the elapsed | |
836 | * time it takes | |
837 | * to do one poll(), and wait a multiple of this time | |
838 | * for the next | |
839 | * poll. However, if that would put us past the end of | |
840 | * the timeout | |
841 | * period we wait only as long as the timeout period, | |
842 | * but in any case | |
843 | * we always wait at least MIN_POLL_INTERVAL (20ms). | |
844 | * The multiple | |
845 | * (`ratio') starts out as 2, and increases by 1 for | |
846 | * each poll to a | |
847 | * maximum of 10; so we use up to between 30% and 10% of | |
848 | * the | |
849 | * machine's resources (assuming a few reasonable things | |
850 | * about system | |
851 | * performance). | |
852 | */ | |
183843cd DS |
853 | xgettimeofday(&stopat); |
854 | stopat.tv_sec += value; | |
855 | ratio = 1; | |
856 | for (;;) { | |
857 | xgettimeofday(&before); | |
d62a17ae | 858 | if (timercmp(&before, &stopat, >)) |
183843cd DS |
859 | goto next_item; |
860 | ||
861 | do_stop(0, 1, &n_killed, &n_notkilled, 0); | |
862 | if (!n_killed) | |
863 | goto x_finished; | |
864 | ||
865 | xgettimeofday(&after); | |
866 | ||
d62a17ae | 867 | if (!timercmp(&after, &stopat, <)) |
183843cd DS |
868 | goto next_item; |
869 | ||
870 | if (ratio < 10) | |
871 | ratio++; | |
872 | ||
9d303b37 DL |
873 | TVCALC(interval, |
874 | ratio * (TVELEM(&after) - TVELEM(&before) | |
875 | + TVADJUST)); | |
876 | TVCALC(maxinterval, | |
877 | TVELEM(&stopat) - TVELEM(&after) | |
878 | + TVADJUST); | |
183843cd | 879 | |
d62a17ae | 880 | if (timercmp(&interval, &maxinterval, >)) |
183843cd DS |
881 | interval = maxinterval; |
882 | ||
d62a17ae | 883 | if (interval.tv_sec == 0 |
884 | && interval.tv_usec <= MIN_POLL_INTERVAL) | |
885 | interval.tv_usec = MIN_POLL_INTERVAL; | |
183843cd | 886 | |
d62a17ae | 887 | r = select(0, 0, 0, 0, &interval); |
183843cd DS |
888 | if (r < 0 && errno != EINTR) |
889 | fatal("select() failed for pause: %s", | |
890 | strerror(errno)); | |
891 | } | |
892 | ||
893 | default: | |
894 | assert(!"schedule[].type value must be valid"); | |
183843cd DS |
895 | } |
896 | ||
897 | next_item: | |
898 | position++; | |
899 | } | |
900 | ||
901 | if (quietmode <= 0) | |
902 | printf("Program %s, %d process(es), refused to die.\n", | |
903 | what_stop, n_killed); | |
904 | ||
905 | return 2; | |
906 | ||
907 | x_finished: | |
908 | if (!anykilled) { | |
909 | if (quietmode <= 0) | |
d62a17ae | 910 | printf("No %s found running; none killed.\n", |
911 | what_stop); | |
183843cd DS |
912 | return exitnodo; |
913 | } else { | |
914 | return 0; | |
915 | } | |
916 | } | |
917 | ||
918 | /* | |
919 | int main(int argc, char **argv) NONRETURNING; | |
920 | */ | |
921 | ||
d62a17ae | 922 | int main(int argc, char **argv) |
183843cd DS |
923 | { |
924 | progname = argv[0]; | |
925 | ||
926 | LIST_INIT(&namespace_head); | |
927 | ||
928 | parse_options(argc, argv); | |
929 | argc -= optind; | |
930 | argv += optind; | |
931 | ||
932 | if (execname && stat(execname, &exec_stat)) | |
933 | fatal("stat %s: %s", execname, strerror(errno)); | |
934 | ||
935 | if (userspec && sscanf(userspec, "%d", &user_id) != 1) { | |
936 | struct passwd *pw; | |
937 | ||
938 | pw = getpwnam(userspec); | |
939 | if (!pw) | |
940 | fatal("user `%s' not found\n", userspec); | |
941 | ||
942 | user_id = pw->pw_uid; | |
943 | } | |
944 | ||
945 | if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) { | |
946 | struct group *gr = getgrnam(changegroup); | |
947 | if (!gr) | |
948 | fatal("group `%s' not found\n", changegroup); | |
949 | runas_gid = gr->gr_gid; | |
950 | } | |
951 | if (changeuser && sscanf(changeuser, "%d", &runas_uid) != 1) { | |
952 | struct passwd *pw = getpwnam(changeuser); | |
953 | if (!pw) | |
954 | fatal("user `%s' not found\n", changeuser); | |
955 | runas_uid = pw->pw_uid; | |
d62a17ae | 956 | if (changegroup |
957 | == NULL) { /* pass the default group of this user */ | |
183843cd DS |
958 | changegroup = ""; /* just empty */ |
959 | runas_gid = pw->pw_gid; | |
960 | } | |
961 | } | |
962 | ||
963 | if (stop) { | |
964 | int i = run_stop_schedule(); | |
965 | exit(i); | |
966 | } | |
967 | ||
968 | do_findprocs(); | |
969 | ||
970 | if (found) { | |
971 | if (quietmode <= 0) | |
972 | printf("%s already running.\n", execname); | |
973 | exit(exitnodo); | |
974 | } | |
975 | if (testmode) { | |
976 | printf("Would start %s ", startas); | |
977 | while (argc-- > 0) | |
978 | printf("%s ", *argv++); | |
979 | if (changeuser != NULL) { | |
980 | printf(" (as user %s[%d]", changeuser, runas_uid); | |
981 | if (changegroup != NULL) | |
d62a17ae | 982 | printf(", and group %s[%d])", changegroup, |
983 | runas_gid); | |
183843cd DS |
984 | else |
985 | printf(")"); | |
986 | } | |
987 | if (changeroot != NULL) | |
988 | printf(" in directory %s", changeroot); | |
989 | if (nicelevel) | |
990 | printf(", and add %i to the priority", nicelevel); | |
991 | printf(".\n"); | |
992 | exit(0); | |
993 | } | |
994 | if (quietmode < 0) | |
995 | printf("Starting %s...\n", startas); | |
996 | *--argv = startas; | |
997 | if (changeroot != NULL) { | |
998 | if (chdir(changeroot) < 0) | |
999 | fatal("Unable to chdir() to %s", changeroot); | |
1000 | if (chroot(changeroot) < 0) | |
1001 | fatal("Unable to chroot() to %s", changeroot); | |
1002 | } | |
1003 | if (changeuser != NULL) { | |
d62a17ae | 1004 | if (setgid(runas_gid)) |
1005 | fatal("Unable to set gid to %d", runas_gid); | |
183843cd | 1006 | if (initgroups(changeuser, runas_gid)) |
d62a17ae | 1007 | fatal("Unable to set initgroups() with gid %d", |
1008 | runas_gid); | |
183843cd DS |
1009 | if (setuid(runas_uid)) |
1010 | fatal("Unable to set uid to %s", changeuser); | |
1011 | } | |
1012 | ||
1013 | if (background) { /* ok, we need to detach this process */ | |
1014 | int i, fd; | |
1015 | if (quietmode < 0) | |
0437e105 | 1016 | printf("Detaching to start %s...", startas); |
183843cd | 1017 | i = fork(); |
d62a17ae | 1018 | if (i < 0) { |
183843cd DS |
1019 | fatal("Unable to fork.\n"); |
1020 | } | |
1021 | if (i) { /* parent */ | |
1022 | if (quietmode < 0) | |
1023 | printf("done.\n"); | |
1024 | exit(0); | |
1025 | } | |
d62a17ae | 1026 | /* child continues here */ |
1027 | /* now close all extra fds */ | |
1028 | for (i = getdtablesize() - 1; i >= 0; --i) | |
1029 | close(i); | |
1030 | /* change tty */ | |
183843cd | 1031 | fd = open("/dev/tty", O_RDWR); |
9b606d6c | 1032 | if (fd >= 0) { |
b0bde9f3 A |
1033 | if (ioctl(fd, TIOCNOTTY, 0) < 0) |
1034 | printf("ioctl TIOCNOTTY failed: %s\n", | |
1035 | strerror(errno)); | |
9b606d6c | 1036 | close(fd); |
1037 | } | |
183843cd | 1038 | chdir("/"); |
d62a17ae | 1039 | umask(022); /* set a default for dumb programs */ |
1040 | setpgid(0, 0); /* set the process group */ | |
1041 | fd = open("/dev/null", O_RDWR); /* stdin */ | |
657f9948 | 1042 | if (fd >= 0) { |
1043 | dup(fd); /* stdout */ | |
1044 | dup(fd); /* stderr */ | |
1045 | } | |
183843cd DS |
1046 | } |
1047 | if (nicelevel) { | |
1048 | errno = 0; | |
1049 | if (nice(nicelevel) < 0 && errno) | |
1050 | fatal("Unable to alter nice level by %i: %s", nicelevel, | |
d62a17ae | 1051 | strerror(errno)); |
183843cd | 1052 | } |
d62a17ae | 1053 | if (mpidfile |
1054 | && pidfile != NULL) { /* user wants _us_ to make the pidfile :) */ | |
183843cd DS |
1055 | FILE *pidf = fopen(pidfile, "w"); |
1056 | pid_t pidt = getpid(); | |
1057 | if (pidf == NULL) | |
d62a17ae | 1058 | fatal("Unable to open pidfile `%s' for writing: %s", |
1059 | pidfile, strerror(errno)); | |
ae9eebca | 1060 | fprintf(pidf, "%ld\n", (long)pidt); |
183843cd DS |
1061 | fclose(pidf); |
1062 | } | |
1063 | set_namespaces(); | |
1064 | execv(startas, argv); | |
1065 | fatal("Unable to start %s: %s", startas, strerror(errno)); | |
1066 | } |