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