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>
45 char krb_realm[REALM_SZ];
46 krb5_context context = NULL;
48 /* Client array and associated data. This needs to be global for _list_users */
50 int nclients, clientssize;
53 int child_exited_abnormally = 0;
54 int child_pid, child_signal, child_status;
56 void reapchild(int x);
57 void godormant(int x);
59 void clist_append(client *cp);
60 void mr_setup_signals(void);
63 * Main Moira server loop.
65 * Initialize the world, then start accepting connections and
66 * making progress on current connections.
69 int main(int argc, char **argv)
71 int status, i, listener;
74 extern char *database;
77 fd_set readfds, writefds, xreadfds, xwritefds;
78 int nfds, counter = 0;
85 set_com_err_hook(mr_com_err);
86 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
88 port = strchr(MOIRA_SERVER, ':') + 1;
90 for (i = 1; i < argc; i++)
92 if (!strcmp(argv[i], "-db") && i + 1 < argc)
94 database = argv[i + 1];
97 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
104 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
109 krb_get_lrealm(krb_realm, 1);
111 status = krb5_init_context(&context);
114 com_err(whoami, status, "Initializing krb5 context.");
119 * Database initialization. Only init if database should be open.
122 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
124 if ((status = mr_open_database()))
126 com_err(whoami, status, "trying to open database.");
129 sanity_check_database();
134 com_err(whoami, 0, "sleeping, not opening database");
137 sanity_check_queries();
140 * Get moira server hostname for authentication
144 com_err(whoami, errno, "Unable to get local hostname");
147 host = canonicalize_hostname(xstrdup(uts.nodename));
148 for (p = host; *p && *p != '.'; p++)
156 * Set up client array handler.
160 clients = xmalloc(clientssize * sizeof(client *));
164 journal = fopen(JOURNAL, "a");
167 com_err(whoami, errno, "opening journal file");
172 * Establish template connection.
174 listener = mr_listen(port);
177 com_err(whoami, MR_ABORTED, "trying to create listening connection");
182 FD_SET(listener, &xreadfds);
185 com_err(whoami, 0, "started (pid %d)", getpid());
186 com_err(whoami, 0, rcsid);
187 if (dormant != ASLEEP)
188 send_zgram("MOIRA", "server started");
190 send_zgram("MOIRA", "server started, but database closed");
193 * Run until shut down.
198 struct timeval timeout = {60, 0}; /* 1 minute */
200 /* If we're supposed to go down and we can, do it */
201 if (((dormant == AWAKE) && (nclients == 0) &&
202 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
206 com_err(whoami, 0, "database closed");
208 send_zgram("MOIRA", "database closed");
212 /* Block until something happens. */
213 memcpy(&readfds, &xreadfds, sizeof(readfds));
214 memcpy(&writefds, &xwritefds, sizeof(writefds));
215 if (select(nfds, &readfds, &writefds, NULL, &timeout) == -1)
218 com_err(whoami, errno, "in select");
219 if (!inc_running || now - inc_started > INC_TIMEOUT)
227 if (child_exited_abnormally)
229 critical_alert("moirad", "%d: child exits with signal %d status %d",
230 child_pid, child_signal, child_status);
231 child_exited_abnormally = 0;
235 if (!inc_running || now - inc_started > INC_TIMEOUT)
237 tardy = now - 30 * 60;
239 /* If we're asleep and we should wake up, do it */
240 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
244 com_err(whoami, 0, "database open");
246 send_zgram("MOIRA", "database open again");
250 /* Handle any new connections */
251 if (FD_ISSET(listener, &readfds))
253 int newconn, addrlen = sizeof(struct sockaddr_in);
254 struct sockaddr_in addr;
257 newconn = accept(listener, (struct sockaddr *)&addr, &addrlen);
259 com_err(whoami, errno, "accepting new connection");
260 else if (newconn > 0)
262 if (newconn + 1 > nfds)
264 FD_SET(newconn, &xreadfds);
266 /* Add a new client to the array */
268 if (nclients > clientssize)
270 clientssize = 2 * clientssize;
271 clients = xrealloc(clients, clientssize * sizeof(client *));
274 clients[nclients - 1] = cp = xmalloc(sizeof(client));
275 memset(cp, 0, sizeof(client));
278 cp->last_time_used = now;
281 cp->tuples = xmalloc(sizeof(mr_params));
282 memset(cp->tuples, 0, sizeof(mr_params));
283 cp->state = CL_ACCEPTING;
288 "New connection from %s port %d (now %d client%s)",
289 inet_ntoa(cp->haddr.sin_addr),
290 (int)ntohs(cp->haddr.sin_port),
291 nclients, nclients != 1 ? "s" : "");
295 /* Handle any existing connections. */
296 for (i = 0; i < nclients; i++)
298 cur_client = clients[i];
300 if (FD_ISSET(clients[i]->con, &writefds))
302 client_write(clients[i]);
303 if (!clients[i]->ntuples)
305 FD_CLR(clients[i]->con, &xwritefds);
306 FD_SET(clients[i]->con, &xreadfds);
308 clients[i]->last_time_used = now;
311 if (FD_ISSET(clients[i]->con, &readfds))
313 if (clients[i]->state == CL_ACCEPTING)
315 switch(mr_cont_accept(clients[i]->con,
323 clients[i]->state = CL_CLOSING;
327 clients[i]->state = CL_ACTIVE;
328 clients[i]->hsbuf = NULL;
334 client_read(clients[i]);
335 if (clients[i]->ntuples)
337 FD_CLR(clients[i]->con, &xreadfds);
338 FD_SET(clients[i]->con, &xwritefds);
340 clients[i]->last_time_used = now;
344 if (clients[i]->last_time_used < tardy)
346 com_err(whoami, 0, "Shutting down connection due to inactivity");
347 clients[i]->state = CL_CLOSING;
350 if (clients[i]->state == CL_CLOSING)
354 com_err(whoami, 0, "Closed connection (now %d client%s, "
355 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
358 shutdown(clients[i]->con, 2);
359 close(clients[i]->con);
360 FD_CLR(clients[i]->con, &xreadfds);
361 FD_CLR(clients[i]->con, &xwritefds);
362 free_rtn_tuples(clients[i]);
363 free(clients[i]->tuples);
364 free(clients[i]->hsbuf);
366 clients[i] = clients[--nclients];
376 com_err(whoami, 0, "%s", takedown);
377 if (dormant != ASLEEP)
379 send_zgram("MOIRA", takedown);
383 void reapchild(int x)
387 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
391 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
393 child_exited_abnormally = 1;
395 child_signal = WTERMSIG(status);
396 child_status = WEXITSTATUS(status);
402 void godormant(int x)
408 com_err(whoami, 0, "requested to go dormant");
411 com_err(whoami, 0, "already asleep");
426 com_err(whoami, 0, "Good morning");
429 com_err(whoami, 0, "already awake");
438 void mr_setup_signals(void)
440 struct sigaction action;
443 sigemptyset(&action.sa_mask);
445 /* There should probably be a few more of these. */
447 action.sa_handler = sigshut;
448 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
449 (sigaction(SIGINT, &action, NULL) < 0) ||
450 (sigaction(SIGHUP, &action, NULL) < 0))
452 com_err(whoami, errno, "Unable to establish signal handlers.");
456 action.sa_handler = godormant;
457 if (sigaction(SIGUSR1, &action, NULL) < 0)
459 com_err(whoami, errno, "Unable to establish signal handlers.");
463 action.sa_handler = gowakeup;
464 if (sigaction(SIGUSR2, &action, NULL) < 0)
466 com_err(whoami, errno, "Unable to establish signal handlers.");
470 action.sa_handler = SIG_IGN;
471 if (sigaction(SIGPIPE, &action, NULL) < 0)
473 com_err(whoami, errno, "Unable to establish signal handlers.");
477 action.sa_handler = reapchild;
478 sigaddset(&action.sa_mask, SIGCHLD);
479 if (sigaction(SIGCHLD, &action, NULL) < 0)
481 com_err(whoami, errno, "Unable to establish signal handlers.");