softdog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SoftDog: A Software Watchdog Device
  3. *
  4. * (c) Copyright 1996 Alan Cox <[email protected]>,
  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 <[email protected]>
  17. *
  18. * Software only watchdog driver. Unlike its big brother the WDT501P
  19. * driver this won't always recover a failed machine.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/reboot.h>
  28. #include <linux/timer.h>
  29. #include <linux/types.h>
  30. #include <linux/watchdog.h>
  31. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  32. static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
  33. module_param(soft_margin, uint, 0);
  34. MODULE_PARM_DESC(soft_margin,
  35. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  36. __MODULE_STRING(TIMER_MARGIN) ")");
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. module_param(nowayout, bool, 0);
  39. MODULE_PARM_DESC(nowayout,
  40. "Watchdog cannot be stopped once started (default="
  41. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  42. static int soft_noboot;
  43. module_param(soft_noboot, int, 0);
  44. MODULE_PARM_DESC(soft_noboot,
  45. "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
  46. static int soft_panic;
  47. module_param(soft_panic, int, 0);
  48. MODULE_PARM_DESC(soft_panic,
  49. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  50. static void softdog_fire(unsigned long data)
  51. {
  52. module_put(THIS_MODULE);
  53. if (soft_noboot) {
  54. pr_crit("Triggered - Reboot ignored\n");
  55. } else if (soft_panic) {
  56. pr_crit("Initiating panic\n");
  57. panic("Software Watchdog Timer expired");
  58. } else {
  59. pr_crit("Initiating system reboot\n");
  60. emergency_restart();
  61. pr_crit("Reboot didn't ?????\n");
  62. }
  63. }
  64. static struct timer_list softdog_ticktock =
  65. TIMER_INITIALIZER(softdog_fire, 0, 0);
  66. static struct watchdog_device softdog_dev;
  67. static void softdog_pretimeout(unsigned long data)
  68. {
  69. watchdog_notify_pretimeout(&softdog_dev);
  70. }
  71. static struct timer_list softdog_preticktock =
  72. TIMER_INITIALIZER(softdog_pretimeout, 0, 0);
  73. static int softdog_ping(struct watchdog_device *w)
  74. {
  75. if (!mod_timer(&softdog_ticktock, jiffies + (w->timeout * HZ)))
  76. __module_get(THIS_MODULE);
  77. if (w->pretimeout)
  78. mod_timer(&softdog_preticktock, jiffies +
  79. (w->timeout - w->pretimeout) * HZ);
  80. else
  81. del_timer(&softdog_preticktock);
  82. return 0;
  83. }
  84. static int softdog_stop(struct watchdog_device *w)
  85. {
  86. if (del_timer(&softdog_ticktock))
  87. module_put(THIS_MODULE);
  88. del_timer(&softdog_preticktock);
  89. return 0;
  90. }
  91. static struct watchdog_info softdog_info = {
  92. .identity = "Software Watchdog",
  93. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
  94. WDIOF_PRETIMEOUT,
  95. };
  96. static const struct watchdog_ops softdog_ops = {
  97. .owner = THIS_MODULE,
  98. .start = softdog_ping,
  99. .stop = softdog_stop,
  100. };
  101. static struct watchdog_device softdog_dev = {
  102. .info = &softdog_info,
  103. .ops = &softdog_ops,
  104. .min_timeout = 1,
  105. .max_timeout = 65535,
  106. .timeout = TIMER_MARGIN,
  107. };
  108. static int __init softdog_init(void)
  109. {
  110. int ret;
  111. watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
  112. watchdog_set_nowayout(&softdog_dev, nowayout);
  113. watchdog_stop_on_reboot(&softdog_dev);
  114. ret = watchdog_register_device(&softdog_dev);
  115. if (ret)
  116. return ret;
  117. pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
  118. soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
  119. return 0;
  120. }
  121. module_init(softdog_init);
  122. static void __exit softdog_exit(void)
  123. {
  124. watchdog_unregister_device(&softdog_dev);
  125. }
  126. module_exit(softdog_exit);
  127. MODULE_AUTHOR("Alan Cox");
  128. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  129. MODULE_LICENSE("GPL");