]> andersk Git - moira.git/blob - dcm/startdcm.c
Change `SMS' to `Moira' where possible.
[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 <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(void)
38 {
39   int stat, serrno = errno;
40   char buf[BUFSIZ];
41
42   buf[0] = '\0';
43
44   while (waitpid(-1, &stat, WNOHANG) > 0)
45     {
46       if (WIFEXITED(stat))
47         {
48           if (WEXITSTATUS(stat))
49             sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
50         }
51       if (WIFSIGNALED(stat))
52         {
53           sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
54                   (WCOREDUMP(stat) ? "; Core dumped" : ""));
55         }
56       write(rdpipe[1], buf, strlen(buf));
57       close(rdpipe[1]);
58     }
59   errno = serrno;
60 }
61
62 int main(int argc, char *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", MOIRA_DIR, PROG);
84   logf = open(buf, O_CREAT | O_WRONLY | O_APPEND, 0640);
85   if (logf < 0)
86     {
87       perror(buf);
88       exit(1);
89     }
90   inf = open("/dev/null", O_RDONLY , 0);
91   if (inf < 0)
92     {
93       perror("/dev/null");
94       exit(1);
95     }
96   pipe(rdpipe);
97   if (fork())
98     exit(0);
99   chdir("/");
100   close(0);
101   close(1);
102   close(2);
103   dup2(inf, 0);
104   dup2(inf, 1);
105   dup2(inf, 2);
106
107   setpgrp();
108   sprintf(buf, "%s/%s", BIN_DIR, PROG);
109
110   if ((pid = fork()) == 0)
111     {
112       dup2(inf, 0);
113       dup2(rdpipe[1], 1);
114       dup2(1, 2);
115       for (i = 3; i < nfds; i++)
116         close(i);
117       execl(buf, PROG, 0);
118       perror("cannot run dcm");
119       exit(1);
120     }
121   if (pid < 0)
122     {
123       perror("startdcm");
124       exit(1);
125     }
126
127   log = fdopen(logf, "w");
128   prog = fdopen(rdpipe[0], "r");
129
130   do
131     {
132       char *time_s;
133       long foo;
134
135       done = 0;
136       errno = 0;
137       if (!fgets(buf, BUFSIZ, prog))
138         {
139           if (errno && errno != EINTR)
140             {
141               strcpy(buf, "Unable to read from program: ");
142               strcat(buf, sys_errlist[errno]);
143               strcat(buf, "\n");
144             }
145           else
146             break;
147         }
148       time(&foo);
149       time_s = ctime(&foo) + 4;
150       time_s[strlen(time_s) - 6] = '\0';
151       fprintf(log, "%s <%d> %s", time_s, pid, buf);
152       fflush(log);
153     }
154   while (!done);
155   exit(0);
156 }
This page took 0.056884 seconds and 5 git commands to generate.