]> andersk Git - moira.git/blob - reg_svr/startreg.c
9cd4e21c1240315febf1986fdb0cd2e9c5e04140
[moira.git] / reg_svr / startreg.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 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 user registration server
11  *      in a "clean" environment, 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 <moira_site.h>
27
28 #define PROG    "reg_svr"
29
30 int rdpipe[2];
31 extern char *sys_siglist[];
32
33 cleanup()
34 {
35         union wait stat;
36         char buf[BUFSIZ];
37         extern int errno;
38         int serrno = errno;
39
40         buf[0]='\0';
41         
42         while (wait3(&stat, WNOHANG, 0) > 0) {
43                 if (WIFEXITED(stat)) {
44                         if (stat.w_retcode)
45                                 sprintf(buf,
46                                         "exited with code %d\n",
47                                         stat.w_retcode);
48                 }
49                 if (WIFSIGNALED(stat)) {
50                         sprintf(buf, "exited on %s signal%s\n",
51                                 sys_siglist[stat.w_termsig],
52                                 (stat.w_coredump?"; 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         
66         extern int errno;
67         extern char *sys_errlist[];
68         
69         int nfds = getdtablesize();
70         
71         setreuid(0);
72         signal(SIGCHLD, cleanup);
73         
74         sprintf(buf, "%s/%s.log", SMS_DIR, PROG);
75         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
76         if (logf<0) {
77                 perror(buf);
78                 exit(1);
79         }
80         inf = open("/dev/null", O_RDONLY , 0);
81         if (inf < 0) {
82                 perror("/dev/null");
83                 exit(1);
84         }
85         pipe(rdpipe);
86         if (fork()) {
87                 exit(0);
88         }
89         chdir("/");     
90         close(0);
91         close(1);
92         close(2);
93         dup2(inf, 0);
94         dup2(inf, 1);
95         dup2(inf, 2);
96         
97         tty = open("/dev/tty");
98         ioctl(tty, TIOCNOTTY, 0);
99         close(tty);
100         sprintf(buf, "%s/%s", BIN_DIR, PROG);
101         
102         if ((pid = fork()) == 0) {
103                 
104                 dup2(inf, 0);
105                 dup2(rdpipe[1], 1);
106                 dup2(1,2);
107                 for (i = 3; i <nfds; i++) close(i);
108                 execl(buf, PROG, 0);
109                 perror("cannot run reg_svr");
110                 exit(1);
111         }
112         if (pid<0) {
113                 perror("startreg");
114                 exit(1);
115         }
116
117         log = fdopen(logf, "w");
118         prog = fdopen(rdpipe[0], "r");
119         
120         
121         do {
122                 char *time_s;
123                 extern char *ctime();
124                 long foo;
125                 
126                 done = 0;
127                 errno = 0;
128                 if (fgets(buf, BUFSIZ, prog) == NULL) {
129                         if (errno) {
130                                 strcpy(buf, "Unable to read from program: ");
131                                 strcat(buf, sys_errlist[errno]);
132                                 strcat(buf, "\n");
133                         } else break;
134                 }
135                 time(&foo);
136                 time_s = ctime(&foo)+4;
137                 time_s[strlen(time_s)-6]='\0';
138                 fprintf(log, "%s <%d> %s", time_s, pid, buf);
139                 fflush(log);
140         } while (!done);
141         exit(0);
142 }
143
144
145
146           
This page took 0.058606 seconds and 3 git commands to generate.