]> andersk Git - moira.git/blame - dcm/startdcm.c
Command line printer manipulation client, and build goo.
[moira.git] / dcm / startdcm.c
CommitLineData
7ac48069 1/* $Id$
4297fecb 2 *
7ac48069 3 * This program starts the DCM in a "clean" environment.
4 * and then waits for it to exit.
4297fecb 5 *
7ac48069 6 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
4297fecb 9 */
10
0a5ff702 11#include <mit-copyright.h>
7ac48069 12#include <moira.h>
13#include <moira_site.h>
14
15#include <sys/resource.h>
16#include <sys/wait.h>
17
b91fbc27 18#include <errno.h>
7ac48069 19#include <fcntl.h>
20#include <signal.h>
4297fecb 21#include <stdio.h>
b91fbc27 22#include <string.h>
7ac48069 23#include <unistd.h>
24
25RCSID("$Header$");
4297fecb 26
e6eb3546 27#define PROG "dcm"
4297fecb 28
29int rdpipe[2];
7ac48069 30
31void cleanup(void);
4297fecb 32
5eaef520 33void cleanup(void)
4297fecb 34{
5eaef520 35 int stat, serrno = errno;
36 char buf[BUFSIZ];
37
38 buf[0] = '\0';
39
40 while (waitpid(-1, &stat, WNOHANG) > 0)
41 {
42 if (WIFEXITED(stat))
43 {
44 if (WEXITSTATUS(stat))
45 sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
46 }
47 if (WIFSIGNALED(stat))
48 {
49 sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
50 (WCOREDUMP(stat) ? "; Core dumped" : ""));
4297fecb 51 }
5eaef520 52 write(rdpipe[1], buf, strlen(buf));
53 close(rdpipe[1]);
54 }
55 errno = serrno;
4297fecb 56}
57
5eaef520 58int main(int argc, char *argv)
4297fecb 59{
5eaef520 60 char buf[BUFSIZ];
61 FILE *log, *prog;
7ac48069 62 int logf, inf, i, done, pid;
5eaef520 63 struct rlimit rl;
64
5eaef520 65 struct sigaction action;
66 int nfds;
67
68 getrlimit(RLIMIT_NOFILE, &rl);
69 nfds = rl.rlim_cur;
70
71 action.sa_handler = cleanup;
72 action.sa_flags = 0;
73 sigemptyset(&action.sa_mask);
74 sigaction(SIGCHLD, &action, NULL);
75
59ec8dae 76 sprintf(buf, "%s/%s.log", MOIRA_DIR, PROG);
5eaef520 77 logf = open(buf, O_CREAT | O_WRONLY | O_APPEND, 0640);
78 if (logf < 0)
79 {
80 perror(buf);
81 exit(1);
82 }
83 inf = open("/dev/null", O_RDONLY , 0);
84 if (inf < 0)
85 {
86 perror("/dev/null");
87 exit(1);
88 }
89 pipe(rdpipe);
90 if (fork())
91 exit(0);
92 chdir("/");
93 close(0);
94 close(1);
95 close(2);
96 dup2(inf, 0);
97 dup2(inf, 1);
98 dup2(inf, 2);
99
100 setpgrp();
101 sprintf(buf, "%s/%s", BIN_DIR, PROG);
102
103 if ((pid = fork()) == 0)
104 {
105 dup2(inf, 0);
106 dup2(rdpipe[1], 1);
107 dup2(1, 2);
108 for (i = 3; i < nfds; i++)
109 close(i);
110 execl(buf, PROG, 0);
111 perror("cannot run dcm");
112 exit(1);
113 }
114 if (pid < 0)
115 {
116 perror("startdcm");
117 exit(1);
118 }
119
120 log = fdopen(logf, "w");
121 prog = fdopen(rdpipe[0], "r");
122
123 do
124 {
125 char *time_s;
126 long foo;
127
128 done = 0;
129 errno = 0;
130 if (!fgets(buf, BUFSIZ, prog))
131 {
132 if (errno && errno != EINTR)
133 {
134 strcpy(buf, "Unable to read from program: ");
7ac48069 135 strcat(buf, strerror(errno));
5eaef520 136 strcat(buf, "\n");
137 }
138 else
139 break;
140 }
141 time(&foo);
142 time_s = ctime(&foo) + 4;
143 time_s[strlen(time_s) - 6] = '\0';
144 fprintf(log, "%s <%d> %s", time_s, pid, buf);
145 fflush(log);
146 }
147 while (!done);
148 exit(0);
4297fecb 149}
This page took 0.75972 seconds and 5 git commands to generate.