ACQ2XX_API
wdt.c
Go to the documentation of this file.
00001 /*
00002  * wdt.c : watchdog timer process
00003  * wdt time-msec parent [parent]
00004  * monitors stdin
00005  * '1' => enable
00006  * '0' => disable
00007  * If enabled and timeout => kill parents
00008  */
00009 
00010 #include "local.h"
00011 #include <sys/types.h>
00012 #include <signal.h>
00013 
00014 extern int fgets_t(char *s, int size, FILE* stream, int timeout);
00015 
00016 int wdt(int timeout_ms)
00017 /* returns != 0 if wdt happened */
00018 {
00019         int enabled = 0;
00020         char buf[80];
00021 
00022         while(1){
00023                 int rc = fgets_t(buf, 80, stdin, timeout_ms);
00024 
00025                 if (rc > 0){
00026                         switch(buf[0]){
00027                         case '1':
00028                                 enabled = 1; break;
00029                         case '0':
00030                                 enabled = 0; break;
00031                         }
00032                 }else if (rc == 0){
00033                         if (enabled){
00034                                 return 1;
00035                         }
00036                 }else{
00037                         return rc;
00038                 }
00039         }
00040 }
00041 
00042 
00043 int main(int argc, char* argv[]){
00044         int timeout;
00045         char** pids;
00046         int npids;
00047         int rc;
00048         pid_t signal = getenv("WDT_SIGNAL")? atoi(getenv("WDT_SIGNAL")): SIGKILL;
00049 
00050         if (argc < 3){
00051                 err("USAGE: wdt timeout pid [pid]");
00052                 return 1;
00053         }
00054 
00055         timeout = atoi(argv[1]);
00056         pids = &argv[2];
00057         npids = argc-2;
00058 
00059         if ((rc = wdt(timeout)) > 0){
00060                 int ii = 0;
00061                 for (ii = 0; ii != npids; ++ii){
00062                         kill(atoi(pids[ii]), signal);
00063                 }
00064         }
00065         return rc;
00066 }