TinyMAZE/
TinyMAZE/config/
TinyMAZE/doc/
TinyMAZE/run/msgs/
TinyMAZE/src/
TinyMAZE/src/db/
TinyMAZE/src/ident/
TinyMAZE/src/io/
TinyMAZE/src/prog/
TinyMAZE/src/softcode/
TinyMAZE/src/util/
/* util/wd.c - October 16, 1998, 4:46PM
 *
 * TinyMARE v1.0 (c) 1993, 1994, 1995, 1996, 1997, 1998 by Byron Stanoszek.
 * 
 * This  archive of TinyMARE has been completely developed by Byron Stanoszek
 * through six years of work in derivation from version 1.5 of  the  original
 * TinyMUSE program. The code is guaranteed to be extremely efficient, and an
 * active effort exists to support upgrades on a regular basis where numerous
 * bugs and glitches shall continue to be fixed. The various utility programs
 * contained within the package are also under this copyright notice. Credits
 * for borrowed ideas and code  used in the  creation of the TinyMARE network
 * system are given to many MUDs  and other such systems listed at the bottom
 * of this text.  Any suggestions, comments, or complaints should be directed
 * to the author at the following E-Mail address: gandalf@chezmoto.ai.mit.edu
 * 
 * Permission to copy and redistribute  this software is granted only through
 * the written consent of the author, Byron Stanoszek. Users of this software
 * agree to make  their best efforts to notify the author of any improvements
 * made for the purposes of including more efficient,  higher quality code in
 * subsequent releases of this distribution.
 * 
 * All materials developed as a consequence of the use of this software shall
 * be in accordance with the standards of acknowledgment in academic research.
 * In conjunction with products arising from the use of this material,  there
 * shall be no use of the names of the authors  nor any adaptation thereof in
 * any advertising,  promotional,  or sales literature without  prior written
 * consent from the authors in each case.
 */

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>

#define MUD_PROGRAM	"../bin/netmaze"

extern int setpgrp();

void init_io()
{
  int fd;
  
/* Jump into background */
  if(fork())
    exit(0);
  setpgrp();

/* Close standard input */
  fclose(stdin);
  
/* Open a link to the log file */
  if((fd = open("logs/wd.log", O_WRONLY|O_CREAT|O_APPEND, 0644)) < 0)
  {
    perror("logs/wd.log");
    exit(1);
  }
  
/* Convert standard output to logfile */
  close(fileno(stdout));
  dup2(fd, fileno(stdout));
  setbuf(stdout, NULL);
  
/* Convert standard error to logfile */
  close(fileno(stderr));
  dup2(fd, fileno(stderr));
  setbuf(stderr, NULL);
  
  close(fd);
}

void copy_logs()
{
  FILE *f, *g;
  char buf[512];

/* Open files for reading and writing */
  if(!(f = fopen("logs/commands~", "r")))
    return;
  if(!(g = fopen("cmd_crash", "w")))
  {
    fclose(f);
    return;
  }

/* Copy logs/commands~ */
  while(fgets(buf, 512, f))
    fputs(buf, g);
  fclose(f);

  if(!(f = fopen("logs/commands", "r")))
  {
    fclose(g);
    return;
  }

/* Copy logs/commands */
  while(fgets(buf, 512, f))
    fputs(buf, g);

  fclose(g);
  fclose(f);
}


int main(int argc, char **argv)
{
  int pid, status, mud_pid = 0;
  time_t tt = time(&tt);

  init_io();

  printf("Watchdog (C) 1997 by the developers of TinyMare.\n"
         "Booting up.     PID%6d, %s  ---\n", getpid(), ctime(&tt));

/* Main Loop */

  while(1)
  {
    copy_logs();

/* Spawn a new process */
    if((mud_pid = fork()) < 0)
    {
      perror(MUD_PROGRAM);
      exit(1);
    }

/* Execute Mud Program in Child Process */
    if(!mud_pid)
    {
      unlink("logs/socket_table");
      argv[0] = MUD_PROGRAM;
      if(execvp(MUD_PROGRAM, argv) < 0)
        perror(MUD_PROGRAM);
      exit(0);
    }

/* Parent Continues Here when Mud Program Terminates */

    tt = time(NULL);
    printf("Game Online:    PID%6d, %s", mud_pid, ctime(&tt));

    pid = wait(&status);
    tt = time(NULL);

    if(WIFSTOPPED(status))
    {
      printf("Game Suspended! PID%6d, %s", mud_pid, ctime(&tt));
      break;
    }
    else if(WIFSIGNALED(status))
    {
      printf("Mud Crashed:  ! PID%6d, -- %s (%d)\n", mud_pid,
        ctime(&tt), WTERMSIG(status));
    }
    else if(!WEXITSTATUS(status))
    {
      printf("Shutdown At:    PID%6d, %s", mud_pid, ctime(&tt));
      break;
    }
    else if(WEXITSTATUS(status) == 1)
    {
      printf("Game Abort:     PID%6d, %s", mud_pid, ctime(&tt));
      break;
    }
    else
      printf("Game Reboot:    PID%6d, %s", mud_pid, ctime(&tt));
  }

  printf("\n--Watchdog Exiting.\n");
  return(0);
}