]> andersk Git - moira.git/blame - dcm/startdcm.c
Change `SMS' to `Moira' where possible.
[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
2ce085d2 15static char *rcsid_mr_starter_c = "$Header$";
4297fecb 16#endif lint
17
0a5ff702 18#include <mit-copyright.h>
b91fbc27 19#include <errno.h>
4297fecb 20#include <stdio.h>
b91fbc27 21#include <string.h>
4297fecb 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>
15fa49b5 27#include <fcntl.h>
28#include <sys/resource.h>
2ce085d2 29#include <moira_site.h>
b91fbc27 30#include <time.h>
4297fecb 31
e6eb3546 32#define PROG "dcm"
4297fecb 33
34int rdpipe[2];
15fa49b5 35extern int errno;
4297fecb 36
5eaef520 37void cleanup(void)
4297fecb 38{
5eaef520 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" : ""));
4297fecb 55 }
5eaef520 56 write(rdpipe[1], buf, strlen(buf));
57 close(rdpipe[1]);
58 }
59 errno = serrno;
4297fecb 60}
61
5eaef520 62int main(int argc, char *argv)
4297fecb 63{
5eaef520 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[];
4297fecb 71
5eaef520 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
59ec8dae 83 sprintf(buf, "%s/%s.log", MOIRA_DIR, PROG);
5eaef520 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);
4297fecb 156}
This page took 5.025418 seconds and 5 git commands to generate.