watchdog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(void)
  59. {
  60. if (wdt_fd < 0)
  61. return;
  62. if (write(wdt_fd, "V", 1) < 0)
  63. ERROR("WDT failed to write release: %m\n");
  64. if (close(wdt_fd) == -1)
  65. ERROR("WDT failed to close watchdog: %m\n");
  66. wdt_fd = -1;
  67. }
  68. static int watchdog_set_drv_timeout(void)
  69. {
  70. if (wdt_fd < 0)
  71. return -1;
  72. return ioctl(wdt_fd, WDIOC_SETTIMEOUT, &wdt_drv_timeout);
  73. }
  74. static void watchdog_print_status(void)
  75. {
  76. struct watchdog_info wdt_info;
  77. int bootstatus;
  78. if (wdt_fd < 0)
  79. return;
  80. if (ioctl(wdt_fd, WDIOC_GETSUPPORT, &wdt_info)) {
  81. DEBUG(2, "Watchdog GETSUPPORT failed\n");
  82. return;
  83. }
  84. if (!(wdt_info.options & WDIOF_CARDRESET)) {
  85. DEBUG(2, "Watchdog does not have CARDRESET support\n");
  86. return;
  87. }
  88. if (ioctl(wdt_fd, WDIOC_GETBOOTSTATUS, &bootstatus)) {
  89. DEBUG(2, "Watchdog GETBOOTSTATUS failed\n");
  90. return;
  91. }
  92. if (bootstatus & WDIOF_CARDRESET)
  93. LOG("Watchdog has previously reset the system\n");
  94. else
  95. DEBUG(2, "Watchdog did not previously reset the system\n");
  96. }
  97. void watchdog_set_magicclose(bool val)
  98. {
  99. wdt_magicclose = val;
  100. }
  101. bool watchdog_get_magicclose(void)
  102. {
  103. return wdt_magicclose;
  104. }
  105. void watchdog_set_stopped(bool val)
  106. {
  107. if (val) {
  108. uloop_timeout_cancel(&wdt_timeout);
  109. if (wdt_magicclose)
  110. watchdog_close();
  111. }
  112. else {
  113. watchdog_open(true);
  114. watchdog_set_drv_timeout();
  115. watchdog_timeout_cb(&wdt_timeout);
  116. }
  117. }
  118. bool watchdog_get_stopped(void)
  119. {
  120. return !wdt_timeout.pending;
  121. }
  122. int watchdog_timeout(int timeout)
  123. {
  124. if (timeout) {
  125. DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
  126. wdt_drv_timeout = timeout;
  127. if (wdt_fd >= 0)
  128. watchdog_set_drv_timeout();
  129. }
  130. return wdt_drv_timeout;
  131. }
  132. int watchdog_frequency(int frequency)
  133. {
  134. if (frequency) {
  135. DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
  136. wdt_frequency = frequency;
  137. }
  138. return wdt_frequency;
  139. }
  140. char* watchdog_fd(void)
  141. {
  142. static char fd_buf[12];
  143. if (wdt_fd < 0)
  144. return NULL;
  145. snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
  146. return fd_buf;
  147. }
  148. void watchdog_init(int preinit)
  149. {
  150. wdt_timeout.cb = watchdog_timeout_cb;
  151. if (watchdog_open(!preinit) < 0)
  152. return;
  153. LOG("- watchdog -\n");
  154. watchdog_set_drv_timeout();
  155. watchdog_timeout_cb(&wdt_timeout);
  156. DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
  157. watchdog_print_status();
  158. }
  159. void watchdog_set_cloexec(bool val)
  160. {
  161. if (wdt_fd < 0)
  162. return;
  163. int flags = fcntl(wdt_fd, F_GETFD);
  164. if (val)
  165. flags |= FD_CLOEXEC;
  166. else
  167. flags &= ~FD_CLOEXEC;
  168. fcntl(wdt_fd, F_SETFD, flags);
  169. }