softdog.c 7.2 KB

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