]> andersk Git - moira.git/blob - server/startmoira.c
startmoira now sends a zephyr to -c moira -i startmoira when moirad
[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                 if (WIFSIGNALED(stat)) {
53                         sprintf(buf, "moirad exited on %s signal%s\n",
54                                 sys_siglist[stat.w_termsig],
55                                 (stat.w_coredump?"; Core dumped":0));
56                         if(stat.w_coredump) send_zgram("startmoira", buf);
57                 }
58                 write(rdpipe[1], buf, strlen(buf));
59                 close(rdpipe[1]);
60         }
61         errno = serrno;
62 }
63
64 main(argc, argv)
65         int argc;
66         char **argv;
67 {
68         char buf[BUFSIZ];
69         FILE *log, *prog;
70         int logf, inf, i, done, pid, tty;
71         
72         extern int errno;
73         extern char *sys_errlist[];
74         
75         int nfds = getdtablesize();
76         
77         whoami = argv[0];
78
79         setreuid(0);
80         signal(SIGCHLD, cleanup);
81         
82         sprintf(buf, "%s/moira.log", SMS_DIR);
83         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
84         if (logf<0) {
85                 perror(buf);
86                 exit(1);
87         }
88         inf = open("/dev/null", O_RDONLY , 0);
89         if (inf < 0) {
90                 perror("/dev/null");
91                 exit(1);
92         }
93         pipe(rdpipe);
94         if (fork()) {
95                 exit(0);
96         }
97         chdir("/");     
98         close(0);
99         close(1);
100         close(2);
101         dup2(inf, 0);
102         dup2(inf, 1);
103         dup2(inf, 2);
104         
105         tty = open("/dev/tty");
106         ioctl(tty, TIOCNOTTY, 0);
107         close(tty);
108         sprintf(buf, "%s/%s", BIN_DIR, PROG);
109         
110         if ((pid = fork()) == 0) {
111                 
112                 dup2(inf, 0);
113                 dup2(rdpipe[1], 1);
114                 dup2(1,2);
115                 for (i = 3; i <nfds; i++) close(i);
116                 execl(buf, PROG, 0);
117                 perror("cannot run moirad");
118                 exit(1);
119         }
120         if (pid<0) {
121                 perror("moira_starter");
122                 exit(1);
123         }
124
125         log = fdopen(logf, "w");
126         prog = fdopen(rdpipe[0], "r");
127         
128         
129         do {
130                 char *time_s;
131                 extern char *ctime();
132                 long foo;
133                 
134                 done = 0;
135                 errno = 0;
136                 if (fgets(buf, BUFSIZ, prog) == NULL) {
137                         if (errno) {
138                                 strcpy(buf, "Unable to read from program: ");
139                                 strcat(buf, sys_errlist[errno]);
140                                 strcat(buf, "\n");
141                         } else break;
142                 }
143                 time(&foo);
144                 time_s = ctime(&foo)+4;
145                 time_s[strlen(time_s)-6]='\0';
146                 fprintf(log, "%s %s", time_s, buf);
147                 fflush(log);
148         } while (!done);
149         exit(0);
150 }
151
152
153
154           
This page took 0.058137 seconds and 5 git commands to generate.