]> andersk Git - moira.git/blob - server/startmoira.c
.dc -> .pc
[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                                 send_zgram("startmoira", buf);
54                         }
55                 }
56                 if (WIFSIGNALED(stat)) {
57                         sprintf(buf, "exited on signal %d%s\n",
58                                 WTERMSIG(stat),
59                                 (WCOREDUMP(stat)?"; Core dumped":0));
60                         if(WCOREDUMP(stat)) send_zgram("startmoira", buf);
61                 }
62                 write(rdpipe[1], buf, strlen(buf));
63                 close(rdpipe[1]);
64         }
65         errno = serrno;
66 }
67
68 main(argc, argv)
69      int argc;
70      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                 perror(buf);
97                 exit(1);
98         }
99         inf = open("/dev/null", O_RDONLY , 0);
100         if (inf < 0) {
101                 perror("/dev/null");
102                 exit(1);
103         }
104         pipe(rdpipe);
105         if (fork()) {
106                 exit(0);
107         }
108         chdir("/");     
109         close(0);
110         close(1);
111         close(2);
112         dup2(inf, 0);
113         dup2(inf, 1);
114         dup2(inf, 2);
115         
116         setpgrp();
117         sprintf(buf, "%s/%s", BIN_DIR, PROG);
118         
119         if ((pid = fork()) == 0) {
120                 
121                 dup2(inf, 0);
122                 dup2(rdpipe[1], 1);
123                 dup2(1,2);
124                 for (i = 3; i <nfds; i++) close(i);
125                 execl(buf, PROG, 0);
126                 perror("cannot run moirad");
127                 exit(1);
128         }
129         if (pid<0) {
130                 perror("moira_starter");
131                 exit(1);
132         }
133
134         log = fdopen(logf, "w");
135         prog = fdopen(rdpipe[0], "r");
136         
137         
138         do {
139                 char *time_s;
140                 long foo;
141                 
142                 done = 0;
143                 errno = 0;
144                 if (fgets(buf, BUFSIZ, prog) == NULL) {
145                         if (errno && errno!=EINTR) {
146                                 strcpy(buf, "Unable to read from program: ");
147                                 strcat(buf, sys_errlist[errno]);
148                                 strcat(buf, "\n");
149                         } else break;
150                 }
151                 time(&foo);
152                 time_s = ctime(&foo)+4;
153                 time_s[strlen(time_s)-6]='\0';
154                 fprintf(log, "%s %s", time_s, buf);
155                 fflush(log);
156         } while (!done);
157         exit(0);
158 }
This page took 0.046936 seconds and 5 git commands to generate.