watchdog.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_frequency = 5;
  27. static void watchdog_timeout_cb(struct uloop_timeout *t)
  28. {
  29. DEBUG(2, "Ping\n");
  30. if (write(wdt_fd, "X", 1) < 0)
  31. ERROR("WDT failed to write: %s\n", strerror(errno));
  32. uloop_timeout_set(t, wdt_frequency * 1000);
  33. }
  34. void watchdog_set_stopped(bool val)
  35. {
  36. if (val)
  37. uloop_timeout_cancel(&wdt_timeout);
  38. else
  39. watchdog_timeout_cb(&wdt_timeout);
  40. }
  41. bool watchdog_get_stopped(void)
  42. {
  43. return !wdt_timeout.pending;
  44. }
  45. int watchdog_timeout(int timeout)
  46. {
  47. if (wdt_fd < 0)
  48. return 0;
  49. if (timeout) {
  50. DEBUG(2, "Set watchdog timeout: %ds\n", timeout);
  51. ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
  52. }
  53. ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout);
  54. return timeout;
  55. }
  56. int watchdog_frequency(int frequency)
  57. {
  58. if (wdt_fd < 0)
  59. return 0;
  60. if (frequency) {
  61. DEBUG(2, "Set watchdog frequency: %ds\n", frequency);
  62. wdt_frequency = frequency;
  63. }
  64. return wdt_frequency;
  65. }
  66. char* watchdog_fd(void)
  67. {
  68. static char fd_buf[3];
  69. if (wdt_fd < 0)
  70. return NULL;
  71. snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
  72. return fd_buf;
  73. }
  74. void watchdog_init(int preinit)
  75. {
  76. char *env = getenv("WDTFD");
  77. if (wdt_fd >= 0)
  78. return;
  79. wdt_timeout.cb = watchdog_timeout_cb;
  80. if (env) {
  81. DEBUG(1, "Watchdog handover: fd=%s\n", env);
  82. wdt_fd = atoi(env);
  83. unsetenv("WDTFD");
  84. } else {
  85. wdt_fd = open("/dev/watchdog", O_WRONLY);
  86. }
  87. if (wdt_fd < 0)
  88. return;
  89. if (!preinit)
  90. fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);
  91. LOG("- watchdog -\n");
  92. watchdog_timeout(30);
  93. watchdog_timeout_cb(&wdt_timeout);
  94. DEBUG(2, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
  95. }