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