home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BIGBRO.LZH / BIGBRO.C next >
C/C++ Source or Header  |  1990-03-25  |  3KB  |  138 lines

  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <ctype.h>
  8. #define INCL_NOPM
  9. #define INCL_DOS
  10. #define INCL_VIO
  11. #define INCL_DOSDEVICES
  12. #define INCL_DOSDEVIOCTL
  13. #include <os2.h>
  14. #if !defined(CALLBACK)
  15. #include "os2fubar.h"
  16. #endif
  17.  
  18. #define STACK3 0x200
  19.  
  20.  
  21. static HFILE hfCom;            // modem
  22.  
  23. static PID pidProc;
  24.  
  25. #define alive  TRUE
  26.  
  27. static int WeKilled = FALSE;
  28. static long Dead = 0L;
  29. static long ThreadDone = 0L;
  30.  
  31. int StartThreads(void);
  32. static VOID FAR _loadds WatchDogThread(void);
  33.  
  34. void main(int argc, char **argv)
  35. {
  36.     PID pidd;
  37.     RESULTCODES rescResults;
  38.     FILE *f;
  39.     time_t t;
  40.     struct tm *lt;
  41.  
  42.     if (argc < 3) {
  43.     printf("BigBrother v0.00 Copyright (c) Peter Fitzsimmons, 1990\n");
  44.     printf("Usage: BigBro <comm handle> <program.exe> [arguments]\n");
  45.     exit(0);
  46.     }
  47.     hfCom = (HFILE) atoi(argv[1]);
  48.     if (hfCom < 0 || hfCom > 255)
  49.         hfCom = 0;
  50.     printf("hfComhandle = %d\n", hfCom);
  51.     DosSemSet(&ThreadDone);
  52.     if (StartThreads()) {
  53.     printf("BigBro is now spawning %s\n", argv[2]);
  54.     pidProc = spawnvp(P_NOWAIT, argv[2], &argv[2]);
  55.     if (pidProc != -1) {
  56.         DosSemSet(&Dead);
  57.         DosCwait(DCWA_PROCESSTREE, DCWW_WAIT, &rescResults, &pidd, pidProc);
  58.         DosSemClear(&Dead);
  59.         DosSemWait(&ThreadDone, -1L);
  60.         if (WeKilled) {
  61.         f = fopen("BigBro.Log", "at");
  62.         time(&t);
  63.         lt = localtime(&t);
  64.         if (f) {
  65.             printf("\n\aBigBro had to kill '%s'\n", argv[2]);
  66.             fprintf(f, "! %02d/%02d/%02d %02d:%02d:%02d BigBro killed %s\n",
  67.             lt->tm_year, lt->tm_mon + 1, lt->tm_mday,
  68.             lt->tm_hour, lt->tm_min, lt->tm_sec, argv[2]);
  69.             fclose(f);
  70.         }
  71.         exit(0);
  72.         }
  73.         else
  74.         exit(rescResults.codeResult);    /* exit with the errorlevel
  75.                          * that the child wanted */
  76.     }
  77.     else
  78.         fprintf(stderr, "Couldn't spawn %s\n", argv[2]);
  79.     }
  80.     exit(3);
  81. }
  82.  
  83. void *zalloc(size_t bytes)
  84. {
  85.     void *ret = malloc(bytes);
  86.  
  87.     if (!ret) {
  88.     fprintf(stderr, "Couldn't allocate %u bytes\n", bytes);
  89.     exit(3);
  90.     }
  91.     return (ret);
  92. }
  93.  
  94.  
  95. int StartThreads(void)
  96. {
  97.     BYTE *stack3;
  98.     TID tid3;
  99.  
  100.     if (hfCom) {
  101.     stack3 = zalloc(STACK3);
  102.     if (DosCreateThread((PFNTHREAD) WatchDogThread, &tid3, stack3 + STACK3)) {
  103.         fprintf(stderr, "Couldn't create Watchdog thread\n");
  104.         return (FALSE);
  105.     }
  106.     printf("WatchDog thread created successfully\n");
  107.     }
  108.     else
  109.         DosSemClear(&ThreadDone);
  110.     return (TRUE);
  111. }
  112.  
  113. #pragma check_stack(off)
  114.  
  115. /* wake up every 5 seconds and check the carrier */
  116. static VOID FAR _loadds WatchDogThread(void)
  117. {
  118.     int stat;
  119.     BYTE msr;
  120.     static char msg[] = "[Big Brother : Carrier dropped.]";
  121.  
  122.     while (alive) {
  123.         DosSleep(500L);
  124.     stat = DosDevIOCtl(&msr, 0L, ASYNC_GETMODEMINPUT, IOCTL_ASYNC, hfCom);
  125.     if (stat || !(msr & DCD_ON)) {
  126.         VioWrtTTY(msg, sizeof(msg) - 1, 0);
  127.         /* give the program a chance to exit without our aid */
  128.             if (DosSemRequest(&Dead, 10000L)) {  // wait 10 seconds.
  129.         WeKilled = TRUE;
  130.         DosKillProcess(DKP_PROCESSTREE, pidProc);
  131.         }
  132.         break;
  133.     }
  134.     }
  135.     DosSemClear(&ThreadDone);
  136.     DosExit(EXIT_THREAD, 0);
  137. }
  138.