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 *malloc();
51 extern char *inet_ntoa();
52 extern void mr_com_err();
53 extern void do_client();
58 void reapchild(), godormant(), gowakeup();
63 * Main MOIRA server loop.
65 * Initialize the world, then start accepting connections and
66 * making progress on current connections.
83 initialize_sms_error_table();
84 initialize_krb_error_table();
85 initialize_gdss_error_table();
86 set_com_err_hook(mr_com_err);
90 com_err(whoami, 0, "Usage: moirad");
94 /* Profiling implies that getting rid of one level of call
95 * indirection here wins us maybe 1% on the VAX.
101 * GDB initialization.
103 if(gdb_init() != 0) {
104 com_err(whoami, 0, "GDB initialization failed.");
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);
113 * Database initialization. Only init if database should be open.
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.");
121 sanity_check_database();
124 com_err(whoami, 0, "sleeping, not opening database");
127 sanity_check_queries();
130 * Set up client array handler.
133 clients = (client **) malloc(0);
137 journal = fopen(JOURNAL, "a");
138 if (journal == NULL) {
139 com_err(whoami, errno, " while opening journal file");
144 * Establish template connection.
146 if ((status = do_listen()) != 0) {
147 com_err(whoami, status,
148 " while trying to create listening connection");
152 op_list = create_list_of_operations(1, listenop);
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");
159 send_zgram("MOIRA", "server started, but database closed");
162 * Run until shut down.
167 * Block until something happens.
170 com_err(whoami, 0, "tick");
172 if (dormant == SLEEPY) {
174 com_err(whoami, 0, "database closed");
176 send_zgram("MOIRA", "database closed");
178 } else if (dormant == GROGGY) {
180 com_err(whoami, 0, "database open");
182 send_zgram("MOIRA", "database open again");
187 status = op_select_any(op_list, 0,
188 (fd_set *)NULL, (fd_set *)NULL,
189 (fd_set *)NULL, (struct timeval *)NULL);
193 com_err(whoami, errno, " error from op_select");
194 if (!inc_running || now - inc_started > INC_TIMEOUT)
197 } else if (status != -2) {
198 com_err(whoami, 0, " wrong return from op_select_any");
203 if (!inc_running || now - inc_started > INC_TIMEOUT)
206 fprintf(stderr, " tick\n");
209 * Handle any new connections; this comes first so
210 * errno isn't tromped on.
212 if (OP_DONE(listenop)) {
213 if (OP_STATUS(listenop) == OP_CANCELLED) {
214 if (errno == EWOULDBLOCK) {
217 static int count = 0;
218 com_err(whoami, errno,
219 " error (%d) on listen", count);
223 } else if ((status = new_connection()) != 0) {
224 com_err(whoami, errno,
225 " Error on listening operation.");
227 * Sleep here to prevent hosing?
230 /* if the new connection is our only connection,
231 * and the server is supposed to be down, then go
234 if ((dormant == AWAKE) && (nclients == 1) &&
235 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
236 com_err(whoami, 0, "motd file exists, slumbertime");
239 /* on new connection, if we are no longer supposed
240 * to be down, then wake up.
242 if ((dormant == ASLEEP) &&
243 (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
245 com_err(whoami, 0, "motd file no longer exists, waking up");
251 * Handle any existing connections.
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);
268 com_err(whoami, 0, "%s", takedown);
270 send_zgram("MOIRA", takedown);
275 * Set up the template connection and queue the first accept.
281 char *service = index(MOIRA_SERVER, ':') + 1;
283 listencon = create_listening_connection(service);
285 if (listencon == NULL)
288 listenop = create_operation();
289 client_addrlen = sizeof(client_addr);
291 start_accepting_client(listencon, listenop, &newconn,
292 (char *)&client_addr,
293 &client_addrlen, &client_tuple);
300 client_addrlen = sizeof(client_addr);
301 start_accepting_client(listencon, listenop, &newconn,
302 (char *)&client_addr,
303 &client_addrlen, &client_tuple);
307 * This routine is called when a new connection comes in.
309 * It sets up a new client and adds it to the list of currently active clients.
318 * Make sure there's been no error
320 if(OP_STATUS(listenop) != OP_COMPLETE) {
324 if (newconn == NULL) {
325 return MR_NOT_CONNECTED;
329 * Set up the new connection and reply to the client
331 cp = (client *)malloc(sizeof *cp);
332 bzero(cp, sizeof(*cp));
333 cp->action = CL_ACCEPT;
337 cp->clname[0] = NULL;
338 cp->reply.mr_argv = NULL;
341 cp->last_time_used = now;
344 cp->pending_op = create_operation();
345 reset_operation(cp->pending_op);
346 oplist_append(&op_list, cp->pending_op);
350 * Add a new client to the array..
355 * Let him know we heard him.
357 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
360 cp->haddr = client_addr;
363 * Log new connection.
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),
373 * Get ready to accept the next connection.
375 reset_operation(listenop);
376 client_addrlen = sizeof(client_addr);
378 start_accepting_client(listencon, listenop, &newconn,
379 (char *)&client_addr,
380 &client_addrlen, &client_tuple);
385 * Add a new client to the known clients.
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);
408 client **clients_n, **scpp, **dcpp; /* source and dest client */
413 clients_n = (client **)malloc
414 ((unsigned)((nclients - 1)* sizeof(client *)));
415 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
420 if (found_it) abort();
425 free((char *)clients);
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);
436 * Add a new operation to a list of operations.
438 * This should be rewritten to use realloc instead, since in most
439 * cases it won't have to copy the array.
443 oplist_append(oplp, op)
444 LIST_OF_OPERATIONS *oplp;
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));
453 newlist->op[count-1] = op;
454 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
459 oplist_delete(oplp, op)
460 LIST_OF_OPERATIONS oplp;
461 register OPERATION op;
463 register OPERATION *s;
466 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
486 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 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);
501 com_err(whoami, 0, "requested to go dormant");
504 com_err(whoami, 0, "already asleep");
518 com_err(whoami, 0, "Good morning");
521 com_err(whoami, 0, "already awake");
532 /* There should probably be a few more of these. */
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.");