Home | History | Annotate | Line # | Download | only in sysmon
swwdog.c revision 1.6.6.1
      1  1.6.6.1      ad /*	$NetBSD: swwdog.c,v 1.6.6.1 2007/07/01 21:49:01 ad Exp $	*/
      2      1.1     smb 
      3      1.1     smb /*
      4      1.1     smb  * Copyright (c) 2004, 2005 Steven M. Bellovin
      5      1.1     smb  * All rights reserved.
      6      1.1     smb  *
      7      1.1     smb  * Redistribution and use in source and binary forms, with or without
      8      1.1     smb  * modification, are permitted provided that the following conditions
      9      1.1     smb  * are met:
     10      1.1     smb  * 1. Redistributions of source code must retain the above copyright
     11      1.1     smb  *    notice, this list of conditions and the following disclaimer.
     12      1.1     smb  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     smb  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     smb  *    documentation and/or other materials provided with the distribution.
     15      1.1     smb  * 3. All advertising materials mentioning features or use of this software
     16      1.1     smb  *    must display the following acknowledgement:
     17      1.1     smb  *      This product includes software developed by Steven M. Bellovin
     18      1.1     smb  * 4. The name of the author contributors may not be used to endorse or
     19      1.1     smb  *    promote products derived from this software without specific prior
     20      1.1     smb  *    written permission.
     21      1.1     smb  *
     22      1.1     smb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
     23      1.1     smb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24      1.1     smb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25      1.1     smb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     26      1.1     smb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27      1.1     smb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28      1.1     smb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29      1.1     smb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30      1.1     smb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31      1.1     smb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32      1.1     smb  * POSSIBILITY OF SUCH DAMAGE.
     33      1.1     smb  */
     34      1.1     smb 
     35      1.1     smb #include <sys/cdefs.h>
     36  1.6.6.1      ad __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.6.6.1 2007/07/01 21:49:01 ad Exp $");
     37      1.1     smb 
     38      1.1     smb /*
     39      1.1     smb  *
     40      1.1     smb  * Software watchdog timer
     41      1.1     smb  *
     42      1.1     smb  */
     43      1.1     smb #include <sys/param.h>
     44      1.2   perry #include <sys/callout.h>
     45      1.2   perry #include <sys/device.h>
     46      1.2   perry #include <sys/kernel.h>
     47      1.1     smb #include <sys/reboot.h>
     48      1.1     smb #include <sys/systm.h>
     49      1.1     smb #include <sys/wdog.h>
     50      1.1     smb #include <dev/sysmon/sysmonvar.h>
     51      1.1     smb 
     52      1.1     smb #include "swwdog.h"
     53      1.1     smb 
     54      1.1     smb struct swwdog_softc {
     55      1.1     smb 	struct sysmon_wdog sc_smw;
     56      1.1     smb 	struct callout sc_c;
     57      1.1     smb 	char sc_name[20];
     58      1.1     smb 	int sc_wdog_armed;
     59      1.1     smb } sc_wdog[NSWWDOG];
     60      1.1     smb 
     61      1.1     smb void	swwdogattach(int);
     62      1.1     smb 
     63      1.1     smb static int swwdog_setmode(struct sysmon_wdog *);
     64      1.1     smb static int swwdog_tickle(struct sysmon_wdog *);
     65      1.1     smb 
     66      1.1     smb static int swwdog_arm(struct swwdog_softc *);
     67      1.1     smb static int swwdog_disarm(struct swwdog_softc *);
     68      1.1     smb 
     69      1.1     smb static void swwdog_panic(void *);
     70      1.1     smb 
     71      1.1     smb int swwdog_reboot = 0;		/* set for panic instead of reboot */
     72      1.1     smb 
     73      1.1     smb #define	SWDOG_DEFAULT	60		/* 60-second default period */
     74      1.1     smb 
     75      1.1     smb void
     76      1.5     smb swwdogattach(int count __unused)
     77      1.1     smb {
     78      1.1     smb 	int i;
     79      1.1     smb 
     80      1.1     smb 	for (i = 0; i < NSWWDOG; i++) {
     81      1.1     smb 		struct swwdog_softc *sc = &sc_wdog[i];
     82      1.1     smb 
     83      1.1     smb 		snprintf(sc->sc_name, sizeof sc->sc_name, "swwdog%d", i);
     84      1.1     smb 		printf("%s: ", sc->sc_name);
     85      1.1     smb 		sc->sc_smw.smw_name = sc->sc_name;
     86      1.1     smb 		sc->sc_smw.smw_cookie = sc;
     87      1.1     smb 		sc->sc_smw.smw_setmode = swwdog_setmode;
     88      1.1     smb 		sc->sc_smw.smw_tickle = swwdog_tickle;
     89      1.1     smb 		sc->sc_smw.smw_period = SWDOG_DEFAULT;
     90  1.6.6.1      ad 		callout_init(&sc->sc_c, 0);
     91      1.1     smb 		callout_setfunc(&sc->sc_c, swwdog_panic, sc);
     92      1.1     smb 
     93      1.1     smb 		if (sysmon_wdog_register(&sc->sc_smw) == 0)
     94      1.1     smb 			printf("software watchdog initialized\n");
     95      1.3  simonb 		else
     96      1.3  simonb 			printf("unable to register software watchdog "
     97      1.3  simonb 			    "with sysmon\n");
     98      1.1     smb 	}
     99      1.1     smb }
    100      1.1     smb 
    101      1.1     smb static int
    102      1.1     smb swwdog_setmode(struct sysmon_wdog *smw)
    103      1.1     smb {
    104      1.2   perry 	struct swwdog_softc *sc = smw->smw_cookie;
    105      1.1     smb 	int error = 0;
    106      1.1     smb 
    107      1.1     smb 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    108      1.1     smb 		error = swwdog_disarm(sc);
    109      1.1     smb 	} else {
    110      1.1     smb 		if (smw->smw_period == 0)
    111      1.1     smb 			return EINVAL;
    112      1.1     smb 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    113      1.1     smb 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
    114      1.3  simonb 		else
    115      1.3  simonb 			sc->sc_smw.smw_period = smw->smw_period;
    116      1.1     smb 		error = swwdog_arm(sc);
    117      1.1     smb 	}
    118      1.1     smb 	return error;
    119      1.1     smb }
    120      1.1     smb 
    121      1.1     smb static int
    122      1.1     smb swwdog_tickle(struct sysmon_wdog *smw)
    123      1.1     smb {
    124      1.1     smb 
    125      1.1     smb 	return swwdog_arm(smw->smw_cookie);
    126      1.1     smb }
    127      1.1     smb 
    128      1.1     smb static int
    129      1.1     smb swwdog_arm(struct swwdog_softc *sc)
    130      1.1     smb {
    131      1.1     smb 
    132      1.1     smb 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
    133      1.1     smb 	return 0;
    134      1.1     smb }
    135      1.1     smb 
    136      1.1     smb static int
    137      1.1     smb swwdog_disarm(struct swwdog_softc *sc)
    138      1.1     smb {
    139      1.1     smb 
    140      1.1     smb 	callout_stop(&sc->sc_c);
    141      1.1     smb 	return 0;
    142      1.1     smb }
    143      1.1     smb 
    144      1.1     smb static void
    145      1.2   perry swwdog_panic(void *vsc)
    146      1.1     smb {
    147      1.1     smb 	struct swwdog_softc *sc = vsc;
    148      1.1     smb 	int do_panic;
    149      1.1     smb 
    150      1.1     smb 	do_panic = swwdog_reboot;
    151      1.1     smb 	swwdog_reboot = 1;
    152      1.1     smb 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
    153      1.1     smb 
    154      1.1     smb 	printf("%s: %d second timer expired", sc->sc_name,
    155      1.1     smb 	    sc->sc_smw.smw_period);
    156      1.1     smb 
    157      1.1     smb 	if (do_panic)
    158      1.1     smb 		panic("watchdog timer expired");
    159      1.3  simonb 	else
    160      1.3  simonb 		cpu_reboot(0, NULL);
    161      1.1     smb }
    162