debuggers.hg

annotate tools/misc/xenwatchdogd.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 9c98ae6e23b2
children
rev   line source
keir@21575 1
keir@21575 2 #include <err.h>
keir@21575 3 #include <limits.h>
keir@21575 4 #include "xenctrl.h"
keir@21575 5 #include <sys/types.h>
keir@21575 6 #include <sys/stat.h>
keir@21575 7 #include <fcntl.h>
keir@21575 8 #include <stdlib.h>
keir@21575 9 #include <unistd.h>
keir@21575 10 #include <signal.h>
keir@21575 11 #include <stdio.h>
keir@21575 12
keir@21575 13 xc_interface *h;
keir@21575 14 int id = 0;
keir@21575 15
keir@21575 16 void daemonize(void)
keir@21575 17 {
keir@21575 18 switch (fork()) {
keir@21575 19 case -1:
keir@21575 20 err(1, "fork");
keir@21575 21 case 0:
keir@21575 22 break;
keir@21575 23 default:
keir@21575 24 exit(0);
keir@21575 25 }
keir@21575 26 umask(0);
keir@21575 27 if (setsid() < 0)
keir@21575 28 err(1, "setsid");
keir@21575 29 if (chdir("/") < 0)
keir@21575 30 err(1, "chdir /");
keir@21586 31 if (freopen("/dev/null", "r", stdin) == NULL)
keir@21586 32 err(1, "reopen stdin");
keir@21586 33 if(freopen("/dev/null", "w", stdout) == NULL)
keir@21586 34 err(1, "reopen stdout");
keir@21586 35 if(freopen("/dev/null", "w", stderr) == NULL)
keir@21586 36 err(1, "reopen stderr");
keir@21575 37 }
keir@21575 38
keir@21575 39 void catch_exit(int sig)
keir@21575 40 {
keir@21575 41 if (id)
keir@21575 42 xc_watchdog(h, id, 300);
keir@21575 43 exit(0);
keir@21575 44 }
keir@21575 45
keir@21575 46 void catch_usr1(int sig)
keir@21575 47 {
keir@21575 48 if (id)
keir@21575 49 xc_watchdog(h, id, 0);
keir@21575 50 exit(0);
keir@21575 51 }
keir@21575 52
keir@21575 53 int main(int argc, char **argv)
keir@21575 54 {
keir@21575 55 int t, s;
keir@21575 56 int ret;
keir@21575 57
keir@21575 58 if (argc < 2)
keir@21575 59 errx(1, "usage: %s <timeout> <sleep>", argv[0]);
keir@21575 60
keir@21575 61 daemonize();
keir@21575 62
keir@21575 63 h = xc_interface_open(NULL, NULL, 0);
keir@21575 64 if (h == NULL)
keir@21575 65 err(1, "xc_interface_open");
keir@21575 66
keir@21575 67 t = strtoul(argv[1], NULL, 0);
keir@21575 68 if (t == ULONG_MAX)
keir@21575 69 err(1, "strtoul");
keir@21575 70
keir@21575 71 s = t / 2;
keir@21575 72 if (argc == 3) {
keir@21575 73 s = strtoul(argv[2], NULL, 0);
keir@21575 74 if (s == ULONG_MAX)
keir@21575 75 err(1, "strtoul");
keir@21575 76 }
keir@21575 77
keir@21575 78 if (signal(SIGHUP, &catch_exit) == SIG_ERR)
keir@21575 79 err(1, "signal");
keir@21575 80 if (signal(SIGINT, &catch_exit) == SIG_ERR)
keir@21575 81 err(1, "signal");
keir@21575 82 if (signal(SIGQUIT, &catch_exit) == SIG_ERR)
keir@21575 83 err(1, "signal");
keir@21575 84 if (signal(SIGTERM, &catch_exit) == SIG_ERR)
keir@21575 85 err(1, "signal");
keir@21575 86 if (signal(SIGUSR1, &catch_usr1) == SIG_ERR)
keir@21575 87 err(1, "signal");
keir@21575 88
keir@21575 89 id = xc_watchdog(h, 0, t);
keir@21575 90 if (id <= 0)
keir@21575 91 err(1, "xc_watchdog setup");
keir@21575 92
keir@21575 93 for (;;) {
keir@21575 94 sleep(s);
keir@21575 95 ret = xc_watchdog(h, id, t);
keir@21575 96 if (ret != 0)
keir@21575 97 err(1, "xc_watchdog");
keir@21575 98 }
keir@21575 99 }