wagthedog.C 2.09 KB
Newer Older
Timothy Stack's avatar
 
Timothy Stack committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2005 University of Utah and the Flux Group.
 * All rights reserved.
 */

/**
 * @file wagthedog.C
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <paths.h>
16
#include <signal.h>
Timothy Stack's avatar
 
Timothy Stack committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <sys/resource.h>

#include "SGGPIO.h"

#define WDT_STARTUP_DELAY 60

#define WDT_INPUT 58
#define WDT_SET1 59
#define WDT_SET2 60

static int debug = 0;

static volatile int looping = 1;

static char *pidfile = _PATH_VARRUN "wagthedog.pid";

static void usage()
{
    fprintf(stderr,
	    "Usage: wagthedog [-h] [-i pidfile]\n"
	    "\n"
	    "Enable and feed the stargate's onboard watchdog timer."
	    "\n"
	    "Options:\n"
	    "  -h\t\tPrint this message.\n"
	    "  -d\t\tTurn on debugging messages and do not daemonize.\n"
	    "  -i pidfile\tSpecify the path for the PID file. (Default: %s)\n",
	    pidfile);
    exit(1);
}

int main(int argc, char **argv)
{
    int c, wiggle = 1, retval = EXIT_SUCCESS;
    unsigned int elapsed = 0;
    SGGPIO_PORT sggpio;
    
54
    while ((c = getopt(argc, argv, "dhi")) != -1) {
Timothy Stack's avatar
 
Timothy Stack committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	switch (c) {
	case 'd':
	    debug += 1;
	    break;
	case 'i':
	    pidfile = optarg;
	    break;
	case 'h':
	case '?':
	default:
	    usage();
	    exit(retval);
	    break;
	}
    }
    
    if (!debug) {
	/* Become a daemon */
	daemon(0, 0);
    }

76 77
    signal(SIGTERM, SIG_IGN);

Timothy Stack's avatar
 
Timothy Stack committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    setpriority(PRIO_PROCESS, 0, 20);
    openlog("wagthedog", LOG_PID, LOG_DAEMON);
    syslog(LOG_INFO, "wagthedog started");

    if (pidfile) {
	FILE *fp;
	
	if ((fp = fopen(pidfile, "w")) != NULL) {
	    fprintf(fp, "%d\n", getpid());
	    (void) fclose(fp);
	}
    }

    sggpio.setDir(WDT_INPUT, 1);
    sggpio.setDir(WDT_SET1, 1);
    sggpio.setDir(WDT_SET2, 1);

    sggpio.setPin(WDT_INPUT, wiggle);
96
    sggpio.setPin(WDT_SET1, 0);
Timothy Stack's avatar
 
Timothy Stack committed
97 98 99 100 101 102
    sggpio.setPin(WDT_SET2, 1);
    
    while (looping) {
	wiggle = !wiggle;
	sggpio.setPin(WDT_INPUT, wiggle);
	
103
	usleep(500);
Timothy Stack's avatar
 
Timothy Stack committed
104 105 106 107 108 109 110 111 112
	
	elapsed += 1;
	if (elapsed == WDT_STARTUP_DELAY)
	    syslog(LOG_INFO, "startup delay reached, watchdog active");
    }
    syslog(LOG_INFO, "wagthedog stopped");
    
    return retval;
}