File: /usr/src/linux/drivers/char/softdog.c
1 /*
2 * SoftDog 0.05: A Software Watchdog Device
3 *
4 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
17 *
18 * Software only watchdog driver. Unlike its big brother the WDT501P
19 * driver this won't always recover a failed machine.
20 *
21 * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
22 * Modularised.
23 * Added soft_margin; use upon insmod to change the timer delay.
24 * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
25 * minors.
26 *
27 * 19980911 Alan Cox
28 * Made SMP safe for 2.3.x
29 */
30
31 #include <linux/module.h>
32 #include <linux/config.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/fs.h>
36 #include <linux/mm.h>
37 #include <linux/miscdevice.h>
38 #include <linux/watchdog.h>
39 #include <linux/reboot.h>
40 #include <linux/smp_lock.h>
41 #include <linux/init.h>
42 #include <asm/uaccess.h>
43
44 #define TIMER_MARGIN 60 /* (secs) Default is 1 minute */
45
46 static int soft_margin = TIMER_MARGIN; /* in seconds */
47
48 MODULE_PARM(soft_margin,"i");
49
50 /*
51 * Our timer
52 */
53
54 static void watchdog_fire(unsigned long);
55
56 static struct timer_list watchdog_ticktock = {
57 function: watchdog_fire,
58 };
59 static int timer_alive;
60
61
62 /*
63 * If the timer expires..
64 */
65
66 static void watchdog_fire(unsigned long data)
67 {
68 #ifdef ONLY_TESTING
69 printk(KERN_CRIT "SOFTDOG: Would Reboot.\n");
70 #else
71 printk(KERN_CRIT "SOFTDOG: Initiating system reboot.\n");
72 machine_restart(NULL);
73 printk("WATCHDOG: Reboot didn't ?????\n");
74 #endif
75 }
76
77 /*
78 * Allow only one person to hold it open
79 */
80
81 static int softdog_open(struct inode *inode, struct file *file)
82 {
83 if(timer_alive)
84 return -EBUSY;
85 #ifdef CONFIG_WATCHDOG_NOWAYOUT
86 MOD_INC_USE_COUNT;
87 #endif
88 /*
89 * Activate timer
90 */
91 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
92 timer_alive=1;
93 return 0;
94 }
95
96 static int softdog_release(struct inode *inode, struct file *file)
97 {
98 /*
99 * Shut off the timer.
100 * Lock it in if it's a module and we defined ...NOWAYOUT
101 */
102 lock_kernel();
103 #ifndef CONFIG_WATCHDOG_NOWAYOUT
104 del_timer(&watchdog_ticktock);
105 #endif
106 timer_alive=0;
107 unlock_kernel();
108 return 0;
109 }
110
111 static ssize_t softdog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
112 {
113 /* Can't seek (pwrite) on this device */
114 if (ppos != &file->f_pos)
115 return -ESPIPE;
116
117 /*
118 * Refresh the timer.
119 */
120 if(len) {
121 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
122 return 1;
123 }
124 return 0;
125 }
126
127 static int softdog_ioctl(struct inode *inode, struct file *file,
128 unsigned int cmd, unsigned long arg)
129 {
130 static struct watchdog_info ident = {
131 identity: "Software Watchdog",
132 };
133 switch (cmd) {
134 default:
135 return -ENOTTY;
136 case WDIOC_GETSUPPORT:
137 if(copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
138 return -EFAULT;
139 return 0;
140 case WDIOC_GETSTATUS:
141 case WDIOC_GETBOOTSTATUS:
142 return put_user(0,(int *)arg);
143 case WDIOC_KEEPALIVE:
144 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
145 return 0;
146 }
147 }
148
149 static struct file_operations softdog_fops = {
150 owner: THIS_MODULE,
151 write: softdog_write,
152 ioctl: softdog_ioctl,
153 open: softdog_open,
154 release: softdog_release,
155 };
156
157 static struct miscdevice softdog_miscdev = {
158 minor: WATCHDOG_MINOR,
159 name: "watchdog",
160 fops: &softdog_fops,
161 };
162
163 static char banner[] __initdata = KERN_INFO "Software Watchdog Timer: 0.05, timer margin: %d sec\n";
164
165 static int __init watchdog_init(void)
166 {
167 int ret;
168
169 ret = misc_register(&softdog_miscdev);
170
171 if (ret)
172 return ret;
173
174 printk(banner, soft_margin);
175
176 return 0;
177 }
178
179 static void __exit watchdog_exit(void)
180 {
181 misc_deregister(&softdog_miscdev);
182 }
183
184 module_init(watchdog_init);
185 module_exit(watchdog_exit);
186