]> andersk Git - moira.git/blame - server/startmoira.c
Build shared libmoira via libtool.
[moira.git] / server / startmoira.c
CommitLineData
7ac48069 1/* $Id$
3c46fcae 2 *
7ac48069 3 * This program starts the moira server in a "clean" environment.
4 * and then waits for it to exit.
5eaef520 5 *
7ac48069 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>.
3c46fcae 9 */
10
c801de4c 11#include <mit-copyright.h>
7ac48069 12#include <moira.h>
13#include <moira_site.h>
14
15#include <sys/resource.h>
16#include <sys/wait.h>
17
960b073b 18#include <errno.h>
7ac48069 19#include <fcntl.h>
20#include <signal.h>
3c46fcae 21#include <stdio.h>
960b073b 22#include <string.h>
7ac48069 23#include <unistd.h>
03c05291 24
7ac48069 25RCSID("$Header$");
3c46fcae 26
2ce085d2 27#define PROG "moirad"
3c46fcae 28
29int rdpipe[2];
03c05291 30char *whoami;
7ac48069 31void cleanup(void);
3c46fcae 32
5eaef520 33void cleanup(void)
3c46fcae 34{
5eaef520 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));
a816420b 47 critical_alert(whoami, "startmoira", "%s", buf);
5eaef520 48 }
3c46fcae 49 }
5eaef520 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))
a816420b 55 critical_alert(whoami, "startmoira", "%s", buf);
5eaef520 56 }
57 write(rdpipe[1], buf, strlen(buf));
58 close(rdpipe[1]);
59 }
60 errno = serrno;
3c46fcae 61}
62
5eaef520 63int main(int argc, char *argv[])
3c46fcae 64{
5eaef520 65 char buf[BUFSIZ];
66 FILE *log, *prog;
7ac48069 67 int logf, inf, i, done, pid;
5eaef520 68 struct rlimit rl;
69
5eaef520 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
59ec8dae 83 sprintf(buf, "%s/moira.log", MOIRA_DIR);
5eaef520 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: ");
7ac48069 142 strcat(buf, strerror(errno));
5eaef520 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);
3c46fcae 156}
This page took 0.150373 seconds and 5 git commands to generate.