softdog.c 7.2 KB

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