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;
54 void reapchild(int x);
55 void godormant(int x);
57 void clist_append(client *cp);
58 void mr_setup_signals(void);
61 * Main Moira server loop.
63 * Initialize the world, then start accepting connections and
64 * making progress on current connections.
67 int main(int argc, char **argv)
69 int status, i, listener;
72 extern char *database;
75 fd_set readfds, writefds, xreadfds, xwritefds;
76 int nfds, counter = 0;
83 set_com_err_hook(mr_com_err);
84 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
86 port = strchr(MOIRA_SERVER, ':') + 1;
88 for (i = 1; i < argc; i++)
90 if (!strcmp(argv[i], "-db") && i + 1 < argc)
92 database = argv[i + 1];
95 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
102 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
107 krb_get_lrealm(krb_realm, 1);
109 status = krb5_init_context(&context);
112 com_err(whoami, status, "Initializing krb5 context.");
115 krb5_init_ets(context);
118 * Database initialization. Only init if database should be open.
121 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
123 if ((status = mr_open_database()))
125 com_err(whoami, status, "trying to open database.");
128 sanity_check_database();
133 com_err(whoami, 0, "sleeping, not opening database");
136 sanity_check_queries();
139 * Get moira server hostname for authentication
143 com_err(whoami, errno, "Unable to get local hostname");
146 host = canonicalize_hostname(xstrdup(uts.nodename));
147 for (p = host; *p && *p != '.'; p++)
155 * Set up client array handler.
159 clients = xmalloc(clientssize * sizeof(client *));
163 journal = fopen(JOURNAL, "a");
166 com_err(whoami, errno, "opening journal file");
171 * Establish template connection.
173 listener = mr_listen(port);
176 com_err(whoami, MR_ABORTED, "trying to create listening connection");
181 FD_SET(listener, &xreadfds);
184 com_err(whoami, 0, "started (pid %d)", getpid());
185 com_err(whoami, 0, rcsid);
186 if (dormant != ASLEEP)
187 send_zgram("MOIRA", "server started");
189 send_zgram("MOIRA", "server started, but database closed");
192 * Run until shut down.
197 struct timeval timeout = {60, 0}; /* 1 minute */
199 /* If we're supposed to go down and we can, do it */
200 if (((dormant == AWAKE) && (nclients == 0) &&
201 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
205 com_err(whoami, 0, "database closed");
207 send_zgram("MOIRA", "database closed");
211 /* Block until something happens. */
212 memcpy(&readfds, &xreadfds, sizeof(readfds));
213 memcpy(&writefds, &xwritefds, sizeof(writefds));
214 if (select(nfds, &readfds, &writefds, NULL, &timeout) == -1)
217 com_err(whoami, errno, "in select");
218 if (!inc_running || now - inc_started > INC_TIMEOUT)
226 if (!inc_running || now - inc_started > INC_TIMEOUT)
228 tardy = now - 30 * 60;
230 /* If we're asleep and we should wake up, do it */
231 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
235 com_err(whoami, 0, "database open");
237 send_zgram("MOIRA", "database open again");
241 /* Handle any new connections */
242 if (FD_ISSET(listener, &readfds))
244 int newconn, addrlen = sizeof(struct sockaddr_in);
245 struct sockaddr_in addr;
248 newconn = accept(listener, (struct sockaddr *)&addr, &addrlen);
250 com_err(whoami, errno, "accepting new connection");
251 else if (newconn > 0)
253 if (newconn + 1 > nfds)
255 FD_SET(newconn, &xreadfds);
257 /* Add a new client to the array */
259 if (nclients > clientssize)
261 clientssize = 2 * clientssize;
262 clients = xrealloc(clients, clientssize * sizeof(client *));
265 clients[nclients - 1] = cp = xmalloc(sizeof(client));
266 memset(cp, 0, sizeof(client));
269 cp->last_time_used = now;
272 cp->tuples = xmalloc(sizeof(mr_params));
273 memset(cp->tuples, 0, sizeof(mr_params));
274 cp->state = CL_ACCEPTING;
279 "New connection from %s port %d (now %d client%s)",
280 inet_ntoa(cp->haddr.sin_addr),
281 (int)ntohs(cp->haddr.sin_port),
282 nclients, nclients != 1 ? "s" : "");
286 /* Handle any existing connections. */
287 for (i = 0; i < nclients; i++)
289 cur_client = clients[i];
291 if (FD_ISSET(clients[i]->con, &writefds))
293 client_write(clients[i]);
294 if (!clients[i]->ntuples)
296 FD_CLR(clients[i]->con, &xwritefds);
297 FD_SET(clients[i]->con, &xreadfds);
299 clients[i]->last_time_used = now;
302 if (FD_ISSET(clients[i]->con, &readfds))
304 if (clients[i]->state == CL_ACCEPTING)
306 switch(mr_cont_accept(clients[i]->con,
314 clients[i]->state = CL_CLOSING;
318 clients[i]->state = CL_ACTIVE;
319 clients[i]->hsbuf = NULL;
325 client_read(clients[i]);
326 if (clients[i]->ntuples)
328 FD_CLR(clients[i]->con, &xreadfds);
329 FD_SET(clients[i]->con, &xwritefds);
331 clients[i]->last_time_used = now;
335 if (clients[i]->last_time_used < tardy)
337 com_err(whoami, 0, "Shutting down connection due to inactivity");
338 clients[i]->state = CL_CLOSING;
341 if (clients[i]->state == CL_CLOSING)
345 com_err(whoami, 0, "Closed connection (now %d client%s, "
346 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
349 shutdown(clients[i]->con, 2);
350 close(clients[i]->con);
351 FD_CLR(clients[i]->con, &xreadfds);
352 FD_CLR(clients[i]->con, &xwritefds);
353 free_rtn_tuples(clients[i]);
354 free(clients[i]->tuples);
355 free(clients[i]->hsbuf);
357 clients[i] = clients[--nclients];
367 com_err(whoami, 0, "%s", takedown);
368 if (dormant != ASLEEP)
370 send_zgram("MOIRA", takedown);
374 void reapchild(int x)
378 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
382 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
384 critical_alert("moirad", "%d: child exits with signal %d status %d",
385 pid, WTERMSIG(status), WEXITSTATUS(status));
391 void godormant(int x)
397 com_err(whoami, 0, "requested to go dormant");
400 com_err(whoami, 0, "already asleep");
415 com_err(whoami, 0, "Good morning");
418 com_err(whoami, 0, "already awake");
427 void mr_setup_signals(void)
429 struct sigaction action;
432 sigemptyset(&action.sa_mask);
434 /* There should probably be a few more of these. */
436 action.sa_handler = sigshut;
437 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
438 (sigaction(SIGINT, &action, NULL) < 0) ||
439 (sigaction(SIGHUP, &action, NULL) < 0))
441 com_err(whoami, errno, "Unable to establish signal handlers.");
445 action.sa_handler = godormant;
446 if (sigaction(SIGUSR1, &action, NULL) < 0)
448 com_err(whoami, errno, "Unable to establish signal handlers.");
452 action.sa_handler = gowakeup;
453 if (sigaction(SIGUSR2, &action, NULL) < 0)
455 com_err(whoami, errno, "Unable to establish signal handlers.");
459 action.sa_handler = SIG_IGN;
460 if (sigaction(SIGPIPE, &action, NULL) < 0)
462 com_err(whoami, errno, "Unable to establish signal handlers.");
466 action.sa_handler = reapchild;
467 sigaddset(&action.sa_mask, SIGCHLD);
468 if (sigaction(SIGCHLD, &action, NULL) < 0)
470 com_err(whoami, errno, "Unable to establish signal handlers.");