]> andersk Git - moira.git/blame - server/startmoira.c
missed one
[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.
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>
3c46fcae 20#include <stdio.h>
95f3bdce 21#include <strings.h>
3c46fcae 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>
2ce085d2 27#include <moira_site.h>
3c46fcae 28
2ce085d2 29#define PROG "moirad"
3c46fcae 30
31int rdpipe[2];
32char *sigdescr[] = {
33 0,
34 "hangup",
35 "interrupt",
36 "quit",
37 "illegal instruction",
38 "trace/BPT trap",
39 "IOT trap",
40 "EMT trap",
41 "floating exception",
42 "kill",
43 "bus error",
44 "segmentation violation",
45 "bad system call",
46 "broken pipe",
47 "alarm clock",
48 "termination",
49 "urgent I/O condition",
50 "stopped",
51 "stopped",
52 "continued",
53 "child exited",
54 "stopped (tty input)",
55 "stopped (tty output)",
56 "I/O possible",
57 "cputime limit exceeded",
58 "filesize limit exceeded",
59 "virtual timer expired",
60 "profiling timer expired",
61 "window size changed",
62 "signal 29",
63 "user defined signal 1",
64 "user defined signal 2",
65 "signal 32"
66};
67
68cleanup()
69{
70 union wait stat;
71 char buf[BUFSIZ];
72 extern int errno;
73 int serrno = errno;
74
75 buf[0]='\0';
76
77 while (wait3(&stat, WNOHANG, 0) > 0) {
78 if (WIFEXITED(stat)) {
79 if (stat.w_retcode)
80 sprintf(buf,
81 "exited with code %d\n",
82 stat.w_retcode);
83 }
84 if (WIFSIGNALED(stat)) {
85 sprintf(buf, "exited on %s signal%s\n",
86 sigdescr[stat.w_termsig],
87 (stat.w_coredump?"; Core dumped":0));
88 }
89 write(rdpipe[1], buf, strlen(buf));
90 close(rdpipe[1]);
91 }
92 errno = serrno;
93}
94
95main(argc, argv)
96{
97 char buf[BUFSIZ];
98 FILE *log, *prog;
99 int logf, inf, i, done, pid, tty;
100
101 extern int errno;
102 extern char *sys_errlist[];
103
104 int nfds = getdtablesize();
105
106 setreuid(0);
107 signal(SIGCHLD, cleanup);
108
2ce085d2 109 sprintf(buf, "%s/moira.log", SMS_DIR);
e6eb3546 110 logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
3c46fcae 111 if (logf<0) {
e6eb3546 112 perror(buf);
3c46fcae 113 exit(1);
114 }
115 inf = open("/dev/null", O_RDONLY , 0);
116 if (inf < 0) {
117 perror("/dev/null");
118 exit(1);
119 }
120 pipe(rdpipe);
121 if (fork()) {
122 exit();
123 }
124 chdir("/");
125 close(0);
126 close(1);
127 close(2);
128 dup2(inf, 0);
129 dup2(inf, 1);
130 dup2(inf, 2);
131
132 tty = open("/dev/tty");
133 ioctl(tty, TIOCNOTTY, 0);
134 close(tty);
e6eb3546 135 sprintf(buf, "%s/%s", BIN_DIR, PROG);
3c46fcae 136
137 if ((pid = fork()) == 0) {
138
139 dup2(inf, 0);
140 dup2(rdpipe[1], 1);
141 dup2(1,2);
142 for (i = 3; i <nfds; i++) close(i);
e6eb3546 143 execl(buf, PROG, 0);
2ce085d2 144 perror("cannot run moirad");
3c46fcae 145 exit(1);
146 }
147 if (pid<0) {
2ce085d2 148 perror("moira_starter");
3c46fcae 149 exit(1);
150 }
151
152 log = fdopen(logf, "w");
153 prog = fdopen(rdpipe[0], "r");
154
155
156 do {
157 char *time_s;
158 extern char *ctime();
159 long foo;
160
161 done = 0;
162 errno = 0;
163 if (fgets(buf, BUFSIZ, prog) == NULL) {
164 if (errno) {
165 strcpy(buf, "Unable to read from program: ");
166 strcat(buf, sys_errlist[errno]);
167 strcat(buf, "\n");
168 } else break;
169 }
170 time(&foo);
171 time_s = ctime(&foo)+4;
172 time_s[strlen(time_s)-6]='\0';
95f3bdce 173 fprintf(log, "%s <%d> %s", time_s, pid, buf);
3c46fcae 174 fflush(log);
175 } while (!done);
95f3bdce 176 exit(0);
3c46fcae 177}
178
179
180
181
This page took 0.179777 seconds and 5 git commands to generate.