home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
BIGBRO.LZH
/
BIGBRO.C
next >
Wrap
C/C++ Source or Header
|
1990-03-25
|
3KB
|
138 lines
#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define INCL_NOPM
#define INCL_DOS
#define INCL_VIO
#define INCL_DOSDEVICES
#define INCL_DOSDEVIOCTL
#include <os2.h>
#if !defined(CALLBACK)
#include "os2fubar.h"
#endif
#define STACK3 0x200
static HFILE hfCom; // modem
static PID pidProc;
#define alive TRUE
static int WeKilled = FALSE;
static long Dead = 0L;
static long ThreadDone = 0L;
int StartThreads(void);
static VOID FAR _loadds WatchDogThread(void);
void main(int argc, char **argv)
{
PID pidd;
RESULTCODES rescResults;
FILE *f;
time_t t;
struct tm *lt;
if (argc < 3) {
printf("BigBrother v0.00 Copyright (c) Peter Fitzsimmons, 1990\n");
printf("Usage: BigBro <comm handle> <program.exe> [arguments]\n");
exit(0);
}
hfCom = (HFILE) atoi(argv[1]);
if (hfCom < 0 || hfCom > 255)
hfCom = 0;
printf("hfComhandle = %d\n", hfCom);
DosSemSet(&ThreadDone);
if (StartThreads()) {
printf("BigBro is now spawning %s\n", argv[2]);
pidProc = spawnvp(P_NOWAIT, argv[2], &argv[2]);
if (pidProc != -1) {
DosSemSet(&Dead);
DosCwait(DCWA_PROCESSTREE, DCWW_WAIT, &rescResults, &pidd, pidProc);
DosSemClear(&Dead);
DosSemWait(&ThreadDone, -1L);
if (WeKilled) {
f = fopen("BigBro.Log", "at");
time(&t);
lt = localtime(&t);
if (f) {
printf("\n\aBigBro had to kill '%s'\n", argv[2]);
fprintf(f, "! %02d/%02d/%02d %02d:%02d:%02d BigBro killed %s\n",
lt->tm_year, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec, argv[2]);
fclose(f);
}
exit(0);
}
else
exit(rescResults.codeResult); /* exit with the errorlevel
* that the child wanted */
}
else
fprintf(stderr, "Couldn't spawn %s\n", argv[2]);
}
exit(3);
}
void *zalloc(size_t bytes)
{
void *ret = malloc(bytes);
if (!ret) {
fprintf(stderr, "Couldn't allocate %u bytes\n", bytes);
exit(3);
}
return (ret);
}
int StartThreads(void)
{
BYTE *stack3;
TID tid3;
if (hfCom) {
stack3 = zalloc(STACK3);
if (DosCreateThread((PFNTHREAD) WatchDogThread, &tid3, stack3 + STACK3)) {
fprintf(stderr, "Couldn't create Watchdog thread\n");
return (FALSE);
}
printf("WatchDog thread created successfully\n");
}
else
DosSemClear(&ThreadDone);
return (TRUE);
}
#pragma check_stack(off)
/* wake up every 5 seconds and check the carrier */
static VOID FAR _loadds WatchDogThread(void)
{
int stat;
BYTE msr;
static char msg[] = "[Big Brother : Carrier dropped.]";
while (alive) {
DosSleep(500L);
stat = DosDevIOCtl(&msr, 0L, ASYNC_GETMODEMINPUT, IOCTL_ASYNC, hfCom);
if (stat || !(msr & DCD_ON)) {
VioWrtTTY(msg, sizeof(msg) - 1, 0);
/* give the program a chance to exit without our aid */
if (DosSemRequest(&Dead, 10000L)) { // wait 10 seconds.
WeKilled = TRUE;
DosKillProcess(DKP_PROCESSTREE, pidProc);
}
break;
}
}
DosSemClear(&ThreadDone);
DosExit(EXIT_THREAD, 0);
}