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>
35 extern char *krb_get_lrealm(char *, int);
46 char krb_realm[REALM_SZ];
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);
110 * Database initialization. Only init if database should be open.
113 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
115 if ((status = mr_open_database()))
117 com_err(whoami, status, "trying to open database.");
120 sanity_check_database();
125 com_err(whoami, 0, "sleeping, not opening database");
128 sanity_check_queries();
131 * Get moira server hostname for authentication
135 com_err(whoami, errno, "Unable to get local hostname");
138 host = canonicalize_hostname(xstrdup(uts.nodename));
139 for (p = host; *p && *p != '.'; p++)
147 * Set up client array handler.
151 clients = xmalloc(clientssize * sizeof(client *));
155 journal = fopen(JOURNAL, "a");
158 com_err(whoami, errno, "opening journal file");
163 * Establish template connection.
165 if (!(listener = mr_listen(port)))
167 com_err(whoami, status, "trying to create listening connection");
172 FD_SET(listener, &xreadfds);
175 com_err(whoami, 0, "started (pid %d)", getpid());
176 com_err(whoami, 0, rcsid);
177 if (dormant != ASLEEP)
178 send_zgram("MOIRA", "server started");
180 send_zgram("MOIRA", "server started, but database closed");
183 * Run until shut down.
188 struct timeval timeout;
190 /* If we're supposed to go down and we can, do it */
191 if (((dormant == AWAKE) && (nclients == 0) &&
192 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
196 com_err(whoami, 0, "database closed");
198 send_zgram("MOIRA", "database closed");
202 /* Block until something happens. */
203 memcpy(&readfds, &xreadfds, sizeof(readfds));
204 memcpy(&writefds, &xwritefds, sizeof(writefds));
205 /* XXX set timeout */
206 if (select(nfds, &readfds, &writefds, NULL, NULL) == -1)
209 com_err(whoami, errno, "in select");
210 if (!inc_running || now - inc_started > INC_TIMEOUT)
218 if (!inc_running || now - inc_started > INC_TIMEOUT)
220 tardy = now - 30 * 60;
222 /* If we're asleep and we should wake up, do it */
223 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
227 com_err(whoami, 0, "database open");
229 send_zgram("MOIRA", "database open again");
233 /* Handle any new connections */
234 if (FD_ISSET(listener, &readfds))
237 struct sockaddr_in addr;
240 newconn = mr_accept(listener, &addr);
242 com_err(whoami, errno, "accepting new connection");
243 else if (newconn > 0)
245 if (newconn + 1 > nfds)
247 FD_SET(newconn, &xreadfds);
249 /* Add a new client to the array */
251 if (nclients > clientssize)
253 clientssize = 2 * clientssize;
254 clients = xrealloc(clients, clientssize * sizeof(client *));
257 clients[nclients - 1] = cp = xmalloc(sizeof(client));
258 memset(cp, 0, sizeof(client));
261 cp->last_time_used = now;
264 cp->tuples = xmalloc(sizeof(mr_params));
265 memset(cp->tuples, 0, sizeof(mr_params));
269 "New connection from %s port %d (now %d client%s)",
270 inet_ntoa(cp->haddr.sin_addr),
271 (int)ntohs(cp->haddr.sin_port),
272 nclients, nclients != 1 ? "s" : "");
276 /* Handle any existing connections. */
277 for (i = 0; i < nclients; i++)
279 cur_client = clients[i];
281 if (FD_ISSET(clients[i]->con, &writefds))
283 client_write(clients[i]);
284 if (!clients[i]->ntuples)
286 FD_CLR(clients[i]->con, &xwritefds);
287 /* Now that we're done writing we can read again */
288 FD_SET(clients[i]->con, &xreadfds);
290 clients[i]->last_time_used = now;
293 if (FD_ISSET(clients[i]->con, &readfds))
295 client_read(clients[i]);
296 if (clients[i]->ntuples)
297 FD_SET(clients[i]->con, &xwritefds);
298 clients[i]->last_time_used = now;
301 if (clients[i]->last_time_used < tardy)
303 com_err(whoami, 0, "Shutting down connection due to inactivity");
304 clients[i]->done = 1;
307 if (clients[i]->done)
311 com_err(whoami, 0, "Closed connection (now %d client%s, "
312 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
315 shutdown(clients[i]->con, 2);
316 close(clients[i]->con);
317 FD_CLR(clients[i]->con, &xreadfds);
318 FD_CLR(clients[i]->con, &xwritefds);
319 free_rtn_tuples(clients[i]);
320 free(clients[i]->tuples);
322 clients[i] = clients[--nclients];
332 com_err(whoami, 0, "%s", takedown);
333 if (dormant != ASLEEP)
335 send_zgram("MOIRA", takedown);
339 void reapchild(int x)
343 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
347 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
348 com_err(whoami, 0, "%d: child exits with signal %d status %d",
349 pid, WTERMSIG(status), WEXITSTATUS(status));
354 void godormant(int x)
360 com_err(whoami, 0, "requested to go dormant");
363 com_err(whoami, 0, "already asleep");
378 com_err(whoami, 0, "Good morning");
381 com_err(whoami, 0, "already awake");
390 void mr_setup_signals(void)
392 struct sigaction action;
395 sigemptyset(&action.sa_mask);
397 /* There should probably be a few more of these. */
399 action.sa_handler = sigshut;
400 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
401 (sigaction(SIGINT, &action, NULL) < 0) ||
402 (sigaction(SIGHUP, &action, NULL) < 0))
404 com_err(whoami, errno, "Unable to establish signal handlers.");
408 action.sa_handler = godormant;
409 if (sigaction(SIGUSR1, &action, NULL) < 0)
411 com_err(whoami, errno, "Unable to establish signal handlers.");
415 action.sa_handler = gowakeup;
416 if (sigaction(SIGUSR2, &action, NULL) < 0)
418 com_err(whoami, errno, "Unable to establish signal handlers.");
422 action.sa_handler = SIG_IGN;
423 if (sigaction(SIGPIPE, &action, NULL) < 0)
425 com_err(whoami, errno, "Unable to establish signal handlers.");
429 action.sa_handler = reapchild;
430 sigaddset(&action.sa_mask, SIGCHLD);
431 if (sigaction(SIGCHLD, &action, NULL) < 0)
433 com_err(whoami, errno, "Unable to establish signal handlers.");