]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
9 | * | |
10 | * MOIRA server process. | |
11 | * | |
12 | * Most of this is stolen from ../gdb/tsr.c | |
13 | * | |
14 | * You are in a maze of twisty little finite automata, all different. | |
15 | * Let the reader beware. | |
16 | * | |
17 | */ | |
18 | ||
19 | static char *rcsid_mr_main_c = "$Header$"; | |
20 | ||
21 | #include <mit-copyright.h> | |
22 | #include <strings.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/errno.h> | |
25 | #include <sys/signal.h> | |
26 | #include <sys/wait.h> | |
27 | #include <sys/stat.h> | |
28 | #include "mr_server.h" | |
29 | #include <krb_et.h> | |
30 | ||
31 | extern CONNECTION newconn, listencon; | |
32 | ||
33 | extern int nclients; | |
34 | extern client **clients, *cur_client; | |
35 | ||
36 | extern OPERATION listenop; | |
37 | extern LIST_OF_OPERATIONS op_list; | |
38 | ||
39 | extern struct sockaddr_in client_addr; | |
40 | extern int client_addrlen; | |
41 | extern TUPLE client_tuple; | |
42 | ||
43 | extern char *whoami; | |
44 | extern char buf1[BUFSIZ]; | |
45 | extern char *takedown; | |
46 | extern int errno; | |
47 | extern FILE *journal; | |
48 | ||
49 | extern char *malloc(); | |
50 | extern int free(); | |
51 | extern char *inet_ntoa(); | |
52 | extern void mr_com_err(); | |
53 | extern void do_client(); | |
54 | ||
55 | extern int sigshut(); | |
56 | void clist_append(); | |
57 | void oplist_append(); | |
58 | void reapchild(), godormant(), gowakeup(); | |
59 | ||
60 | extern time_t now; | |
61 | ||
62 | /* | |
63 | * Main MOIRA server loop. | |
64 | * | |
65 | * Initialize the world, then start accepting connections and | |
66 | * making progress on current connections. | |
67 | */ | |
68 | ||
69 | /*ARGSUSED*/ | |
70 | int | |
71 | main(argc, argv) | |
72 | int argc; | |
73 | char **argv; | |
74 | { | |
75 | int status; | |
76 | time_t tardy; | |
77 | struct stat stbuf; | |
78 | ||
79 | whoami = argv[0]; | |
80 | /* | |
81 | * Error handler init. | |
82 | */ | |
83 | initialize_sms_error_table(); | |
84 | initialize_krb_error_table(); | |
85 | initialize_gdss_error_table(); | |
86 | set_com_err_hook(mr_com_err); | |
87 | setlinebuf(stderr); | |
88 | ||
89 | if (argc != 1) { | |
90 | com_err(whoami, 0, "Usage: moirad"); | |
91 | exit(1); | |
92 | } | |
93 | ||
94 | /* Profiling implies that getting rid of one level of call | |
95 | * indirection here wins us maybe 1% on the VAX. | |
96 | */ | |
97 | gdb_amv = malloc; | |
98 | gdb_fmv = free; | |
99 | ||
100 | /* | |
101 | * GDB initialization. | |
102 | */ | |
103 | if(gdb_init() != 0) { | |
104 | com_err(whoami, 0, "GDB initialization failed."); | |
105 | exit(1); | |
106 | } | |
107 | gdb_debug(0); /* this can be patched, if necessary, to enable */ | |
108 | /* GDB level debugging .. */ | |
109 | krb_realm = malloc(REALM_SZ); | |
110 | krb_get_lrealm(krb_realm, 1); | |
111 | ||
112 | /* | |
113 | * Database initialization. Only init if database should be open. | |
114 | */ | |
115 | ||
116 | if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) { | |
117 | if ((status = mr_open_database()) != 0) { | |
118 | com_err(whoami, status, " when trying to open database."); | |
119 | exit(1); | |
120 | } | |
121 | sanity_check_database(); | |
122 | } else { | |
123 | dormant = ASLEEP; | |
124 | com_err(whoami, 0, "sleeping, not opening database"); | |
125 | } | |
126 | ||
127 | sanity_check_queries(); | |
128 | ||
129 | /* | |
130 | * Set up client array handler. | |
131 | */ | |
132 | nclients = 0; | |
133 | clients = (client **) malloc(0); | |
134 | ||
135 | mr_setup_signals(); | |
136 | ||
137 | journal = fopen(JOURNAL, "a"); | |
138 | if (journal == NULL) { | |
139 | com_err(whoami, errno, " while opening journal file"); | |
140 | exit(1); | |
141 | } | |
142 | ||
143 | /* | |
144 | * Establish template connection. | |
145 | */ | |
146 | if ((status = do_listen()) != 0) { | |
147 | com_err(whoami, status, | |
148 | " while trying to create listening connection"); | |
149 | exit(1); | |
150 | } | |
151 | ||
152 | op_list = create_list_of_operations(1, listenop); | |
153 | ||
154 | com_err(whoami, 0, "started (pid %d)", getpid()); | |
155 | com_err(whoami, 0, rcsid_mr_main_c); | |
156 | if (dormant != ASLEEP) | |
157 | send_zgram("MOIRA", "server started"); | |
158 | else | |
159 | send_zgram("MOIRA", "server started, but database closed"); | |
160 | ||
161 | /* | |
162 | * Run until shut down. | |
163 | */ | |
164 | while(!takedown) { | |
165 | register int i; | |
166 | /* | |
167 | * Block until something happens. | |
168 | */ | |
169 | #ifdef notdef | |
170 | com_err(whoami, 0, "tick"); | |
171 | #endif notdef | |
172 | if (dormant == SLEEPY) { | |
173 | mr_close_database(); | |
174 | com_err(whoami, 0, "database closed"); | |
175 | mr_setup_signals(); | |
176 | send_zgram("MOIRA", "database closed"); | |
177 | dormant = ASLEEP; | |
178 | } else if (dormant == GROGGY) { | |
179 | mr_open_database(); | |
180 | com_err(whoami, 0, "database open"); | |
181 | mr_setup_signals(); | |
182 | send_zgram("MOIRA", "database open again"); | |
183 | dormant = AWAKE; | |
184 | } | |
185 | ||
186 | errno = 0; | |
187 | status = op_select_any(op_list, 0, | |
188 | (fd_set *)NULL, (fd_set *)NULL, | |
189 | (fd_set *)NULL, (struct timeval *)NULL); | |
190 | ||
191 | if (status == -1) { | |
192 | if (errno != EINTR) | |
193 | com_err(whoami, errno, " error from op_select"); | |
194 | if (!inc_running || now - inc_started > INC_TIMEOUT) | |
195 | next_incremental(); | |
196 | continue; | |
197 | } else if (status != -2) { | |
198 | com_err(whoami, 0, " wrong return from op_select_any"); | |
199 | continue; | |
200 | } | |
201 | if (takedown) break; | |
202 | time(&now); | |
203 | if (!inc_running || now - inc_started > INC_TIMEOUT) | |
204 | next_incremental(); | |
205 | #ifdef notdef | |
206 | fprintf(stderr, " tick\n"); | |
207 | #endif notdef | |
208 | /* | |
209 | * Handle any new connections; this comes first so | |
210 | * errno isn't tromped on. | |
211 | */ | |
212 | if (OP_DONE(listenop)) { | |
213 | if (OP_STATUS(listenop) == OP_CANCELLED) { | |
214 | if (errno == EWOULDBLOCK) { | |
215 | do_reset_listen(); | |
216 | } else { | |
217 | static int count = 0; | |
218 | com_err(whoami, errno, | |
219 | " error (%d) on listen", count); | |
220 | if (count++ > 10) | |
221 | exit(1); | |
222 | } | |
223 | } else if ((status = new_connection()) != 0) { | |
224 | com_err(whoami, errno, | |
225 | " Error on listening operation."); | |
226 | /* | |
227 | * Sleep here to prevent hosing? | |
228 | */ | |
229 | } | |
230 | /* if the new connection is our only connection, | |
231 | * and the server is supposed to be down, then go | |
232 | * down now. | |
233 | */ | |
234 | if ((dormant == AWAKE) && (nclients == 1) && | |
235 | (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) { | |
236 | com_err(whoami, 0, "motd file exists, slumbertime"); | |
237 | dormant = SLEEPY; | |
238 | } | |
239 | /* on new connection, if we are no longer supposed | |
240 | * to be down, then wake up. | |
241 | */ | |
242 | if ((dormant == ASLEEP) && | |
243 | (stat(MOIRA_MOTD_FILE, &stbuf) == -1) && | |
244 | (errno == ENOENT)) { | |
245 | com_err(whoami, 0, "motd file no longer exists, waking up"); | |
246 | dormant = GROGGY; | |
247 | } | |
248 | ||
249 | } | |
250 | /* | |
251 | * Handle any existing connections. | |
252 | */ | |
253 | tardy = now - 30*60; | |
254 | ||
255 | for (i=0; i<nclients; i++) { | |
256 | cur_client = clients[i]; | |
257 | if (OP_DONE(clients[i]->pending_op)) { | |
258 | cur_client->last_time_used = now; | |
259 | do_client(cur_client); | |
260 | } else if (clients[i]->last_time_used < tardy) { | |
261 | com_err(whoami, 0, "Shutting down connection due to inactivity"); | |
262 | shutdown(cur_client->con->in.fd, 0); | |
263 | } | |
264 | cur_client = NULL; | |
265 | if (takedown) break; | |
266 | } | |
267 | } | |
268 | com_err(whoami, 0, "%s", takedown); | |
269 | mr_close_database(); | |
270 | send_zgram("MOIRA", takedown); | |
271 | return 0; | |
272 | } | |
273 | ||
274 | /* | |
275 | * Set up the template connection and queue the first accept. | |
276 | */ | |
277 | ||
278 | int | |
279 | do_listen() | |
280 | { | |
281 | char *service = index(MOIRA_SERVER, ':') + 1; | |
282 | ||
283 | listencon = create_listening_connection(service); | |
284 | ||
285 | if (listencon == NULL) | |
286 | return errno; | |
287 | ||
288 | listenop = create_operation(); | |
289 | client_addrlen = sizeof(client_addr); | |
290 | ||
291 | start_accepting_client(listencon, listenop, &newconn, | |
292 | (char *)&client_addr, | |
293 | &client_addrlen, &client_tuple); | |
294 | return 0; | |
295 | } | |
296 | ||
297 | ||
298 | do_reset_listen() | |
299 | { | |
300 | client_addrlen = sizeof(client_addr); | |
301 | start_accepting_client(listencon, listenop, &newconn, | |
302 | (char *)&client_addr, | |
303 | &client_addrlen, &client_tuple); | |
304 | } | |
305 | ||
306 | /* | |
307 | * This routine is called when a new connection comes in. | |
308 | * | |
309 | * It sets up a new client and adds it to the list of currently active clients. | |
310 | */ | |
311 | int | |
312 | new_connection() | |
313 | { | |
314 | register client *cp; | |
315 | static counter = 0; | |
316 | ||
317 | /* | |
318 | * Make sure there's been no error | |
319 | */ | |
320 | if(OP_STATUS(listenop) != OP_COMPLETE) { | |
321 | return errno; | |
322 | } | |
323 | ||
324 | if (newconn == NULL) { | |
325 | return MR_NOT_CONNECTED; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Set up the new connection and reply to the client | |
330 | */ | |
331 | cp = (client *)malloc(sizeof *cp); | |
332 | bzero(cp, sizeof(*cp)); | |
333 | cp->action = CL_ACCEPT; | |
334 | cp->con = newconn; | |
335 | cp->id = counter++; | |
336 | cp->args = NULL; | |
337 | cp->clname[0] = NULL; | |
338 | cp->reply.mr_argv = NULL; | |
339 | cp->first = NULL; | |
340 | cp->last = NULL; | |
341 | cp->last_time_used = now; | |
342 | newconn = NULL; | |
343 | ||
344 | cp->pending_op = create_operation(); | |
345 | reset_operation(cp->pending_op); | |
346 | oplist_append(&op_list, cp->pending_op); | |
347 | cur_client = cp; | |
348 | ||
349 | /* | |
350 | * Add a new client to the array.. | |
351 | */ | |
352 | clist_append(cp); | |
353 | ||
354 | /* | |
355 | * Let him know we heard him. | |
356 | */ | |
357 | start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, | |
358 | "", ""); | |
359 | ||
360 | cp->haddr = client_addr; | |
361 | ||
362 | /* | |
363 | * Log new connection. | |
364 | */ | |
365 | ||
366 | com_err(whoami, 0, "New connection from %s port %d (now %d client%s)", | |
367 | inet_ntoa(cp->haddr.sin_addr), | |
368 | (int)ntohs(cp->haddr.sin_port), | |
369 | nclients, | |
370 | nclients!=1?"s":""); | |
371 | ||
372 | /* | |
373 | * Get ready to accept the next connection. | |
374 | */ | |
375 | reset_operation(listenop); | |
376 | client_addrlen = sizeof(client_addr); | |
377 | ||
378 | start_accepting_client(listencon, listenop, &newconn, | |
379 | (char *)&client_addr, | |
380 | &client_addrlen, &client_tuple); | |
381 | return 0; | |
382 | } | |
383 | ||
384 | /* | |
385 | * Add a new client to the known clients. | |
386 | */ | |
387 | void | |
388 | clist_append(cp) | |
389 | client *cp; | |
390 | { | |
391 | client **clients_n; | |
392 | ||
393 | nclients++; | |
394 | clients_n = (client **)malloc | |
395 | ((unsigned)(nclients * sizeof(client *))); | |
396 | bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp)); | |
397 | clients_n[nclients-1] = cp; | |
398 | free((char *)clients); | |
399 | clients = clients_n; | |
400 | clients_n = NULL; | |
401 | } | |
402 | ||
403 | ||
404 | void | |
405 | clist_delete(cp) | |
406 | client *cp; | |
407 | { | |
408 | client **clients_n, **scpp, **dcpp; /* source and dest client */ | |
409 | /* ptr ptr */ | |
410 | ||
411 | int found_it = 0; | |
412 | ||
413 | clients_n = (client **)malloc | |
414 | ((unsigned)((nclients - 1)* sizeof(client *))); | |
415 | for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) { | |
416 | if (*scpp != cp) { | |
417 | *dcpp++ = *scpp++; | |
418 | } else { | |
419 | scpp++; | |
420 | if (found_it) abort(); | |
421 | found_it = 1; | |
422 | } | |
423 | } | |
424 | --nclients; | |
425 | free((char *)clients); | |
426 | clients = clients_n; | |
427 | clients_n = NULL; | |
428 | oplist_delete(op_list, cp->pending_op); | |
429 | reset_operation(cp->pending_op); | |
430 | delete_operation(cp->pending_op); | |
431 | sever_connection(cp->con); | |
432 | free((char *)cp); | |
433 | } | |
434 | ||
435 | /* | |
436 | * Add a new operation to a list of operations. | |
437 | * | |
438 | * This should be rewritten to use realloc instead, since in most | |
439 | * cases it won't have to copy the array. | |
440 | */ | |
441 | ||
442 | void | |
443 | oplist_append(oplp, op) | |
444 | LIST_OF_OPERATIONS *oplp; | |
445 | OPERATION op; | |
446 | { | |
447 | int count = (*oplp)->count+1; | |
448 | LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS) | |
449 | db_alloc(size_of_list_of_operations(count)); | |
450 | bcopy((char *)(*oplp), (char *)newlist, | |
451 | size_of_list_of_operations((*oplp)->count)); | |
452 | newlist->count++; | |
453 | newlist->op[count-1] = op; | |
454 | db_free((char *)(*oplp), size_of_list_of_operations(count-1)); | |
455 | (*oplp) = newlist; | |
456 | } | |
457 | ||
458 | ||
459 | oplist_delete(oplp, op) | |
460 | LIST_OF_OPERATIONS oplp; | |
461 | register OPERATION op; | |
462 | { | |
463 | register OPERATION *s; | |
464 | register int c; | |
465 | ||
466 | for (s = oplp->op, c=oplp->count; c; --c, ++s) { | |
467 | if (*s == op) { | |
468 | while (c > 0) { | |
469 | *s = *(s+1); | |
470 | ++s; | |
471 | --c; | |
472 | } | |
473 | oplp->count--; | |
474 | return; | |
475 | } | |
476 | } | |
477 | abort(); | |
478 | } | |
479 | ||
480 | ||
481 | void reapchild() | |
482 | { | |
483 | union wait status; | |
484 | int pid; | |
485 | ||
486 | while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) { | |
487 | if (pid == inc_pid) | |
488 | inc_running = 0; | |
489 | if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0)) | |
490 | com_err(whoami, 0, "%d: child exits with signal %d status %d", | |
491 | pid, status.w_termsig, status.w_retcode); | |
492 | } | |
493 | } | |
494 | ||
495 | ||
496 | void godormant() | |
497 | { | |
498 | switch (dormant) { | |
499 | case AWAKE: | |
500 | case GROGGY: | |
501 | com_err(whoami, 0, "requested to go dormant"); | |
502 | break; | |
503 | case ASLEEP: | |
504 | com_err(whoami, 0, "already asleep"); | |
505 | break; | |
506 | case SLEEPY: | |
507 | break; | |
508 | } | |
509 | dormant = SLEEPY; | |
510 | } | |
511 | ||
512 | ||
513 | void gowakeup() | |
514 | { | |
515 | switch (dormant) { | |
516 | case ASLEEP: | |
517 | case SLEEPY: | |
518 | com_err(whoami, 0, "Good morning"); | |
519 | break; | |
520 | case AWAKE: | |
521 | com_err(whoami, 0, "already awake"); | |
522 | break; | |
523 | case GROGGY: | |
524 | break; | |
525 | } | |
526 | dormant = GROGGY; | |
527 | } | |
528 | ||
529 | ||
530 | mr_setup_signals() | |
531 | { | |
532 | /* There should probably be a few more of these. */ | |
533 | ||
534 | if ((((int)signal (SIGTERM, sigshut)) < 0) || | |
535 | (((int)signal (SIGCHLD, reapchild)) < 0) || | |
536 | (((int)signal (SIGUSR1, godormant)) < 0) || | |
537 | (((int)signal (SIGUSR2, gowakeup)) < 0) || | |
538 | (((int)signal (SIGHUP, sigshut)) < 0)) { | |
539 | com_err(whoami, errno, " Unable to establish signal handlers."); | |
540 | exit(1); | |
541 | } | |
542 | } |