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