/* Weather Alert v1.0 - GBPPR www.gbppr.org */ #include #include #include #include #include #include #include #include #include #ifdef __FreeBSD__ #include #else #include /* GNU/Linux */ #endif #define BASE_ADDRESS 0x378 /* 0x3BC, 0x378, 0x278 */ #define SEC "60" /* seconds to wait */ #define SCRIPT "/usr/local/bin/weather.sh" /* script to run */ #define NORMAL "\033[0m" #define BOLDWHITE "\033[37m\033[1m" unsigned char Nib1, Nib2; char *optarg, buf[64]; int a, c, on, status; pid_t pid; extern int errno; struct tm *l; time_t t; int check (void); int initial (void); void usage (char *); void onalarm (int); int main (int argc, char **argv) { if (geteuid () && getuid ()) { fprintf (stderr, "\n\n%s must be run as root to access hardware ports.\n", argv[0]); fprintf (stderr, "Leaving...\n\n"); exit (1); } if (argc < 1) usage (argv[0]); while ((a = getopt (argc, argv, "o")) != EOF) { switch (a) { case 'o': /* Receiver is on */ on = 1; break; default: usage (argv[0]); break; } } if (on != 1) { fprintf (stderr, "\n\t%sReceiver must be already turned ON.%s\n", BOLDWHITE, NORMAL); fprintf (stderr, "\tIf it is not, turn it on now.\n\n"); fprintf (stderr, "Hit any key to continue..."); getchar (); } initial (); for (;;) { check (); if (Nib2 == 0) { c++; t = time (NULL); l = localtime (&t); #ifdef DEBUG printf ("Nib1 = 0x%x - Fake 20 second wait\n\a", Nib1); sleep (20); #else if ((pid = fork ()) < 0) perror ("fork"); else if (pid == 0) { if (system (SCRIPT) < 0) perror ("system"); sleep(30); } signal (SIGALRM, onalarm); alarm ((atoi (SEC) + 5)); if (wait (&status) == -1 || (status & 0177) != 0) fprintf (stderr, "system killed.\n"); #endif printf ("Alert done.\n\n"); } /* Loop forever, lots of cpu time */ } } int check (void) { Nib2 = (inb (BASE_ADDRESS + 1) & 0x40); /* Isolate Pin 10 - 01000000 */ #ifdef DEBUG printf ("Nib2 = 0x%x\n", Nib2); #endif return (0); } int initial (void) { printf ("Running ...\n\n"); #ifdef __FreeBSD__ if (open ("/dev/io", O_RDWR) < 0) #else if (ioperm (BASE_ADDRESS, 3, 1) < 0) #endif { fprintf (stderr, "\nERROR: Can't open port 0x%x\n", BASE_ADDRESS); perror ("port"); exit (errno); } Nib1 = (inb (BASE_ADDRESS + 1) & 0x40); /* Isolate Pin 10 - 01000000 */ #ifdef DEBUG printf ("Nib1 = 0x%x\n", Nib1); #endif return (0); } void usage (char *pname) { fprintf (stderr, "%s ", pname); fprintf (stderr, "[-o] \n\n\t-o If receiver is already on\n\n"); exit (1); } void onalarm (int val) { kill (pid, SIGHUP); }