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