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