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