]>
Commit | Line | Data |
---|---|---|
3c46fcae | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
c801de4c | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
3c46fcae | 9 | * |
2ce085d2 | 10 | * This program starts the moira server in a "clean" environment. |
3c46fcae | 11 | * and then waits for it to exit. |
12 | * | |
3c46fcae | 13 | */ |
14 | ||
15 | #ifndef lint | |
a8db6c01 | 16 | static char *rcsid_mr_starter_c = "$Header$"; |
3c46fcae | 17 | #endif lint |
18 | ||
c801de4c | 19 | #include <mit-copyright.h> |
3c46fcae | 20 | #include <stdio.h> |
95f3bdce | 21 | #include <strings.h> |
3c46fcae | 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> | |
2ce085d2 | 27 | #include <moira_site.h> |
3c46fcae | 28 | |
2ce085d2 | 29 | #define PROG "moirad" |
3c46fcae | 30 | |
31 | int rdpipe[2]; | |
f41d9bba | 32 | extern char *sys_siglist[]; |
3c46fcae | 33 | |
34 | cleanup() | |
35 | { | |
36 | union wait stat; | |
37 | char buf[BUFSIZ]; | |
38 | extern int errno; | |
39 | int serrno = errno; | |
40 | ||
41 | buf[0]='\0'; | |
42 | ||
43 | while (wait3(&stat, WNOHANG, 0) > 0) { | |
44 | if (WIFEXITED(stat)) { | |
45 | if (stat.w_retcode) | |
46 | sprintf(buf, | |
47 | "exited with code %d\n", | |
48 | stat.w_retcode); | |
49 | } | |
50 | if (WIFSIGNALED(stat)) { | |
51 | sprintf(buf, "exited on %s signal%s\n", | |
f41d9bba | 52 | sys_siglist[stat.w_termsig], |
3c46fcae | 53 | (stat.w_coredump?"; Core dumped":0)); |
54 | } | |
55 | write(rdpipe[1], buf, strlen(buf)); | |
56 | close(rdpipe[1]); | |
57 | } | |
58 | errno = serrno; | |
59 | } | |
60 | ||
61 | main(argc, argv) | |
62 | { | |
63 | char buf[BUFSIZ]; | |
64 | FILE *log, *prog; | |
65 | int logf, inf, i, done, pid, tty; | |
66 | ||
67 | extern int errno; | |
68 | extern char *sys_errlist[]; | |
69 | ||
70 | int nfds = getdtablesize(); | |
71 | ||
72 | setreuid(0); | |
73 | signal(SIGCHLD, cleanup); | |
74 | ||
2ce085d2 | 75 | sprintf(buf, "%s/moira.log", SMS_DIR); |
e6eb3546 | 76 | logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640); |
3c46fcae | 77 | if (logf<0) { |
e6eb3546 | 78 | perror(buf); |
3c46fcae | 79 | exit(1); |
80 | } | |
81 | inf = open("/dev/null", O_RDONLY , 0); | |
82 | if (inf < 0) { | |
83 | perror("/dev/null"); | |
84 | exit(1); | |
85 | } | |
86 | pipe(rdpipe); | |
87 | if (fork()) { | |
f41d9bba | 88 | exit(0); |
3c46fcae | 89 | } |
90 | chdir("/"); | |
91 | close(0); | |
92 | close(1); | |
93 | close(2); | |
94 | dup2(inf, 0); | |
95 | dup2(inf, 1); | |
96 | dup2(inf, 2); | |
97 | ||
98 | tty = open("/dev/tty"); | |
99 | ioctl(tty, TIOCNOTTY, 0); | |
100 | close(tty); | |
e6eb3546 | 101 | sprintf(buf, "%s/%s", BIN_DIR, PROG); |
3c46fcae | 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++) close(i); | |
e6eb3546 | 109 | execl(buf, PROG, 0); |
2ce085d2 | 110 | perror("cannot run moirad"); |
3c46fcae | 111 | exit(1); |
112 | } | |
113 | if (pid<0) { | |
2ce085d2 | 114 | perror("moira_starter"); |
3c46fcae | 115 | exit(1); |
116 | } | |
117 | ||
118 | log = fdopen(logf, "w"); | |
119 | prog = fdopen(rdpipe[0], "r"); | |
120 | ||
121 | ||
122 | do { | |
123 | char *time_s; | |
124 | extern char *ctime(); | |
125 | long foo; | |
126 | ||
127 | done = 0; | |
128 | errno = 0; | |
129 | if (fgets(buf, BUFSIZ, prog) == NULL) { | |
130 | if (errno) { | |
131 | strcpy(buf, "Unable to read from program: "); | |
132 | strcat(buf, sys_errlist[errno]); | |
133 | strcat(buf, "\n"); | |
134 | } else break; | |
135 | } | |
136 | time(&foo); | |
137 | time_s = ctime(&foo)+4; | |
138 | time_s[strlen(time_s)-6]='\0'; | |
f41d9bba | 139 | fprintf(log, "%s %s", time_s, buf); |
3c46fcae | 140 | fflush(log); |
141 | } while (!done); | |
95f3bdce | 142 | exit(0); |
3c46fcae | 143 | } |
144 | ||
145 | ||
146 | ||
147 |