softdog.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * SoftDog 0.07: A Software Watchdog Device
  3. *
  4. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  5. * All Rights Reserved.
  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. * 20011127 Joel Becker (jlbec@evilplan.org>
  31. * Added soft_noboot; Allows testing the softdog trigger without
  32. * requiring a recompile.
  33. * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
  34. *
  35. * 20020530 Joel Becker <joel.becker@oracle.com>
  36. * Added Matt Domsch's nowayout module option.
  37. */
  38. #include <linux/module.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/types.h>
  41. #include <linux/timer.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/watchdog.h>
  44. #include <linux/fs.h>
  45. #include <linux/notifier.h>
  46. #include <linux/reboot.h>
  47. #include <linux/init.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/kernel.h>
  51. #define PFX "SoftDog: "
  52. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  53. static int soft_margin = TIMER_MARGIN; /* in seconds */
  54. module_param(soft_margin, int, 0);
  55. MODULE_PARM_DESC(soft_margin,
  56. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  57. __MODULE_STRING(TIMER_MARGIN) ")");
  58. static int nowayout = WATCHDOG_NOWAYOUT;
  59. module_param(nowayout, int, 0);
  60. MODULE_PARM_DESC(nowayout,
  61. "Watchdog cannot be stopped once started (default="
  62. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  63. #ifdef ONLY_TESTING
  64. static int soft_noboot = 1;
  65. #else
  66. static int soft_noboot = 0;
  67. #endif /* ONLY_TESTING */
  68. module_param(soft_noboot, int, 0);
  69. MODULE_PARM_DESC(soft_noboot,
  70. "Softdog action, set to 1 to ignore reboots, 0 to reboot "
  71. "(default depends on ONLY_TESTING)");
  72. static int soft_panic;
  73. module_param(soft_panic, int, 0);
  74. MODULE_PARM_DESC(soft_panic,
  75. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  76. /*
  77. * Our timer
  78. */
  79. static void watchdog_fire(unsigned long);
  80. static struct timer_list watchdog_ticktock =
  81. TIMER_INITIALIZER(watchdog_fire, 0, 0);
  82. static unsigned long driver_open, orphan_timer;
  83. static char expect_close;
  84. /*
  85. * If the timer expires..
  86. */
  87. static void watchdog_fire(unsigned long data)
  88. {
  89. if (test_and_clear_bit(0, &orphan_timer))
  90. module_put(THIS_MODULE);
  91. if (soft_noboot)
  92. printk(KERN_CRIT PFX "Triggered - Reboot ignored.\n");
  93. else if (soft_panic) {
  94. printk(KERN_CRIT PFX "Initiating panic.\n");
  95. panic("Software Watchdog Timer expired.");
  96. } else {
  97. printk(KERN_CRIT PFX "Initiating system reboot.\n");
  98. emergency_restart();
  99. printk(KERN_CRIT PFX "Reboot didn't ?????\n");
  100. }
  101. }
  102. /*
  103. * Softdog operations
  104. */
  105. static int softdog_keepalive(void)
  106. {
  107. mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
  108. return 0;
  109. }
  110. static int softdog_stop(void)
  111. {
  112. del_timer(&watchdog_ticktock);
  113. return 0;
  114. }
  115. static int softdog_set_heartbeat(int t)
  116. {
  117. if ((t < 0x0001) || (t > 0xFFFF))
  118. return -EINVAL;
  119. soft_margin = t;
  120. return 0;
  121. }
  122. /*
  123. * /dev/watchdog handling
  124. */
  125. static int softdog_open(struct inode *inode, struct file *file)
  126. {
  127. if (test_and_set_bit(0, &driver_open))
  128. return -EBUSY;
  129. if (!test_and_clear_bit(0, &orphan_timer))
  130. __module_get(THIS_MODULE);
  131. /*
  132. * Activate timer
  133. */
  134. softdog_keepalive();
  135. return nonseekable_open(inode, file);
  136. }
  137. static int softdog_release(struct inode *inode, struct file *file)
  138. {
  139. /*
  140. * Shut off the timer.
  141. * Lock it in if it's a module and we set nowayout
  142. */
  143. if (expect_close == 42) {
  144. softdog_stop();
  145. module_put(THIS_MODULE);
  146. } else {
  147. printk(KERN_CRIT PFX
  148. "Unexpected close, not stopping watchdog!\n");
  149. set_bit(0, &orphan_timer);
  150. softdog_keepalive();
  151. }
  152. clear_bit(0, &driver_open);
  153. expect_close = 0;
  154. return 0;
  155. }
  156. static ssize_t softdog_write(struct file *file, const char __user *data,
  157. size_t len, loff_t *ppos)
  158. {
  159. /*
  160. * Refresh the timer.
  161. */
  162. if (len) {
  163. if (!nowayout) {
  164. size_t i;
  165. /* In case it was set long ago */
  166. expect_close = 0;
  167. for (i = 0; i != len; i++) {
  168. char c;
  169. if (get_user(c, data + i))
  170. return -EFAULT;
  171. if (c == 'V')
  172. expect_close = 42;
  173. }
  174. }
  175. softdog_keepalive();
  176. }
  177. return len;
  178. }
  179. static long softdog_ioctl(struct file *file, unsigned int cmd,
  180. unsigned long arg)
  181. {
  182. void __user *argp = (void __user *)arg;
  183. int __user *p = argp;
  184. int new_margin;
  185. static const struct watchdog_info ident = {
  186. .options = WDIOF_SETTIMEOUT |
  187. WDIOF_KEEPALIVEPING |
  188. WDIOF_MAGICCLOSE,
  189. .firmware_version = 0,
  190. .identity = "Software Watchdog",
  191. };
  192. switch (cmd) {
  193. case WDIOC_GETSUPPORT:
  194. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  195. case WDIOC_GETSTATUS:
  196. case WDIOC_GETBOOTSTATUS:
  197. return put_user(0, p);
  198. case WDIOC_KEEPALIVE:
  199. softdog_keepalive();
  200. return 0;
  201. case WDIOC_SETTIMEOUT:
  202. if (get_user(new_margin, p))
  203. return -EFAULT;
  204. if (softdog_set_heartbeat(new_margin))
  205. return -EINVAL;
  206. softdog_keepalive();
  207. /* Fall */
  208. case WDIOC_GETTIMEOUT:
  209. return put_user(soft_margin, p);
  210. default:
  211. return -ENOTTY;
  212. }
  213. }
  214. /*
  215. * Notifier for system down
  216. */
  217. static int softdog_notify_sys(struct notifier_block *this, unsigned long code,
  218. void *unused)
  219. {
  220. if (code == SYS_DOWN || code == SYS_HALT)
  221. /* Turn the WDT off */
  222. softdog_stop();
  223. return NOTIFY_DONE;
  224. }
  225. /*
  226. * Kernel Interfaces
  227. */
  228. static const struct file_operations softdog_fops = {
  229. .owner = THIS_MODULE,
  230. .llseek = no_llseek,
  231. .write = softdog_write,
  232. .unlocked_ioctl = softdog_ioctl,
  233. .open = softdog_open,
  234. .release = softdog_release,
  235. };
  236. static struct miscdevice softdog_miscdev = {
  237. .minor = WATCHDOG_MINOR,
  238. .name = "watchdog",
  239. .fops = &softdog_fops,
  240. };
  241. static struct notifier_block softdog_notifier = {
  242. .notifier_call = softdog_notify_sys,
  243. };
  244. static char banner[] __initdata = KERN_INFO "Software Watchdog Timer: 0.07 "
  245. "initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d "
  246. "(nowayout= %d)\n";
  247. static int __init watchdog_init(void)
  248. {
  249. int ret;
  250. /* Check that the soft_margin value is within it's range;
  251. if not reset to the default */
  252. if (softdog_set_heartbeat(soft_margin)) {
  253. softdog_set_heartbeat(TIMER_MARGIN);
  254. printk(KERN_INFO PFX
  255. "soft_margin must be 0 < soft_margin < 65536, using %d\n",
  256. TIMER_MARGIN);
  257. }
  258. ret = register_reboot_notifier(&softdog_notifier);
  259. if (ret) {
  260. printk(KERN_ERR PFX
  261. "cannot register reboot notifier (err=%d)\n", ret);
  262. return ret;
  263. }
  264. ret = misc_register(&softdog_miscdev);
  265. if (ret) {
  266. printk(KERN_ERR PFX
  267. "cannot register miscdev on minor=%d (err=%d)\n",
  268. WATCHDOG_MINOR, ret);
  269. unregister_reboot_notifier(&softdog_notifier);
  270. return ret;
  271. }
  272. printk(banner, soft_noboot, soft_margin, soft_panic, nowayout);
  273. return 0;
  274. }
  275. static void __exit watchdog_exit(void)
  276. {
  277. misc_deregister(&softdog_miscdev);
  278. unregister_reboot_notifier(&softdog_notifier);
  279. }
  280. module_init(watchdog_init);
  281. module_exit(watchdog_exit);
  282. MODULE_AUTHOR("Alan Cox");
  283. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  284. MODULE_LICENSE("GPL");
  285. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);