这是一个watch dog test case的代码,可以用来测试watchdog的功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <sys/signal.h>
#include "watchdog.h"
#define OPTION_REBOOT 0
#define OPTION_BASIC 1
#define REBOOT "reboot"
int zsleep(int millisecond)
{
unsigned long usec;
usec=1000*millisecond;
usleep(usec);
}
/*
void keep_alive(void)
{
int dummy;
ioctl(fd, WDIOC_KEEPALIVE, &dummy);
}
void wdt_getstatus(int *arg)
{
ioctl(fd, WDIOC_KEEPALIVE, *arg);
}
void keep_getbootstatus(int *arg)
{
ioctl(fd, WDIOC_KEEPALIVE, *arg);
}
*/
int Init()
{
int fd;
//open device file
fd = open("/dev/watchdog",O_RDWR);
if(fd < 0)
{
printf("device open fail\n");
return -1;
}
else
printf("enable watchdog\n");
return fd;
}
int main(int argc,char **argv)
{
int fd,ch;
int i,j,k=1,option,timeout;
char *arg;
struct watchdog_info wi;
fd=Init();
if(argv[1]!=NULL){
if(strcmp(argv[1],REBOOT)==0){
printf("reboot mode\n");
arg = argv[2];
i = strtoul(arg,&arg,10);
option = OPTION_REBOOT;
}
else{
printf("invalid option arguments,only do the basic action\n");
option = OPTION_BASIC;
}
}
else{
printf("only basic action\n");
option = OPTION_BASIC;
}
if(option == OPTION_BASIC){
//read watchdog information
ioctl(fd,WDIOC_GETSUPPORT,&wi);
printf("%d,%s\n",wi.options,wi.identity);
//set watchdog timeout
//set the timeout is 10s,if success return 0,else return -1
i=5;
j=ioctl(fd,WDIOC_SETTIMEOUT,&i);
if(j==0)
printf("Set watchdog timeout success!\nSet watchdog timeout: %ds\n",i*2);
else
printf("Set watchdog timeout failed!\n");
//read watchdog timeout
j=ioctl(fd,WDIOC_GETTIMEOUT,&i);
if(j==0)
printf("Read watchdog timeout success!\nRead watchdog timeout: %ds\n",i*2);
else
printf("Read watchdog timeout failed!\n");
//disable watchdog
close(fd);
printf("disable watchdog\n");
}
else{
//read watchdog information
ioctl(fd,WDIOC_GETSUPPORT,&wi);
printf("%d,%s\n",wi.options,wi.identity);
//set watchdog timeout
//set the timeout is 10s,if success return 0,else return -1
timeout = i/2;
j=ioctl(fd,WDIOC_SETTIMEOUT,&timeout);
if(j==0)
printf("Set watchdog timeout success!\nSet watchdog timeout: %ds\n",timeout*2);
else
printf("Set watchdog timeout failed!\n");
//read watchdog timeout
j=ioctl(fd,WDIOC_GETTIMEOUT,&timeout);
if(j==0)
printf("Read watchdog timeout success!\nRead watchdog timeout: %ds\n",timeout*2);
else
printf("Read watchdog timeout failed!\n");
timeout = timeout*2;
while(1){
printf("I 'll do the reboot after %d seconds\n",timeout--);
sleep(1);
}
//disable watchdog
close(fd);
printf("disable watchdog\n");
}
return 0;
}
watchdog.h 代码如下
/*
* Generic watchdog defines. Derived from..
*
* Berkshire PC Watchdog Defines
* by Ken Hollis <khollis@bitgate.com>
*
*/
#ifndef _LINUX_WATCHDOG_H
#define _LINUX_WATCHDOG_H
#include <linux/ioctl.h>
#define WATCHDOG_IOCTL_BASE 'W'
struct watchdog_info {
unsigned int options; /* Options the card/driver supports */
unsigned int firmware_version; /* Firmware version of the card */
unsigned char identity[32]; /* Identity of the board */
};
#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
#define WDIOC_GETSTATUS _IOR(WATCHDOG_IOCTL_BASE, 1, int)
#define WDIOC_GETBOOTSTATUS _IOR(WATCHDOG_IOCTL_BASE, 2, int)
#define WDIOC_GETTEMP _IOR(WATCHDOG_IOCTL_BASE, 3, int)
#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int)
#define WDIOF_UNKNOWN -1 /* Unknown flag error */
#define WDIOS_UNKNOWN -1 /* Unknown status error */
#define WDIOF_OVERHEAT 0x0001 /* Reset due to CPU overheat */
#define WDIOF_FANFAULT 0x0002 /* Fan failed */
#define WDIOF_EXTERN1 0x0004 /* External relay 1 */
#define WDIOF_EXTERN2 0x0008 /* External relay 2 */
#define WDIOF_POWERUNDER 0x0010 /* Power bad/power fault */
#define WDIOF_CARDRESET 0x0020 /* Card previously reset the CPU */
#define WDIOF_POWEROVER 0x0040 /* Power over voltage */
#define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
#define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
#define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */
#define WDIOS_DISABLECARD 0x0001 /* Turn off the watchdog timer */
#define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */
#define WDIOS_TEMPPANIC 0x0004 /* Kernel panic on temperature trip */
#endif /* ifndef _LINUX_WATCHDOG_H */