]> andersk Git - moira.git/blobdiff - server/startmoira.c
Build shared libmoira via libtool.
[moira.git] / server / startmoira.c
index 9f6a9e698cece4b7eecebc32874b16310ff03632..2692cac623afdca662302406990a17af7dc658e2 100644 (file)
-/*
- *     $Source$
- *     $Author$
- *     $Header$
+/* $Id$
  *
- *     Copyright (C) 1987 by the Massachusetts Institute of Technology
- *
- *     This program starts the sms server in a "clean" environment.
- *     and then waits for it to exit.
- * 
- *     $Log$
- *     Revision 1.1  1987-06-01 03:35:33  wesommer
- *     Initial revision
+ * This program starts the moira server in a "clean" environment.
+ * and then waits for it to exit.
  *
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
  */
 
-#ifndef lint
-static char *rcsid_sms_starter_c = "$Header$";
-#endif lint
+#include <mit-copyright.h>
+#include <moira.h>
+#include <moira_site.h>
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/file.h>
+#include <sys/resource.h>
 #include <sys/wait.h>
-#include <sys/signal.h>
-#include <sys/ioctl.h>
 
-#define SMS_LOG_FILE "/u1/sms/sms.log"
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+RCSID("$Header$");
 
-#define SMS_PROG "/u1/sms/rpc/smsd"
+#define PROG   "moirad"
 
 int rdpipe[2];
-char *sigdescr[] = {
-       0,
-       "hangup",
-       "interrupt",    
-       "quit",
-       "illegal instruction",
-       "trace/BPT trap",
-       "IOT trap",
-       "EMT trap",
-       "floating exception",
-       "kill",
-       "bus error",
-       "segmentation violation",
-       "bad system call",
-       "broken pipe",
-       "alarm clock",
-       "termination",
-       "urgent I/O condition",
-       "stopped",
-       "stopped",
-       "continued",
-       "child exited",
-       "stopped (tty input)",
-       "stopped (tty output)",
-       "I/O possible",
-       "cputime limit exceeded",
-       "filesize limit exceeded",
-       "virtual timer expired",
-       "profiling timer expired",
-       "window size changed",
-       "signal 29",
-       "user defined signal 1",
-       "user defined signal 2",
-       "signal 32"
-};
-
-cleanup()
+char *whoami;
+void cleanup(void);
+
+void cleanup(void)
 {
-       union wait stat;
-       char buf[BUFSIZ];
-       extern int errno;
-       int serrno = errno;
-
-       buf[0]='\0';
-       
-       while (wait3(&stat, WNOHANG, 0) > 0) {
-               if (WIFEXITED(stat)) {
-                       if (stat.w_retcode)
-                               sprintf(buf,
-                                       "exited with code %d\n",
-                                       stat.w_retcode);
-               }
-               if (WIFSIGNALED(stat)) {
-                       sprintf(buf, "exited on %s signal%s\n",
-                               sigdescr[stat.w_termsig],
-                               (stat.w_coredump?"; Core dumped":0));
-               }
-               write(rdpipe[1], buf, strlen(buf));
-               close(rdpipe[1]);
+  int stat, serrno = errno;
+  char buf[BUFSIZ];
+
+  buf[0] = '\0';
+
+  while (waitpid(-1, &stat, WNOHANG) > 0)
+    {
+      if (WIFEXITED(stat))
+       {
+         if (WEXITSTATUS(stat))
+           {
+             sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
+             critical_alert(whoami, "startmoira", "%s", buf);
+           }
+       }
+      if (WIFSIGNALED(stat))
+       {
+         sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
+                 (WCOREDUMP(stat) ? "; Core dumped" : ""));
+         if (WCOREDUMP(stat))
+           critical_alert(whoami, "startmoira", "%s", buf);
        }
-       errno = serrno;
+      write(rdpipe[1], buf, strlen(buf));
+      close(rdpipe[1]);
+    }
+  errno = serrno;
 }
 
