2019-06-13 01:53:01 +08:00
|
|
|
=============================
|
|
|
|
The Linux Watchdog driver API
|
|
|
|
=============================
|
|
|
|
|
2007-05-24 05:43:52 +08:00
|
|
|
Last reviewed: 10/05/2007
|
|
|
|
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
Copyright 2002 Christer Weingel <wingel@nano-system.com>
|
|
|
|
|
|
|
|
Some parts of this document are copied verbatim from the sbc60xxwdt
|
|
|
|
driver which is (c) Copyright 2000 Jakob Oestergaard <jakob@ostenfeld.dk>
|
|
|
|
|
|
|
|
This document describes the state of the Linux 2.4.18 kernel.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Introduction
|
|
|
|
============
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
A Watchdog Timer (WDT) is a hardware circuit that can reset the
|
|
|
|
computer system in case of a software fault. You probably knew that
|
|
|
|
already.
|
|
|
|
|
|
|
|
Usually a userspace daemon will notify the kernel watchdog driver via the
|
|
|
|
/dev/watchdog special device file that userspace is still alive, at
|
|
|
|
regular intervals. When such a notification occurs, the driver will
|
|
|
|
usually tell the hardware watchdog that everything is in order, and
|
|
|
|
that the watchdog should wait for yet another little while to reset
|
|
|
|
the system. If userspace fails (RAM error, kernel bug, whatever), the
|
|
|
|
notifications cease to occur, and the hardware watchdog will reset the
|
|
|
|
system (causing a reboot) after the timeout occurs.
|
|
|
|
|
2007-05-24 05:43:52 +08:00
|
|
|
The Linux watchdog API is a rather ad-hoc construction and different
|
2005-04-17 06:20:36 +08:00
|
|
|
drivers implement different, and sometimes incompatible, parts of it.
|
|
|
|
This file is an attempt to document the existing usage and allow
|
|
|
|
future driver writers to use it as a reference.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
The simplest API
|
|
|
|
================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
All drivers support the basic mode of operation, where the watchdog
|
|
|
|
activates as soon as /dev/watchdog is opened and will reboot unless
|
|
|
|
the watchdog is pinged within a certain time, this time is called the
|
|
|
|
timeout or margin. The simplest way to ping the watchdog is to write
|
|
|
|
some data to the device. So a very simple watchdog daemon would look
|
2016-09-17 07:40:40 +08:00
|
|
|
like this source file: see samples/watchdog/watchdog-simple.c
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
A more advanced driver could for example check that a HTTP server is
|
|
|
|
still responding before doing the write call to ping the watchdog.
|
|
|
|
|
2008-01-09 04:40:37 +08:00
|
|
|
When the device is closed, the watchdog is disabled, unless the "Magic
|
|
|
|
Close" feature is supported (see below). This is not always such a
|
|
|
|
good idea, since if there is a bug in the watchdog daemon and it
|
|
|
|
crashes the system will not reboot. Because of this, some of the
|
|
|
|
drivers support the configuration option "Disable watchdog shutdown on
|
|
|
|
close", CONFIG_WATCHDOG_NOWAYOUT. If it is set to Y when compiling
|
|
|
|
the kernel, there is no way of disabling the watchdog once it has been
|
|
|
|
started. So, if the watchdog daemon crashes, the system will reboot
|
|
|
|
after the timeout has passed. Watchdog devices also usually support
|
|
|
|
the nowayout module parameter so that this option can be controlled at
|
|
|
|
runtime.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Magic Close feature
|
|
|
|
===================
|
2008-01-09 04:40:37 +08:00
|
|
|
|
|
|
|
If a driver supports "Magic Close", the driver will not disable the
|
|
|
|
watchdog unless a specific magic character 'V' has been sent to
|
|
|
|
/dev/watchdog just before closing the file. If the userspace daemon
|
|
|
|
closes the file without sending this special character, the driver
|
|
|
|
will assume that the daemon (and userspace in general) died, and will
|
|
|
|
stop pinging the watchdog without disabling it first. This will then
|
|
|
|
cause a reboot if the watchdog is not re-opened in sufficient time.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
The ioctl API
|
|
|
|
=============
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
All conforming drivers also support an ioctl API.
|
|
|
|
|
|
|
|
Pinging the watchdog using an ioctl:
|
|
|
|
|
|
|
|
All drivers that have an ioctl interface support at least one ioctl,
|
|
|
|
KEEPALIVE. This ioctl does exactly the same thing as a write to the
|
|
|
|
watchdog device, so the main loop in the above program could be
|
2019-06-13 01:53:01 +08:00
|
|
|
replaced with::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ioctl(fd, WDIOC_KEEPALIVE, 0);
|
|
|
|
sleep(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
the argument to the ioctl is ignored.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Setting and getting the timeout
|
|
|
|
===============================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
For some drivers it is possible to modify the watchdog timeout on the
|
|
|
|
fly with the SETTIMEOUT ioctl, those drivers have the WDIOF_SETTIMEOUT
|
|
|
|
flag set in their option field. The argument is an integer
|
|
|
|
representing the timeout in seconds. The driver returns the real
|
|
|
|
timeout used in the same variable, and this timeout might differ from
|
2019-06-13 01:53:01 +08:00
|
|
|
the requested one due to limitation of the hardware::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int timeout = 45;
|
|
|
|
ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
|
|
|
|
printf("The timeout was set to %d seconds\n", timeout);
|
|
|
|
|
|
|
|
This example might actually print "The timeout was set to 60 seconds"
|
|
|
|
if the device has a granularity of minutes for its timeout.
|
|
|
|
|
|
|
|
Starting with the Linux 2.4.18 kernel, it is possible to query the
|
2019-06-13 01:53:01 +08:00
|
|
|
current timeout using the GETTIMEOUT ioctl::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
|
|
|
|
printf("The timeout was is %d seconds\n", timeout);
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Pretimeouts
|
|
|
|
===========
|
2006-04-20 04:40:53 +08:00
|
|
|
|
|
|
|
Some watchdog timers can be set to have a trigger go off before the
|
|
|
|
actual time they will reset the system. This can be done with an NMI,
|
|
|
|
interrupt, or other mechanism. This allows Linux to record useful
|
|
|
|
information (like panic information and kernel coredumps) before it
|
2019-06-13 01:53:01 +08:00
|
|
|
resets::
|
2006-04-20 04:40:53 +08:00
|
|
|
|
|
|
|
pretimeout = 10;
|
|
|
|
ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout);
|
|
|
|
|
|
|
|
Note that the pretimeout is the number of seconds before the time
|
|
|
|
when the timeout will go off. It is not the number of seconds until
|
|
|
|
the pretimeout. So, for instance, if you set the timeout to 60 seconds
|
2014-07-30 08:56:21 +08:00
|
|
|
and the pretimeout to 10 seconds, the pretimeout will go off in 50
|
2006-04-20 04:40:53 +08:00
|
|
|
seconds. Setting a pretimeout to zero disables it.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
There is also a get function for getting the pretimeout::
|
2006-04-20 04:40:53 +08:00
|
|
|
|
|
|
|
ioctl(fd, WDIOC_GETPRETIMEOUT, &timeout);
|
|
|
|
printf("The pretimeout was is %d seconds\n", timeout);
|
|
|
|
|
|
|
|
Not all watchdog drivers will support a pretimeout.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Get the number of seconds before reboot
|
|
|
|
=======================================
|
2006-05-21 18:48:44 +08:00
|
|
|
|
|
|
|
Some watchdog drivers have the ability to report the remaining time
|
|
|
|
before the system will reboot. The WDIOC_GETTIMELEFT is the ioctl
|
2019-06-13 01:53:01 +08:00
|
|
|
that returns the number of seconds before reboot::
|
2006-05-21 18:48:44 +08:00
|
|
|
|
|
|
|
ioctl(fd, WDIOC_GETTIMELEFT, &timeleft);
|
|
|
|
printf("The timeout was is %d seconds\n", timeleft);
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
Environmental monitoring
|
|
|
|
========================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
All watchdog drivers are required return more information about the system,
|
|
|
|
some do temperature, fan and power level monitoring, some can tell you
|
|
|
|
the reason for the last reboot of the system. The GETSUPPORT ioctl is
|
2019-06-13 01:53:01 +08:00
|
|
|
available to ask what the device can do::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct watchdog_info ident;
|
|
|
|
ioctl(fd, WDIOC_GETSUPPORT, &ident);
|
|
|
|
|
|
|
|
the fields returned in the ident struct are:
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =============================================
|
2005-04-17 06:20:36 +08:00
|
|
|
identity a string identifying the watchdog driver
|
|
|
|
firmware_version the firmware version of the card if available
|
|
|
|
options a flags describing what the device supports
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =============================================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
the options field can have the following bits set, and describes what
|
|
|
|
kind of information that the GET_STATUS and GET_BOOT_STATUS ioctls can
|
2020-06-12 03:17:42 +08:00
|
|
|
return.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =========================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_OVERHEAT Reset due to CPU overheat
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =========================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The machine was last rebooted by the watchdog because the thermal limit was
|
2019-06-13 01:53:01 +08:00
|
|
|
exceeded:
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
============== ==========
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_FANFAULT Fan failed
|
2019-06-13 01:53:01 +08:00
|
|
|
============== ==========
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
A system fan monitored by the watchdog card has failed
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
============= ================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_EXTERN1 External relay 1
|
2019-06-13 01:53:01 +08:00
|
|
|
============= ================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
External monitoring relay/source 1 was triggered. Controllers intended for
|
|
|
|
real world applications include external monitoring pins that will trigger
|
|
|
|
a reset.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
============= ================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_EXTERN2 External relay 2
|
2019-06-13 01:53:01 +08:00
|
|
|
============= ================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
External monitoring relay/source 2 was triggered
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_POWERUNDER Power bad/power fault
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The machine is showing an undervoltage status
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
=============== =============================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_CARDRESET Card previously reset the CPU
|
2019-06-13 01:53:01 +08:00
|
|
|
=============== =============================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The last reboot was caused by the watchdog card
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_POWEROVER Power over voltage
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The machine is showing an overvoltage status. Note that if one level is
|
|
|
|
under and one over both bits will be set - this may seem odd but makes
|
|
|
|
sense.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
=================== =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_KEEPALIVEPING Keep alive ping reply
|
2019-06-13 01:53:01 +08:00
|
|
|
=================== =====================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The watchdog saw a keepalive ping since it was last queried.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =======================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOF_SETTIMEOUT Can set/get the timeout
|
2019-06-13 01:53:01 +08:00
|
|
|
================ =======================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-04-20 04:40:53 +08:00
|
|
|
The watchdog can do pretimeouts.
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================ ================================
|
2006-04-20 04:40:53 +08:00
|
|
|
WDIOF_PRETIMEOUT Pretimeout (in seconds), get/set
|
2019-06-13 01:53:01 +08:00
|
|
|
================ ================================
|
2006-04-20 04:40:53 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
For those drivers that return any bits set in the option field, the
|
|
|
|
GETSTATUS and GETBOOTSTATUS ioctls can be used to ask for the current
|
2019-06-13 01:53:01 +08:00
|
|
|
status, and the status at the last reboot, respectively::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int flags;
|
|
|
|
ioctl(fd, WDIOC_GETSTATUS, &flags);
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
|
|
|
|
|
|
|
|
Note that not all devices support these two calls, and some only
|
|
|
|
support the GETBOOTSTATUS call.
|
|
|
|
|
|
|
|
Some drivers can measure the temperature using the GETTEMP ioctl. The
|
2019-06-13 01:53:01 +08:00
|
|
|
returned value is the temperature in degrees fahrenheit::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int temperature;
|
|
|
|
ioctl(fd, WDIOC_GETTEMP, &temperature);
|
|
|
|
|
|
|
|
Finally the SETOPTIONS ioctl can be used to control some aspects of
|
2019-06-13 01:53:01 +08:00
|
|
|
the cards operation::
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int options = 0;
|
2010-04-05 18:31:29 +08:00
|
|
|
ioctl(fd, WDIOC_SETOPTIONS, &options);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
The following options are available:
|
|
|
|
|
2019-06-13 01:53:01 +08:00
|
|
|
================= ================================
|
2005-04-17 06:20:36 +08:00
|
|
|
WDIOS_DISABLECARD Turn off the watchdog timer
|
|
|
|
WDIOS_ENABLECARD Turn on the watchdog timer
|
|
|
|
WDIOS_TEMPPANIC Kernel panic on temperature trip
|
2019-06-13 01:53:01 +08:00
|
|
|
================= ================================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
[FIXME -- better explanations]
|