/*
 * bb.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id: bb.c,v 1.3 2001/10/12 13:51:12 root Exp root $";

/*
 * $Log: bb.c,v $
 * Revision 1.3  2001/10/12 13:51:12  root
 * #
 *
 * Revision 1.2  2000/12/24 00:44:36  root
 * #
 *
 * Revision 1.1  2000/12/24 00:42:11  root
 * #
 *
 */

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

static void
gak (int sig, siginfo_t * info, void *dummy) //last arg should be ucontext_t
{
  unsigned char buf[1024];
  struct sigaction sa;
  time_t t;


  time (&t);

  sprintf (buf, "/blackbox/%d", (int) t);

  mkdir (buf, 0777);
  chdir (buf);

  sprintf (buf,
           "echo \"attach %d\nwhere\ndetach\n\" | gdb /proc/%d/exe -n -batch -x /dev/stdin > where ",
           getpid (), getpid ());


  system (buf);

  sa.sa_handler = SIG_DFL;
  sa.sa_sigaction = NULL;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = SA_RESTART;

  sigaction (SIGSEGV, &sa, NULL);

  kill (getpid (), SIGSEGV);

}

void
initdatarecorder (void)
{
  struct sigaction sa;
  int i;
  int signals[] =
    { SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGPIPE,
    SIGALRM, SIGTERM, SIGUSR1, SIGUSR2
  };

  sa.sa_handler = NULL;
  sa.sa_sigaction = gak;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = SA_RESTART | SA_SIGINFO;


  for (i = 0; i < (sizeof (signals) / sizeof (int)); ++i)
    {
      sigaction (signals[i], &sa, NULL);
    }

}
