softdog.c 3.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *	SoftDog:	A Software Watchdog Device
L
Linus Torvalds 已提交
3
 *
4 5
 *	(c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
 *							All Rights Reserved.
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
 *	warranty for any of this software. This material is provided
 *	"AS-IS" and at no charge.
 *
 *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
 *
 *	Software only watchdog driver. Unlike its big brother the WDT501P
 *	driver this won't always recover a failed machine.
 */

22 23
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

24 25 26
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
L
Linus Torvalds 已提交
27 28
#include <linux/module.h>
#include <linux/moduleparam.h>
29
#include <linux/reboot.h>
L
Linus Torvalds 已提交
30
#include <linux/timer.h>
31
#include <linux/types.h>
L
Linus Torvalds 已提交
32 33 34
#include <linux/watchdog.h>

#define TIMER_MARGIN	60		/* Default is 60 seconds */
35 36
static unsigned int soft_margin = TIMER_MARGIN;	/* in seconds */
module_param(soft_margin, uint, 0);
37 38 39
MODULE_PARM_DESC(soft_margin,
	"Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
					__MODULE_STRING(TIMER_MARGIN) ")");
L
Linus Torvalds 已提交
40

W
Wim Van Sebroeck 已提交
41 42
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
43 44 45
MODULE_PARM_DESC(nowayout,
		"Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
L
Linus Torvalds 已提交
46

47
static int soft_noboot;
L
Linus Torvalds 已提交
48
module_param(soft_noboot, int, 0);
49
MODULE_PARM_DESC(soft_noboot,
50
	"Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
L
Linus Torvalds 已提交
51

52 53 54 55 56
static int soft_panic;
module_param(soft_panic, int, 0);
MODULE_PARM_DESC(soft_panic,
	"Softdog action, set to 1 to panic, 0 to reboot (default=0)");

57
static void softdog_fire(unsigned long data)
L
Linus Torvalds 已提交
58
{
59
	module_put(THIS_MODULE);
60
	if (soft_noboot) {
61
		pr_crit("Triggered - Reboot ignored\n");
62
	} else if (soft_panic) {
63 64
		pr_crit("Initiating panic\n");
		panic("Software Watchdog Timer expired");
65
	} else {
66
		pr_crit("Initiating system reboot\n");
A
Andrew Morton 已提交
67
		emergency_restart();
68
		pr_crit("Reboot didn't ?????\n");
L
Linus Torvalds 已提交
69 70 71
	}
}

72 73 74
static struct timer_list softdog_ticktock =
		TIMER_INITIALIZER(softdog_fire, 0, 0);

75
static int softdog_ping(struct watchdog_device *w)
L
Linus Torvalds 已提交
76
{
77
	if (!mod_timer(&softdog_ticktock, jiffies + (w->timeout * HZ)))
78
		__module_get(THIS_MODULE);
L
Linus Torvalds 已提交
79 80 81
	return 0;
}

82
static int softdog_stop(struct watchdog_device *w)
L
Linus Torvalds 已提交
83
{
84
	if (del_timer(&softdog_ticktock))
85 86
		module_put(THIS_MODULE);

L
Linus Torvalds 已提交
87 88 89
	return 0;
}

90 91 92
static struct watchdog_info softdog_info = {
	.identity = "Software Watchdog",
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
L
Linus Torvalds 已提交
93 94
};

95 96 97 98 99 100 101 102 103 104
static struct watchdog_ops softdog_ops = {
	.owner = THIS_MODULE,
	.start = softdog_ping,
	.stop = softdog_stop,
};

static struct watchdog_device softdog_dev = {
	.info = &softdog_info,
	.ops = &softdog_ops,
	.min_timeout = 1,
105 106
	.max_timeout = 65535,
	.timeout = TIMER_MARGIN,
L
Linus Torvalds 已提交
107 108
};

109
static int __init softdog_init(void)
L
Linus Torvalds 已提交
110 111 112
{
	int ret;

113
	watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
114
	watchdog_set_nowayout(&softdog_dev, nowayout);
115
	watchdog_stop_on_reboot(&softdog_dev);
L
Linus Torvalds 已提交
116

117
	ret = watchdog_register_device(&softdog_dev);
118
	if (ret)
L
Linus Torvalds 已提交
119 120
		return ret;

121 122
	pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
		soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
L
Linus Torvalds 已提交
123 124 125

	return 0;
}
126
module_init(softdog_init);
L
Linus Torvalds 已提交
127

128
static void __exit softdog_exit(void)
L
Linus Torvalds 已提交
129
{
130
	watchdog_unregister_device(&softdog_dev);
L
Linus Torvalds 已提交
131
}
132
module_exit(softdog_exit);
L
Linus Torvalds 已提交
133 134 135 136

MODULE_AUTHOR("Alan Cox");
MODULE_DESCRIPTION("Software Watchdog Device Driver");
MODULE_LICENSE("GPL");