/* 
 * $Id: reboot.c,v 1.2 1999/02/16 17:37:15 labovit Exp $
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <mrt.h>

int init_mrt_reboot (int argc, char *argv[]) {
  char tmp[MAXLINE];

  getcwd (tmp, MAXLINE);
  MRT->cwd = strdup (tmp);
  MRT->argc = argc;
  MRT->argv = argv;

  return (1);
}

void mrt_reboot () {
  char tmp[MAXLINE], *cp;
  int i = 0;

  if (MRT->daemon_mode == 0) {
    for (; i < 3; i++)
      fcntl (i, F_SETFD, 0); /* keep open */
  }

  for (; i < getdtablesize (); i++)
    fcntl (i, F_SETFD, 1); /* close */

  /* signals ? */
  alarm (0);

  cp = tmp;
  for (i = 0; MRT->argv[i]; i++) {
    if (i > 0) *cp++ = ' ';
    sprintf (cp, "%s", MRT->argv[i]);
    cp += strlen (cp);
  }
  trace (TR_WARN, MRT->trace, "MRT rebooting %s\n", tmp);
  execvp (MRT->argv[0], MRT->argv);
  trace (TR_ERROR, MRT->trace, "MRT ERROR rebooting %s\n", tmp);
  exit (1);
}
