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