3 * Moira server process.
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
12 #include "mr_server.h"
14 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <sys/utsname.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
33 extern char *krb_get_lrealm(char *, int);
35 extern CONNECTION newconn, listencon;
38 extern client **clients, *cur_client;
40 extern OPERATION listenop;
41 extern LIST_OF_OPERATIONS op_list;
43 extern struct sockaddr_in client_addr;
44 extern int client_addrlen;
45 extern TUPLE client_tuple;
48 extern char buf1[BUFSIZ];
49 extern char *takedown;
56 void reapchild(int x);
57 void godormant(int x);
59 int do_listen(char *port);
60 void do_reset_listen(void);
61 void clist_append(client *cp);
62 void oplist_append(LIST_OF_OPERATIONS *oplp, OPERATION op);
63 void oplist_delete(LIST_OF_OPERATIONS oplp, OPERATION op);
64 void mr_setup_signals(void);
65 int new_connection(void);
68 * Main Moira server loop.
70 * Initialize the world, then start accepting connections and
71 * making progress on current connections.
74 int main(int argc, char **argv)
79 extern char *database;
87 initialize_sms_error_table();
88 initialize_krb_error_table();
89 initialize_gdss_error_table();
90 set_com_err_hook(mr_com_err);
91 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
93 port = strchr(MOIRA_SERVER, ':') + 1;
95 for (i = 1; i < argc; i++)
97 if (!strcmp(argv[i], "-db") && i + 1 < argc)
99 database = argv[i + 1];
102 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
109 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
115 * GDB initialization.
119 com_err(whoami, 0, "GDB initialization failed.");
122 gdb_debug(0); /* this can be patched, if necessary, to enable */
123 /* GDB level debugging .. */
124 krb_realm = malloc(REALM_SZ);
125 krb_get_lrealm(krb_realm, 1);
128 * Database initialization. Only init if database should be open.
131 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
133 if ((status = mr_open_database()))
135 com_err(whoami, status, " when trying to open database.");
138 sanity_check_database();
143 com_err(whoami, 0, "sleeping, not opening database");
146 sanity_check_queries();
149 * Get moira server hostname for authentication
153 com_err(whoami, errno, "Unable to get local hostname");
156 host = canonicalize_hostname(strdup(uts.nodename));
157 for (p = host; *p && *p != '.'; p++)
165 * Set up client array handler.
172 journal = fopen(JOURNAL, "a");
175 com_err(whoami, errno, " while opening journal file");
180 * Establish template connection.
182 if ((status = do_listen(port)))
184 com_err(whoami, status, " while trying to create listening connection");
188 op_list = create_list_of_operations(1, listenop);
190 com_err(whoami, 0, "started (pid %d)", getpid());
191 com_err(whoami, 0, rcsid);
192 if (dormant != ASLEEP)
193 send_zgram("MOIRA", "server started");
195 send_zgram("MOIRA", "server started, but database closed");
198 * Run until shut down.
204 * Block until something happens.
206 if (dormant == SLEEPY)
209 com_err(whoami, 0, "database closed");
211 send_zgram("MOIRA", "database closed");
214 else if (dormant == GROGGY)
217 com_err(whoami, 0, "database open");
219 send_zgram("MOIRA", "database open again");
224 status = op_select_any(op_list, 0, NULL, NULL, NULL, NULL);
228 com_err(whoami, errno, " error from op_select");
229 if (!inc_running || now - inc_started > INC_TIMEOUT)
233 else if (status != -2)
235 com_err(whoami, 0, " wrong return from op_select_any");
241 if (!inc_running || now - inc_started > INC_TIMEOUT)
245 * Handle any new connections; this comes first so
246 * errno isn't tromped on.
248 if (OP_DONE(listenop))
250 if (OP_STATUS(listenop) == OP_CANCELLED)
252 if (errno == EWOULDBLOCK)
256 static int count = 0;
257 com_err(whoami, errno, " error (%d) on listen", count);
262 else if ((status = new_connection()))
264 com_err(whoami, errno, " Error on listening operation.");
266 * Sleep here to prevent hosing?
269 /* if the new connection is our only connection,
270 * and the server is supposed to be down, then go
273 if ((dormant == AWAKE) && (nclients == 1) &&
274 (stat(MOIRA_MOTD_FILE, &stbuf) == 0))
276 com_err(whoami, 0, "motd file exists, slumbertime");
279 /* on new connection, if we are no longer supposed
280 * to be down, then wake up.
282 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
285 com_err(whoami, 0, "motd file no longer exists, waking up");
290 * Handle any existing connections.
292 tardy = now - 30 * 60;
294 for (i = 0; i < nclients; i++)
296 cur_client = clients[i];
297 if (OP_DONE(clients[i]->pending_op))
299 cur_client->last_time_used = now;
300 do_client(cur_client);
302 else if (clients[i]->last_time_used < tardy)
304 com_err(whoami, 0, "Shutting down connection due to inactivity");
305 shutdown(cur_client->con->in.fd, 2);
312 com_err(whoami, 0, "%s", takedown);
313 if (dormant != ASLEEP)
315 send_zgram("MOIRA", takedown);
320 * Set up the template connection and queue the first accept.
323 int do_listen(char *port)
325 listencon = create_listening_connection(port);
330 listenop = create_operation();
331 client_addrlen = sizeof(client_addr);
333 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
334 &client_addrlen, &client_tuple);
339 void do_reset_listen(void)
341 client_addrlen = sizeof(client_addr);
342 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
343 &client_addrlen, &client_tuple);
347 * This routine is called when a new connection comes in.
349 * It sets up a new client and adds it to the list of currently active clients.
351 int new_connection(void)
357 * Make sure there's been no error
359 if (OP_STATUS(listenop) != OP_COMPLETE)
363 return MR_NOT_CONNECTED;
366 * Set up the new connection and reply to the client
368 cp = malloc(sizeof(client));
369 memset(cp, 0, sizeof(*cp));
370 cp->action = CL_ACCEPT;
374 cp->clname[0] = '\0';
375 cp->reply.mr_argv = NULL;
378 cp->last_time_used = now;
381 cp->pending_op = create_operation();
382 reset_operation(cp->pending_op);
383 oplist_append(&op_list, cp->pending_op);
387 * Add a new client to the array..
392 * Let him know we heard him.
394 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, "", "");
396 cp->haddr = client_addr;
399 * Log new connection.
401 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
402 inet_ntoa(cp->haddr.sin_addr), (int)ntohs(cp->haddr.sin_port),
403 nclients, nclients != 1 ? "s" : "");
406 * Get ready to accept the next connection.
408 reset_operation(listenop);
409 client_addrlen = sizeof(client_addr);
411 start_accepting_client(listencon, listenop, &newconn, (char *)&client_addr,
412 &client_addrlen, &client_tuple);
417 * Add a new client to the known clients.
419 void clist_append(client *cp)
424 clients_n = malloc(nclients * sizeof(client *));
425 memcpy(clients_n, clients, (nclients - 1) * sizeof(cp));
426 clients_n[nclients - 1] = cp;
433 void clist_delete(client *cp)
435 client **clients_n, **scpp, **dcpp; /* source and dest client ptr ptr */
439 clients_n = malloc((nclients - 1) * sizeof(client *));
440 for (scpp = clients, dcpp = clients_n; scpp < clients + nclients; )
456 oplist_delete(op_list, cp->pending_op);
457 reset_operation(cp->pending_op);
458 delete_operation(cp->pending_op);
459 sever_connection(cp->con);
464 * Add a new operation to a list of operations.
466 * This should be rewritten to use realloc instead, since in most
467 * cases it won't have to copy the array.
470 void oplist_append(LIST_OF_OPERATIONS *oplp, OPERATION op)
472 int count = (*oplp)->count + 1;
473 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
474 db_alloc(size_of_list_of_operations(count));
475 memcpy(newlist, *oplp, size_of_list_of_operations((*oplp)->count));
477 newlist->op[count - 1] = op;
478 db_free(*oplp, size_of_list_of_operations(count - 1));
482 void oplist_delete(LIST_OF_OPERATIONS oplp, OPERATION op)
487 for (s = oplp->op, c = oplp->count; c; --c, ++s)
505 void reapchild(int x)
509 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
513 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
514 com_err(whoami, 0, "%d: child exits with signal %d status %d",
515 pid, WTERMSIG(status), WEXITSTATUS(status));
520 void godormant(int x)
526 com_err(whoami, 0, "requested to go dormant");
529 com_err(whoami, 0, "already asleep");
544 com_err(whoami, 0, "Good morning");
547 com_err(whoami, 0, "already awake");
556 void mr_setup_signals(void)
558 struct sigaction action;
561 sigemptyset(&action.sa_mask);
563 /* There should probably be a few more of these. */
565 action.sa_handler = sigshut;
566 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
567 (sigaction(SIGINT, &action, NULL) < 0) ||
568 (sigaction(SIGHUP, &action, NULL) < 0))
570 com_err(whoami, errno, "Unable to establish signal handlers.");
574 action.sa_handler = godormant;
575 if (sigaction(SIGUSR1, &action, NULL) < 0)
577 com_err(whoami, errno, "Unable to establish signal handlers.");
581 action.sa_handler = gowakeup;
582 if (sigaction(SIGUSR2, &action, NULL) < 0)
584 com_err(whoami, errno, "Unable to establish signal handlers.");
588 action.sa_handler = reapchild;
589 sigaddset(&action.sa_mask, SIGCHLD);
590 if (sigaction(SIGCHLD, &action, NULL) < 0)
592 com_err(whoami, errno, "Unable to establish signal handlers.");