Personal tools
You are here: Home Members gillesgigan Sensor Gateway misc watchdog pinging program
Document Actions

watchdog pinging program

by Gilles Gigan last modified 2007-09-28 17:31

Ping the watchdog to prevent the gateway from being reset

Click here to get the file

Size 1.9 kB - File type text/x-csrc

File contents

/*
 * Watchdog Driver Test Program
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#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.
 */
void keep_alive(void)
{
    int dummy;

    ioctl(fd, WDIOC_KEEPALIVE, &dummy);
}

/*
 * The main program.  Run the program with "-d" to disable the card,
 * or "-e" to enable the card.
 */
int main(int argc, char *argv[])
{
    int timeout, ret;
    fd = open("/dev/watchdog", O_WRONLY);

    if (fd == -1) {
	fprintf(stderr, "Watchdog device not enabled.\n");
	fflush(stderr);
	exit(-1);
    }

    if (argc > 1) {
	if (!strncasecmp(argv[1], "-d", 2)) {
	    ret = ioctl(fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD);
	    fprintf(stderr, "Watchdog card disabled. %d\n", ret);
	    fflush(stderr);
	    exit(0);
	} else if (!strncasecmp(argv[1], "-e", 2)) {
	    ret = ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
	    fprintf(stderr, "Watchdog card enabled.\n%d",ret);
	    fflush(stderr);
	    exit(0);
	} else if (!strncasecmp(argv[1], "-g", 2)) {
	    ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
	    fprintf(stderr, "Timeout: %d sec\n", timeout);
	    fflush(stderr);
	    exit(0);
	} else if (!strncasecmp(argv[1], "-s", 2)) {
	    timeout = atoi(argv[2]);
	    ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
	    fprintf(stderr, "Timeout: %d sec\n", timeout);
	    fflush(stderr);
	    exit(0);
	} else {
	    fprintf(stderr, "-d to disable, -e to enable, -g to get the timeout, -s XX to set the timeout.\n");
	    fprintf(stderr, "run by itself to tick the card.\n");
	    fflush(stderr);
	    exit(0);
	}
    }

	fprintf(stdout, "Whacking the dog ...\n");
	

    while(1) {
	keep_alive();
	sleep(1);
    }
}

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: