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>
44 char krb_realm[REALM_SZ];
46 /* Client array and associated data. This needs to be global for _list_users */
48 int nclients, clientssize;
52 void reapchild(int x);
53 void godormant(int x);
55 void clist_append(client *cp);
56 void mr_setup_signals(void);
59 * Main Moira server loop.
61 * Initialize the world, then start accepting connections and
62 * making progress on current connections.
65 int main(int argc, char **argv)
67 int status, i, listener;
70 extern char *database;
73 fd_set readfds, writefds, xreadfds, xwritefds;
74 int nfds, counter = 0;
81 set_com_err_hook(mr_com_err);
82 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
84 port = strchr(MOIRA_SERVER, ':') + 1;
86 for (i = 1; i < argc; i++)
88 if (!strcmp(argv[i], "-db") && i + 1 < argc)
90 database = argv[i + 1];
93 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
100 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
105 krb_get_lrealm(krb_realm, 1);
108 * Database initialization. Only init if database should be open.
111 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
113 if ((status = mr_open_database()))
115 com_err(whoami, status, "trying to open database.");
118 sanity_check_database();
123 com_err(whoami, 0, "sleeping, not opening database");
126 sanity_check_queries();
129 * Get moira server hostname for authentication
133 com_err(whoami, errno, "Unable to get local hostname");
136 host = canonicalize_hostname(xstrdup(uts.nodename));
137 for (p = host; *p && *p != '.'; p++)
145 * Set up client array handler.
149 clients = xmalloc(clientssize * sizeof(client *));
153 journal = fopen(JOURNAL, "a");
156 com_err(whoami, errno, "opening journal file");
161 * Establish template connection.
163 listener = mr_listen(port);
166 com_err(whoami, status, "trying to create listening connection");
171 FD_SET(listener, &xreadfds);
174 com_err(whoami, 0, "started (pid %d)", getpid());
175 com_err(whoami, 0, rcsid);
176 if (dormant != ASLEEP)
177 send_zgram("MOIRA", "server started");
179 send_zgram("MOIRA", "server started, but database closed");
182 * Run until shut down.
187 struct timeval timeout = {60, 0}; /* 1 minute */
189 /* If we're supposed to go down and we can, do it */
190 if (((dormant == AWAKE) && (nclients == 0) &&
191 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
195 com_err(whoami, 0, "database closed");
197 send_zgram("MOIRA", "database closed");
201 /* Block until something happens. */
202 memcpy(&readfds, &xreadfds, sizeof(readfds));
203 memcpy(&writefds, &xwritefds, sizeof(writefds));
204 if (select(nfds, &readfds, &writefds, NULL, &timeout) == -1)
207 com_err(whoami, errno, "in select");
208 if (!inc_running || now - inc_started > INC_TIMEOUT)
216 if (!inc_running || now - inc_started > INC_TIMEOUT)
218 tardy = now - 30 * 60;
220 /* If we're asleep and we should wake up, do it */
221 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
225 com_err(whoami, 0, "database open");
227 send_zgram("MOIRA", "database open again");
231 /* Handle any new connections */
232 if (FD_ISSET(listener, &readfds))
234 int newconn, addrlen = sizeof(struct sockaddr_in);
235 struct sockaddr_in addr;
238 newconn = accept(listener, (struct sockaddr *)&addr, &addrlen);
240 com_err(whoami, errno, "accepting new connection");
241 else if (newconn > 0)
243 if (newconn + 1 > nfds)
245 FD_SET(newconn, &xreadfds);
247 /* Add a new client to the array */
249 if (nclients > clientssize)
251 clientssize = 2 * clientssize;
252 clients = xrealloc(clients, clientssize * sizeof(client *));
255 clients[nclients - 1] = cp = xmalloc(sizeof(client));
256 memset(cp, 0, sizeof(client));
259 cp->last_time_used = now;
262 cp->tuples = xmalloc(sizeof(mr_params));
263 memset(cp->tuples, 0, sizeof(mr_params));
264 cp->state = CL_ACCEPTING;
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 FD_SET(clients[i]->con, &xreadfds);
289 clients[i]->last_time_used = now;
292 if (FD_ISSET(clients[i]->con, &readfds))
294 if (clients[i]->state == CL_ACCEPTING)
296 switch(mr_cont_accept(clients[i]->con,
304 clients[i]->state = CL_CLOSING;
308 clients[i]->state = CL_ACTIVE;
309 clients[i]->hsbuf = NULL;
315 client_read(clients[i]);
316 if (clients[i]->ntuples)
318 FD_CLR(clients[i]->con, &xreadfds);
319 FD_SET(clients[i]->con, &xwritefds);
321 clients[i]->last_time_used = now;
325 if (clients[i]->last_time_used < tardy)
327 com_err(whoami, 0, "Shutting down connection due to inactivity");
328 clients[i]->state = CL_CLOSING;
331 if (clients[i]->state == CL_CLOSING)
335 com_err(whoami, 0, "Closed connection (now %d client%s, "
336 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
339 shutdown(clients[i]->con, 2);
340 close(clients[i]->con);
341 FD_CLR(clients[i]->con, &xreadfds);
342 FD_CLR(clients[i]->con, &xwritefds);
343 free_rtn_tuples(clients[i]);
344 free(clients[i]->tuples);
345 free(clients[i]->hsbuf);
347 clients[i] = clients[--nclients];
357 com_err(whoami, 0, "%s", takedown);
358 if (dormant != ASLEEP)
360 send_zgram("MOIRA", takedown);
364 void reapchild(int x)
368 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
372 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
374 critical_alert("moirad", "%d: child exits with signal %d status %d",
375 pid, WTERMSIG(status), WEXITSTATUS(status));
381 void godormant(int x)
387 com_err(whoami, 0, "requested to go dormant");
390 com_err(whoami, 0, "already asleep");
405 com_err(whoami, 0, "Good morning");
408 com_err(whoami, 0, "already awake");
417 void mr_setup_signals(void)
419 struct sigaction action;
422 sigemptyset(&action.sa_mask);
424 /* There should probably be a few more of these. */
426 action.sa_handler = sigshut;
427 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
428 (sigaction(SIGINT, &action, NULL) < 0) ||
429 (sigaction(SIGHUP, &action, NULL) < 0))
431 com_err(whoami, errno, "Unable to establish signal handlers.");
435 action.sa_handler = godormant;
436 if (sigaction(SIGUSR1, &action, NULL) < 0)
438 com_err(whoami, errno, "Unable to establish signal handlers.");
442 action.sa_handler = gowakeup;
443 if (sigaction(SIGUSR2, &action, NULL) < 0)
445 com_err(whoami, errno, "Unable to establish signal handlers.");
449 action.sa_handler = SIG_IGN;
450 if (sigaction(SIGPIPE, &action, NULL) < 0)
452 com_err(whoami, errno, "Unable to establish signal handlers.");
456 action.sa_handler = reapchild;
457 sigaddset(&action.sa_mask, SIGCHLD);
458 if (sigaction(SIGCHLD, &action, NULL) < 0)
460 com_err(whoami, errno, "Unable to establish signal handlers.");