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