]> andersk Git - moira.git/blame - dcm/startdcm.c
fixup SIGCHLD before and after call to system()
[moira.git] / dcm / startdcm.c
CommitLineData
4297fecb 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
0a5ff702 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>.
4297fecb 9 *
10 * This program starts the DCM in a "clean" environment.
11 * and then waits for it to exit.
12 */
13
14#ifndef lint
15static char *rcsid_sms_starter_c = "$Header$";
16#endif lint
17
0a5ff702 18#include <mit-copyright.h>
4297fecb 19#include <stdio.h>
20#include <strings.h>
21#include <sys/types.h>
22#include <sys/file.h>
23#include <sys/wait.h>
24#include <sys/signal.h>
25#include <sys/ioctl.h>
26
27#define SMS_LOG_FILE "/u1/sms/dcm.log"
28
29#define SMS_PROG "/u1/sms/bin/dcm"
30
31int rdpipe[2];
32char *sigdescr[] = {
33 0,
34 "hangup",
35 "interrupt",
36 "quit",
37 "illegal instruction",
38 "trace/BPT trap",
39 "IOT trap",
40 "EMT trap",
41 "floating exception",
42 "kill",
43 "bus error",
44 "segmentation violation",
45 "bad system call",
46 "broken pipe",
47 "alarm clock",
48 "termination",
49 "urgent I/O condition",
50 "stopped",
51 "stopped",
52 "continued",
53 "child exited",
54 "stopped (tty input)",
55 "stopped (tty output)",
56 "I/O possible",
57 "cputime limit exceeded",
58 "filesize limit exceeded",
59 "virtual timer expired",
60 "profiling timer expired",
61 "window size changed",
62 "signal 29",
63 "user defined signal 1",
64 "user defined signal 2",
65 "signal 32"
66};
67
68cleanup()
69{
70 union wait stat;
71 char buf[BUFSIZ];
72 extern int errno;
73 int serrno = errno;
74
75 buf[0]='\0';
76
77 while (wait3(&stat, WNOHANG, 0) > 0) {
78 if (WIFEXITED(stat)) {
79 if (stat.w_retcode)
80 sprintf(buf,
81 "exited with code %d\n",
82 stat.w_retcode);
83 }
84 if (WIFSIGNALED(stat)) {
85 sprintf(buf, "exited on %s signal%s\n",
86 sigdescr[stat.w_termsig],
87 (stat.w_coredump?"; Core dumped":0));
88 }
89 write(rdpipe[1], buf, strlen(buf));
90 close(rdpipe[1]);
91 }
92 errno = serrno;
93}
94
95main(argc, argv)
96{
97 char buf[BUFSIZ];
98 FILE *log, *prog;
99 int logf, inf, i, done, pid, tty;
100
101 extern int errno;
102 extern char *sys_errlist[];
103
104 int nfds = getdtablesize();
105
106 setreuid(0);
107 signal(SIGCHLD, cleanup);
108
109 logf = open(SMS_LOG_FILE, O_CREAT|O_WRONLY|O_APPEND, 0640);
110 if (logf<0) {
111 perror(SMS_LOG_FILE);
112 exit(1);
113 }
114 inf = open("/dev/null", O_RDONLY , 0);
115 if (inf < 0) {
116 perror("/dev/null");
117 exit(1);
118 }
119 pipe(rdpipe);
120 if (fork()) {
121 exit();
122 }
123 chdir("/");
124 close(0);
125 close(1);
126 close(2);
127 dup2(inf, 0);
128 dup2(inf, 1);
129 dup2(inf, 2);
130
131 tty = open("/dev/tty");
132 ioctl(tty, TIOCNOTTY, 0);
133 close(tty);
134
135 if ((pid = fork()) == 0) {
136
137 dup2(inf, 0);
138 dup2(rdpipe[1], 1);
139 dup2(1,2);
140 for (i = 3; i <nfds; i++) close(i);
141 execl(SMS_PROG, "dcm", 0);
142 perror("cannot run dcm");
143 exit(1);
144 }
145 if (pid<0) {
146 perror("startdcm");
147 exit(1);
148 }
149
150 log = fdopen(logf, "w");
151 prog = fdopen(rdpipe[0], "r");
152
153
154 do {
155 char *time_s;
156 extern char *ctime();
157 long foo;
158
159 done = 0;
160 errno = 0;
161 if (fgets(buf, BUFSIZ, prog) == NULL) {
162 if (errno) {
163 strcpy(buf, "Unable to read from program: ");
164 strcat(buf, sys_errlist[errno]);
165 strcat(buf, "\n");
166 } else break;
167 }
168 time(&foo);
169 time_s = ctime(&foo)+4;
170 time_s[strlen(time_s)-6]='\0';
171 fprintf(log, "%s <%d> %s", time_s, pid, buf);
172 fflush(log);
173 } while (!done);
174 exit(0);
175}
176
177
178
179
This page took 0.096491 seconds and 5 git commands to generate.