]> andersk Git - moira.git/blame - reg_svr/startreg.c
Solaris/POSIX changes
[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>
27#include <sys/stat.h>
28#include <sys/resource.h>
2ce085d2 29#include <moira_site.h>
a3c170f4 30
e6eb3546 31#define PROG "reg_svr"
a3c170f4 32
33int rdpipe[2];
a3c170f4 34
35cleanup()
36{
15fa49b5 37 int stat;
a3c170f4 38 char buf[BUFSIZ];
39 extern int errno;
40 int serrno = errno;
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),
54 (WCOREDUMP(stat)?"; Core dumped":0));
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;
130 extern char *ctime();
131 long foo;
132
133 done = 0;
134 errno = 0;
135 if (fgets(buf, BUFSIZ, prog) == NULL) {
136 if (errno) {
137 strcpy(buf, "Unable to read from program: ");
138 strcat(buf, sys_errlist[errno]);
139 strcat(buf, "\n");
140 } else break;
141 }
142 time(&foo);
143 time_s = ctime(&foo)+4;
144 time_s[strlen(time_s)-6]='\0';
145 fprintf(log, "%s <%d> %s", time_s, pid, buf);
146 fflush(log);
147 } while (!done);
148 exit(0);
149}
150
151
152
153
This page took 0.104909 seconds and 5 git commands to generate.