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