watchdog.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. void watchdog_set_magicclose(bool val)
  75. {
  76. wdt_magicclose = val;
  77. }
  78. bool watchdog_get_magicclose(void)
  79. {
  80. return wdt_magicclose;
  81. }
  82. void watchdog_set_stopped(bool val)
  83. {
  84. if (val) {
  85. uloop_timeout_cancel(&wdt_timeout);
  86. if (wdt_magicclose)
  87. watchdog_close();
  88. }
  89. else {
  90. watchdog_open(true);
  91. watchdog_set_drv_timeout();
  92. watchdog_timeout_cb(&wdt_timeout);
  93. }
  94. }
  95. bool watchdog_get_stopped(void)
  96. {
  97. return !wdt_timeout.pending;
  98. }
  99. int watchdog_timeout(int timeout)
  100. {
  101. if (timeout) {
  102. DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
  103. wdt_drv_timeout = timeout;
  104. if (wdt_fd >= 0)
  105. watchdog_set_drv_timeout();
  106. }
  107. return wdt_drv_timeout;
  108. }
  109. int watchdog_frequency(int frequency)
  110. {
  111. if (frequency) {
  112. DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
  113. wdt_frequency = frequency;
  114. }
  115. return wdt_frequency;
  116. }
  117. char* watchdog_fd(void)
  118. {
  119. static char fd_buf[12];
  120. if (wdt_fd < 0)
  121. return NULL;
  122. snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
  123. return fd_buf;
  124. }
  125. void watchdog_init(int preinit)
  126. {
  127. wdt_timeout.cb = watchdog_timeout_cb;
  128. if (watchdog_open(!preinit) < 0)
  129. return;
  130. LOG("- watchdog -\n");
  131. watchdog_set_drv_timeout();
  132. watchdog_timeout_cb(&wdt_timeout);
  133. DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
  134. }
  135. void watchdog_set_cloexec(bool val)
  136. {
  137. if (wdt_fd < 0)
  138. return;
  139. int flags = fcntl(wdt_fd, F_GETFD);
  140. if (val)
  141. flags |= FD_CLOEXEC;
  142. else
  143. flags &= ~FD_CLOEXEC;
  144. fcntl(wdt_fd, F_SETFD, flags);
  145. }