watchdog-api.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. Last reviewed: 10/05/2007
  2. The Linux Watchdog driver API.
  3. Copyright 2002 Christer Weingel <wingel@nano-system.com>
  4. Some parts of this document are copied verbatim from the sbc60xxwdt
  5. driver which is (c) Copyright 2000 Jakob Oestergaard <jakob@ostenfeld.dk>
  6. This document describes the state of the Linux 2.4.18 kernel.
  7. Introduction:
  8. A Watchdog Timer (WDT) is a hardware circuit that can reset the
  9. computer system in case of a software fault. You probably knew that
  10. already.
  11. Usually a userspace daemon will notify the kernel watchdog driver via the
  12. /dev/watchdog special device file that userspace is still alive, at
  13. regular intervals. When such a notification occurs, the driver will
  14. usually tell the hardware watchdog that everything is in order, and
  15. that the watchdog should wait for yet another little while to reset
  16. the system. If userspace fails (RAM error, kernel bug, whatever), the
  17. notifications cease to occur, and the hardware watchdog will reset the
  18. system (causing a reboot) after the timeout occurs.
  19. The Linux watchdog API is a rather ad-hoc construction and different
  20. drivers implement different, and sometimes incompatible, parts of it.
  21. This file is an attempt to document the existing usage and allow
  22. future driver writers to use it as a reference.
  23. The simplest API:
  24. All drivers support the basic mode of operation, where the watchdog
  25. activates as soon as /dev/watchdog is opened and will reboot unless
  26. the watchdog is pinged within a certain time, this time is called the
  27. timeout or margin. The simplest way to ping the watchdog is to write
  28. some data to the device. So a very simple watchdog daemon would look
  29. like this source file: see Documentation/watchdog/src/watchdog-simple.c
  30. A more advanced driver could for example check that a HTTP server is
  31. still responding before doing the write call to ping the watchdog.
  32. When the device is closed, the watchdog is disabled. This is not
  33. always such a good idea, since if there is a bug in the watchdog
  34. daemon and it crashes the system will not reboot. Because of this,
  35. some of the drivers support the configuration option "Disable watchdog
  36. shutdown on close", CONFIG_WATCHDOG_NOWAYOUT. If it is set to Y when
  37. compiling the kernel, there is no way of disabling the watchdog once
  38. it has been started. So, if the watchdog daemon crashes, the system
  39. will reboot after the timeout has passed. Watchdog devices also usually
  40. support the nowayout module parameter so that this option can be controlled
  41. at runtime.
  42. Drivers will not disable the watchdog, unless a specific magic character 'V'
  43. has been sent /dev/watchdog just before closing the file. If the userspace
  44. daemon closes the file without sending this special character, the driver
  45. will assume that the daemon (and userspace in general) died, and will stop
  46. pinging the watchdog without disabling it first. This will then cause a
  47. reboot if the watchdog is not re-opened in sufficient time.
  48. The ioctl API:
  49. All conforming drivers also support an ioctl API.
  50. Pinging the watchdog using an ioctl:
  51. All drivers that have an ioctl interface support at least one ioctl,
  52. KEEPALIVE. This ioctl does exactly the same thing as a write to the
  53. watchdog device, so the main loop in the above program could be
  54. replaced with:
  55. while (1) {
  56. ioctl(fd, WDIOC_KEEPALIVE, 0);
  57. sleep(10);
  58. }
  59. the argument to the ioctl is ignored.
  60. Setting and getting the timeout:
  61. For some drivers it is possible to modify the watchdog timeout on the
  62. fly with the SETTIMEOUT ioctl, those drivers have the WDIOF_SETTIMEOUT
  63. flag set in their option field. The argument is an integer
  64. representing the timeout in seconds. The driver returns the real
  65. timeout used in the same variable, and this timeout might differ from
  66. the requested one due to limitation of the hardware.
  67. int timeout = 45;
  68. ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
  69. printf("The timeout was set to %d seconds\n", timeout);
  70. This example might actually print "The timeout was set to 60 seconds"
  71. if the device has a granularity of minutes for its timeout.
  72. Starting with the Linux 2.4.18 kernel, it is possible to query the
  73. current timeout using the GETTIMEOUT ioctl.
  74. ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
  75. printf("The timeout was is %d seconds\n", timeout);
  76. Pretimeouts:
  77. Some watchdog timers can be set to have a trigger go off before the
  78. actual time they will reset the system. This can be done with an NMI,
  79. interrupt, or other mechanism. This allows Linux to record useful
  80. information (like panic information and kernel coredumps) before it
  81. resets.
  82. pretimeout = 10;
  83. ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout);
  84. Note that the pretimeout is the number of seconds before the time
  85. when the timeout will go off. It is not the number of seconds until
  86. the pretimeout. So, for instance, if you set the timeout to 60 seconds
  87. and the pretimeout to 10 seconds, the pretimout will go of in 50
  88. seconds. Setting a pretimeout to zero disables it.
  89. There is also a get function for getting the pretimeout:
  90. ioctl(fd, WDIOC_GETPRETIMEOUT, &timeout);
  91. printf("The pretimeout was is %d seconds\n", timeout);
  92. Not all watchdog drivers will support a pretimeout.
  93. Get the number of seconds before reboot:
  94. Some watchdog drivers have the ability to report the remaining time
  95. before the system will reboot. The WDIOC_GETTIMELEFT is the ioctl
  96. that returns the number of seconds before reboot.
  97. ioctl(fd, WDIOC_GETTIMELEFT, &timeleft);
  98. printf("The timeout was is %d seconds\n", timeleft);
  99. Environmental monitoring:
  100. All watchdog drivers are required return more information about the system,
  101. some do temperature, fan and power level monitoring, some can tell you
  102. the reason for the last reboot of the system. The GETSUPPORT ioctl is
  103. available to ask what the device can do:
  104. struct watchdog_info ident;
  105. ioctl(fd, WDIOC_GETSUPPORT, &ident);
  106. the fields returned in the ident struct are:
  107. identity a string identifying the watchdog driver
  108. firmware_version the firmware version of the card if available
  109. options a flags describing what the device supports
  110. the options field can have the following bits set, and describes what
  111. kind of information that the GET_STATUS and GET_BOOT_STATUS ioctls can
  112. return. [FIXME -- Is this correct?]
  113. WDIOF_OVERHEAT Reset due to CPU overheat
  114. The machine was last rebooted by the watchdog because the thermal limit was
  115. exceeded
  116. WDIOF_FANFAULT Fan failed
  117. A system fan monitored by the watchdog card has failed
  118. WDIOF_EXTERN1 External relay 1
  119. External monitoring relay/source 1 was triggered. Controllers intended for
  120. real world applications include external monitoring pins that will trigger
  121. a reset.
  122. WDIOF_EXTERN2 External relay 2
  123. External monitoring relay/source 2 was triggered
  124. WDIOF_POWERUNDER Power bad/power fault
  125. The machine is showing an undervoltage status
  126. WDIOF_CARDRESET Card previously reset the CPU
  127. The last reboot was caused by the watchdog card
  128. WDIOF_POWEROVER Power over voltage
  129. The machine is showing an overvoltage status. Note that if one level is
  130. under and one over both bits will be set - this may seem odd but makes
  131. sense.
  132. WDIOF_KEEPALIVEPING Keep alive ping reply
  133. The watchdog saw a keepalive ping since it was last queried.
  134. WDIOF_SETTIMEOUT Can set/get the timeout
  135. The watchdog can do pretimeouts.
  136. WDIOF_PRETIMEOUT Pretimeout (in seconds), get/set
  137. For those drivers that return any bits set in the option field, the
  138. GETSTATUS and GETBOOTSTATUS ioctls can be used to ask for the current
  139. status, and the status at the last reboot, respectively.
  140. int flags;
  141. ioctl(fd, WDIOC_GETSTATUS, &flags);
  142. or
  143. ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
  144. Note that not all devices support these two calls, and some only
  145. support the GETBOOTSTATUS call.
  146. Some drivers can measure the temperature using the GETTEMP ioctl. The
  147. returned value is the temperature in degrees fahrenheit.
  148. int temperature;
  149. ioctl(fd, WDIOC_GETTEMP, &temperature);
  150. Finally the SETOPTIONS ioctl can be used to control some aspects of
  151. the cards operation; right now the pcwd driver is the only one
  152. supporting this ioctl.
  153. int options = 0;
  154. ioctl(fd, WDIOC_SETOPTIONS, options);
  155. The following options are available:
  156. WDIOS_DISABLECARD Turn off the watchdog timer
  157. WDIOS_ENABLECARD Turn on the watchdog timer
  158. WDIOS_TEMPPANIC Kernel panic on temperature trip
  159. [FIXME -- better explanations]