]> andersk Git - moira.git/blame - reg_svr/startreg.c
encrypt ID if necessary to use as encryption key
[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
2ce085d2 15static char *rcsid_mr_starter_c = "$Header$";
a3c170f4 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>
2ce085d2 26#include <moira_site.h>
a3c170f4 27
e6eb3546 28#define PROG "reg_svr"
a3c170f4 29
30int rdpipe[2];
262ea7f3 31extern char *sys_siglist[];
a3c170f4 32
33cleanup()
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",
262ea7f3 51 sys_siglist[stat.w_termsig],
a3c170f4 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
60main(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
e6eb3546 74 sprintf(buf, "%s/%s.log", SMS_DIR, PROG);
75 logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
a3c170f4 76 if (logf<0) {
e6eb3546 77 perror(buf);
a3c170f4 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()) {
262ea7f3 87 exit(0);
a3c170f4 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);
e6eb3546 100 sprintf(buf, "%s/%s", BIN_DIR, PROG);
a3c170f4 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);
e6eb3546 108 execl(buf, PROG, 0);
a3c170f4 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 1.489446 seconds and 5 git commands to generate.