softdog.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * SoftDog 0.07: A Software Watchdog Device
  3. *
  4. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  12. * warranty for any of this software. This material is provided
  13. * "AS-IS" and at no charge.
  14. *
  15. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  16. *
  17. * Software only watchdog driver. Unlike its big brother the WDT501P
  18. * driver this won't always recover a failed machine.
  19. *
  20. * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
  21. * Modularised.
  22. * Added soft_margin; use upon insmod to change the timer delay.
  23. * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
  24. * minors.
  25. *
  26. * 19980911 Alan Cox
  27. * Made SMP safe for 2.3.x
  28. *
  29. * 20011127 Joel Becker (jlbec@evilplan.org>
  30. * Added soft_noboot; Allows testing the softdog trigger without
  31. * requiring a recompile.
  32. * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
  33. *
  34. * 20020530 Joel Becker <joel.becker@oracle.com>
  35. * Added Matt Domsch's nowayout module option.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/types.h>
  40. #include <linux/timer.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/watchdog.h>
  43. #include <linux/fs.h>
  44. #include <linux/notifier.h>
  45. #include <linux/reboot.h>
  46. #include <linux/init.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/uaccess.h>
  49. #define PFX "SoftDog: "
  50. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  51. static int soft_margin = TIMER_MARGIN; /* in seconds */
  52. module_param(soft_margin, int, 0);
  53. MODULE_PARM_DESC(soft_margin,
  54. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  55. __MODULE_STRING(TIMER_MARGIN) ")");
  56. static int nowayout = WATCHDOG_NOWAYOUT;
  57. module_param(nowayout, int, 0);
  58. MODULE_PARM_DESC(nowayout,
  59. "Watchdog cannot be stopped once started (default="
  60. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  61. #ifdef ONLY_TESTING
  62. static int soft_noboot = 1;
  63. #else
  64. static int soft_noboot = 0;
  65. #endif /* ONLY_TESTING */
  66. module_param(soft_noboot, int, 0);
  67. MODULE_PARM_DESC(soft_noboot, "Softdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
  68. /*
  69. * Our timer
  70. */
  71. static void watchdog_fire(unsigned long);
  72. static struct timer_list watchdog_ticktock =
  73. TIMER_INITIALIZER(watchdog_fire, 0, 0);
  74. static unsigned long driver_open, orphan_timer;
  75. static char expect_close;
  76. /*
  77. * If the timer expires..
  78. */
  79. static void watchdog_fire(unsigned long data)
  80. {
  81. if (test_and_clear_bit(0, &orphan_timer))
  82. module_put(THIS_MODULE);
  83. if (soft_noboot)
  84. printk(KERN_CRIT PFX "Triggered - Reboot ignored.\n");
  85. else {
  86. printk(KERN_CRIT PFX "Initiating system reboot.\n");
  87. emergency_restart();
  88. printk(KERN_CRIT PFX "Reboot didn't ?????\n");
  89. }
  90. }
  91. /*
  92. * Softdog operations
  93. */
  94. static int softdog_keepalive(void)
  95. {
  96. mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
  97. return 0;
  98. }
  99. static int softdog_stop(void)
  100. {
  101. del_timer(&watchdog_ticktock);
  102. return 0;
  103. }
  104. static int softdog_set_heartbeat(int t)
  105. {
  106. if ((t < 0x0001) || (t > 0xFFFF))
  107. return -EINVAL;
  108. soft_margin = t;
  109. return 0;
  110. }
  111. /*
  112. * /dev/watchdog handling
  113. */
  114. static int softdog_open(struct inode *inode, struct file *file)
  115. {
  116. if (test_and_set_bit(0, &driver_open))
  117. return -EBUSY;
  118. if (!test_and_clear_bit(0, &orphan_timer))
  119. __module_get(THIS_MODULE);
  120. /*
  121. * Activate timer
  122. */
  123. softdog_keepalive();
  124. return nonseekable_open(inode, file);
  125. }
  126. static int softdog_release(struct inode *inode, struct file *file)
  127. {
  128. /*
  129. * Shut off the timer.
  130. * Lock it in if it's a module and we set nowayout
  131. */
  132. if (expect_close == 42) {
  133. softdog_stop();
  134. module_put(THIS_MODULE);
  135. } else {
  136. printk(KERN_CRIT PFX
  137. "Unexpected close, not stopping watchdog!\n");
  138. set_bit(0, &orphan_timer);
  139. softdog_keepalive();
  140. }
  141. clear_bit(0, &driver_open);
  142. expect_close = 0;
  143. return 0;
  144. }
  145. static ssize_t softdog_write(struct file *file, const char __user *data,
  146. size_t len, loff_t *ppos)
  147. {
  148. /*
  149. * Refresh the timer.
  150. */
  151. if (len) {
  152. if (!nowayout) {
  153. size_t i;
  154. /* In case it was set long ago */
  155. expect_close = 0;
  156. for (i = 0; i != len; i++) {
  157. char c;
  158. if (get_user(c, data + i))
  159. return -EFAULT;
  160. if (c == 'V')
  161. expect_close = 42;
  162. }
  163. }
  164. softdog_keepalive();
  165. }
  166. return len;
  167. }
  168. static long softdog_ioctl(struct file *file, unsigned int cmd,
  169. unsigned long arg)
  170. {
  171. void __user *argp = (void __user *)arg;
  172. int __user *p = argp;
  173. int new_margin;
  174. static const struct watchdog_info ident = {
  175. .options = WDIOF_SETTIMEOUT |
  176. WDIOF_KEEPALIVEPING |
  177. WDIOF_MAGICCLOSE,
  178. .firmware_version = 0,
  179. .identity = "Software Watchdog",
  180. };
  181. switch (cmd) {
  182. case WDIOC_GETSUPPORT:
  183. return copy_to_user(argp, &ident, 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. default:
  200. return -ENOTTY;
  201. }
  202. }
  203. /*
  204. * Notifier for system down
  205. */
  206. static int softdog_notify_sys(struct notifier_block *this, unsigned long code,
  207. void *unused)
  208. {
  209. if (code == SYS_DOWN || code == SYS_HALT)
  210. /* Turn the WDT off */
  211. softdog_stop();
  212. return NOTIFY_DONE;
  213. }
  214. /*
  215. * Kernel Interfaces
  216. */
  217. static const struct file_operations softdog_fops = {
  218. .owner = THIS_MODULE,
  219. .llseek = no_llseek,
  220. .write = softdog_write,
  221. .unlocked_ioctl = softdog_ioctl,
  222. .open = softdog_open,
  223. .release = softdog_release,
  224. };
  225. static struct miscdevice softdog_miscdev = {
  226. .minor = WATCHDOG_MINOR,
  227. .name = "watchdog",
  228. .fops = &softdog_fops,
  229. };
  230. static struct notifier_block softdog_notifier = {
  231. .notifier_call = softdog_notify_sys,
  232. };
  233. static char banner[] __initdata = KERN_INFO "Software Watchdog Timer: 0.07 initialized. soft_noboot=%d soft_margin=%d sec (nowayout= %d)\n";
  234. static int __init watchdog_init(void)
  235. {
  236. int ret;
  237. /* Check that the soft_margin value is within it's range;
  238. if not reset to the default */
  239. if (softdog_set_heartbeat(soft_margin)) {
  240. softdog_set_heartbeat(TIMER_MARGIN);
  241. printk(KERN_INFO PFX
  242. "soft_margin must be 0 < soft_margin < 65536, using %d\n",
  243. TIMER_MARGIN);
  244. }
  245. ret = register_reboot_notifier(&softdog_notifier);
  246. if (ret) {
  247. printk(KERN_ERR PFX
  248. "cannot register reboot notifier (err=%d)\n", ret);
  249. return ret;
  250. }
  251. ret = misc_register(&softdog_miscdev);
  252. if (ret) {
  253. printk(KERN_ERR PFX
  254. "cannot register miscdev on minor=%d (err=%d)\n",
  255. WATCHDOG_MINOR, ret);
  256. unregister_reboot_notifier(&softdog_notifier);
  257. return ret;
  258. }
  259. printk(banner, soft_noboot, soft_margin, nowayout);
  260. return 0;
  261. }
  262. static void __exit watchdog_exit(void)
  263. {
  264. misc_deregister(&softdog_miscdev);
  265. unregister_reboot_notifier(&softdog_notifier);
  266. }
  267. module_init(watchdog_init);
  268. module_exit(watchdog_exit);
  269. MODULE_AUTHOR("Alan Cox");
  270. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  271. MODULE_LICENSE("GPL");
  272. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);