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