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