]> andersk Git - moira.git/blob - server/startmoira.c
NULL != ""
[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()
41 {
42         int stat, serrno = errno;
43         char buf[BUFSIZ];
44
45         buf[0]='\0';
46         
47         while (waitpid(-1, &stat, WNOHANG) > 0) {
48                 if (WIFEXITED(stat)) {
49                         if (WEXITSTATUS(stat)) {
50                                 sprintf(buf,
51                                         "exited with code %d\n",
52                                         WEXITSTATUS(stat));
53                                 critical_alert("startmoira", "%s", buf);
54                         }
55                 }
56                 if (WIFSIGNALED(stat)) {
57                         sprintf(buf, "exited on signal %d%s\n",
58                                 WTERMSIG(stat),
59                                 (WCOREDUMP(stat)?"; Core dumped":""));
60                         if(WCOREDUMP(stat))
61                           critical_alert("startmoira", "%s", buf);
62                 }
63                 write(rdpipe[1], buf, strlen(buf));
64                 close(rdpipe[1]);
65         }
66         errno = serrno;
67 }
68
69 main(argc, argv)
70      int argc;
71      char *argv[];
72 {
73         char buf[BUFSIZ];
74         FILE *log, *prog;
75         int logf, inf, i, done, pid, tty;
76         struct rlimit rl;
77         
78         extern int errno;
79         extern char *sys_errlist[];
80         
81         struct sigaction action;
82         int nfds;
83         
84         whoami = argv[0];
85         
86         getrlimit(RLIMIT_NOFILE, &rl);
87         nfds = rl.rlim_cur;
88
89         action.sa_handler = cleanup;
90         action.sa_flags = 0;
91         sigemptyset(&action.sa_mask);
92         sigaction(SIGCHLD, &action, NULL);
93         
94         sprintf(buf, "%s/moira.log", SMS_DIR);
95         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
96         if (logf<0) {
97                 perror(buf);
98                 exit(1);
99         }
100         inf = open("/dev/null", O_RDONLY , 0);
101         if (inf < 0) {
102                 perror("/dev/null");
103                 exit(1);
104         }
105         pipe(rdpipe);
106         if (fork()) {
107                 exit(0);
108         }
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++) close(i);
126                 execl(buf, PROG, 0);
127                 perror("cannot run moirad");
128                 exit(1);
129         }
130         if (pid<0) {
131                 perror("moira_starter");
132                 exit(1);
133         }
134
135         log = fdopen(logf, "w");
136         prog = fdopen(rdpipe[0], "r");
137         
138         
139         do {
140                 char *time_s;
141                 long foo;
142                 
143                 done = 0;
144                 errno = 0;
145                 if (fgets(buf, BUFSIZ, prog) == NULL) {
146                         if (errno && errno!=EINTR) {
147                                 strcpy(buf, "Unable to read from program: ");
148                                 strcat(buf, sys_errlist[errno]);
149                                 strcat(buf, "\n");
150                         } else break;
151                 }
152                 time(&foo);
153                 time_s = ctime(&foo)+4;
154                 time_s[strlen(time_s)-6]='\0';
155                 fprintf(log, "%s %s", time_s, buf);
156                 fflush(log);
157         } while (!done);
158         exit(0);
159 }
This page took 0.829996 seconds and 5 git commands to generate.