]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
a3c170f4 | 2 | * |
7ac48069 | 3 | * This program starts the user registration server |
4 | * in a "clean" environment, and then waits for it to exit. | |
a3c170f4 | 5 | * |
7ac48069 | 6 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
a3c170f4 | 9 | */ |
10 | ||
0a5ff702 | 11 | #include <mit-copyright.h> |
7ac48069 | 12 | #include <moira.h> |
13 | #include <moira_site.h> | |
14 | ||
15 | #include <sys/resource.h> | |
16 | #include <sys/wait.h> | |
17 | ||
16e5277e | 18 | #include <errno.h> |
7ac48069 | 19 | #include <fcntl.h> |
20 | #include <signal.h> | |
a3c170f4 | 21 | #include <stdio.h> |
16e5277e | 22 | #include <string.h> |
7ac48069 | 23 | #include <unistd.h> |
24 | ||
25 | RCSID("$Header$"); | |
a3c170f4 | 26 | |
e6eb3546 | 27 | #define PROG "reg_svr" |
a3c170f4 | 28 | |
29 | int rdpipe[2]; | |
7ac48069 | 30 | |
31 | void cleanup(void); | |
a3c170f4 | 32 | |
5eaef520 | 33 | void cleanup(void) |
a3c170f4 | 34 | { |
5eaef520 | 35 | int stat, serrno = errno; |
36 | char buf[BUFSIZ]; | |
37 | ||
38 | buf[0] = '\0'; | |
39 | ||
40 | while (waitpid(-1, &stat, WNOHANG) > 0) | |
41 | { | |
42 | if (WIFEXITED(stat)) | |
43 | { | |
44 | if (WEXITSTATUS(stat)) | |
45 | sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat)); | |
46 | } | |
47 | if (WIFSIGNALED(stat)) | |
48 | { | |
49 | sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat), | |
50 | (WCOREDUMP(stat) ? "; Core dumped" : "")); | |
a3c170f4 | 51 | } |
5eaef520 | 52 | write(rdpipe[1], buf, strlen(buf)); |
53 | close(rdpipe[1]); | |
54 | } | |
55 | errno = serrno; | |
a3c170f4 | 56 | } |
57 | ||
5eaef520 | 58 | int main(int argc, char **argv) |
a3c170f4 | 59 | { |
5eaef520 | 60 | char buf[BUFSIZ]; |
61 | FILE *log, *prog; | |
7ac48069 | 62 | int logf, inf, i, done, pid; |
5eaef520 | 63 | struct rlimit rl; |
64 | ||
5eaef520 | 65 | struct sigaction action; |
66 | int nfds; | |
67 | ||
68 | getrlimit(RLIMIT_NOFILE, &rl); | |
69 | nfds = rl.rlim_cur; | |
70 | ||
71 | action.sa_handler = cleanup; | |
72 | action.sa_flags = 0; | |
73 | sigemptyset(&action.sa_mask); | |
74 | sigaction(SIGCHLD, &action, NULL); | |
75 | ||
59ec8dae | 76 | sprintf(buf, "%s/%s.log", MOIRA_DIR, PROG); |
5eaef520 | 77 | logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640); |
78 | if (logf < 0) | |
79 | { | |
80 | perror(buf); | |
81 | exit(1); | |
82 | } | |
83 | inf = open("/dev/null", O_RDONLY , 0); | |
84 | if (inf < 0) | |
85 | { | |
86 | perror("/dev/null"); | |
87 | exit(1); | |
88 | } | |
89 | pipe(rdpipe); | |
90 | if (fork()) | |
91 | exit(0); | |
92 | chdir("/"); | |
93 | close(0); | |
94 | close(1); | |
95 | close(2); | |
96 | dup2(inf, 0); | |
97 | dup2(inf, 1); | |
98 | dup2(inf, 2); | |
99 | ||
100 | setpgrp(); | |
101 | sprintf(buf, "%s/%s", BIN_DIR, PROG); | |
102 | ||
103 | if ((pid = fork()) == 0) | |
104 | { | |
105 | dup2(inf, 0); | |
106 | dup2(rdpipe[1], 1); | |
107 | dup2(1,2); | |
108 | for (i = 3; i < nfds; i++) | |
109 | close(i); | |
110 | execl(buf, PROG, 0); | |
111 | perror("cannot run reg_svr"); | |
112 | exit(1); | |
113 | } | |
114 | if (pid < 0) | |
115 | { | |
116 | perror("startreg"); | |
117 | exit(1); | |
118 | } | |
119 | ||
120 | log = fdopen(logf, "w"); | |
121 | prog = fdopen(rdpipe[0], "r"); | |
122 | ||
123 | do | |
124 | { | |
125 | char *time_s; | |
126 | long foo; | |
127 | ||
128 | done = 0; | |
129 | errno = 0; | |
130 | if (!fgets(buf, BUFSIZ, prog)) | |
131 | { | |
132 | if (errno && errno != EINTR) | |
133 | { | |
134 | strcpy(buf, "Unable to read from program: "); | |
7ac48069 | 135 | strcat(buf, strerror(errno)); |
5eaef520 | 136 | strcat(buf, "\n"); |
137 | } | |
138 | else | |
139 | break; | |
140 | } | |
141 | time(&foo); | |
142 | time_s = ctime(&foo) + 4; | |
143 | time_s[strlen(time_s) - 6] = '\0'; | |
144 | fprintf(log, "%s <%d> %s", time_s, pid, buf); | |
145 | fflush(log); | |
146 | } | |
147 | while (!done); | |
148 | exit(0); | |
a3c170f4 | 149 | } |