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