watchdogd.c revision 128705
1/* 2 * Copyright (c) 2003 Sean M. Kelly <smkelly@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Software watchdog daemon. 29 */ 30 31#include <sys/types.h> 32__FBSDID("$FreeBSD: head/usr.sbin/watchdogd/watchdogd.c 128705 2004-04-28 07:35:03Z smkelly $"); 33 34#include <sys/rtprio.h> 35#include <sys/stat.h> 36#include <sys/time.h> 37#include <sys/watchdog.h> 38 39#include <err.h> 40#include <errno.h> 41#include <fcntl.h> 42#include <math.h> 43#include <paths.h> 44#include <signal.h> 45#include <stdio.h> 46#include <stdlib.h> 47#include <string.h> 48#include <sysexits.h> 49#include <unistd.h> 50 51static void parseargs(int, char *[]); 52static void sighandler(int); 53static void watchdog_loop(void); 54static int watchdog_init(void); 55static int watchdog_onoff(int onoff); 56static int watchdog_patpat(void); 57static void usage(void); 58 59int debugging = 0; 60int end_program = 0; 61const char *pidfile = _PATH_VARRUN "watchdogd.pid"; 62int reset_mib[3]; 63size_t reset_miblen = 3; 64u_int timeout = WD_TO_16SEC; 65u_int passive = 0; 66int is_daemon = 0; 67int fd = -1; 68int nap = 1; 69char *test_cmd = NULL; 70 71/* 72 * Periodically pat the watchdog, preventing it from firing. 73 */ 74int 75main(int argc, char *argv[]) 76{ 77 struct rtprio rtp; 78 FILE *fp; 79 80 if (getuid() != 0) 81 errx(EX_SOFTWARE, "not super user"); 82 83 parseargs(argc, argv); 84 85 rtp.type = RTP_PRIO_REALTIME; 86 rtp.prio = 0; 87 if (rtprio(RTP_SET, 0, &rtp) == -1) 88 err(EX_OSERR, "rtprio"); 89 90 if (watchdog_init() == -1) 91 errx(EX_SOFTWARE, "unable to initialize watchdog"); 92 93 if (is_daemon) { 94 if (watchdog_onoff(1) == -1) 95 exit(EX_SOFTWARE); 96 97 if (debugging == 0 && daemon(0, 0) == -1) { 98 watchdog_onoff(0); 99 err(EX_OSERR, "daemon"); 100 } 101 102 signal(SIGHUP, SIG_IGN); 103 signal(SIGINT, sighandler); 104 signal(SIGTERM, sighandler); 105 106 fp = fopen(pidfile, "w"); 107 if (fp != NULL) { 108 fprintf(fp, "%d\n", getpid()); 109 fclose(fp); 110 } 111 112 watchdog_loop(); 113 114 /* exiting */ 115 watchdog_onoff(0); 116 unlink(pidfile); 117 return (EX_OK); 118 } else { 119 if (passive) 120 timeout |= WD_PASSIVE; 121 else 122 timeout |= WD_ACTIVE; 123 if (watchdog_patpat() < 0) 124 err(EX_OSERR, "patting the dog"); 125 return (EX_OK); 126 } 127} 128 129/* 130 * Catch signals and begin shutdown process. 131 */ 132static void 133sighandler(int signum) 134{ 135 136 if (signum == SIGINT || signum == SIGTERM) 137 end_program = 1; 138} 139 140/* 141 * Open the watchdog device. 142 */ 143static int 144watchdog_init() 145{ 146 147 fd = open("/dev/" _PATH_WATCHDOG, O_RDWR); 148 if (fd >= 0) 149 return (0); 150 warn("Could not open watchdog device"); 151 return (-1); 152} 153 154/* 155 * Main program loop which is iterated every second. 156 */ 157static void 158watchdog_loop(void) 159{ 160 struct stat sb; 161 int failed; 162 163 while (end_program == 0) { 164 failed = 0; 165 166 if (test_cmd != NULL) 167 failed = system(test_cmd); 168 else 169 failed = stat("/etc", &sb); 170 171 if (failed == 0) 172 watchdog_patpat(); 173 sleep(nap); 174 } 175} 176 177/* 178 * Reset the watchdog timer. This function must be called periodically 179 * to keep the watchdog from firing. 180 */ 181int 182watchdog_patpat(void) 183{ 184 185 return ioctl(fd, WDIOCPATPAT, &timeout); 186} 187 188/* 189 * Toggle the kernel's watchdog. This routine is used to enable and 190 * disable the watchdog. 191 */ 192static int 193watchdog_onoff(int onoff) 194{ 195 196 if (onoff) 197 timeout |= WD_ACTIVE; 198 else 199 timeout &= ~WD_ACTIVE; 200 return watchdog_patpat(); 201} 202 203/* 204 * Tell user how to use the program. 205 */ 206static void 207usage() 208{ 209 if (is_daemon) 210 fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file]\n"); 211 else 212 fprintf(stderr, "usage: watchdog [-d] [-t]\n"); 213 exit(EX_USAGE); 214} 215 216/* 217 * Handle the few command line arguments supported. 218 */ 219static void 220parseargs(int argc, char *argv[]) 221{ 222 int c; 223 char *p; 224 double a; 225 226 c = strlen(argv[0]); 227 if (argv[0][c - 1] == 'd') 228 is_daemon = 1; 229 while ((c = getopt(argc, argv, 230 is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) { 231 switch (c) { 232 case 'I': 233 pidfile = optarg; 234 break; 235 case 'd': 236 debugging = 1; 237 break; 238 case 'e': 239 test_cmd = strdup(optarg); 240 break; 241#ifdef notyet 242 case 'p': 243 passive = 1; 244 break; 245#endif 246 case 's': 247 p = NULL; 248 errno = 0; 249 nap = strtol(optarg, &p, 0); 250 if ((p != NULL && *p != '\0') || errno != 0) 251 errx(EX_USAGE, "-s argument is not a number"); 252 break; 253 case 't': 254 p = NULL; 255 errno = 0; 256 a = strtod(optarg, &p); 257 if ((p != NULL && *p != '\0') || errno != 0) 258 errx(EX_USAGE, "-t argument is not a number"); 259 if (a < 0) 260 errx(EX_USAGE, "-t argument must be positive"); 261 if (a == 0) 262 timeout = WD_TO_NEVER; 263 else 264 timeout = 1.0 + log(a * 1e9) / log(2.0); 265 if (debugging) 266 printf("Timeout is 2^%d nanoseconds\n", 267 timeout); 268 break; 269 case '?': 270 default: 271 usage(); 272 /* NOTREACHED */ 273 } 274 } 275 if (is_daemon && timeout < WD_TO_1SEC) 276 errx(EX_USAGE, "-t argument is less than one second."); 277} 278