/*
// $Id$
//
// AUTHOR : Charles C. Bundy IV
// FILE   : Start-monitor.c
// SUMMARY: This is part 1 of 2 parts.  Since there 
//          are only two small files no Makefile is 
//          provided.  To compile, type the following:
//
//             gcc -c Start-monitor.c
//             gcc -c writepid.c
//             gcc -o Start-monitor Start-monitor.o \
//                    writepid.o 
//
//          This program is invoked by a root crontab
//          entry as a background process at the 
//          beginning of a work day.  It sleeps for 30 
//          seconds, checks to see that 
//
//             /usr/spool/uucp/LCK..modem 
//
//          exists and if not calls an external script 
//          file which fires up pppd.    
*/

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
// Prototypes
*/
int    writePID (char *file_name);
int    stat  (const char *file_name, struct stat *buf);
time_t time  (time_t *t);

extern int errno;

int main ()
{
char lockfile[255];
char *RCSID;
unsigned int num_sec;
struct stat statbuf;
time_t rettime;

   strcpy(lockfile,"/usr/spool/uucp/LCK..modem");
   num_sec = 30;
   writePID("/var/run/monitor.pid");

   while (1)
   {
      if (stat(lockfile, &statbuf) == -1)
         if (errno == ENOENT)
         {
            time(&rettime);
            printf("PPP link down, RESTARTING on %s\n",
                    ctime(&rettime));
            system("/usr/lib/ppp/check>/dev/null 2>&1");
         }
      sleep(num_sec);
   }
   RCSID = "$Id$";
}
