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.
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);
92 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
94 port = strchr(MOIRA_SERVER, ':') + 1;
96 for (i = 1; i < argc; i++) {
97 if (!strcmp(argv[i], "-db") && i+1 < argc) {
100 } else if (!strcmp(argv[i], "-p") && i+1 < argc) {
104 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
110 * GDB initialization.
112 if(gdb_init() != 0) {
113 com_err(whoami, 0, "GDB initialization failed.");
116 gdb_debug(0); /* this can be patched, if necessary, to enable */
117 /* GDB level debugging .. */
118 krb_realm = malloc(REALM_SZ);
119 krb_get_lrealm(krb_realm, 1);
122 * Database initialization. Only init if database should be open.
125 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) {
126 if ((status = mr_open_database()) != 0) {
127 com_err(whoami, status, " when trying to open database.");
130 sanity_check_database();
133 com_err(whoami, 0, "sleeping, not opening database");
136 sanity_check_queries();
139 * Get moira server hostname for authentication
141 if (gethostname(hostbuf, sizeof(hostbuf)) < 0) {
142 com_err(whoami, errno, "Unable to get local hostname");
145 host = canonicalize_hostname(strsave(hostbuf));
146 for (p = host; *p && *p != '.'; p++)
152 * Set up client array handler.
155 clients = (client **) malloc(0);
159 journal = fopen(JOURNAL, "a");
160 if (journal == NULL) {
161 com_err(whoami, errno, " while opening journal file");
166 * Establish template connection.
168 if ((status = do_listen(port)) != 0) {
169 com_err(whoami, status,
170 " while trying to create listening connection");
174 op_list = create_list_of_operations(1, listenop);
176 com_err(whoami, 0, "started (pid %d)", getpid());
177 com_err(whoami, 0, rcsid_mr_main_c);
178 if (dormant != ASLEEP)
179 send_zgram("MOIRA", "server started");
181 send_zgram("MOIRA", "server started, but database closed");
184 * Run until shut down.
189 * Block until something happens.
192 com_err(whoami, 0, "tick");
194 if (dormant == SLEEPY) {
196 com_err(whoami, 0, "database closed");
198 send_zgram("MOIRA", "database closed");
200 } else if (dormant == GROGGY) {
202 com_err(whoami, 0, "database open");
204 send_zgram("MOIRA", "database open again");
209 status = op_select_any(op_list, 0,
210 (fd_set *)NULL, (fd_set *)NULL,
211 (fd_set *)NULL, (struct timeval *)NULL);
215 com_err(whoami, errno, " error from op_select");
216 if (!inc_running || now - inc_started > INC_TIMEOUT)
219 } else if (status != -2) {
220 com_err(whoami, 0, " wrong return from op_select_any");
225 if (!inc_running || now - inc_started > INC_TIMEOUT)
228 fprintf(stderr, " tick\n");
231 * Handle any new connections; this comes first so
232 * errno isn't tromped on.
234 if (OP_DONE(listenop)) {
235 if (OP_STATUS(listenop) == OP_CANCELLED) {
236 if (errno == EWOULDBLOCK) {
239 static int count = 0;
240 com_err(whoami, errno,
241 " error (%d) on listen", count);
245 } else if ((status = new_connection()) != 0) {
246 com_err(whoami, errno,
247 " Error on listening operation.");
249 * Sleep here to prevent hosing?
252 /* if the new connection is our only connection,
253 * and the server is supposed to be down, then go
256 if ((dormant == AWAKE) && (nclients == 1) &&
257 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
258 com_err(whoami, 0, "motd file exists, slumbertime");
261 /* on new connection, if we are no longer supposed
262 * to be down, then wake up.
264 if ((dormant == ASLEEP) &&
265 (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
267 com_err(whoami, 0, "motd file no longer exists, waking up");
273 * Handle any existing connections.
277 for (i=0; i<nclients; i++) {
278 cur_client = clients[i];
279 if (OP_DONE(clients[i]->pending_op)) {
280 cur_client->last_time_used = now;
281 do_client(cur_client);
282 } else if (clients[i]->last_time_used < tardy) {
283 com_err(whoami, 0, "Shutting down connection due to inactivity");
284 shutdown(cur_client->con->in.fd, 2);
290 com_err(whoami, 0, "%s", takedown);
292 send_zgram("MOIRA", takedown);
297 * 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);
318 void do_reset_listen(void)
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.
331 int new_connection(void)
337 * Make sure there's been no error
339 if(OP_STATUS(listenop) != OP_COMPLETE) {
343 if (newconn == NULL) {
344 return MR_NOT_CONNECTED;
348 * Set up the new connection and reply to the client
350 cp = (client *)malloc(sizeof *cp);
351 memset(cp, 0, sizeof(*cp));
352 cp->action = CL_ACCEPT;
356 cp->clname[0] = NULL;
357 cp->reply.mr_argv = NULL;
360 cp->last_time_used = now;
363 cp->pending_op = create_operation();
364 reset_operation(cp->pending_op);
365 oplist_append(&op_list, cp->pending_op);
369 * Add a new client to the array..
374 * Let him know we heard him.
376 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
379 cp->haddr = client_addr;
382 * Log new connection.
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),
392 * Get ready to accept the next connection.
394 reset_operation(listenop);
395 client_addrlen = sizeof(client_addr);
397 start_accepting_client(listencon, listenop, &newconn,
398 (char *)&client_addr,
399 &client_addrlen, &client_tuple);
404 * Add a new client to the known clients.
406 void clist_append(cp)
412 clients_n = (client **)malloc
413 ((unsigned)(nclients * sizeof(client *)));
414 memcpy((char *)clients_n, (char *)clients, (nclients-1)*sizeof(cp));
415 clients_n[nclients-1] = cp;
416 free((char *)clients);
422 void clist_delete(cp)
425 client **clients_n, **scpp, **dcpp; /* source and dest client */
430 clients_n = (client **)malloc
431 ((unsigned)((nclients - 1)* sizeof(client *)));
432 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
437 if (found_it) abort();
442 free((char *)clients);
445 oplist_delete(op_list, cp->pending_op);
446 reset_operation(cp->pending_op);
447 delete_operation(cp->pending_op);
448 sever_connection(cp->con);
453 * Add a new operation to a list of operations.
455 * This should be rewritten to use realloc instead, since in most
456 * cases it won't have to copy the array.
459 void oplist_append(oplp, op)
460 LIST_OF_OPERATIONS *oplp;
463 int count = (*oplp)->count+1;
464 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
465 db_alloc(size_of_list_of_operations(count));
466 memcpy((char *)newlist, (char *)(*oplp),
467 size_of_list_of_operations((*oplp)->count));
469 newlist->op[count-1] = op;
470 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
475 void oplist_delete(oplp, op)
476 LIST_OF_OPERATIONS oplp;
479 register OPERATION *s;
482 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
502 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
504 com_err(whoami, 0, "Reaping %d (inc_pid=%d, inc_running=%d)",
505 pid, inc_pid, inc_running);
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));
522 com_err(whoami, 0, "requested to go dormant");
525 com_err(whoami, 0, "already asleep");
540 com_err(whoami, 0, "Good morning");
543 com_err(whoami, 0, "already awake");
552 void mr_setup_signals(void)
554 struct sigaction action;
557 sigemptyset(&action.sa_mask);
559 /* There should probably be a few more of these. */
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.");
569 action.sa_handler = godormant;
570 if (sigaction(SIGUSR1, &action, NULL) < 0) {
571 com_err(whoami, errno, "Unable to establish signal handlers.");
575 action.sa_handler = gowakeup;
576 if (sigaction(SIGUSR2, &action, NULL) < 0) {
577 com_err(whoami, errno, "Unable to establish signal handlers.");
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.");