source: trunk/third/moira/server/startmoira.c @ 24319

Revision 24319, 3.1 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: startmoira.c 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 * This program starts the moira server in a "clean" environment.
4 * and then waits for it to exit.
5 *
6 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 */
10
11#include <mit-copyright.h>
12#include <moira.h>
13#include <moira_site.h>
14
15#include <sys/resource.h>
16#include <sys/wait.h>
17
18#include <errno.h>
19#include <fcntl.h>
20#include <signal.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24#include <time.h>
25
26RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/server/startmoira.c $ $Id: startmoira.c 3956 2010-01-05 20:56:56Z zacheiss $");
27
28#define PROG    "moirad"
29
30int rdpipe[2];
31char *whoami;
32void cleanup(int signal);
33
34void cleanup(int signal)
35{
36  int stat, serrno = errno;
37  char buf[BUFSIZ];
38
39  buf[0] = '\0';
40
41  while (waitpid(-1, &stat, WNOHANG) > 0)
42    {
43      if (WIFEXITED(stat))
44        {
45          if (WEXITSTATUS(stat))
46            {
47              sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
48              critical_alert(whoami, "startmoira", "%s", buf);
49            }
50        }
51      if (WIFSIGNALED(stat))
52        {
53          sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
54                  (WCOREDUMP(stat) ? "; Core dumped" : ""));
55          if (WCOREDUMP(stat))
56            critical_alert(whoami, "startmoira", "%s", buf);
57        }
58      write(rdpipe[1], buf, strlen(buf));
59      close(rdpipe[1]);
60    }
61  errno = serrno;
62}
63
64int main(int argc, char *argv[])
65{
66  char buf[BUFSIZ];
67  FILE *log, *prog;
68  int logf, inf, i, done, pid;
69  struct rlimit rl;
70
71  struct sigaction action;
72  int nfds;
73
74  whoami = argv[0];
75
76  getrlimit(RLIMIT_NOFILE, &rl);
77  nfds = rl.rlim_cur;
78
79  action.sa_handler = cleanup;
80  action.sa_flags = 0;
81  sigemptyset(&action.sa_mask);
82  sigaction(SIGCHLD, &action, NULL);
83
84  sprintf(buf, "%s/moira.log", MOIRA_DIR);
85  logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
86  if (logf < 0)
87    {
88      perror(buf);
89      exit(1);
90    }
91  inf = open("/dev/null", O_RDONLY , 0);
92  if (inf < 0)
93    {
94      perror("/dev/null");
95      exit(1);
96    }
97  pipe(rdpipe);
98  if (fork())
99    exit(0);
100  chdir("/");
101  close(0);
102  close(1);
103  close(2);
104  dup2(inf, 0);
105  dup2(inf, 1);
106  dup2(inf, 2);
107
108  setpgrp();
109  sprintf(buf, "%s/%s", BIN_DIR, PROG);
110
111  if ((pid = fork()) == 0)
112    {
113      dup2(inf, 0);
114      dup2(rdpipe[1], 1);
115      dup2(1, 2);
116      for (i = 3; i < nfds; i++)
117        close(i);
118      execl(buf, PROG, 0);
119      perror("cannot run moirad");
120      exit(1);
121    }
122  if (pid < 0)
123    {
124      perror("moira_starter");
125      exit(1);
126    }
127
128  log = fdopen(logf, "w");
129  prog = fdopen(rdpipe[0], "r");
130
131  do
132    {
133      char *time_s;
134      time_t now;
135
136      done = 0;
137      errno = 0;
138      if (!fgets(buf, BUFSIZ, prog))
139        {
140          if (errno && errno != EINTR)
141            {
142              strcpy(buf, "Unable to read from program: ");
143              strcat(buf, strerror(errno));
144              strcat(buf, "\n");
145            }
146          else
147            break;
148        }
149      time(&now);
150      time_s = ctime(&now) + 4;
151      time_s[strlen(time_s) - 6] = '\0';
152      fprintf(log, "%s %s", time_s, buf);
153      fflush(log);
154    }
155  while (!done);
156  exit(0);
157}
Note: See TracBrowser for help on using the repository browser.