2006-05-22 03:58:10 +00:00
|
|
|
/*
|
|
|
|
* Watchdog Driver Test Program
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-17 09:37:48 +00:00
|
|
|
#include <signal.h>
|
2006-05-22 03:58:10 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
int fd;
|
2016-06-21 23:00:15 +00:00
|
|
|
const char v = 'V';
|
2006-05-22 03:58:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function simply sends an IOCTL to the driver, which in turn ticks
|
|
|
|
* the PC Watchdog card to reset its internal timer so it doesn't trigger
|
|
|
|
* a computer reset.
|
|
|
|
*/
|
2009-09-22 23:43:42 +00:00
|
|
|
static void keep_alive(void)
|
2006-05-22 03:58:10 +00:00
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
|
2016-06-21 23:00:14 +00:00
|
|
|
printf(".");
|
2006-05-22 03:58:10 +00:00
|
|
|
ioctl(fd, WDIOC_KEEPALIVE, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main program. Run the program with "-d" to disable the card,
|
|
|
|
* or "-e" to enable the card.
|
|
|
|
*/
|
2012-05-17 09:37:48 +00:00
|
|
|
|
2012-07-23 17:46:11 +00:00
|
|
|
static void term(int sig)
|
2012-05-17 09:37:48 +00:00
|
|
|
{
|
2016-06-21 23:00:15 +00:00
|
|
|
write(fd, &v, 1);
|
2012-05-17 09:37:48 +00:00
|
|
|
close(fd);
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("\nStopping watchdog ticks...\n");
|
2012-05-17 09:37:48 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2006-05-22 03:58:10 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-04-05 10:31:29 +00:00
|
|
|
int flags;
|
2015-06-29 16:46:17 +00:00
|
|
|
unsigned int ping_rate = 1;
|
2010-04-05 10:31:29 +00:00
|
|
|
|
2016-06-21 23:00:14 +00:00
|
|
|
setbuf(stdout, NULL);
|
|
|
|
|
2006-05-22 03:58:10 +00:00
|
|
|
fd = open("/dev/watchdog", O_WRONLY);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog device not enabled.\n");
|
2006-05-22 03:58:10 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (!strncasecmp(argv[1], "-d", 2)) {
|
2010-04-05 10:31:29 +00:00
|
|
|
flags = WDIOS_DISABLECARD;
|
|
|
|
ioctl(fd, WDIOC_SETOPTIONS, &flags);
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog card disabled.\n");
|
2012-05-14 18:12:02 +00:00
|
|
|
goto end;
|
2006-05-22 03:58:10 +00:00
|
|
|
} else if (!strncasecmp(argv[1], "-e", 2)) {
|
2010-04-05 10:31:29 +00:00
|
|
|
flags = WDIOS_ENABLECARD;
|
|
|
|
ioctl(fd, WDIOC_SETOPTIONS, &flags);
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog card enabled.\n");
|
2012-05-14 18:12:02 +00:00
|
|
|
goto end;
|
2015-06-29 16:46:17 +00:00
|
|
|
} else if (!strncasecmp(argv[1], "-t", 2) && argv[2]) {
|
|
|
|
flags = atoi(argv[2]);
|
|
|
|
ioctl(fd, WDIOC_SETTIMEOUT, &flags);
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog timeout set to %u seconds.\n", flags);
|
2015-06-29 16:46:17 +00:00
|
|
|
goto end;
|
|
|
|
} else if (!strncasecmp(argv[1], "-p", 2) && argv[2]) {
|
|
|
|
ping_rate = strtoul(argv[2], NULL, 0);
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
|
2006-05-22 03:58:10 +00:00
|
|
|
} else {
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("-d to disable, -e to enable, -t <n> to set " \
|
2015-06-29 16:46:17 +00:00
|
|
|
"the timeout,\n-p <n> to set the ping rate, and \n");
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("run by itself to tick the card.\n");
|
2012-05-14 18:12:02 +00:00
|
|
|
goto end;
|
2006-05-22 03:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:00:14 +00:00
|
|
|
printf("Watchdog Ticking Away!\n");
|
2015-06-29 16:46:17 +00:00
|
|
|
|
2012-05-17 09:37:48 +00:00
|
|
|
signal(SIGINT, term);
|
|
|
|
|
2006-05-22 03:58:10 +00:00
|
|
|
while(1) {
|
|
|
|
keep_alive();
|
2015-06-29 16:46:17 +00:00
|
|
|
sleep(ping_rate);
|
2006-05-22 03:58:10 +00:00
|
|
|
}
|
2012-05-14 18:12:02 +00:00
|
|
|
end:
|
2016-06-21 23:00:15 +00:00
|
|
|
write(fd, &v, 1);
|
2012-05-14 18:12:02 +00:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
2006-05-22 03:58:10 +00:00
|
|
|
}
|