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