Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 1 | ============================= |
| 2 | The Linux Watchdog driver API |
| 3 | ============================= |
| 4 | |
Alan Cox | 4d389dc | 2007-05-23 14:43:52 -0700 | [diff] [blame] | 5 | Last reviewed: 10/05/2007 |
| 6 | |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | Copyright 2002 Christer Weingel <wingel@nano-system.com> |
| 10 | |
| 11 | Some parts of this document are copied verbatim from the sbc60xxwdt |
| 12 | driver which is (c) Copyright 2000 Jakob Oestergaard <jakob@ostenfeld.dk> |
| 13 | |
| 14 | This document describes the state of the Linux 2.4.18 kernel. |
| 15 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 16 | Introduction |
| 17 | ============ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | A Watchdog Timer (WDT) is a hardware circuit that can reset the |
| 20 | computer system in case of a software fault. You probably knew that |
| 21 | already. |
| 22 | |
| 23 | Usually a userspace daemon will notify the kernel watchdog driver via the |
| 24 | /dev/watchdog special device file that userspace is still alive, at |
| 25 | regular intervals. When such a notification occurs, the driver will |
| 26 | usually tell the hardware watchdog that everything is in order, and |
| 27 | that the watchdog should wait for yet another little while to reset |
| 28 | the system. If userspace fails (RAM error, kernel bug, whatever), the |
| 29 | notifications cease to occur, and the hardware watchdog will reset the |
| 30 | system (causing a reboot) after the timeout occurs. |
| 31 | |
Alan Cox | 4d389dc | 2007-05-23 14:43:52 -0700 | [diff] [blame] | 32 | The Linux watchdog API is a rather ad-hoc construction and different |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | drivers implement different, and sometimes incompatible, parts of it. |
| 34 | This file is an attempt to document the existing usage and allow |
| 35 | future driver writers to use it as a reference. |
| 36 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 37 | The simplest API |
| 38 | ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | All drivers support the basic mode of operation, where the watchdog |
| 41 | activates as soon as /dev/watchdog is opened and will reboot unless |
| 42 | the watchdog is pinged within a certain time, this time is called the |
| 43 | timeout or margin. The simplest way to ping the watchdog is to write |
| 44 | some data to the device. So a very simple watchdog daemon would look |
Shuah Khan | 071bf69 | 2016-09-16 17:40:40 -0600 | [diff] [blame] | 45 | like this source file: see samples/watchdog/watchdog-simple.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | A more advanced driver could for example check that a HTTP server is |
| 48 | still responding before doing the write call to ping the watchdog. |
| 49 | |
Andrew Dyer | 0d710cb | 2008-01-08 14:40:37 -0600 | [diff] [blame] | 50 | When the device is closed, the watchdog is disabled, unless the "Magic |
| 51 | Close" feature is supported (see below). This is not always such a |
| 52 | good idea, since if there is a bug in the watchdog daemon and it |
| 53 | crashes the system will not reboot. Because of this, some of the |
| 54 | drivers support the configuration option "Disable watchdog shutdown on |
| 55 | close", CONFIG_WATCHDOG_NOWAYOUT. If it is set to Y when compiling |
| 56 | the kernel, there is no way of disabling the watchdog once it has been |
| 57 | started. So, if the watchdog daemon crashes, the system will reboot |
| 58 | after the timeout has passed. Watchdog devices also usually support |
| 59 | the nowayout module parameter so that this option can be controlled at |
| 60 | runtime. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 62 | Magic Close feature |
| 63 | =================== |
Andrew Dyer | 0d710cb | 2008-01-08 14:40:37 -0600 | [diff] [blame] | 64 | |
| 65 | If a driver supports "Magic Close", the driver will not disable the |
| 66 | watchdog unless a specific magic character 'V' has been sent to |
| 67 | /dev/watchdog just before closing the file. If the userspace daemon |
| 68 | closes the file without sending this special character, the driver |
| 69 | will assume that the daemon (and userspace in general) died, and will |
| 70 | stop pinging the watchdog without disabling it first. This will then |
| 71 | cause a reboot if the watchdog is not re-opened in sufficient time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 73 | The ioctl API |
| 74 | ============= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | All conforming drivers also support an ioctl API. |
| 77 | |
| 78 | Pinging the watchdog using an ioctl: |
| 79 | |
| 80 | All drivers that have an ioctl interface support at least one ioctl, |
| 81 | KEEPALIVE. This ioctl does exactly the same thing as a write to the |
| 82 | watchdog device, so the main loop in the above program could be |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 83 | replaced with:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | while (1) { |
| 86 | ioctl(fd, WDIOC_KEEPALIVE, 0); |
| 87 | sleep(10); |
| 88 | } |
| 89 | |
| 90 | the argument to the ioctl is ignored. |
| 91 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 92 | Setting and getting the timeout |
| 93 | =============================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | For some drivers it is possible to modify the watchdog timeout on the |
| 96 | fly with the SETTIMEOUT ioctl, those drivers have the WDIOF_SETTIMEOUT |
| 97 | flag set in their option field. The argument is an integer |
| 98 | representing the timeout in seconds. The driver returns the real |
| 99 | timeout used in the same variable, and this timeout might differ from |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 100 | the requested one due to limitation of the hardware:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | int timeout = 45; |
| 103 | ioctl(fd, WDIOC_SETTIMEOUT, &timeout); |
| 104 | printf("The timeout was set to %d seconds\n", timeout); |
| 105 | |
| 106 | This example might actually print "The timeout was set to 60 seconds" |
| 107 | if the device has a granularity of minutes for its timeout. |
| 108 | |
| 109 | Starting with the Linux 2.4.18 kernel, it is possible to query the |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 110 | current timeout using the GETTIMEOUT ioctl:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | ioctl(fd, WDIOC_GETTIMEOUT, &timeout); |
| 113 | printf("The timeout was is %d seconds\n", timeout); |
| 114 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 115 | Pretimeouts |
| 116 | =========== |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 117 | |
| 118 | Some watchdog timers can be set to have a trigger go off before the |
| 119 | actual time they will reset the system. This can be done with an NMI, |
| 120 | interrupt, or other mechanism. This allows Linux to record useful |
| 121 | information (like panic information and kernel coredumps) before it |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 122 | resets:: |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 123 | |
| 124 | pretimeout = 10; |
| 125 | ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout); |
| 126 | |
| 127 | Note that the pretimeout is the number of seconds before the time |
| 128 | when the timeout will go off. It is not the number of seconds until |
| 129 | the pretimeout. So, for instance, if you set the timeout to 60 seconds |
Sangjung Woo | 5be876c | 2014-07-30 09:56:21 +0900 | [diff] [blame] | 130 | and the pretimeout to 10 seconds, the pretimeout will go off in 50 |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 131 | seconds. Setting a pretimeout to zero disables it. |
| 132 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 133 | There is also a get function for getting the pretimeout:: |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 134 | |
| 135 | ioctl(fd, WDIOC_GETPRETIMEOUT, &timeout); |
| 136 | printf("The pretimeout was is %d seconds\n", timeout); |
| 137 | |
| 138 | Not all watchdog drivers will support a pretimeout. |
| 139 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 140 | Get the number of seconds before reboot |
| 141 | ======================================= |
Wim Van Sebroeck | 58b519f | 2006-05-21 12:48:44 +0200 | [diff] [blame] | 142 | |
| 143 | Some watchdog drivers have the ability to report the remaining time |
| 144 | before the system will reboot. The WDIOC_GETTIMELEFT is the ioctl |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 145 | that returns the number of seconds before reboot:: |
Wim Van Sebroeck | 58b519f | 2006-05-21 12:48:44 +0200 | [diff] [blame] | 146 | |
| 147 | ioctl(fd, WDIOC_GETTIMELEFT, &timeleft); |
| 148 | printf("The timeout was is %d seconds\n", timeleft); |
| 149 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 150 | Environmental monitoring |
| 151 | ======================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | All watchdog drivers are required return more information about the system, |
| 154 | some do temperature, fan and power level monitoring, some can tell you |
| 155 | the reason for the last reboot of the system. The GETSUPPORT ioctl is |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 156 | available to ask what the device can do:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | struct watchdog_info ident; |
| 159 | ioctl(fd, WDIOC_GETSUPPORT, &ident); |
| 160 | |
| 161 | the fields returned in the ident struct are: |
| 162 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 163 | ================ ============================================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | identity a string identifying the watchdog driver |
| 165 | firmware_version the firmware version of the card if available |
| 166 | options a flags describing what the device supports |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 167 | ================ ============================================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | the options field can have the following bits set, and describes what |
| 170 | kind of information that the GET_STATUS and GET_BOOT_STATUS ioctls can |
Ahmad Fatoum | d51d385 | 2020-06-11 21:17:42 +0200 | [diff] [blame] | 171 | return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 173 | ================ ========================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | WDIOF_OVERHEAT Reset due to CPU overheat |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 175 | ================ ========================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | The machine was last rebooted by the watchdog because the thermal limit was |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 178 | exceeded: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 180 | ============== ========== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | WDIOF_FANFAULT Fan failed |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 182 | ============== ========== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | A system fan monitored by the watchdog card has failed |
| 185 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 186 | ============= ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | WDIOF_EXTERN1 External relay 1 |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 188 | ============= ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | External monitoring relay/source 1 was triggered. Controllers intended for |
| 191 | real world applications include external monitoring pins that will trigger |
| 192 | a reset. |
| 193 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 194 | ============= ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | WDIOF_EXTERN2 External relay 2 |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 196 | ============= ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | External monitoring relay/source 2 was triggered |
| 199 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 200 | ================ ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | WDIOF_POWERUNDER Power bad/power fault |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 202 | ================ ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | The machine is showing an undervoltage status |
| 205 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 206 | =============== ============================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | WDIOF_CARDRESET Card previously reset the CPU |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 208 | =============== ============================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | The last reboot was caused by the watchdog card |
| 211 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 212 | ================ ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | WDIOF_POWEROVER Power over voltage |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 214 | ================ ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | The machine is showing an overvoltage status. Note that if one level is |
| 217 | under and one over both bits will be set - this may seem odd but makes |
| 218 | sense. |
| 219 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 220 | =================== ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | WDIOF_KEEPALIVEPING Keep alive ping reply |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 222 | =================== ===================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | The watchdog saw a keepalive ping since it was last queried. |
| 225 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 226 | ================ ======================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | WDIOF_SETTIMEOUT Can set/get the timeout |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 228 | ================ ======================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 230 | The watchdog can do pretimeouts. |
| 231 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 232 | ================ ================================ |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 233 | WDIOF_PRETIMEOUT Pretimeout (in seconds), get/set |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 234 | ================ ================================ |
Corey Minyard | e05b59f | 2006-04-19 22:40:53 +0200 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | For those drivers that return any bits set in the option field, the |
| 238 | GETSTATUS and GETBOOTSTATUS ioctls can be used to ask for the current |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 239 | status, and the status at the last reboot, respectively:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | int flags; |
| 242 | ioctl(fd, WDIOC_GETSTATUS, &flags); |
| 243 | |
| 244 | or |
| 245 | |
| 246 | ioctl(fd, WDIOC_GETBOOTSTATUS, &flags); |
| 247 | |
| 248 | Note that not all devices support these two calls, and some only |
| 249 | support the GETBOOTSTATUS call. |
| 250 | |
| 251 | Some drivers can measure the temperature using the GETTEMP ioctl. The |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 252 | returned value is the temperature in degrees fahrenheit:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | int temperature; |
| 255 | ioctl(fd, WDIOC_GETTEMP, &temperature); |
| 256 | |
| 257 | Finally the SETOPTIONS ioctl can be used to control some aspects of |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 258 | the cards operation:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | int options = 0; |
James Hogan | dfc3338 | 2010-04-05 11:31:29 +0100 | [diff] [blame] | 261 | ioctl(fd, WDIOC_SETOPTIONS, &options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | The following options are available: |
| 264 | |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 265 | ================= ================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | WDIOS_DISABLECARD Turn off the watchdog timer |
| 267 | WDIOS_ENABLECARD Turn on the watchdog timer |
| 268 | WDIOS_TEMPPANIC Kernel panic on temperature trip |
Mauro Carvalho Chehab | cc2a2d1 | 2019-06-12 14:53:01 -0300 | [diff] [blame] | 269 | ================= ================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | [FIXME -- better explanations] |