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