]>
Commit | Line | Data |
---|---|---|
3c46fcae | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | * This program starts the sms server in a "clean" environment. | |
9 | * and then waits for it to exit. | |
10 | * | |
11 | * $Log$ | |
7c40fb8c | 12 | * Revision 1.3 1987-08-22 17:30:58 wesommer |
13 | * Changed path used to start server. | |
3c46fcae | 14 | * |
7c40fb8c | 15 | * Revision 1.2 87/06/02 20:08:16 wesommer |
16 | * Changed logging, location of daemon to run. | |
17 | * | |
95f3bdce | 18 | * Revision 1.1 87/06/01 03:35:33 wesommer |
19 | * Initial revision | |
20 | * | |
3c46fcae | 21 | */ |
22 | ||
23 | #ifndef lint | |
24 | static char *rcsid_sms_starter_c = "$Header$"; | |
25 | #endif lint | |
26 | ||
27 | #include <stdio.h> | |
95f3bdce | 28 | #include <strings.h> |
3c46fcae | 29 | #include <sys/types.h> |
30 | #include <sys/file.h> | |
31 | #include <sys/wait.h> | |
32 | #include <sys/signal.h> | |
33 | #include <sys/ioctl.h> | |
34 | ||
35 | #define SMS_LOG_FILE "/u1/sms/sms.log" | |
36 | ||
7c40fb8c | 37 | #define SMS_PROG "/u1/sms/bin/smsd" |
3c46fcae | 38 | |
39 | int rdpipe[2]; | |
40 | char *sigdescr[] = { | |
41 | 0, | |
42 | "hangup", | |
43 | "interrupt", | |
44 | "quit", | |
45 | "illegal instruction", | |
46 | "trace/BPT trap", | |
47 | "IOT trap", | |
48 | "EMT trap", | |
49 | "floating exception", | |
50 | "kill", | |
51 | "bus error", | |
52 | "segmentation violation", | |
53 | "bad system call", | |
54 | "broken pipe", | |
55 | "alarm clock", | |
56 | "termination", | |
57 | "urgent I/O condition", | |
58 | "stopped", | |
59 | "stopped", | |
60 | "continued", | |
61 | "child exited", | |
62 | "stopped (tty input)", | |
63 | "stopped (tty output)", | |
64 | "I/O possible", | |
65 | "cputime limit exceeded", | |
66 | "filesize limit exceeded", | |
67 | "virtual timer expired", | |
68 | "profiling timer expired", | |
69 | "window size changed", | |
70 | "signal 29", | |
71 | "user defined signal 1", | |
72 | "user defined signal 2", | |
73 | "signal 32" | |
74 | }; | |
75 | ||
76 | cleanup() | |
77 | { | |
78 | union wait stat; | |
79 | char buf[BUFSIZ]; | |
80 | extern int errno; | |
81 | int serrno = errno; | |
82 | ||
83 | buf[0]='\0'; | |
84 | ||
85 | while (wait3(&stat, WNOHANG, 0) > 0) { | |
86 | if (WIFEXITED(stat)) { | |
87 | if (stat.w_retcode) | |
88 | sprintf(buf, | |
89 | "exited with code %d\n", | |
90 | stat.w_retcode); | |
91 | } | |
92 | if (WIFSIGNALED(stat)) { | |
93 | sprintf(buf, "exited on %s signal%s\n", | |
94 | sigdescr[stat.w_termsig], | |
95 | (stat.w_coredump?"; Core dumped":0)); | |
96 | } | |
97 | write(rdpipe[1], buf, strlen(buf)); | |
98 | close(rdpipe[1]); | |
99 | } | |
100 | errno = serrno; | |
101 | } | |
102 | ||
103 | main(argc, argv) | |
104 | { | |
105 | char buf[BUFSIZ]; | |
106 | FILE *log, *prog; | |
107 | int logf, inf, i, done, pid, tty; | |
108 | ||
109 | extern int errno; | |
110 | extern char *sys_errlist[]; | |
111 | ||
112 | int nfds = getdtablesize(); | |
113 | ||
114 | setreuid(0); | |
115 | signal(SIGCHLD, cleanup); | |
116 | ||
117 | logf = open(SMS_LOG_FILE, O_CREAT|O_WRONLY|O_APPEND, 0640); | |
118 | if (logf<0) { | |
119 | perror(SMS_LOG_FILE); | |
120 | exit(1); | |
121 | } | |
122 | inf = open("/dev/null", O_RDONLY , 0); | |
123 | if (inf < 0) { | |
124 | perror("/dev/null"); | |
125 | exit(1); | |
126 | } | |
127 | pipe(rdpipe); | |
128 | if (fork()) { | |
129 | exit(); | |
130 | } | |
131 | chdir("/"); | |
132 | close(0); | |
133 | close(1); | |
134 | close(2); | |
135 | dup2(inf, 0); | |
136 | dup2(inf, 1); | |
137 | dup2(inf, 2); | |
138 | ||
139 | tty = open("/dev/tty"); | |
140 | ioctl(tty, TIOCNOTTY, 0); | |
141 | close(tty); | |
142 | ||
143 | if ((pid = fork()) == 0) { | |
144 | ||
145 | dup2(inf, 0); | |
146 | dup2(rdpipe[1], 1); | |
147 | dup2(1,2); | |
148 | for (i = 3; i <nfds; i++) close(i); | |
149 | execl(SMS_PROG, "smsd", 0); | |
150 | perror("cannot run smsd"); | |
151 | exit(1); | |
152 | } | |
153 | if (pid<0) { | |
154 | perror("sms_starter"); | |
155 | exit(1); | |
156 | } | |
157 | ||
158 | log = fdopen(logf, "w"); | |
159 | prog = fdopen(rdpipe[0], "r"); | |
160 | ||
161 | ||
162 | do { | |
163 | char *time_s; | |
164 | extern char *ctime(); | |
165 | long foo; | |
166 | ||
167 | done = 0; | |
168 | errno = 0; | |
169 | if (fgets(buf, BUFSIZ, prog) == NULL) { | |
170 | if (errno) { | |
171 | strcpy(buf, "Unable to read from program: "); | |
172 | strcat(buf, sys_errlist[errno]); | |
173 | strcat(buf, "\n"); | |
174 | } else break; | |
175 | } | |
176 | time(&foo); | |
177 | time_s = ctime(&foo)+4; | |
178 | time_s[strlen(time_s)-6]='\0'; | |
95f3bdce | 179 | fprintf(log, "%s <%d> %s", time_s, pid, buf); |
3c46fcae | 180 | fflush(log); |
181 | } while (!done); | |
95f3bdce | 182 | exit(0); |
3c46fcae | 183 | } |
184 | ||
185 | ||
186 | ||
187 |