]> andersk Git - moira.git/blame_incremental - server/startmoira.c
Build shared libmoira via libtool.
[moira.git] / server / startmoira.c
... / ...
CommitLineData
1/* $Id$
2 *
3 * This program starts the moira server in a "clean" environment.
4 * and then waits for it to exit.
5 *
6 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 */
10
11#include <mit-copyright.h>
12#include <moira.h>
13#include <moira_site.h>
14
15#include <sys/resource.h>
16#include <sys/wait.h>
17
18#include <errno.h>
19#include <fcntl.h>
20#include <signal.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24
25RCSID("$Header$");
26
27#define PROG "moirad"
28
29int rdpipe[2];
30char *whoami;
31void cleanup(void);
32
33void cleanup(void)
34{
35 int stat, serrno = errno;
36 char buf[BUFSIZ];
37
38 buf[0] = '\0';
39
40 while (waitpid(-1, &stat, WNOHANG) > 0)
41 {
42 if (WIFEXITED(stat))
43 {
44 if (WEXITSTATUS(stat))
45 {
46 sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
47 critical_alert(whoami, "startmoira", "%s", buf);
48 }
49 }
50 if (WIFSIGNALED(stat))
51 {
52 sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
53 (WCOREDUMP(stat) ? "; Core dumped" : ""));
54 if (WCOREDUMP(stat))
55 critical_alert(whoami, "startmoira", "%s", buf);
56 }
57 write(rdpipe[1], buf, strlen(buf));
58 close(rdpipe[1]);
59 }
60 errno = serrno;
61}
62
63int main(int argc, char *argv[])
64{
65 char buf[BUFSIZ];
66 FILE *log, *prog;
67 int logf, inf, i, done, pid;
68 struct rlimit rl;
69
70 struct sigaction action;
71 int nfds;
72
73 whoami = argv[0];
74
75 getrlimit(RLIMIT_NOFILE, &rl);
76 nfds = rl.rlim_cur;
77
78 action.sa_handler = cleanup;
79 action.sa_flags = 0;
80 sigemptyset(&action.sa_mask);
81 sigaction(SIGCHLD, &action, NULL);
82
83 sprintf(buf, "%s/moira.log", MOIRA_DIR);
84 logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
85 if (logf < 0)
86 {
87 perror(buf);
88 exit(1);
89 }
90 inf = open("/dev/null", O_RDONLY , 0);
91 if (inf < 0)
92 {
93 perror("/dev/null");
94 exit(1);
95 }
96 pipe(rdpipe);
97 if (fork())
98 exit(0);
99 chdir("/");
100 close(0);
101 close(1);
102 close(2);
103 dup2(inf, 0);
104 dup2(inf, 1);
105 dup2(inf, 2);
106
107 setpgrp();
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++)
116 close(i);
117 execl(buf, PROG, 0);
118 perror("cannot run moirad");
119 exit(1);
120 }
121 if (pid < 0)
122 {
123 perror("moira_starter");
124 exit(1);
125 }
126
127 log = fdopen(logf, "w");
128 prog = fdopen(rdpipe[0], "r");
129
130 do
131 {
132 char *time_s;
133 long foo;
134
135 done = 0;
136 errno = 0;
137 if (!fgets(buf, BUFSIZ, prog))
138 {
139 if (errno && errno != EINTR)
140 {
141 strcpy(buf, "Unable to read from program: ");
142 strcat(buf, strerror(errno));
143 strcat(buf, "\n");
144 }
145 else
146 break;
147 }
148 time(&foo);
149 time_s = ctime(&foo) + 4;
150 time_s[strlen(time_s) - 6] = '\0';
151 fprintf(log, "%s %s", time_s, buf);
152 fflush(log);
153 }
154 while (!done);
155 exit(0);
156}
This page took 0.784126 seconds and 5 git commands to generate.