softdog.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * SoftDog: 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/types.h>
  42. #include <linux/timer.h>
  43. #include <linux/miscdevice.h>
  44. #include <linux/watchdog.h>
  45. #include <linux/notifier.h>
  46. #include <linux/reboot.h>
  47. #include <linux/init.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/kernel.h>
  50. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  51. static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
  52. module_param(soft_margin, uint, 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 bool nowayout = WATCHDOG_NOWAYOUT;
  57. module_param(nowayout, bool, 0);
  58. MODULE_PARM_DESC(nowayout,
  59. "Watchdog cannot be stopped once started (default="
  60. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  61. static int soft_noboot = 0;
  62. module_param(soft_noboot, int, 0);
  63. MODULE_PARM_DESC(soft_noboot,
  64. "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
  65. static int soft_panic;
  66. module_param(soft_panic, int, 0);
  67. MODULE_PARM_DESC(soft_panic,
  68. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  69. /*
  70. * Our timer
  71. */
  72. static void watchdog_fire(unsigned long);
  73. static struct timer_list watchdog_ticktock =
  74. TIMER_INITIALIZER(watchdog_fire, 0, 0);
  75. /*
  76. * If the timer expires..
  77. */
  78. static void watchdog_fire(unsigned long data)
  79. {
  80. if (soft_noboot)
  81. pr_crit("Triggered - Reboot ignored\n");
  82. else if (soft_panic) {
  83. pr_crit("Initiating panic\n");
  84. panic("Software Watchdog Timer expired");
  85. } else {
  86. pr_crit("Initiating system reboot\n");
  87. emergency_restart();
  88. pr_crit("Reboot didn't ?????\n");
  89. }
  90. }
  91. /*
  92. * Softdog operations
  93. */
  94. static int softdog_ping(struct watchdog_device *w)
  95. {
  96. mod_timer(&watchdog_ticktock, jiffies+(w->timeout*HZ));
  97. return 0;
  98. }
  99. static int softdog_stop(struct watchdog_device *w)
  100. {
  101. del_timer(&watchdog_ticktock);
  102. return 0;
  103. }
  104. static int softdog_set_timeout(struct watchdog_device *w, unsigned int t)
  105. {
  106. w->timeout = t;
  107. return 0;
  108. }
  109. /*
  110. * Notifier for system down
  111. */
  112. static int softdog_notify_sys(struct notifier_block *this, unsigned long code,
  113. void *unused)
  114. {
  115. if (code == SYS_DOWN || code == SYS_HALT)
  116. /* Turn the WDT off */
  117. softdog_stop(NULL);
  118. return NOTIFY_DONE;
  119. }
  120. /*
  121. * Kernel Interfaces
  122. */
  123. static struct notifier_block softdog_notifier = {
  124. .notifier_call = softdog_notify_sys,
  125. };
  126. static struct watchdog_info softdog_info = {
  127. .identity = "Software Watchdog",
  128. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  129. };
  130. static struct watchdog_ops softdog_ops = {
  131. .owner = THIS_MODULE,
  132. .start = softdog_ping,
  133. .stop = softdog_stop,
  134. .ping = softdog_ping,
  135. .set_timeout = softdog_set_timeout,
  136. };
  137. static struct watchdog_device softdog_dev = {
  138. .info = &softdog_info,
  139. .ops = &softdog_ops,
  140. .min_timeout = 1,
  141. .max_timeout = 0xFFFF
  142. };
  143. static int __init watchdog_init(void)
  144. {
  145. int ret;
  146. /* Check that the soft_margin value is within it's range;
  147. if not reset to the default */
  148. if (soft_margin < 1 || soft_margin > 65535) {
  149. pr_info("soft_margin must be 0 < soft_margin < 65536, using %d\n",
  150. TIMER_MARGIN);
  151. return -EINVAL;
  152. }
  153. softdog_dev.timeout = soft_margin;
  154. watchdog_set_nowayout(&softdog_dev, nowayout);
  155. ret = register_reboot_notifier(&softdog_notifier);
  156. if (ret) {
  157. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  158. return ret;
  159. }
  160. ret = watchdog_register_device(&softdog_dev);
  161. if (ret) {
  162. unregister_reboot_notifier(&softdog_notifier);
  163. return ret;
  164. }
  165. pr_info("Software Watchdog Timer: 0.08 initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
  166. soft_noboot, soft_margin, soft_panic, nowayout);
  167. return 0;
  168. }
  169. static void __exit watchdog_exit(void)
  170. {
  171. watchdog_unregister_device(&softdog_dev);
  172. unregister_reboot_notifier(&softdog_notifier);
  173. }
  174. module_init(watchdog_init);
  175. module_exit(watchdog_exit);
  176. MODULE_AUTHOR("Alan Cox");
  177. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  178. MODULE_LICENSE("GPL");
  179. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);