watchdog-test.c 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Watchdog Driver Test Program
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
10
#include <signal.h>
11 12 13 14 15 16 17 18 19 20 21
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>

int fd;

/*
 * 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.
 */
22
static void keep_alive(void)
23 24 25
{
    int dummy;

26
    printf(".");
27 28 29 30 31 32 33
    ioctl(fd, WDIOC_KEEPALIVE, &dummy);
}

/*
 * The main program.  Run the program with "-d" to disable the card,
 * or "-e" to enable the card.
 */
34

35
static void term(int sig)
36 37
{
    close(fd);
38
    printf("\nStopping watchdog ticks...\n");
39 40 41
    exit(0);
}

42 43
int main(int argc, char *argv[])
{
44
    int flags;
45
    unsigned int ping_rate = 1;
46

47 48
    setbuf(stdout, NULL);

49 50 51
    fd = open("/dev/watchdog", O_WRONLY);

    if (fd == -1) {
52
	printf("Watchdog device not enabled.\n");
53 54 55 56 57
	exit(-1);
    }

    if (argc > 1) {
	if (!strncasecmp(argv[1], "-d", 2)) {
58 59
	    flags = WDIOS_DISABLECARD;
	    ioctl(fd, WDIOC_SETOPTIONS, &flags);
60
	    printf("Watchdog card disabled.\n");
61
	    goto end;
62
	} else if (!strncasecmp(argv[1], "-e", 2)) {
63 64
	    flags = WDIOS_ENABLECARD;
	    ioctl(fd, WDIOC_SETOPTIONS, &flags);
65
	    printf("Watchdog card enabled.\n");
66
	    goto end;
67 68 69
	} else if (!strncasecmp(argv[1], "-t", 2) && argv[2]) {
	    flags = atoi(argv[2]);
	    ioctl(fd, WDIOC_SETTIMEOUT, &flags);
70
	    printf("Watchdog timeout set to %u seconds.\n", flags);
71 72 73
	    goto end;
	} else if (!strncasecmp(argv[1], "-p", 2) && argv[2]) {
	    ping_rate = strtoul(argv[2], NULL, 0);
74
	    printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
75
	} else {
76
	    printf("-d to disable, -e to enable, -t <n> to set " \
77
		"the timeout,\n-p <n> to set the ping rate, and \n");
78
	    printf("run by itself to tick the card.\n");
79
	    goto end;
80 81 82
	}
    }

83
    printf("Watchdog Ticking Away!\n");
84

85 86
    signal(SIGINT, term);

87 88
    while(1) {
	keep_alive();
89
	sleep(ping_rate);
90
    }
91 92 93
end:
    close(fd);
    return 0;
94
}