]> andersk Git - moira.git/blame - reg_svr/startreg.c
s/-S/-db/
[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>
15fa49b5 26#include <fcntl.h>
15fa49b5 27#include <sys/resource.h>
2ce085d2 28#include <moira_site.h>
a3c170f4 29
e6eb3546 30#define PROG "reg_svr"
a3c170f4 31
32int rdpipe[2];
c666a149 33extern int errno;
a3c170f4 34
35cleanup()
36{
c666a149 37 int stat, serrno = errno;
a3c170f4 38 char buf[BUFSIZ];
a3c170f4 39
40 buf[0]='\0';
41
15fa49b5 42 while (waitpid(-1, &stat, WNOHANG) > 0) {
a3c170f4 43 if (WIFEXITED(stat)) {
15fa49b5 44 if (WEXITSTATUS(stat))
a3c170f4 45 sprintf(buf,
46 "exited with code %d\n",
15fa49b5 47 WEXITSTATUS(stat));
a3c170f4 48 }
49 if (WIFSIGNALED(stat)) {
15fa49b5 50 sprintf(buf, "exited on signal %d%s\n",
51 WTERMSIG(stat),
52 (WCOREDUMP(stat)?"; Core dumped":0));
a3c170f4 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;
15fa49b5 65 struct rlimit rl;
a3c170f4 66
67 extern int errno;
68 extern char *sys_errlist[];
69
15fa49b5 70 struct sigaction action;
71 int nfds;
a3c170f4 72
15fa49b5 73 getrlimit(RLIMIT_NOFILE, &rl);
74 nfds = rl.rlim_cur;
75
76 action.sa_handler = cleanup;
77 action.sa_flags = 0;
78 sigemptyset(&action.sa_mask);
79 sigaction(SIGCHLD, &action, NULL);
a3c170f4 80
e6eb3546 81 sprintf(buf, "%s/%s.log", SMS_DIR, PROG);
82 logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
a3c170f4 83 if (logf<0) {
e6eb3546 84 perror(buf);
a3c170f4 85 exit(1);
86 }
87 inf = open("/dev/null", O_RDONLY , 0);
88 if (inf < 0) {
89 perror("/dev/null");
90 exit(1);
91 }
92 pipe(rdpipe);
93 if (fork()) {
262ea7f3 94 exit(0);
a3c170f4 95 }
96 chdir("/");
97 close(0);
98 close(1);
99 close(2);
100 dup2(inf, 0);
101 dup2(inf, 1);
102 dup2(inf, 2);
103
15fa49b5 104 setpgrp();
e6eb3546 105 sprintf(buf, "%s/%s", BIN_DIR, PROG);
a3c170f4 106
107 if ((pid = fork()) == 0) {
108
109 dup2(inf, 0);
110 dup2(rdpipe[1], 1);
111 dup2(1,2);
112 for (i = 3; i <nfds; i++) close(i);
e6eb3546 113 execl(buf, PROG, 0);
a3c170f4 114 perror("cannot run reg_svr");
115 exit(1);
116 }
117 if (pid<0) {
118 perror("startreg");
119 exit(1);
120 }
121
122 log = fdopen(logf, "w");
123 prog = fdopen(rdpipe[0], "r");
124
125
126 do {
127 char *time_s;
128 extern char *ctime();
129 long foo;
130
131 done = 0;
132 errno = 0;
133 if (fgets(buf, BUFSIZ, prog) == NULL) {
c666a149 134 if (errno && errno!=EINTR) {
a3c170f4 135 strcpy(buf, "Unable to read from program: ");
136 strcat(buf, sys_errlist[errno]);
137 strcat(buf, "\n");
138 } else break;
139 }
140 time(&foo);
141 time_s = ctime(&foo)+4;
142 time_s[strlen(time_s)-6]='\0';
143 fprintf(log, "%s <%d> %s", time_s, pid, buf);
144 fflush(log);
145 } while (!done);
146 exit(0);
147}
This page took 0.138934 seconds and 5 git commands to generate.