-main(argc, argv)
+int main(int argc, char *argv[])
 {
-       char buf[BUFSIZ];
-       FILE *log, *prog;
-       int logf, inf, i, done, pid, tty;
-       
-       extern int errno;
-       extern char *sys_errlist[];
-       
-       int nfds = getdtablesize();
-       
-       setreuid(0);
-       signal(SIGCHLD, cleanup);
-       
-       logf = open(SMS_LOG_FILE, O_CREAT|O_WRONLY|O_APPEND, 0640);
-       if (logf<0) {
-               perror(SMS_LOG_FILE);
-               exit(1);
-       }
-       inf = open("/dev/null", O_RDONLY , 0);
-       if (inf < 0) {
-               perror("/dev/null");
-               exit(1);
-       }
-       pipe(rdpipe);
-       if (fork()) {
-               exit();
-       }
-       chdir("/");     
-       close(0);
-       close(1);
-       close(2);
-       dup2(inf, 0);
-       dup2(inf, 1);
-       dup2(inf, 2);
-       
-       tty = open("/dev/tty");
-       ioctl(tty, TIOCNOTTY, 0);
-       close(tty);
-       
-       if ((pid = fork()) == 0) {
-               
-               dup2(inf, 0);
-               dup2(rdpipe[1], 1);
-               dup2(1,2);
-               for (i = 3; i <nfds; i++) close(i);
-               execl(SMS_PROG, "smsd", 0);
-               perror("cannot run smsd");
-               exit(1);
-       }
-       if (pid<0) {
-               perror("sms_starter");
-               exit(1);
-       }
+  char buf[BUFSIZ];
+  FILE *log, *prog;
+  int logf, inf, i, done, pid;
+  struct rlimit rl;
 
-       log = fdopen(logf, "w");
-       prog = fdopen(rdpipe[0], "r");
-       
-       
-       do {
-               char *time_s;
-               extern char *ctime();
-               long foo;
-               
-               done = 0;
-               errno = 0;
-               if (fgets(buf, BUFSIZ, prog) == NULL) {
-                       if (errno) {
-                               strcpy(buf, "Unable to read from program: ");
-                               strcat(buf, sys_errlist[errno]);
-                               strcat(buf, "\n");
-                       } else break;
-               }
-               time(&foo);
-               time_s = ctime(&foo)+4;
-               time_s[strlen(time_s)-6]='\0';
-               fprintf(log, "%s %s", time_s, buf);
-               fflush(log);
-       } while (!done);
-}
+  struct sigaction action;
+  int nfds;
+
+  whoami = argv[0];
+
+  getrlimit(RLIMIT_NOFILE, &rl);
+  nfds = rl.rlim_cur;
+
+  action.sa_handler = cleanup;
+  action.sa_flags = 0;
+  sigemptyset(&action.sa_mask);
+  sigaction(SIGCHLD, &action, NULL);
 
+  sprintf(buf, "%s/moira.log", MOIRA_DIR);
+  logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
+  if (logf < 0)
+    {
+      perror(buf);
+      exit(1);
+    }
+  inf = open("/dev/null", O_RDONLY , 0);
+  if (inf < 0)
+    {
+      perror("/dev/null");
+      exit(1);
+    }
+  pipe(rdpipe);
+  if (fork())
+    exit(0);
+  chdir("/");
+  close(0);
+  close(1);
+  close(2);
+  dup2(inf, 0);
+  dup2(inf, 1);
+  dup2(inf, 2);
 
+  setpgrp();
+  sprintf(buf, "%s/%s", BIN_DIR, PROG);
 
-         
+  if ((pid = fork()) == 0)
+    {
+      dup2(inf, 0);
+      dup2(rdpipe[1], 1);
+      dup2(1, 2);
+      for (i = 3; i < nfds; i++)
+       close(i);
+      execl(buf, PROG, 0);
+      perror("cannot run moirad");
+      exit(1);
+    }
+  if (pid < 0)
+    {
+      perror("moira_starter");
+      exit(1);
+    }
+
+  log = fdopen(logf, "w");
+  prog = fdopen(rdpipe[0], "r");
+
+  do
+    {
+      char *time_s;
+      long foo;
+
+      done = 0;
+      errno = 0;
+      if (!fgets(buf, BUFSIZ, prog))
+       {
+         if (errno && errno != EINTR)
+           {
+             strcpy(buf, "Unable to read from program: ");
+             strcat(buf, strerror(errno));
+             strcat(buf, "\n");
+           }
+         else
+           break;
+       }
+      time(&foo);
+      time_s = ctime(&foo) + 4;
+      time_s[strlen(time_s) - 6] = '\0';
+      fprintf(log, "%s %s", time_s, buf);
+      fflush(log);
+    }
+  while (!done);
+  exit(0);
+}
This page took 0.060086 seconds and 4 git commands to generate.