]> andersk Git - moira.git/blob - server/startmoira.c
another random typo
[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 <stdio.h>
21 #include <strings.h>
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <sys/wait.h>
25 #include <sys/signal.h>
26 #include <sys/ioctl.h>
27 #include <moira_site.h>
28
29 #define PROG    "moirad"
30 char *whoami;
31
32 int rdpipe[2];
33 extern char *sys_siglist[];
34
35 cleanup()
36 {
37         union wait stat;
38         char buf[BUFSIZ];
39         extern int errno;
40         int serrno = errno;
41
42         buf[0]='\0';
43         
44         while (wait3(&stat, WNOHANG, 0) > 0) {
45                 if (WIFEXITED(stat)) {
46                         if (stat.w_retcode) {
47                                 sprintf(buf,
48                                         "moirad exited with code %d\n",
49                                         stat.w_retcode);
50                                 send_zgram("startmoira", buf);
51                         }
52                 }
53                 if (WIFSIGNALED(stat)) {
54                         sprintf(buf, "moirad exited on %s signal%s\n",
55                                 sys_siglist[stat.w_termsig],
56                                 (stat.w_coredump?"; Core dumped":0));
57                         if(stat.w_coredump) send_zgram("startmoira", buf);
58                 }
59                 write(rdpipe[1], buf, strlen(buf));
60                 close(rdpipe[1]);
61         }
62         errno = serrno;
63 }
64
65 main(argc, argv)
66         int argc;
67         char **argv;
68 {
69         char buf[BUFSIZ];
70         FILE *log, *prog;
71         int logf, inf, i, done, pid, tty;
72         
73         extern int errno;
74         extern char *sys_errlist[];
75         
76         int nfds = getdtablesize();
77         
78         whoami = argv[0];
79
80         setreuid(0);
81         signal(SIGCHLD, cleanup);
82         
83         sprintf(buf, "%s/moira.log", SMS_DIR);
84         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
85         if (logf<0) {
86                 perror(buf);
87                 exit(1);
88         }
89         inf = open("/dev/null", O_RDONLY , 0);
90         if (inf < 0) {
91                 perror("/dev/null");
92                 exit(1);
93         }
94         pipe(rdpipe);
95         if (fork()) {
96                 exit(0);
97         }
98         chdir("/");     
99         close(0);
100         close(1);
101         close(2);
102         dup2(inf, 0);
103         dup2(inf, 1);
104         dup2(inf, 2);
105         
106         tty = open("/dev/tty");
107         ioctl(tty, TIOCNOTTY, 0);
108         close(tty);
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++) close(i);
117                 execl(buf, PROG, 0);
118                 perror("cannot run moirad");
119                 exit(1);
120         }
121         if (pid<0) {
122                 perror("moira_starter");
123                 exit(1);
124         }
125
126         log = fdopen(logf, "w");
127         prog = fdopen(rdpipe[0], "r");
128         
129         
130         do {
131                 char *time_s;
132                 extern char *ctime();
133                 long foo;
134                 
135                 done = 0;
136                 errno = 0;
137                 if (fgets(buf, BUFSIZ, prog) == NULL) {
138                         if (errno) {
139                                 strcpy(buf, "Unable to read from program: ");
140                                 strcat(buf, sys_errlist[errno]);
141                                 strcat(buf, "\n");
142                         } else break;
143                 }
144                 time(&foo);
145                 time_s = ctime(&foo)+4;
146                 time_s[strlen(time_s)-6]='\0';
147                 fprintf(log, "%s %s", time_s, buf);
148                 fflush(log);
149         } while (!done);
150         exit(0);
151 }
152
153
154
155           
This page took 0.114928 seconds and 5 git commands to generate.