watchdog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/watchdog.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <libubox/uloop.h>
  21. #include "procd.h"
  22. #include "watchdog.h"
  23. #define WDT_PATH "/dev/watchdog"
  24. static struct uloop_timeout wdt_timeout;
  25. static int wdt_fd = -1;
  26. static int wdt_drv_timeout = 30;
  27. static int wdt_frequency = 5;
  28. static bool wdt_magicclose = false;
  29. void watchdog_ping(void)
  30. {
  31. DEBUG(4, "Ping\n");
  32. if (wdt_fd >= 0 && write(wdt_fd, "X", 1) < 0)
  33. ERROR("WDT failed to write: %m\n");
  34. }
  35. static void watchdog_timeout_cb(struct uloop_timeout *t)
  36. {
  37. watchdog_ping();
  38. uloop_timeout_set(t, wdt_frequency * 1000);
  39. }
  40. static int watchdog_open(bool cloexec)
  41. {
  42. char *env = getenv("WDTFD");
  43. if (wdt_fd >= 0)
  44. return wdt_fd;
  45. if (env) {
  46. DEBUG(2, "Watchdog handover: fd=%s\n", env);
  47. wdt_fd = atoi(env);
  48. unsetenv("WDTFD");
  49. } else {
  50. wdt_fd = open(WDT_PATH, O_WRONLY);
  51. }
  52. if (wdt_fd < 0)
  53. return wdt_fd;
  54. if (cloexec)
  55. fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);
  56. return wdt_fd;
  57. }
  58. static void watchdog_close(bool with_release)
  59. {
  60. if (wdt_fd < 0)
  61. return;
  62. if (with_release) {
  63. if (write(wdt_fd, "V", 1) < 0)
  64. ERROR("WDT failed to write release: %m\n");
  65. }
  66. if (close(wdt_fd) == -1)
  67. ERROR("WDT failed to close watchdog: %m\n");
  68. wdt_fd = -1;
  69. }
  70. static int watchdog_set_drv_timeout(void)
  71. {
  72. if (wdt_fd < 0)
  73. return -1;
  74. return ioctl(wdt_fd, WDIOC_SETTIMEOUT, &wdt_drv_timeout);
  75. }
  76. static void watchdog_print_status(void)
  77. {
  78. struct watchdog_info wdt_info;
  79. int bootstatus;
  80. if (wdt_fd < 0)
  81. return;
  82. if (ioctl(wdt_fd, WDIOC_GETSUPPORT, &wdt_info)) {
  83. DEBUG(2, "Watchdog GETSUPPORT failed\n");
  84. return;
  85. }
  86. if (!(wdt_info.options & WDIOF_CARDRESET)) {
  87. DEBUG(2, "Watchdog does not have CARDRESET support\n");
  88. return;
  89. }
  90. if (ioctl(wdt_fd, WDIOC_GETBOOTSTATUS, &bootstatus)) {
  91. DEBUG(2, "Watchdog GETBOOTSTATUS failed\n");
  92. return;
  93. }
  94. if (bootstatus & WDIOF_CARDRESET)
  95. LOG("Watchdog has previously reset the system\n");
  96. else
  97. DEBUG(2, "Watchdog did not previously reset the system\n");
  98. }
  99. void watchdog_set_magicclose(bool val)
  100. {
  101. wdt_magicclose = val;
  102. }
  103. bool watchdog_get_magicclose(void)
  104. {
  105. return wdt_magicclose;
  106. }
  107. void watchdog_set_stopped(bool val)
  108. {
  109. if (val) {
  110. uloop_timeout_cancel(&wdt_timeout);
  111. watchdog_close(wdt_magicclose);
  112. }
  113. else {
  114. watchdog_open(true);
  115. watchdog_set_drv_timeout();
  116. watchdog_timeout_cb(&wdt_timeout);
  117. }
  118. }
  119. bool watchdog_get_stopped(void)
  120. {
  121. return !wdt_timeout.pending;
  122. }
  123. int watchdog_timeout(int timeout)
  124. {
  125. if (timeout) {
  126. DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
  127. wdt_drv_timeout = timeout;
  128. if (wdt_fd >= 0)
  129. watchdog_set_drv_timeout();
  130. }
  131. return wdt_drv_timeout;
  132. }
  133. int watchdog_frequency(int frequency)
  134. {
  135. if (frequency) {
  136. DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
  137. wdt_frequency = frequency;
  138. }
  139. return wdt_frequency;
  140. }
  141. char* watchdog_fd(void)
  142. {
  143. static char fd_buf[12];
  144. if (wdt_fd < 0)
  145. return NULL;
  146. snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
  147. return fd_buf;
  148. }
  149. void watchdog_init(int preinit)
  150. {
  151. wdt_timeout.cb = watchdog_timeout_cb;
  152. if (watchdog_open(!preinit) < 0)
  153. return;
  154. LOG("- watchdog -\n");
  155. watchdog_set_drv_timeout();
  156. watchdog_timeout_cb(&wdt_timeout);
  157. DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
  158. watchdog_print_status();
  159. }
  160. void watchdog_set_cloexec(bool val)
  161. {
  162. if (wdt_fd < 0)
  163. return;
  164. int flags = fcntl(wdt_fd, F_GETFD);
  165. if (val)
  166. flags |= FD_CLOEXEC;
  167. else
  168. flags &= ~FD_CLOEXEC;
  169. fcntl(wdt_fd, F_SETFD, flags);
  170. }