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>
24 #include <sys/types.h>
25 #include <sys/errno.h>
26 #include <sys/signal.h>
31 #include "mr_server.h"
34 #include <arpa/inet.h>
36 extern CONNECTION newconn, listencon;
39 extern client **clients, *cur_client;
41 extern OPERATION listenop;
42 extern LIST_OF_OPERATIONS op_list;
44 extern struct sockaddr_in client_addr;
45 extern int client_addrlen;
46 extern TUPLE client_tuple;
49 extern char buf1[BUFSIZ];
50 extern char *takedown;
56 char hostbuf[BUFSIZ], *host;
58 int do_listen(char *port);
59 void do_reset_listen(void);
60 void clist_append(client *cp);
61 void oplist_append(LIST_OF_OPERATIONS *oplp, OPERATION op);
62 void oplist_delete(LIST_OF_OPERATIONS oplp, OPERATION op);
63 void mr_setup_signals(void);
64 int new_connection(void);
67 * Main Moira server loop.
69 * Initialize the world, then start accepting connections and
70 * making progress on current connections.
73 int main(int argc, char **argv)
78 extern char *database;
85 initialize_sms_error_table();
86 initialize_krb_error_table();
87 initialize_gdss_error_table();
88 set_com_err_hook(mr_com_err);
89 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
91 port = strchr(MOIRA_SERVER, ':') + 1;
93 for (i = 1; i < argc; i++)
95 if (!strcmp(argv[i], "-db") && i + 1 < argc)
97 database = argv[i + 1];
100 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
107 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
113 * GDB initialization.
117 com_err(whoami, 0, "GDB initialization failed.");
120 gdb_debug(0); /* this can be patched, if necessary, to enable */
121 /* GDB level debugging .. */
122 krb_realm = malloc(REALM_SZ);
123 krb_get_lrealm(krb_realm, 1);
126 * Database initialization. Only init if database should be open.
129 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
131 if ((status = mr_open_database()))
133 com_err(whoami, status, " when trying to open database.");
136 sanity_check_database();
141 com_err(whoami, 0, "sleeping, not opening database");
144 sanity_check_queries();
147 * Get moira server hostname for authentication
149 if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
151 com_err(whoami, errno, "Unable to get local hostname");
154 host = canonicalize_hostname(strsave(hostbuf));
155 for (p = host; *p && *p != '.'; p++)
163 * Set up client array handler.
170 journal = fopen(JOURNAL, "a");
173 com_err(whoami, errno, " while opening journal file");
178 * Establish template connection.
180 if ((status = do_listen(port)))
182 com_err(whoami, status, " while trying to create listening connection");
186 op_list = create_list_of_operations(1, listenop);
188 com_err(whoami, 0, "started (pid %d)", getpid());
189 com_err(whoami, 0, rcsid_mr_main_c);
190 if (dormant != ASLEEP)
191 send_zgram("MOIRA", "server started");
193 send_zgram("MOIRA", "server started, but database closed");
196 * Run until shut down.
202 * Block until something happens.
204 if (dormant == SLEEPY)
207 com_err(whoami, 0, "database closed");
209 send_zgram("MOIRA", "database closed");
212 else if (dormant == GROGGY)
215 com_err(whoami, 0, "database open");
217 send_zgram("MOIRA", "database open again");
222 status = op_select_any(op_list, 0, NULL, NULL, NULL, NULL);
226 com_err(whoami, errno, " error from op_select");
227 if (!inc_running || now - inc_started > INC_TIMEOUT)
231 else if (status != -2)
233 com_err(whoami, 0, " wrong return from op_select_any");
239 if (!inc_running || now - inc_started > INC_TIMEOUT)
243 * Handle any new connections; this comes first so
244 * errno isn't tromped on.
246 if (OP_DONE(listenop))
248 if (OP_STATUS(listenop) == OP_CANCELLED)
250 if (errno == EWOULDBLOCK)
254 static int count = 0;
255 com_err(whoami, errno, " error (%d) on listen", count);
260 else if ((status = new_connection()))
262 com_err(whoami, errno, " Error on listening operation.");
264 * Sleep here to prevent hosing?
267 /* if the new connection is our only connection,
268 * and the server is supposed to be down, then go
271 if ((dormant == AWAKE) && (nclients == 1) &&
272 (stat(MOIRA_MOTD_FILE, &stbuf) == 0))
274 com_err(whoami, 0, "motd file exists, slumbertime");
277 /* on new connection, if we are no longer supposed
278 * to be down, then wake up.
280 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
283 com_err(whoami, 0, "motd file no longer exists, waking up");
288 * Handle any existing connections.
290 tardy = now - 30 * 60;
292 for (i = 0; i < nclients; i++)
294 cur_client = clients[i];
295 if (OP_DONE(clients[i]->pending_op))
297 cur_client->last_time_used = now;
298 do_client(cur_client);
300 else if (clients[i]->last_time_used < tardy)
302 com_err(whoami, 0, "Shutting down connection due to inactivity");
303 shutdown(cur_client->con->in.fd, 2);
310 com_err(whoami, 0, "%s", takedown);
311 if (dormant != ASLEEP)
313 send_zgram("MOIRA", takedown);
318 * Set up the template connection and queue the first accept.
321 int do_listen(char *port)
323 listencon = create_listening_connection(port);
328 listenop = create_operation();
329 client_addrlen = sizeof(client_addr);
331 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
332 &client_addrlen, &client_tuple);
337 void do_reset_listen(void)
339 client_addrlen = sizeof(client_addr);
340 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
341 &client_addrlen, &client_tuple);
345 * This routine is called when a new connection comes in.
347 * It sets up a new client and adds it to the list of currently active clients.
349 int new_connection(void)
355 * Make sure there's been no error
357 if (OP_STATUS(listenop) != OP_COMPLETE)
361 return MR_NOT_CONNECTED;
364 * Set up the new connection and reply to the client
366 cp = malloc(sizeof(client));
367 memset(cp, 0, sizeof(*cp));
368 cp->action = CL_ACCEPT;
372 cp->clname[0] = NULL;
373 cp->reply.mr_argv = NULL;
376 cp->last_time_used = now;
379 cp->pending_op = create_operation();
380 reset_operation(cp->pending_op);
381 oplist_append(&op_list, cp->pending_op);
385 * Add a new client to the array..
390 * Let him know we heard him.
392 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, "", "");
394 cp->haddr = client_addr;
397 * Log new connection.
399 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
400 inet_ntoa(cp->haddr.sin_addr), (int)ntohs(cp->haddr.sin_port),
401 nclients, nclients != 1 ? "s" : "");
404 * Get ready to accept the next connection.
406 reset_operation(listenop);
407 client_addrlen = sizeof(client_addr);
409 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
410 &client_addrlen, &client_tuple);
415 * Add a new client to the known clients.
417 void clist_append(client *cp)
422 clients_n = malloc(nclients * sizeof(client *));
423 memcpy(clients_n, clients, (nclients - 1) * sizeof(cp));
424 clients_n[nclients - 1] = cp;
431 void clist_delete(client *cp)
433 client **clients_n, **scpp, **dcpp; /* source and dest client ptr ptr */
437 clients_n = malloc((nclients - 1) * sizeof(client *));
438 for (scpp = clients, dcpp = clients_n; scpp < clients + nclients; )
454 oplist_delete(op_list, cp->pending_op);
455 reset_operation(cp->pending_op);
456 delete_operation(cp->pending_op);
457 sever_connection(cp->con);
462 * Add a new operation to a list of operations.
464 * This should be rewritten to use realloc instead, since in most
465 * cases it won't have to copy the array.
468 void oplist_append(LIST_OF_OPERATIONS *oplp, OPERATION op)
470 int count = (*oplp)->count + 1;
471 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
472 db_alloc(size_of_list_of_operations(count));
473 memcpy(newlist, *oplp, size_of_list_of_operations((*oplp)->count));
475 newlist->op[count - 1] = op;
476 db_free(*oplp, size_of_list_of_operations(count - 1));
480 void oplist_delete(LIST_OF_OPERATIONS oplp, OPERATION op)
485 for (s = oplp->op, c = oplp->count; c; --c, ++s)
503 void reapchild(int x)
507 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
511 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
512 com_err(whoami, 0, "%d: child exits with signal %d status %d",
513 pid, WTERMSIG(status), WEXITSTATUS(status));
518 void godormant(int x)
524 com_err(whoami, 0, "requested to go dormant");
527 com_err(whoami, 0, "already asleep");
542 com_err(whoami, 0, "Good morning");
545 com_err(whoami, 0, "already awake");
554 void mr_setup_signals(void)
556 struct sigaction action;
559 sigemptyset(&action.sa_mask);
561 /* There should probably be a few more of these. */
563 action.sa_handler = sigshut;
564 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
565 (sigaction(SIGINT, &action, NULL) < 0) ||
566 (sigaction(SIGHUP, &action, NULL) < 0))
568 com_err(whoami, errno, "Unable to establish signal handlers.");
572 action.sa_handler = godormant;
573 if (sigaction(SIGUSR1, &action, NULL) < 0)
575 com_err(whoami, errno, "Unable to establish signal handlers.");
579 action.sa_handler = gowakeup;
580 if (sigaction(SIGUSR2, &action, NULL) < 0)
582 com_err(whoami, errno, "Unable to establish signal handlers.");
586 action.sa_handler = reapchild;
587 sigaddset(&action.sa_mask, SIGCHLD);
588 if (sigaction(SIGCHLD, &action, NULL) < 0)
590 com_err(whoami, errno, "Unable to establish signal handlers.");