source: trunk/third/moira/reg_svr/startreg.c @ 23178

Revision 23178, 2.7 KB checked in by broder, 16 years ago (diff)
Take a new snapshot from CVS for Moira, and add a debathena-moira-update-server package
Line 
1/* $Id$
2 *
3 * This program starts the user registration server
4 * in a "clean" environment, and then waits for it to exit.
5 *
6 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 */
10
11#include <mit-copyright.h>
12#include <moira.h>
13#include <moira_site.h>
14
15#include <sys/resource.h>
16#include <sys/wait.h>
17
18#include <errno.h>
19#include <fcntl.h>
20#include <signal.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24
25RCSID("$Header$");
26
27#define PROG    "reg_svr"
28
29int rdpipe[2];
30
31void cleanup(void);
32
33void cleanup(void)
34{
35  int stat, serrno = errno;
36  char buf[BUFSIZ];
37
38  buf[0] = '\0';
39
40  while (waitpid(-1, &stat, WNOHANG) > 0)
41    {
42      if (WIFEXITED(stat))
43        {
44          if (WEXITSTATUS(stat))
45            sprintf(buf, "exited with code %d\n", WEXITSTATUS(stat));
46        }
47      if (WIFSIGNALED(stat))
48        {
49          sprintf(buf, "exited on signal %d%s\n", WTERMSIG(stat),
50                  (WCOREDUMP(stat) ? "; Core dumped" : ""));
51        }
52      write(rdpipe[1], buf, strlen(buf));
53      close(rdpipe[1]);
54    }
55  errno = serrno;
56}
57
58int main(int argc, char **argv)
59{
60  char buf[BUFSIZ];
61  FILE *log, *prog;
62  int logf, inf, i, done, pid;
63  struct rlimit rl;
64
65  struct sigaction action;
66  int nfds;
67
68  getrlimit(RLIMIT_NOFILE, &rl);
69  nfds = rl.rlim_cur;
70
71  action.sa_handler = cleanup;
72  action.sa_flags = 0;
73  sigemptyset(&action.sa_mask);
74  sigaction(SIGCHLD, &action, NULL);
75
76  sprintf(buf, "%s/%s.log", MOIRA_DIR, PROG);
77  logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
78  if (logf < 0)
79    {
80      perror(buf);
81      exit(1);
82    }
83  inf = open("/dev/null", O_RDONLY , 0);
84  if (inf < 0)
85    {
86      perror("/dev/null");
87      exit(1);
88    }
89  pipe(rdpipe);
90  if (fork())
91    exit(0);
92  chdir("/");
93  close(0);
94  close(1);
95  close(2);
96  dup2(inf, 0);
97  dup2(inf, 1);
98  dup2(inf, 2);
99
100  setpgrp();
101  sprintf(buf, "%s/%s", BIN_DIR, PROG);
102
103  if ((pid = fork()) == 0)
104    {
105      dup2(inf, 0);
106      dup2(rdpipe[1], 1);
107      dup2(1,2);
108      for (i = 3; i < nfds; i++)
109        close(i);
110      execl(buf, PROG, 0);
111      perror("cannot run reg_svr");
112      exit(1);
113    }
114  if (pid < 0)
115    {
116      perror("startreg");
117      exit(1);
118    }
119
120  log = fdopen(logf, "w");
121  prog = fdopen(rdpipe[0], "r");
122
123  do
124    {
125      char *time_s;
126      long foo;
127
128      done = 0;
129      errno = 0;
130      if (!fgets(buf, BUFSIZ, prog))
131        {
132          if (errno && errno != EINTR)
133            {
134              strcpy(buf, "Unable to read from program: ");
135              strcat(buf, strerror(errno));
136              strcat(buf, "\n");
137            }
138          else
139            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    }
147  while (!done);
148  exit(0);
149}
Note: See TracBrowser for help on using the repository browser.