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