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