/*
* Watchdog usage.
* Author: Serval Li
* Date: Jun 27th, 2012
* */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <linux/types.h>
#include <linux/watchdog.h>
int main(void)
{
int fd;
int timeout, flag;
char cmd[256];
if ((fd = open("/dev/watchdog", O_RDWR)) < 0) {
perror("");
exit(1);
}
ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
printf("Default timeout: %d\n", timeout);
timeout = 12;
printf("Set timeout to %d\n", timeout);
ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
printf("New timeout: %d\n", timeout);
flag = fcntl(0, F_GETFL, 0);
flag |= O_NONBLOCK;
if (fcntl(0, F_SETFL, flag) < 0) { /* fgets no-block now */
perror("Set stdin to non-block fails.");
exit(1);
}
while (1) {
ioctl(fd, WDIOC_KEEPALIVE, 0);
fgets(cmd, sizeof(cmd) - 1, stdin);
if(strncmp(cmd, "stop", 4) == 0)
goto stopwd;
sleep(timeout / 2);
}
stopwd:
write(fd, "V", 1); /* This is "V", not "v"! */
close(fd);
printf("Close watchdog!");
return 0;
}
代碼先存下來,使用的是softdog.c實現的Watchdog。其內核模塊名爲softdog.ko,位於/lib/modules/$kernelver/kernel/drivers/watchdog下面。