]> andersk Git - moira.git/blob - dcm/startdcm.c
Solaris/POSIX changes
[moira.git] / dcm / startdcm.c
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 <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 #include <fcntl.h>
27 #include <sys/resource.h>
28 #include <moira_site.h>
29
30 #define PROG    "dcm"
31
32 int rdpipe[2];
33 extern int errno;
34
35 void cleanup()
36 {
37         int stat, serrno = errno;
38         char buf[BUFSIZ];
39
40         buf[0]='\0';
41         
42         while (waitpid(-1, &stat, WNOHANG) > 0) {
43                 if (WIFEXITED(stat)) {
44                         if (WEXITSTATUS(stat))
45                                 sprintf(buf,
46                                         "exited with code %d\n",
47                                         WEXITSTATUS(stat));
48                 }
49                 if (WIFSIGNALED(stat)) {
50                         sprintf(buf, "exited with signal %d%s\n",
51                                 WTERMSIG(stat),
52                                 (WCOREDUMP(stat)?"; Core dumped":0));
53                 }
54                 write(rdpipe[1], buf, strlen(buf));
55                 close(rdpipe[1]);
56         }
57         errno = serrno;
58 }
59
60 main(argc, argv)
61 {
62         char buf[BUFSIZ];
63         FILE *log, *prog;
64         int logf, inf, i, done, pid, tty;
65         struct rlimit rl;
66         
67         extern int errno;
68         extern char *sys_errlist[];
69         
70         struct sigaction action;
71         int nfds;
72
73         getrlimit(RLIMIT_NOFILE, &rl);
74         nfds = rl.rlim_cur;
75
76         action.sa_handler = cleanup;
77         action.sa_flags = 0;
78         sigemptyset(&action.sa_mask);
79         sigaction(SIGCHLD, &action, NULL);
80
81         sprintf(buf, "%s/%s.log", SMS_DIR, PROG);
82         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
83         if (logf<0) {
84                 perror(buf);
85                 exit(1);
86         }
87         inf = open("/dev/null", O_RDONLY , 0);
88         if (inf < 0) {
89                 perror("/dev/null");
90                 exit(1);
91         }
92         pipe(rdpipe);
93         if (fork()) {
94                 exit(0);
95         }
96         chdir("/");     
97         close(0);
98         close(1);
99         close(2);
100         dup2(inf, 0);
101         dup2(inf, 1);
102         dup2(inf, 2);
103         
104         setpgrp();
105         sprintf(buf, "%s/%s", BIN_DIR, PROG);
106         
107         if ((pid = fork()) == 0) {
108                 
109                 dup2(inf, 0);
110                 dup2(rdpipe[1], 1);
111                 dup2(1,2);
112                 for (i = 3; i <nfds; i++) close(i);
113                 execl(buf, PROG, 0);
114                 perror("cannot run dcm");
115                 exit(1);
116         }
117         if (pid<0) {
118                 perror("startdcm");
119                 exit(1);
120         }
121
122         log = fdopen(logf, "w");
123         prog = fdopen(rdpipe[0], "r");
124         
125         
126         do {
127                 char *time_s;
128                 extern char *ctime();
129                 long foo;
130                 
131                 done = 0;
132                 errno = 0;
133                 if (fgets(buf, BUFSIZ, prog) == NULL) {
134                         if (errno) {
135                                 strcpy(buf, "Unable to read from program: ");
136                                 strcat(buf, sys_errlist[errno]);
137                                 strcat(buf, "\n");
138                         } else break;
139                 }
140                 time(&foo);
141                 time_s = ctime(&foo)+4;
142                 time_s[strlen(time_s)-6]='\0';
143                 fprintf(log, "%s <%d> %s", time_s, pid, buf);
144                 fflush(log);
145         } while (!done);
146         exit(0);
147 }
148
149
150
151           
This page took 0.045616 seconds and 5 git commands to generate.