Home | History | Annotate | Line # | Download | only in wdogctl
wdogctl.c revision 1.9
      1  1.9   itojun /*	$NetBSD: wdogctl.c,v 1.9 2003/07/13 08:05:58 itojun 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.8      agc #include <sys/cdefs.h>
     36  1.8      agc 
     37  1.8      agc #ifndef lint
     38  1.9   itojun __RCSID("$NetBSD: wdogctl.c,v 1.9 2003/07/13 08:05:58 itojun Exp $");
     39  1.8      agc #endif
     40  1.8      agc 
     41  1.1  thorpej 
     42  1.1  thorpej #include <sys/param.h>
     43  1.1  thorpej #include <sys/ioctl.h>
     44  1.1  thorpej #include <sys/wdog.h>
     45  1.1  thorpej 
     46  1.1  thorpej #include <err.h>
     47  1.1  thorpej #include <errno.h>
     48  1.1  thorpej #include <fcntl.h>
     49  1.1  thorpej #include <stdio.h>
     50  1.1  thorpej #include <stdlib.h>
     51  1.1  thorpej #include <time.h>
     52  1.1  thorpej #include <signal.h>
     53  1.1  thorpej #include <syslog.h>
     54  1.1  thorpej #include <unistd.h>
     55  1.2  minoura #include <string.h>
     56  1.1  thorpej 
     57  1.1  thorpej #define	_PATH_WATCHDOG		"/dev/watchdog"
     58  1.1  thorpej 
     59  1.1  thorpej int	main(int, char *[]);
     60  1.1  thorpej void	enable_kernel(const char *, u_int);
     61  1.1  thorpej void	enable_user(const char *, u_int);
     62  1.1  thorpej void	disable(void);
     63  1.1  thorpej void	list_timers(void);
     64  1.1  thorpej void	usage(void);
     65  1.1  thorpej 
     66  1.1  thorpej int	Aflag;
     67  1.1  thorpej 
     68  1.1  thorpej int
     69  1.1  thorpej main(int argc, char *argv[])
     70  1.1  thorpej {
     71  1.1  thorpej 	int cmds = 0, kflag = 0, uflag = 0, dflag = 0, ch;
     72  1.1  thorpej 	u_int period = WDOG_PERIOD_DEFAULT;
     73  1.1  thorpej 
     74  1.1  thorpej 	while ((ch = getopt(argc, argv, "Adkp:u")) != -1) {
     75  1.1  thorpej 		switch (ch) {
     76  1.1  thorpej 		case 'A':
     77  1.1  thorpej 			if (cmds == 0 || dflag)
     78  1.1  thorpej 				usage();
     79  1.1  thorpej 			Aflag = 1;
     80  1.1  thorpej 			break;
     81  1.1  thorpej 
     82  1.1  thorpej 		case 'd':
     83  1.1  thorpej 			dflag = 1;
     84  1.1  thorpej 			cmds++;
     85  1.1  thorpej 			break;
     86  1.1  thorpej 
     87  1.1  thorpej 		case 'k':
     88  1.1  thorpej 			kflag = 1;
     89  1.1  thorpej 			cmds++;
     90  1.1  thorpej 			break;
     91  1.1  thorpej 
     92  1.1  thorpej 		case 'p':
     93  1.1  thorpej 			if (cmds == 0 || dflag)
     94  1.1  thorpej 				usage();
     95  1.1  thorpej 			period = atoi(optarg);
     96  1.1  thorpej 			if (period == -1)
     97  1.1  thorpej 				usage();
     98  1.1  thorpej 			break;
     99  1.1  thorpej 
    100  1.1  thorpej 		case 'u':
    101  1.1  thorpej 			uflag = 1;
    102  1.1  thorpej 			cmds++;
    103  1.1  thorpej 			break;
    104  1.1  thorpej 
    105  1.1  thorpej 		default:
    106  1.1  thorpej 			usage();
    107  1.1  thorpej 		}
    108  1.1  thorpej 	}
    109  1.1  thorpej 
    110  1.1  thorpej 	argc -= optind;
    111  1.1  thorpej 	argv += optind;
    112  1.1  thorpej 
    113  1.1  thorpej 	if (cmds > 1)
    114  1.1  thorpej 		usage();
    115  1.1  thorpej 
    116  1.1  thorpej 	if (kflag) {
    117  1.1  thorpej 		if (argc != 1)
    118  1.1  thorpej 			usage();
    119  1.1  thorpej 		enable_kernel(argv[0], period);
    120  1.1  thorpej 	} else if (uflag) {
    121  1.1  thorpej 		if (argc != 1)
    122  1.1  thorpej 			usage();
    123  1.1  thorpej 		enable_user(argv[0], period);
    124  1.1  thorpej 	} else if (dflag) {
    125  1.1  thorpej 		if (argc != 0)
    126  1.1  thorpej 			usage();
    127  1.1  thorpej 		disable();
    128  1.1  thorpej 	} else
    129  1.1  thorpej 		list_timers();
    130  1.1  thorpej 
    131  1.1  thorpej 	exit(0);
    132  1.1  thorpej }
    133  1.1  thorpej 
    134  1.1  thorpej void
    135  1.1  thorpej enable_kernel(const char *name, u_int period)
    136  1.1  thorpej {
    137  1.1  thorpej 	struct wdog_mode wm;
    138  1.1  thorpej 	int fd;
    139  1.1  thorpej 
    140  1.1  thorpej 	if (strlen(name) >= WDOG_NAMESIZE)
    141  1.1  thorpej 		errx(1, "invalid watchdog timer name: %s", name);
    142  1.9   itojun 	strlcpy(wm.wm_name, name, sizeof(wm.wm_name));
    143  1.1  thorpej 	wm.wm_mode = WDOG_MODE_KTICKLE;
    144  1.1  thorpej 	wm.wm_period = period;
    145  1.1  thorpej 
    146  1.1  thorpej 	if (Aflag)
    147  1.1  thorpej 		wm.wm_mode |= WDOG_FEATURE_ALARM;
    148  1.1  thorpej 
    149  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    150  1.1  thorpej 	if (fd == -1)
    151  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    152  1.1  thorpej 
    153  1.1  thorpej 	if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
    154  1.1  thorpej 		err(1, "WDOGIOC_SMODE");
    155  1.1  thorpej }
    156  1.1  thorpej 
    157  1.1  thorpej void
    158  1.1  thorpej enable_user(const char *name, u_int period)
    159  1.1  thorpej {
    160  1.1  thorpej 	struct wdog_mode wm;
    161  1.1  thorpej 	struct timespec ts;
    162  1.1  thorpej 	pid_t tickler;
    163  1.1  thorpej 	int fd, rv;
    164  1.1  thorpej 
    165  1.1  thorpej 	if (strlen(name) >= WDOG_NAMESIZE)
    166  1.1  thorpej 		errx(1, "invalid watchdog timer name: %s", name);
    167  1.9   itojun 	strlcpy(wm.wm_name, name, sizeof(wm.wm_name));
    168  1.1  thorpej 	wm.wm_mode = WDOG_MODE_UTICKLE;
    169  1.1  thorpej 	wm.wm_period = period;
    170  1.1  thorpej 
    171  1.1  thorpej 	if (Aflag)
    172  1.1  thorpej 		wm.wm_mode |= WDOG_FEATURE_ALARM;
    173  1.1  thorpej 
    174  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    175  1.1  thorpej 	if (fd == -1)
    176  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    177  1.1  thorpej 
    178  1.1  thorpej 	/* ...so we can log failures to tickle the timer. */
    179  1.4    lukem 	openlog("wdogctl", LOG_PERROR|LOG_PID, LOG_DAEMON);
    180  1.1  thorpej 
    181  1.1  thorpej 	/*
    182  1.1  thorpej 	 * We fork a child process which detaches from the controlling
    183  1.1  thorpej 	 * terminal once the timer is armed, and tickles the timer
    184  1.1  thorpej 	 * until we send it a SIGTERM.
    185  1.1  thorpej 	 */
    186  1.1  thorpej 	tickler = fork();
    187  1.1  thorpej 	if (tickler == -1)
    188  1.1  thorpej 		err(1, "unable to fork tickler process");
    189  1.1  thorpej 	else if (tickler != 0) {
    190  1.1  thorpej 		if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1) {
    191  1.1  thorpej 			err(1, "WDOGIOC_SMODE");
    192  1.1  thorpej 			(void) kill(tickler, SIGTERM);
    193  1.1  thorpej 		}
    194  1.1  thorpej 		(void) close(fd);
    195  1.1  thorpej 		return;
    196  1.1  thorpej 	}
    197  1.1  thorpej 
    198  1.1  thorpej 
    199  1.1  thorpej 	/*
    200  1.1  thorpej 	 * Wait for the watchdog to be armed.  When it is, loop,
    201  1.1  thorpej 	 * tickling the timer, then waiting 1/2 the period before
    202  1.1  thorpej 	 * doing it again.
    203  1.1  thorpej 	 *
    204  1.1  thorpej 	 * If the parent fails to enable the watchdog, it will kill
    205  1.1  thorpej 	 * us.
    206  1.1  thorpej 	 */
    207  1.1  thorpej 	do {
    208  1.1  thorpej 		rv = ioctl(fd, WDOGIOC_WHICH, &wm);
    209  1.1  thorpej 	} while (rv == -1);
    210  1.1  thorpej 
    211  1.1  thorpej 	if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    212  1.1  thorpej 		syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
    213  1.1  thorpej 		    wm.wm_name);
    214  1.1  thorpej 
    215  1.1  thorpej 	/*
    216  1.1  thorpej 	 * Now detach from the controlling terminal, and just run
    217  1.1  thorpej 	 * in the background.  The kernel will keep track of who
    218  1.1  thorpej 	 * we are, each time we tickle the timer.
    219  1.1  thorpej 	 */
    220  1.1  thorpej 	if (daemon(0, 0) == -1) {
    221  1.1  thorpej 		/*
    222  1.1  thorpej 		 * We weren't able to go into the background.  When
    223  1.1  thorpej 		 * we exit, the kernel will disable the watchdog so
    224  1.1  thorpej 		 * that the system won't die.
    225  1.1  thorpej 		 */
    226  1.3    lukem 		err(1, "unable to detach from terminal");
    227  1.1  thorpej 	}
    228  1.1  thorpej 
    229  1.1  thorpej 	if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    230  1.1  thorpej 		syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
    231  1.1  thorpej 		    wm.wm_name);
    232  1.1  thorpej 
    233  1.1  thorpej 	for (;;) {
    234  1.1  thorpej 		ts.tv_sec = wm.wm_period / 2;
    235  1.1  thorpej 		ts.tv_nsec = 0;
    236  1.1  thorpej 		(void) nanosleep(&ts, NULL);
    237  1.1  thorpej 
    238  1.1  thorpej 		if (ioctl(fd, WDOGIOC_TICKLE) == -1)
    239  1.1  thorpej 			syslog(LOG_EMERG,
    240  1.1  thorpej 			    "unable to tickle watchdog timer %s: %m",
    241  1.1  thorpej 			    wm.wm_name);
    242  1.1  thorpej 	}
    243  1.1  thorpej 	/* NOTREACHED */
    244  1.1  thorpej }
    245  1.1  thorpej 
    246  1.1  thorpej void
    247  1.1  thorpej disable(void)
    248  1.1  thorpej {
    249  1.1  thorpej 	struct wdog_mode wm;
    250  1.1  thorpej 	pid_t tickler;
    251  1.1  thorpej 	int fd;
    252  1.1  thorpej 
    253  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
    254  1.1  thorpej 	if (fd == -1)
    255  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    256  1.1  thorpej 
    257  1.1  thorpej 	if (ioctl(fd, WDOGIOC_WHICH, &wm) == -1) {
    258  1.1  thorpej 		printf("No watchdog timer running.\n");
    259  1.1  thorpej 		return;
    260  1.1  thorpej 	}
    261  1.1  thorpej 
    262  1.1  thorpej 	/*
    263  1.1  thorpej 	 * If the timer is running in UTICKLE mode, we need
    264  1.1  thorpej 	 * to kill the wdogctl(8) process that is tickling
    265  1.1  thorpej 	 * the timer.
    266  1.1  thorpej 	 */
    267  1.1  thorpej 	if (wm.wm_mode == WDOG_MODE_UTICKLE) {
    268  1.1  thorpej 		if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
    269  1.1  thorpej 			err(1, "WDOGIOC_GTICKLER");
    270  1.1  thorpej 		(void) close(fd);
    271  1.1  thorpej 		(void) kill(tickler, SIGTERM);
    272  1.1  thorpej 	} else {
    273  1.1  thorpej 		wm.wm_mode = WDOG_MODE_DISARMED;
    274  1.1  thorpej 		if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
    275  1.1  thorpej 			err(1, "unable to disarm watchdog %s", wm.wm_name);
    276  1.1  thorpej 		(void) close(fd);
    277  1.1  thorpej 	}
    278  1.1  thorpej }
    279  1.1  thorpej 
    280  1.1  thorpej void
    281  1.1  thorpej list_timers(void)
    282  1.1  thorpej {
    283  1.1  thorpej 	struct wdog_conf wc;
    284  1.1  thorpej 	struct wdog_mode wm;
    285  1.1  thorpej 	char *buf, *cp;
    286  1.1  thorpej 	int fd, count, i;
    287  1.1  thorpej 	pid_t tickler;
    288  1.1  thorpej 
    289  1.1  thorpej 	fd = open(_PATH_WATCHDOG, O_RDONLY, 0644);
    290  1.1  thorpej 	if (fd == -1)
    291  1.1  thorpej 		err(1, "open %s", _PATH_WATCHDOG);
    292  1.1  thorpej 
    293  1.1  thorpej 	wc.wc_names = NULL;
    294  1.1  thorpej 	wc.wc_count = 0;
    295  1.1  thorpej 
    296  1.1  thorpej 	if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
    297  1.1  thorpej 		err(1, "ioctl WDOGIOC_GWDOGS for count");
    298  1.1  thorpej 
    299  1.1  thorpej 	count = wc.wc_count;
    300  1.1  thorpej 	if (count == 0) {
    301  1.1  thorpej 		printf("No watchdog timers present.\n");
    302  1.1  thorpej 		goto out;
    303  1.1  thorpej 	}
    304  1.1  thorpej 
    305  1.1  thorpej 	buf = malloc(count * WDOG_NAMESIZE);
    306  1.1  thorpej 	if (buf == NULL)
    307  1.1  thorpej 		err(1, "malloc %d byte for watchdog names",
    308  1.1  thorpej 		    count * WDOG_NAMESIZE);
    309  1.1  thorpej 
    310  1.1  thorpej 	wc.wc_names = buf;
    311  1.1  thorpej 	if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
    312  1.1  thorpej 		err(1, "ioctl WDOGIOC_GWDOGS for names");
    313  1.1  thorpej 
    314  1.1  thorpej 	count = wc.wc_count;
    315  1.1  thorpej 	if (count == 0) {
    316  1.1  thorpej 		printf("No watchdog timers present.\n");
    317  1.1  thorpej 		free(buf);
    318  1.1  thorpej 		goto out;
    319  1.1  thorpej 	}
    320  1.1  thorpej 
    321  1.1  thorpej 	printf("Available watchdog timers:\n");
    322  1.1  thorpej 	for (i = 0, cp = buf; i < count; i++, cp += WDOG_NAMESIZE) {
    323  1.1  thorpej 		cp[WDOG_NAMESIZE - 1] = '\0';
    324  1.9   itojun 		strlcpy(wm.wm_name, cp, sizeof(wm.wm_name));
    325  1.1  thorpej 
    326  1.1  thorpej 		if (ioctl(fd, WDOGIOC_GMODE, &wm) == -1)
    327  1.1  thorpej 			wm.wm_mode = -1;
    328  1.1  thorpej 		else if (wm.wm_mode == WDOG_MODE_UTICKLE) {
    329  1.1  thorpej 			if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
    330  1.1  thorpej 				tickler = (pid_t) -1;
    331  1.1  thorpej 		}
    332  1.1  thorpej 
    333  1.1  thorpej 		printf("\t%s, %u second period", cp, wm.wm_period);
    334  1.1  thorpej 		if (wm.wm_mode != WDOG_MODE_DISARMED) {
    335  1.1  thorpej 			printf(" [armed, %s tickle",
    336  1.1  thorpej 			    wm.wm_mode == WDOG_MODE_KTICKLE ?
    337  1.1  thorpej 			    "kernel" : "user");
    338  1.1  thorpej 			if (wm.wm_mode == WDOG_MODE_UTICKLE &&
    339  1.1  thorpej 			    tickler != (pid_t) -1)
    340  1.1  thorpej 				printf(", pid %d", tickler);
    341  1.1  thorpej 			printf("]");
    342  1.1  thorpej 		}
    343  1.1  thorpej 		printf("\n");
    344  1.1  thorpej 	}
    345  1.1  thorpej  out:
    346  1.1  thorpej 	(void) close(fd);
    347  1.1  thorpej }
    348  1.1  thorpej 
    349  1.1  thorpej void
    350  1.1  thorpej usage(void)
    351  1.1  thorpej {
    352  1.7      cgd 	fprintf(stderr, "Usage: %s\n", getprogname());
    353  1.7      cgd 	fprintf(stderr, "       %s -k [-A] [-p seconds] timer\n",
    354  1.7      cgd 	    getprogname());
    355  1.7      cgd 	fprintf(stderr, "       %s -u [-A] [-p seconds] timer\n",
    356  1.7      cgd 	    getprogname());
    357  1.7      cgd 	fprintf(stderr, "       %s -d\n", getprogname());
    358  1.1  thorpej 
    359  1.1  thorpej 	exit(1);
    360  1.1  thorpej }
    361