Basic principle:
Linux comes with a watchdog implementation that monitors the operation of the system, including a kernel watchdog module and a user-space watchdog program. The kernel watchdog module communicates with the user space by/dev/watchdog this character device. User space program once opened/dev/watchdog device (commonly known as "open the Dog"), will cause a 1-minute timer in the kernel (system default time), after that, the user space program needs to ensure in 1 minutes to the device to write data (commonly known as "feed the Dog regularly"), Each write operation causes the timer to be reset. If the user space program does not write within 1 minutes, the timer expires will cause a system reboot operation ("Dog bites people" hehe). Through this mechanism, we can ensure that the core process of the system is running for most of the time, even if the process crashes in a particular situation, because it is not possible to "feed the dog" at regular intervals, and the Linux system restarts (reboot) under the watchdog function, the core process is running again. More for embedded systems.
The above principle is the basic principle of watchdog, other types of watchdog are also implemented according to this principle.
Example test:
Like the 6300ESB test pass.
The equipment is as follows:
[[email protected] ~]# lspci
00:00.0 Host bridge:intel Corporation 440fx-82441fx PMC [Natoma] (rev)
00 : 01.0 Isa Bridge:intel Corporation 82371SB PIIX3 Isa [Natoma/triton II]
00:01.1 IDE Interface:intel Corporation 82371 SB PIIX3 IDE [Natoma/triton II]
00:01.2 USB controller:intel Corporation 82371SB PIIX3 USB [Natoma/triton II] (rev 01)
00:01.3 bridge:intel Corporation 82371ab/eb/mb PIIX4 ACPI (rev.)
00:02.0 VGA compatible Controller:cirrus Logic GD 5446
00:03.0 Ethernet Controller:realtek Semiconductor Co., Ltd. rtl-8139/8139c/8139c+ (rev.)
00:04.0 Audio D Evice:intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) high Definition Audio Controller (rev.)
00:05.0 Communic ation controller:red Hat, Inc Virtio console
00:06.0 RAM memory:red Hat, Inc Virtio memory balloon
00:07.0 System Peripheral:intel Corporation 6300ESB Watchdog Timer
[Email protected] ~]# Dmesg|grep 6300
I6300esb Timer:intel 6300ESB WatchDog timer Driver v0.04
I6300esb timer:initialized (0xffffc90000e8c000). Heartbeat=30 sec (nowayout=0)
[Email protected] ~]# Lsmod|grep Softdog
[Email protected] ~]# Lsmod|grep watchdog
[Email protected] ~]# Ps-ef|grep watchdog
Root 6 2 0 01:59? 00:00:00 [watchdog/0]
Root 1748 1711 0 02:28 pts/0 00:00:00 grep watchdog
[Email protected] ~]# Lsmod|grep 6300
I6300esb 5669 0
Close (Fd_watchdog);
The overall procedure is as follows:
#include <unistd.h>
#include <sys/stat.h>
#include <syslog.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main (int argc,char** argv)
{
1. Open the/dev/watchdog device ("Open the door and put the Dog"):
int fd_watchdog = open ("/dev/watchdog", o_wronly);
if (Fd_watchdog = =-1)
{
int err = errno;
printf ("\n!! Failed to open/dev/watchdog,errno:%d,%s \ n ", Err,strerror (err));
Syslog (Log_warning, "failed to open/dev/watchdog,errno:%d,%s", Err,strerror (err));
}
2. Write data to the/dev/watchdog device at regular intervals ("Feed the Dog regularly"):
if (fd_watchdog >= 0)
{
while (1)
{
Sleep (10);
static unsigned char food = 0;
ssize_t Earten = write (fd_watchdog,&food,1);
if (Earten! = 1)
{
Puts ("\ n!!! Failed feeding watchdog ");
Syslog (log_warning, "failted feeding watchdog");
}
}
}
Close (Fd_watchdog);
}
[Email protected] watchdog]# GCC watchdog.c-o watchdog
[Email protected] watchdog]#./watchdog &
[1] 1790
[Email protected] watchdog]#
To crash the system:
[[email protected] ~]# echo C >/proc/sysrq-trigger
The system will restart automatically.
If you connect through a spice system, you will see a number of character hints appearing before the system crashes, and the system will restart.
Linux watchdog 6300ESB