]>
Commit | Line | Data |
---|---|---|
0fa91a0a | 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>. | |
0fa91a0a | 9 | * |
d548a4e7 | 10 | * MOIRA server process. |
0fa91a0a | 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 | * | |
0fa91a0a | 17 | */ |
18 | ||
d548a4e7 | 19 | static char *rcsid_mr_main_c = "$Header$"; |
0fa91a0a | 20 | |
c801de4c | 21 | #include <mit-copyright.h> |
70d54e43 | 22 | #include <strings.h> |
589993be | 23 | #include <sys/types.h> |
70d54e43 | 24 | #include <sys/errno.h> |
70d54e43 | 25 | #include <sys/signal.h> |
554776b1 | 26 | #include <sys/wait.h> |
589993be | 27 | #include <sys/stat.h> |
d548a4e7 | 28 | #include "mr_server.h" |
40165bd0 | 29 | #include <krb_et.h> |
70d54e43 | 30 | |
0ec57336 | 31 | extern CONNECTION newconn, listencon; |
0fa91a0a | 32 | |
0ec57336 | 33 | extern int nclients; |
34 | extern client **clients, *cur_client; | |
0fa91a0a | 35 | |
0ec57336 | 36 | extern OPERATION listenop; |
37 | extern LIST_OF_OPERATIONS op_list; | |
0fa91a0a | 38 | |
0ec57336 | 39 | extern struct sockaddr_in client_addr; |
40 | extern int client_addrlen; | |
41 | extern TUPLE client_tuple; | |
0fa91a0a | 42 | |
0ec57336 | 43 | extern char *whoami; |
44 | extern char buf1[BUFSIZ]; | |
45 | extern char *takedown; | |
46 | extern int errno; | |
2f4ff9cd | 47 | extern FILE *journal; |
70d54e43 | 48 | |
0ec57336 | 49 | extern char *malloc(); |
f5c72293 | 50 | extern int free(); |
0ec57336 | 51 | extern char *inet_ntoa(); |
d548a4e7 | 52 | extern void mr_com_err(); |
0ec57336 | 53 | extern void do_client(); |
70d54e43 | 54 | |
0ec57336 | 55 | extern int sigshut(); |
56 | void clist_append(); | |
57 | void oplist_append(); | |
589993be | 58 | void reapchild(), godormant(), gowakeup(); |
0fa91a0a | 59 | |
0eb9f52f | 60 | extern time_t now; |
61 | ||
0ec57336 | 62 | /* |
d548a4e7 | 63 | * Main MOIRA server loop. |
0ec57336 | 64 | * |
65 | * Initialize the world, then start accepting connections and | |
66 | * making progress on current connections. | |
67 | */ | |
0fa91a0a | 68 | |
0ec57336 | 69 | /*ARGSUSED*/ |
70 | int | |
0fa91a0a | 71 | main(argc, argv) |
70d54e43 | 72 | int argc; |
73 | char **argv; | |
0fa91a0a | 74 | { |
0ec57336 | 75 | int status; |
0eb9f52f | 76 | time_t tardy; |
589993be | 77 | struct stat stbuf; |
0ec57336 | 78 | |
79 | whoami = argv[0]; | |
80 | /* | |
81 | * Error handler init. | |
82 | */ | |
40165bd0 | 83 | initialize_sms_error_table(); |
84 | initialize_krb_error_table(); | |
adae415b | 85 | initialize_gdss_error_table(); |
d548a4e7 | 86 | set_com_err_hook(mr_com_err); |
b4182127 | 87 | setlinebuf(stderr); |
88 | ||
0ec57336 | 89 | if (argc != 1) { |
d548a4e7 | 90 | com_err(whoami, 0, "Usage: moirad"); |
0ec57336 | 91 | exit(1); |
92 | } | |
f5c72293 | 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; | |
0ec57336 | 99 | |
100 | /* | |
101 | * GDB initialization. | |
102 | */ | |
5dbd09a0 | 103 | if(gdb_init() != 0) { |
104 | com_err(whoami, 0, "GDB initialization failed."); | |
105 | exit(1); | |
106 | } | |
7f024a70 | 107 | gdb_debug(0); /* this can be patched, if necessary, to enable */ |
108 | /* GDB level debugging .. */ | |
4a06eb54 | 109 | krb_realm = malloc(REALM_SZ); |
42b1d0ae | 110 | krb_get_lrealm(krb_realm, 1); |
7f024a70 | 111 | |
f6cc38de | 112 | /* |
083a6e94 | 113 | * Database initialization. Only init if database should be open. |
f6cc38de | 114 | */ |
115 | ||
d548a4e7 | 116 | if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) { |
117 | if ((status = mr_open_database()) != 0) { | |
2f4ff9cd | 118 | com_err(whoami, status, " when trying to open database."); |
f6cc38de | 119 | exit(1); |
083a6e94 | 120 | } |
121 | sanity_check_database(); | |
122 | } else { | |
123 | dormant = ASLEEP; | |
124 | com_err(whoami, 0, "sleeping, not opening database"); | |
f6cc38de | 125 | } |
126 | ||
2f4ff9cd | 127 | sanity_check_queries(); |
2f4ff9cd | 128 | |
0ec57336 | 129 | /* |
130 | * Set up client array handler. | |
131 | */ | |
0fa91a0a | 132 | nclients = 0; |
133 | clients = (client **) malloc(0); | |
70d54e43 | 134 | |
d548a4e7 | 135 | mr_setup_signals(); |
0ec57336 | 136 | |
2f4ff9cd | 137 | journal = fopen(JOURNAL, "a"); |
138 | if (journal == NULL) { | |
139 | com_err(whoami, errno, " while opening journal file"); | |
140 | exit(1); | |
141 | } | |
142 | ||
0ec57336 | 143 | /* |
144 | * Establish template connection. | |
145 | */ | |
146 | if ((status = do_listen()) != 0) { | |
147 | com_err(whoami, status, | |
2f4ff9cd | 148 | " while trying to create listening connection"); |
0ec57336 | 149 | exit(1); |
150 | } | |
0fa91a0a | 151 | |
0fa91a0a | 152 | op_list = create_list_of_operations(1, listenop); |
0fa91a0a | 153 | |
b4182127 | 154 | com_err(whoami, 0, "started (pid %d)", getpid()); |
d548a4e7 | 155 | com_err(whoami, 0, rcsid_mr_main_c); |
083a6e94 | 156 | if (dormant != ASLEEP) |
d548a4e7 | 157 | send_zgram("MOIRA", "server started"); |
083a6e94 | 158 | else |
d548a4e7 | 159 | send_zgram("MOIRA", "server started, but database closed"); |
0ec57336 | 160 | |
161 | /* | |
162 | * Run until shut down. | |
163 | */ | |
70d54e43 | 164 | while(!takedown) { |
0ec57336 | 165 | register int i; |
166 | /* | |
167 | * Block until something happens. | |
168 | */ | |
5dbd09a0 | 169 | #ifdef notdef |
170 | com_err(whoami, 0, "tick"); | |
171 | #endif notdef | |
589993be | 172 | if (dormant == SLEEPY) { |
d548a4e7 | 173 | mr_close_database(); |
589993be | 174 | com_err(whoami, 0, "database closed"); |
d548a4e7 | 175 | mr_setup_signals(); |
176 | send_zgram("MOIRA", "database closed"); | |
589993be | 177 | dormant = ASLEEP; |
178 | } else if (dormant == GROGGY) { | |
d548a4e7 | 179 | mr_open_database(); |
589993be | 180 | com_err(whoami, 0, "database open"); |
d548a4e7 | 181 | mr_setup_signals(); |
182 | send_zgram("MOIRA", "database open again"); | |
589993be | 183 | dormant = AWAKE; |
184 | } | |
185 | ||
5dbd09a0 | 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) { | |
42b1d0ae | 192 | if (errno != EINTR) |
193 | com_err(whoami, errno, " error from op_select"); | |
2e182bc3 | 194 | if (!inc_running || now - inc_started > INC_TIMEOUT) |
195 | next_incremental(); | |
5dbd09a0 | 196 | continue; |
197 | } else if (status != -2) { | |
2f4ff9cd | 198 | com_err(whoami, 0, " wrong return from op_select_any"); |
5dbd09a0 | 199 | continue; |
200 | } | |
70d54e43 | 201 | if (takedown) break; |
0eb9f52f | 202 | time(&now); |
2e182bc3 | 203 | if (!inc_running || now - inc_started > INC_TIMEOUT) |
204 | next_incremental(); | |
70d54e43 | 205 | #ifdef notdef |
206 | fprintf(stderr, " tick\n"); | |
207 | #endif notdef | |
b4182127 | 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 { | |
14e2d5ce | 217 | static int count = 0; |
b4182127 | 218 | com_err(whoami, errno, |
14e2d5ce | 219 | " error (%d) on listen", count); |
220 | if (count++ > 10) | |
221 | exit(1); | |
b4182127 | 222 | } |
223 | } else if ((status = new_connection()) != 0) { | |
224 | com_err(whoami, errno, | |
2f4ff9cd | 225 | " Error on listening operation."); |
b4182127 | 226 | /* |
227 | * Sleep here to prevent hosing? | |
228 | */ | |
229 | } | |
589993be | 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) && | |
d548a4e7 | 235 | (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) { |
589993be | 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) && | |
d548a4e7 | 243 | (stat(MOIRA_MOTD_FILE, &stbuf) == -1) && |
589993be | 244 | (errno == ENOENT)) { |
245 | com_err(whoami, 0, "motd file no longer exists, waking up"); | |
246 | dormant = GROGGY; | |
247 | } | |
248 | ||
b4182127 | 249 | } |
0ec57336 | 250 | /* |
251 | * Handle any existing connections. | |
252 | */ | |
0eb9f52f | 253 | tardy = now - 30*60; |
254 | ||
0fa91a0a | 255 | for (i=0; i<nclients; i++) { |
0eb9f52f | 256 | cur_client = clients[i]; |
0fa91a0a | 257 | if (OP_DONE(clients[i]->pending_op)) { |
0eb9f52f | 258 | cur_client->last_time_used = now; |
70d54e43 | 259 | do_client(cur_client); |
0eb9f52f | 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); | |
0fa91a0a | 263 | } |
0eb9f52f | 264 | cur_client = NULL; |
265 | if (takedown) break; | |
0fa91a0a | 266 | } |
0fa91a0a | 267 | } |
b4182127 | 268 | com_err(whoami, 0, "%s", takedown); |
d548a4e7 | 269 | mr_close_database(); |
270 | send_zgram("MOIRA", takedown); | |
0ec57336 | 271 | return 0; |
70d54e43 | 272 | } |
273 | ||
0ec57336 | 274 | /* |
275 | * Set up the template connection and queue the first accept. | |
276 | */ | |
277 | ||
278 | int | |
279 | do_listen() | |
70d54e43 | 280 | { |
d548a4e7 | 281 | char *service = index(MOIRA_SERVER, ':') + 1; |
0ec57336 | 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; | |
0fa91a0a | 295 | } |
296 | ||
b4182127 | 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 | ||
0ec57336 | 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 | |
5dbd09a0 | 312 | new_connection() |
0fa91a0a | 313 | { |
7f024a70 | 314 | register client *cp; |
70d54e43 | 315 | static counter = 0; |
0fa91a0a | 316 | |
0fa91a0a | 317 | /* |
318 | * Make sure there's been no error | |
319 | */ | |
c27b3454 | 320 | if(OP_STATUS(listenop) != OP_COMPLETE) { |
0ec57336 | 321 | return errno; |
c27b3454 | 322 | } |
323 | ||
324 | if (newconn == NULL) { | |
d548a4e7 | 325 | return MR_NOT_CONNECTED; |
0fa91a0a | 326 | } |
327 | ||
0fa91a0a | 328 | /* |
329 | * Set up the new connection and reply to the client | |
330 | */ | |
7f024a70 | 331 | cp = (client *)malloc(sizeof *cp); |
23e476e8 | 332 | bzero(cp, sizeof(*cp)); |
0fa91a0a | 333 | cp->action = CL_ACCEPT; |
334 | cp->con = newconn; | |
70d54e43 | 335 | cp->id = counter++; |
0ec57336 | 336 | cp->args = NULL; |
42b1d0ae | 337 | cp->clname[0] = NULL; |
d548a4e7 | 338 | cp->reply.mr_argv = NULL; |
4a06eb54 | 339 | cp->first = NULL; |
340 | cp->last = NULL; | |
0eb9f52f | 341 | cp->last_time_used = now; |
0fa91a0a | 342 | newconn = NULL; |
343 | ||
344 | cp->pending_op = create_operation(); | |
345 | reset_operation(cp->pending_op); | |
346 | oplist_append(&op_list, cp->pending_op); | |
70d54e43 | 347 | cur_client = cp; |
348 | ||
0ec57336 | 349 | /* |
350 | * Add a new client to the array.. | |
351 | */ | |
352 | clist_append(cp); | |
353 | ||
354 | /* | |
355 | * Let him know we heard him. | |
356 | */ | |
0fa91a0a | 357 | start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, |
358 | "", ""); | |
0ec57336 | 359 | |
360 | cp->haddr = client_addr; | |
361 | ||
362 | /* | |
363 | * Log new connection. | |
364 | */ | |
365 | ||
b4182127 | 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":""); | |
0fa91a0a | 371 | |
372 | /* | |
0ec57336 | 373 | * Get ready to accept the next connection. |
0fa91a0a | 374 | */ |
375 | reset_operation(listenop); | |
0ec57336 | 376 | client_addrlen = sizeof(client_addr); |
0fa91a0a | 377 | |
378 | start_accepting_client(listencon, listenop, &newconn, | |
0ec57336 | 379 | (char *)&client_addr, |
380 | &client_addrlen, &client_tuple); | |
381 | return 0; | |
0fa91a0a | 382 | } |
383 | ||
0fa91a0a | 384 | /* |
385 | * Add a new client to the known clients. | |
386 | */ | |
0ec57336 | 387 | void |
0fa91a0a | 388 | clist_append(cp) |
389 | client *cp; | |
390 | { | |
391 | client **clients_n; | |
392 | ||
393 | nclients++; | |
0ec57336 | 394 | clients_n = (client **)malloc |
395 | ((unsigned)(nclients * sizeof(client *))); | |
0fa91a0a | 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 | ||
70d54e43 | 403 | |
0ec57336 | 404 | void |
70d54e43 | 405 | clist_delete(cp) |
406 | client *cp; | |
407 | { | |
70d54e43 | 408 | client **clients_n, **scpp, **dcpp; /* source and dest client */ |
409 | /* ptr ptr */ | |
410 | ||
70d54e43 | 411 | int found_it = 0; |
412 | ||
0ec57336 | 413 | clients_n = (client **)malloc |
414 | ((unsigned)((nclients - 1)* sizeof(client *))); | |
70d54e43 | 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; | |
c27b3454 | 428 | oplist_delete(op_list, cp->pending_op); |
70d54e43 | 429 | reset_operation(cp->pending_op); |
430 | delete_operation(cp->pending_op); | |
7f024a70 | 431 | sever_connection(cp->con); |
0ec57336 | 432 | free((char *)cp); |
70d54e43 | 433 | } |
0ec57336 | 434 | |
0fa91a0a | 435 | /* |
0ec57336 | 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. | |
0fa91a0a | 440 | */ |
441 | ||
0ec57336 | 442 | void |
0fa91a0a | 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)); | |
0fa91a0a | 452 | newlist->count++; |
453 | newlist->op[count-1] = op; | |
467f5982 | 454 | db_free((char *)(*oplp), size_of_list_of_operations(count-1)); |
0fa91a0a | 455 | (*oplp) = newlist; |
456 | } | |
457 | ||
c27b3454 | 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 | } | |
554776b1 | 479 | |
480 | ||
481 | void reapchild() | |
482 | { | |
483 | union wait status; | |
484 | int pid; | |
485 | ||
486 | while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) { | |
2e182bc3 | 487 | if (pid == inc_pid) |
488 | inc_running = 0; | |
42b1d0ae | 489 | if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0)) |
589993be | 490 | com_err(whoami, 0, "%d: child exits with signal %d status %d", |
554776b1 | 491 | pid, status.w_termsig, status.w_retcode); |
492 | } | |
493 | } | |
589993be | 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 | } | |
083a6e94 | 528 | |
529 | ||
d548a4e7 | 530 | mr_setup_signals() |
083a6e94 | 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 | } |