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