Home | History | Annotate | Line # | Download | only in wdogctl
wdogctl.c revision 1.2
      1  1.2  minoura /*	$NetBSD: wdogctl.c,v 1.2 2000/11/06 07:48:28 minoura Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2000 Zembu Labs, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
      8  1.1  thorpej  *
      9  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1  thorpej  * modification, are permitted provided that the following conditions
     11  1.1  thorpej  * are met:
     12  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1  thorpej  *    must display the following acknowledgement:
     19  1.1  thorpej  *	This product includes software developed by Zembu Labs, Inc.
     20  1.1  thorpej  * 4. Neither the name of Zembu Labs nor the names of its employees may
     21  1.1  thorpej  *    be used to endorse or promote products derived from this software
     22  1.1  thorpej  *    without specific prior written permission.
     23  1.1  thorpej  *
     24  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     25  1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     26  1.1  thorpej  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     27  1.1  thorpej  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1  thorpej  */
     35  1.1  thorpej 
     36  1.1  thorpej #include <sys/param.h>
     37  1.1  thorpej #include <sys/ioctl.h>
     38  1.1  thorpej #include <sys/wdog.h>
     39  1.1  thorpej 
     40  1.1  thorpej #include <err.h>
     41  1.1  thorpej #include <errno.h>
     42  1.1  thorpej #include <fcntl.h>
     43  1.1  thorpej #include <stdio.h>
     44  1.1  thorpej #include <stdlib.h>
     45  1.1  thorpej #include <time.h>
     46  1.1  thorpej #include <signal.h>
     47  1.1  thorpej #include <syslog.h>
     48  1.1  thorpej #include <unistd.h>
     49  1.2  minoura #include <string.h>
     50  1.1  thorpej 
     51  1.1  thorpej #define	_PATH_WATCHDOG		"/dev/watchdog"
     52  1.1  thorpej 
     53  1.1  thorpej int	main(int, char *[]);
     54  1.1  thorpej void	enable_kernel(const char *, u_int);
     55  1.1  thorpej void	enable_user(const char *, u_int);
     56  1.1  thorpej void	disable(void);
     57  1.1  thorpej void	list_timers(void);
     58  1.1  thorpej void	usage(void);
     59  1.1  thorpej 
     60  1.1  thorpej int	Aflag;
     61  1.1  thorpej 
     62  1.1  thorpej int
     63  1.1  thorpej main(int argc, char *argv[])
     64  1.1  thorpej {
     65  1.1  thorpej 	int cmds = 0, kflag = 0, uflag = 0, dflag = 0, ch;
     66  1.1  thorpej 	u_int period = WDOG_PERIOD_DEFAULT;
     67  1.1  thorpej 
     68  1.1  thorpej 	while ((ch = getopt(argc, argv, "Adkp:u")) != -1) {
     69  1.1  thorpej 		switch (ch) {
     70  1.1  thorpej 		case 'A':
     71  1.1  thorpej 			if (cmds == 0 || dflag)
     72  1.1  thorpej 				usage();
     73  1.1  thorpej 			Aflag = 1;
     74  1.1  thorpej 			break;
     75  1.1  thorpej 
     76  1.1  thorpej 		case 'd':
     77  1.1  thorpej 			dflag = 1;
     78  1.1  thorpej 			cmds++;
     79  1.1  thorpej 			break;
     80  1.1  thorpej 
     81  1.1  thorpej 		case 'k':
     82  1.1  thorpej 			kflag = 1;
     83  1.1  thorpej 			cmds++;
     84  1.1  thorpej 			break;
     85  1.1  thorpej 
     86  1.1  thorpej 		case 'p':
     87  1.1  thorpej 			if (cmds == 0 || dflag)
     88  1.1  thorpej 				usage();
     89  1.1  thorpej 			period = atoi(optarg);
     90  1.1  thorpej 			if (period == -1)
     91  1.1  thorpej 				usage();
     92  1.1  thorpej 			break;
     93  1.1  thorpej 
     94  1.1  thorpej 		case 'u':
     95  1.1  thorpej 			uflag = 1;
     96  1.1  thorpej 			cmds++;
     97  1.1  thorpej 			break;
     98  1.1  thorpej 
     99  1.1  thorpej 		default:
    100  1.1  thorpej 			usage();
    101  1.1  thorpej 		}
    102  1.1  thorpej 	}
    103  1.1  thorpej 
    104  1.1  thorpej 	argc -= optind;
    105  1.1  thorpej 	argv += optind;
    106  1.1  thorpej 
    107  1.1  thorpej 	if (cmds > 1)
    108  1.1  thorpej 		usage();
    109  1.1  thorpej 
    110  1.1  thorpej 	if (kflag) {
    111  1.1  thorpej 		if (argc != 1)
    112  1.1  thorpej 			usage();
    113  1.1  thorpej 		enable_kernel(argv[0], period);
    114  1.1  thorpej 	} else if (uflag) {
    115  1.1  thorpej 		if (argc != 1)
    116  1.1  thorpej 			usage();
    117  1.1  thorpej 		enable_user(argv[0], period);
    118  1.1  thorpej 	} else if (dflag) {
    119  1.1  thorpej 		if (argc != 0)
    120  1.1  thorpej 			usage();
    121  1.1  thorpej 		disable();
    122  1.1  thorpej 	} else
    123  1.1  thorpej 		list_timers();
    124  1.1  thorpej 
    125  1.1  thorpej 	exit(0);
    126  1.1  thorpej }
    127  1.1  thorpej 
    128  1.1  thorpej void
    129  1.1  thorpej enable_kernel(const char *name, u_int period)
    130  1.1  thorpej {
    131  1.1  thorpej 	struct wdog_mode wm;
    132  1.1  thorpej 	int fd;
    133  1.1  thorpej 
    134  1.1  thorpej 	if (strlen(name) >= WDOG_NAMESIZE)
    135  1.1  thorpej 		errx(1, "invalid watchdog timer name: %s", name);
    136  1.1  thorpej 	strcpy(wm.wm_name, name);
    137  1.1  thorpej 	wm.wm_mode = WDOG_MODE_KTICKLE;
    138  1.1  thorpej 	wm.wm_period = period;
    139  1.1  thorpej 
    140  1.1  thorpej 	if (Aflag)
    141  1.1  thorpej 		wm.wm_mode |= WDOG_FEATURE_ALARM;
    142  1.1  thorpej 
    143  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    144  1.1  thorpej 	if (fd == -1)
    145  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    146  1.1  thorpej 
    147  1.1  thorpej 	if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
    148  1.1  thorpej 		err(1, "WDOGIOC_SMODE");
    149  1.1  thorpej }
    150  1.1  thorpej 
    151  1.1  thorpej void
    152  1.1  thorpej enable_user(const char *name, u_int period)
    153  1.1  thorpej {
    154  1.1  thorpej 	extern const char *__progname;
    155  1.1  thorpej 	struct wdog_mode wm;
    156  1.1  thorpej 	struct timespec ts;
    157  1.1  thorpej 	pid_t tickler;
    158  1.1  thorpej 	int fd, rv;
    159  1.1  thorpej 
    160  1.1  thorpej 	if (strlen(name) >= WDOG_NAMESIZE)
    161  1.1  thorpej 		errx(1, "invalid watchdog timer name: %s", name);
    162  1.1  thorpej 	strcpy(wm.wm_name, name);
    163  1.1  thorpej 	wm.wm_mode = WDOG_MODE_UTICKLE;
    164  1.1  thorpej 	wm.wm_period = period;
    165  1.1  thorpej 
    166  1.1  thorpej 	if (Aflag)
    167  1.1  thorpej 		wm.wm_mode |= WDOG_FEATURE_ALARM;
    168  1.1  thorpej 
    169  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    170  1.1  thorpej 	if (fd == -1)
    171  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    172  1.1  thorpej 
    173  1.1  thorpej 	/* ...so we can log failures to tickle the timer. */
    174  1.1  thorpej 	openlog(__progname, LOG_CONS|LOG_PERROR|LOG_PID, LOG_DAEMON);
    175  1.1  thorpej 
    176  1.1  thorpej 	/*
    177  1.1  thorpej 	 * We fork a child process which detaches from the controlling
    178  1.1  thorpej 	 * terminal once the timer is armed, and tickles the timer
    179  1.1  thorpej 	 * until we send it a SIGTERM.
    180  1.1  thorpej 	 */
    181  1.1  thorpej 	tickler = fork();
    182  1.1  thorpej 	if (tickler == -1)
    183  1.1  thorpej 		err(1, "unable to fork tickler process");
    184  1.1  thorpej 	else if (tickler != 0) {
    185  1.1  thorpej 		if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1) {
    186  1.1  thorpej 			err(1, "WDOGIOC_SMODE");
    187  1.1  thorpej 			(void) kill(tickler, SIGTERM);
    188  1.1  thorpej 		}
    189  1.1  thorpej 		(void) close(fd);
    190  1.1  thorpej 		return;
    191  1.1  thorpej 	}
    192  1.1  thorpej 
    193  1.1  thorpej 
    194  1.1  thorpej 	/*
    195  1.1  thorpej 	 * Wait for the watchdog to be armed.  When it is, loop,
    196  1.1  thorpej 	 * tickling the timer, then waiting 1/2 the period before
    197  1.1  thorpej 	 * doing it again.
    198  1.1  thorpej 	 *
    199  1.1  thorpej 	 * If the parent fails to enable the watchdog, it will kill
    200  1.1  thorpej 	 * us.
    201  1.1  thorpej 	 */
    202  1.1  thorpej 	do {
    203  1.1  thorpej 		rv = ioctl(fd, WDOGIOC_WHICH, &wm);
    204  1.1  thorpej 	} while (rv == -1);
    205  1.1  thorpej 
    206  1.1  thorpej 	if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    207  1.1  thorpej 		syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
    208  1.1  thorpej 		    wm.wm_name);
    209  1.1  thorpej 
    210  1.1  thorpej 	/*
    211  1.1  thorpej 	 * Now detach from the controlling terminal, and just run
    212  1.1  thorpej 	 * in the background.  The kernel will keep track of who
    213  1.1  thorpej 	 * we are, each time we tickle the timer.
    214  1.1  thorpej 	 */
    215  1.1  thorpej 	if (daemon(0, 0) == -1) {
    216  1.1  thorpej 		/*
    217  1.1  thorpej 		 * We weren't able to go into the background.  When
    218  1.1  thorpej 		 * we exit, the kernel will disable the watchdog so
    219  1.1  thorpej 		 * that the system won't die.
    220  1.1  thorpej 		 */
    221  1.1  thorpej 		err(1, "unable to deatch from terminal");
    222  1.1  thorpej 	}
    223  1.1  thorpej 
    224  1.1  thorpej 	if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    225  1.1  thorpej 		syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
    226  1.1  thorpej 		    wm.wm_name);
    227  1.1  thorpej 
    228  1.1  thorpej 	for (;;) {
    229  1.1  thorpej 		ts.tv_sec = wm.wm_period / 2;
    230  1.1  thorpej 		ts.tv_nsec = 0;
    231  1.1  thorpej 		(void) nanosleep(&ts, NULL);
    232  1.1  thorpej 
    233  1.1  thorpej 		if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    234  1.1  thorpej 			syslog(LOG_EMERG,
    235  1.1  thorpej 			    "unable to tickle watchdog timer %s: %m",
    236  1.1  thorpej 			    wm.wm_name);
    237  1.1  thorpej 	}
    238  1.1  thorpej 	/* NOTREACHED */
    239  1.1  thorpej }
    240  1.1  thorpej 
    241  1.1  thorpej void
    242  1.1  thorpej disable(void)
    243  1.1  thorpej {
    244  1.1  thorpej 	struct wdog_mode wm;
    245  1.1  thorpej 	pid_t tickler;
    246  1.1  thorpej 	int fd;
    247  1.1  thorpej 
    248  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    249  1.1  thorpej 	if (fd == -1)
    250  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    251  1.1  thorpej 
    252  1.1  thorpej 	if (ioctl(fd, WDOGIOC_WHICH, &wm) == -1) {
    253  1.1  thorpej 		printf("No watchdog timer running.\n");
    254  1.1  thorpej 		return;
    255  1.1  thorpej 	}
    256  1.1  thorpej 
    257  1.1  thorpej 	/*
    258  1.1  thorpej 	 * If the timer is running in UTICKLE mode, we need
    259  1.1  thorpej 	 * to kill the wdogctl(8) process that is tickling
    260  1.1  thorpej 	 * the timer.
    261  1.1  thorpej 	 */
    262  1.1  thorpej 	if (wm.wm_mode == WDOG_MODE_UTICKLE) {
    263  1.1  thorpej 		if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
    264  1.1  thorpej 			err(1, "WDOGIOC_GTICKLER");
    265  1.1  thorpej 		(void) close(fd);
    266  1.1  thorpej 		(void) kill(tickler, SIGTERM);
    267  1.1  thorpej 	} else {
    268  1.1  thorpej 		wm.wm_mode = WDOG_MODE_DISARMED;
    269  1.1  thorpej 		if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
    270  1.1  thorpej 			err(1, "unable to disarm watchdog %s", wm.wm_name);
    271  1.1  thorpej 		(void) close(fd);
    272  1.1  thorpej 	}
    273  1.1  thorpej }
    274  1.1  thorpej 
    275  1.1  thorpej void
    276  1.1  thorpej list_timers(void)
    277  1.1  thorpej {
    278  1.1  thorpej 	struct wdog_conf wc;
    279  1.1  thorpej 	struct wdog_mode wm;
    280  1.1  thorpej 	char *buf, *cp;
    281  1.1  thorpej 	int fd, count, i;
    282  1.1  thorpej 	pid_t tickler;
    283  1.1  thorpej 
    284  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDONLY, 0644);
    285  1.1  thorpej 	if (fd == -1)
    286  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    287  1.1  thorpej 
    288  1.1  thorpej 	wc.wc_names = NULL;
    289  1.1  thorpej 	wc.wc_count = 0;
    290  1.1  thorpej 
    291  1.1  thorpej 	if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
    292  1.1  thorpej 		err(1, "ioctl WDOGIOC_GWDOGS for count");
    293  1.1  thorpej 
    294  1.1  thorpej 	count = wc.wc_count;
    295  1.1  thorpej 	if (count == 0) {
    296  1.1  thorpej 		printf("No watchdog timers present.\n");
    297  1.1  thorpej 		goto out;
    298  1.1  thorpej 	}
    299  1.1  thorpej 
    300  1.1  thorpej 	buf = malloc(count * WDOG_NAMESIZE);
    301  1.1  thorpej 	if (buf == NULL)
    302  1.1  thorpej 		err(1, "malloc %d byte for watchdog names",
    303  1.1  thorpej 		    count * WDOG_NAMESIZE);
    304  1.1  thorpej 
    305  1.1  thorpej 	wc.wc_names = buf;
    306  1.1  thorpej 	if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
    307  1.1  thorpej 		err(1, "ioctl WDOGIOC_GWDOGS for names");
    308  1.1  thorpej 
    309  1.1  thorpej 	count = wc.wc_count;
    310  1.1  thorpej 	if (count == 0) {
    311  1.1  thorpej 		printf("No watchdog timers present.\n");
    312  1.1  thorpej 		free(buf);
    313  1.1  thorpej 		goto out;
    314  1.1  thorpej 	}
    315  1.1  thorpej 
    316  1.1  thorpej 	printf("Available watchdog timers:\n");
    317  1.1  thorpej 	for (i = 0, cp = buf; i < count; i++, cp += WDOG_NAMESIZE) {
    318  1.1  thorpej 		cp[WDOG_NAMESIZE - 1] = '\0';
    319  1.1  thorpej 		strcpy(wm.wm_name, cp);
    320  1.1  thorpej 
    321  1.1  thorpej 		if (ioctl(fd, WDOGIOC_GMODE, &wm) == -1)
    322  1.1  thorpej 			wm.wm_mode = -1;
    323  1.1  thorpej 		else if (wm.wm_mode == WDOG_MODE_UTICKLE) {
    324  1.1  thorpej 			if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
    325  1.1  thorpej 				tickler = (pid_t) -1;
    326  1.1  thorpej 		}
    327  1.1  thorpej 
    328  1.1  thorpej 		printf("\t%s, %u second period", cp, wm.wm_period);
    329  1.1  thorpej 		if (wm.wm_mode != WDOG_MODE_DISARMED) {
    330  1.1  thorpej 			printf(" [armed, %s tickle",
    331  1.1  thorpej 			    wm.wm_mode == WDOG_MODE_KTICKLE ?
    332  1.1  thorpej 			    "kernel" : "user");
    333  1.1  thorpej 			if (wm.wm_mode == WDOG_MODE_UTICKLE &&
    334  1.1  thorpej 			    tickler != (pid_t) -1)
    335  1.1  thorpej 				printf(", pid %d", tickler);
    336  1.1  thorpej 			printf("]");
    337  1.1  thorpej 		}
    338  1.1  thorpej 		printf("\n");
    339  1.1  thorpej 	}
    340  1.1  thorpej  out:
    341  1.1  thorpej 	(void) close(fd);
    342  1.1  thorpej }
    343  1.1  thorpej 
    344  1.1  thorpej void
    345  1.1  thorpej usage(void)
    346  1.1  thorpej {
    347  1.1  thorpej 	extern const char *__progname;
    348  1.1  thorpej 
    349  1.1  thorpej 	fprintf(stderr, "usage: %s\n", __progname);
    350  1.1  thorpej 	fprintf(stderr, "       %s -k [-A] [-p seconds] timer\n", __progname);
    351  1.1  thorpej 	fprintf(stderr, "       %s -u [-A] [-p seconds] timer\n", __progname);
    352  1.1  thorpej 	fprintf(stderr, "       %s -d\n", __progname);
    353  1.1  thorpej 
    354  1.1  thorpej 	exit(1);
    355  1.1  thorpej }
    356