6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
10 * MOIRA server process.
12 * Most of this is stolen from ../gdb/tsr.c
14 * You are in a maze of twisty little finite automata, all different.
15 * Let the reader beware.
19 static char *rcsid_mr_main_c = "$Header$";
21 #include <mit-copyright.h>
23 #include <sys/types.h>
24 #include <sys/errno.h>
25 #include <sys/signal.h>
28 #include "mr_server.h"
31 extern CONNECTION newconn, listencon;
34 extern client **clients, *cur_client;
36 extern OPERATION listenop;
37 extern LIST_OF_OPERATIONS op_list;
39 extern struct sockaddr_in client_addr;
40 extern int client_addrlen;
41 extern TUPLE client_tuple;
44 extern char buf1[BUFSIZ];
45 extern char *takedown;
49 extern char *inet_ntoa();
50 extern void mr_com_err();
51 extern void do_client();
56 void reapchild(), godormant(), gowakeup();
60 #ifdef _DEBUG_MALLOC_INC
61 static char *dbg_malloc();
62 static int dbg_free();
66 * Main MOIRA server loop.
68 * Initialize the world, then start accepting connections and
69 * making progress on current connections.
81 extern char *database;
88 initialize_sms_error_table();
89 initialize_krb_error_table();
90 initialize_gdss_error_table();
91 set_com_err_hook(mr_com_err);
95 port = index(MOIRA_SERVER, ':') + 1;
97 for (i = 1; i < argc; i++) {
98 if (!strcmp(argv[i], "-db") && i+1 < argc) {
101 } else if (!strcmp(argv[i], "-p") && i+1 < argc) {
105 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
110 /* Profiling implies that getting rid of one level of call
111 * indirection here wins us maybe 1% on the VAX.
113 #ifdef _DEBUG_MALLOC_INC
114 gdb_amv = dbg_malloc;
118 gdb_fmv = (int (*)()) free;
122 * GDB initialization.
124 if(gdb_init() != 0) {
125 com_err(whoami, 0, "GDB initialization failed.");
128 gdb_debug(0); /* this can be patched, if necessary, to enable */
129 /* GDB level debugging .. */
130 krb_realm = malloc(REALM_SZ);
131 krb_get_lrealm(krb_realm, 1);
134 * Database initialization. Only init if database should be open.
137 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) {
138 if ((status = mr_open_database()) != 0) {
139 com_err(whoami, status, " when trying to open database.");
142 sanity_check_database();
145 com_err(whoami, 0, "sleeping, not opening database");
148 sanity_check_queries();
151 * Set up client array handler.
154 clients = (client **) malloc(0);
158 journal = fopen(JOURNAL, "a");
159 if (journal == NULL) {
160 com_err(whoami, errno, " while opening journal file");
165 * Establish template connection.
167 if ((status = do_listen(port)) != 0) {
168 com_err(whoami, status,
169 " while trying to create listening connection");
173 op_list = create_list_of_operations(1, listenop);
175 com_err(whoami, 0, "started (pid %d)", getpid());
176 com_err(whoami, 0, rcsid_mr_main_c);
177 if (dormant != ASLEEP)
178 send_zgram("MOIRA", "server started");
180 send_zgram("MOIRA", "server started, but database closed");
183 * Run until shut down.
188 * Block until something happens.
191 com_err(whoami, 0, "tick");
193 if (dormant == SLEEPY) {
195 com_err(whoami, 0, "database closed");
197 send_zgram("MOIRA", "database closed");
199 } else if (dormant == GROGGY) {
201 com_err(whoami, 0, "database open");
203 send_zgram("MOIRA", "database open again");
208 status = op_select_any(op_list, 0,
209 (fd_set *)NULL, (fd_set *)NULL,
210 (fd_set *)NULL, (struct timeval *)NULL);
214 com_err(whoami, errno, " error from op_select");
215 if (!inc_running || now - inc_started > INC_TIMEOUT)
218 } else if (status != -2) {
219 com_err(whoami, 0, " wrong return from op_select_any");
224 if (!inc_running || now - inc_started > INC_TIMEOUT)
227 fprintf(stderr, " tick\n");
230 * Handle any new connections; this comes first so
231 * errno isn't tromped on.
233 if (OP_DONE(listenop)) {
234 if (OP_STATUS(listenop) == OP_CANCELLED) {
235 if (errno == EWOULDBLOCK) {
238 static int count = 0;
239 com_err(whoami, errno,
240 " error (%d) on listen", count);
244 } else if ((status = new_connection()) != 0) {
245 com_err(whoami, errno,
246 " Error on listening operation.");
248 * Sleep here to prevent hosing?
251 /* if the new connection is our only connection,
252 * and the server is supposed to be down, then go
255 if ((dormant == AWAKE) && (nclients == 1) &&
256 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
257 com_err(whoami, 0, "motd file exists, slumbertime");
260 /* on new connection, if we are no longer supposed
261 * to be down, then wake up.
263 if ((dormant == ASLEEP) &&
264 (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
266 com_err(whoami, 0, "motd file no longer exists, waking up");
272 * Handle any existing connections.
276 for (i=0; i<nclients; i++) {
277 cur_client = clients[i];
278 if (OP_DONE(clients[i]->pending_op)) {
279 cur_client->last_time_used = now;
280 do_client(cur_client);
281 } else if (clients[i]->last_time_used < tardy) {
282 com_err(whoami, 0, "Shutting down connection due to inactivity");
283 shutdown(cur_client->con->in.fd, 0);
289 com_err(whoami, 0, "%s", takedown);
291 send_zgram("MOIRA", takedown);
296 * Set up the template connection and queue the first accept.
303 listencon = create_listening_connection(port);
305 if (listencon == NULL)
308 listenop = create_operation();
309 client_addrlen = sizeof(client_addr);
311 start_accepting_client(listencon, listenop, &newconn,
312 (char *)&client_addr,
313 &client_addrlen, &client_tuple);
320 client_addrlen = sizeof(client_addr);
321 start_accepting_client(listencon, listenop, &newconn,
322 (char *)&client_addr,
323 &client_addrlen, &client_tuple);
327 * This routine is called when a new connection comes in.
329 * It sets up a new client and adds it to the list of currently active clients.
338 * Make sure there's been no error
340 if(OP_STATUS(listenop) != OP_COMPLETE) {
344 if (newconn == NULL) {
345 return MR_NOT_CONNECTED;
349 * Set up the new connection and reply to the client
351 cp = (client *)malloc(sizeof *cp);
352 bzero(cp, sizeof(*cp));
353 cp->action = CL_ACCEPT;
357 cp->clname[0] = NULL;
358 cp->reply.mr_argv = NULL;
361 cp->last_time_used = now;
364 cp->pending_op = create_operation();
365 reset_operation(cp->pending_op);
366 oplist_append(&op_list, cp->pending_op);
370 * Add a new client to the array..
375 * Let him know we heard him.
377 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
380 cp->haddr = client_addr;
383 * Log new connection.
386 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
387 inet_ntoa(cp->haddr.sin_addr),
388 (int)ntohs(cp->haddr.sin_port),
393 * Get ready to accept the next connection.
395 reset_operation(listenop);
396 client_addrlen = sizeof(client_addr);
398 start_accepting_client(listencon, listenop, &newconn,
399 (char *)&client_addr,
400 &client_addrlen, &client_tuple);
405 * Add a new client to the known clients.
414 clients_n = (client **)malloc
415 ((unsigned)(nclients * sizeof(client *)));
416 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
417 clients_n[nclients-1] = cp;
418 free((char *)clients);
428 client **clients_n, **scpp, **dcpp; /* source and dest client */
433 clients_n = (client **)malloc
434 ((unsigned)((nclients - 1)* sizeof(client *)));
435 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
440 if (found_it) abort();
445 free((char *)clients);
448 oplist_delete(op_list, cp->pending_op);
449 reset_operation(cp->pending_op);
450 delete_operation(cp->pending_op);
451 sever_connection(cp->con);
456 * Add a new operation to a list of operations.
458 * This should be rewritten to use realloc instead, since in most
459 * cases it won't have to copy the array.
463 oplist_append(oplp, op)
464 LIST_OF_OPERATIONS *oplp;
467 int count = (*oplp)->count+1;
468 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
469 db_alloc(size_of_list_of_operations(count));
470 bcopy((char *)(*oplp), (char *)newlist,
471 size_of_list_of_operations((*oplp)->count));
473 newlist->op[count-1] = op;
474 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
479 oplist_delete(oplp, op)
480 LIST_OF_OPERATIONS oplp;
481 register OPERATION op;
483 register OPERATION *s;
486 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
506 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
509 if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0))
510 com_err(whoami, 0, "%d: child exits with signal %d status %d",
511 pid, status.w_termsig, status.w_retcode);
521 com_err(whoami, 0, "requested to go dormant");
524 com_err(whoami, 0, "already asleep");
538 com_err(whoami, 0, "Good morning");
541 com_err(whoami, 0, "already awake");
552 /* There should probably be a few more of these. */
554 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
555 (((int)signal (SIGCHLD, reapchild)) < 0) ||
556 (((int)signal (SIGUSR1, godormant)) < 0) ||
557 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
558 (((int)signal (SIGHUP, sigshut)) < 0)) {
559 com_err(whoami, errno, " Unable to establish signal handlers.");
564 #ifdef _DEBUG_MALLOC_INC
565 static char *dbg_malloc(size)
568 return( debug_malloc("somewhere in the gdb code",1,size) );
571 static int dbg_free(cptr)
574 debug_free((char *)NULL, 0, cptr);
575 return 0; /* GDB is being stupid */