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 #ifdef MODULE
49 MODULE_PARM(soft_margin,"i");
50 #endif
51
52 /*
53 * Our timer
54 */
55
56 static void watchdog_fire(unsigned long);
57
58 static struct timer_list watchdog_ticktock = {
59 function: watchdog_fire,
60 };
61 static int timer_alive;
62
63
64 /*
65 * If the timer expires..
66 */
67
68 static void watchdog_fire(unsigned long data)
69 {
70 #ifdef ONLY_TESTING
71 printk(KERN_CRIT "SOFTDOG: Would Reboot.\n");
72 #else
73 printk(KERN_CRIT "SOFTDOG: Initiating system reboot.\n");
74 machine_restart(NULL);
75 printk("WATCHDOG: Reboot didn't ?????\n");
76 #endif
77 }
78
79 /*
80 * Allow only one person to hold it open
81 */
82
83 static int softdog_open(struct inode *inode, struct file *file)
84 {
85 if(timer_alive)
86 return -EBUSY;
87 #ifdef CONFIG_WATCHDOG_NOWAYOUT
88 MOD_INC_USE_COUNT;
89 #endif
90 /*
91 * Activate timer
92 */
93 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
94 timer_alive=1;
95 return 0;
96 }
97
98 static int softdog_release(struct inode *inode, struct file *file)
99 {
100 /*
101 * Shut off the timer.
102 * Lock it in if it's a module and we defined ...NOWAYOUT
103 */
104 lock_kernel();
105 #ifndef CONFIG_WATCHDOG_NOWAYOUT
106 del_timer(&watchdog_ticktock);
107 #endif
108 timer_alive=0;
109 unlock_kernel();
110 return 0;
111 }
112
113 static ssize_t softdog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
114 {
115 /* Can't seek (pwrite) on this device */
116 if (ppos != &file->f_pos)
117 return -ESPIPE;
118
119 /*
120 * Refresh the timer.
121 */
122 if(len) {
123 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
124 return 1;
125 }
126 return 0;
127 }
128
129 static int softdog_ioctl(struct inode *inode, struct file *file,
130 unsigned int cmd, unsigned long arg)
131 {
132 static struct watchdog_info ident=
133 {
134 0,
135 0,
136 "Software Watchdog"
137 };
138 switch(cmd)
139 {
140 default:
141 return -ENOIOCTLCMD;
142 case WDIOC_GETSUPPORT:
143 if(copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
144 return -EFAULT;
145 return 0;
146 case WDIOC_GETSTATUS:
147 case WDIOC_GETBOOTSTATUS:
148 return put_user(0,(int *)arg);
149 case WDIOC_KEEPALIVE:
150 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
151 return 0;
152 }
153 }
154
155 static struct file_operations softdog_fops=
156 {
157 owner: THIS_MODULE,
158 write: softdog_write,
159 ioctl: softdog_ioctl,
160 open: softdog_open,
161 release: softdog_release,
162 };
163
164 static struct miscdevice softdog_miscdev=
165 {
166 WATCHDOG_MINOR,
167 "watchdog",
168 &softdog_fops
169 };
170
171 static int __init watchdog_init(void)
172 {
173 int ret;
174
175 ret = misc_register(&softdog_miscdev);
176
177 if (ret)
178 return ret;
179
180 printk("Software Watchdog Timer: 0.05, timer margin: %d sec\n", soft_margin);
181
182 return 0;
183 }
184
185 static void __exit watchdog_exit(void)
186 {
187 misc_deregister(&softdog_miscdev);
188 }
189
190 module_init(watchdog_init);
191 module_exit(watchdog_exit);
192
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.