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>
47 krb5_context context = NULL;
48 char *krb_realm = NULL;
50 /* Client array and associated data. This needs to be global for _list_users */
52 int nclients, clientssize;
55 int child_exited_abnormally = 0;
56 int child_pid, child_signal, child_status;
58 void reapchild(int x);
59 void godormant(int x);
61 void clist_append(client *cp);
62 void mr_setup_signals(void);
65 * Main Moira server loop.
67 * Initialize the world, then start accepting connections and
68 * making progress on current connections.
71 int main(int argc, char **argv)
73 int status, i, listener;
76 extern char *database;
79 fd_set readfds, writefds, xreadfds, xwritefds;
80 int nfds, counter = 0;
87 set_com_err_hook(mr_com_err);
88 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
90 port = strchr(MOIRA_SERVER, ':') + 1;
92 for (i = 1; i < argc; i++)
94 if (!strcmp(argv[i], "-db") && i + 1 < argc)
96 database = argv[i + 1];
99 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
106 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
111 status = krb5_init_context(&context);
114 com_err(whoami, status, "Initializing krb5 context.");
118 status = krb5_get_default_realm(context, &krb_realm);
121 com_err(whoami, status, "Getting default Kerberos realm.");
126 * Database initialization. Only init if database should be open.
129 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
131 if ((status = mr_open_database()))
133 com_err(whoami, status, "trying to open database.");
136 sanity_check_database();
141 com_err(whoami, 0, "sleeping, not opening database");
144 sanity_check_queries();
147 * Get moira server hostname for authentication
151 com_err(whoami, errno, "Unable to get local hostname");
154 host = canonicalize_hostname(xstrdup(uts.nodename));
155 for (p = host; *p && *p != '.'; p++)
163 * Set up client array handler.
167 clients = xmalloc(clientssize * sizeof(client *));
171 journal = fopen(JOURNAL, "a");
174 com_err(whoami, errno, "opening journal file");
179 * Establish template connection.
181 listener = mr_listen(port);
184 com_err(whoami, MR_ABORTED, "trying to create listening connection");
189 FD_SET(listener, &xreadfds);
192 com_err(whoami, 0, "started (pid %d)", getpid());
193 com_err(whoami, 0, rcsid);
194 if (dormant != ASLEEP)
195 send_zgram("MOIRA", "server started");
197 send_zgram("MOIRA", "server started, but database closed");
200 * Run until shut down.
205 struct timeval timeout = {60, 0}; /* 1 minute */
207 /* If we're supposed to go down and we can, do it */
208 if (((dormant == AWAKE) && (nclients == 0) &&
209 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
213 com_err(whoami, 0, "database closed");
215 send_zgram("MOIRA", "database closed");
219 /* Block until something happens. */
220 memcpy(&readfds, &xreadfds, sizeof(readfds));
221 memcpy(&writefds, &xwritefds, sizeof(writefds));
222 if (select(nfds, &readfds, &writefds, NULL, &timeout) == -1)
225 com_err(whoami, errno, "in select");
226 if (!inc_running || now - inc_started > INC_TIMEOUT)
234 if (child_exited_abnormally)
236 critical_alert(whoami, "moirad", "%d: child exits with signal %d status %d",
237 child_pid, child_signal, child_status);
238 child_exited_abnormally = 0;
242 if (!inc_running || now - inc_started > INC_TIMEOUT)
244 tardy = now - 30 * 60;
246 /* If we're asleep and we should wake up, do it */
247 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
251 com_err(whoami, 0, "database open");
253 send_zgram("MOIRA", "database open again");
257 /* Handle any new connections */
258 if (FD_ISSET(listener, &readfds))
260 int newconn, addrlen = sizeof(struct sockaddr_in);
261 struct sockaddr_in addr;
264 newconn = accept(listener, (struct sockaddr *)&addr, &addrlen);
266 com_err(whoami, errno, "accepting new connection");
267 else if (newconn > 0)
269 if (newconn + 1 > nfds)
271 FD_SET(newconn, &xreadfds);
273 /* Add a new client to the array */
275 if (nclients > clientssize)
277 clientssize = 2 * clientssize;
278 clients = xrealloc(clients, clientssize * sizeof(client *));
281 clients[nclients - 1] = cp = xmalloc(sizeof(client));
282 memset(cp, 0, sizeof(client));
285 cp->last_time_used = now;
288 cp->tuples = xmalloc(sizeof(mr_params));
289 memset(cp->tuples, 0, sizeof(mr_params));
290 cp->state = CL_ACCEPTING;
295 "New connection from %s port %d (now %d client%s)",
296 inet_ntoa(cp->haddr.sin_addr),
297 (int)ntohs(cp->haddr.sin_port),
298 nclients, nclients != 1 ? "s" : "");
302 /* Handle any existing connections. */
303 for (i = 0; i < nclients; i++)
305 cur_client = clients[i];
307 if (FD_ISSET(clients[i]->con, &writefds))
309 client_write(clients[i]);
310 if (!clients[i]->ntuples)
312 FD_CLR(clients[i]->con, &xwritefds);
313 FD_SET(clients[i]->con, &xreadfds);
315 clients[i]->last_time_used = now;
318 if (FD_ISSET(clients[i]->con, &readfds))
320 if (clients[i]->state == CL_ACCEPTING)
322 switch(mr_cont_accept(clients[i]->con,
330 clients[i]->state = CL_CLOSING;
334 clients[i]->state = CL_ACTIVE;
335 clients[i]->hsbuf = NULL;
341 client_read(clients[i]);
342 if (clients[i]->ntuples)
344 FD_CLR(clients[i]->con, &xreadfds);
345 FD_SET(clients[i]->con, &xwritefds);
347 clients[i]->last_time_used = now;
351 if (clients[i]->last_time_used < tardy)
353 com_err(whoami, 0, "Shutting down connection due to inactivity");
354 clients[i]->state = CL_CLOSING;
357 if (clients[i]->state == CL_CLOSING)
361 com_err(whoami, 0, "Closed connection (now %d client%s, "
362 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
365 shutdown(clients[i]->con, 2);
366 close(clients[i]->con);
367 FD_CLR(clients[i]->con, &xreadfds);
368 FD_CLR(clients[i]->con, &xwritefds);
369 free_rtn_tuples(clients[i]);
370 free(clients[i]->tuples);
371 free(clients[i]->hsbuf);
373 clients[i] = clients[--nclients];
383 com_err(whoami, 0, "%s", takedown);
384 if (dormant != ASLEEP)
386 send_zgram("MOIRA", takedown);
390 void reapchild(int x)
394 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
398 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
400 child_exited_abnormally = 1;
402 child_signal = WTERMSIG(status);
403 child_status = WEXITSTATUS(status);
409 void godormant(int x)
415 com_err(whoami, 0, "requested to go dormant");
418 com_err(whoami, 0, "already asleep");
433 com_err(whoami, 0, "Good morning");
436 com_err(whoami, 0, "already awake");
445 void mr_setup_signals(void)
447 struct sigaction action;
450 sigemptyset(&action.sa_mask);
452 /* There should probably be a few more of these. */
454 action.sa_handler = sigshut;
455 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
456 (sigaction(SIGINT, &action, NULL) < 0) ||
457 (sigaction(SIGHUP, &action, NULL) < 0))
459 com_err(whoami, errno, "Unable to establish signal handlers.");
463 action.sa_handler = godormant;
464 if (sigaction(SIGUSR1, &action, NULL) < 0)
466 com_err(whoami, errno, "Unable to establish signal handlers.");
470 action.sa_handler = gowakeup;
471 if (sigaction(SIGUSR2, &action, NULL) < 0)
473 com_err(whoami, errno, "Unable to establish signal handlers.");
477 action.sa_handler = SIG_IGN;
478 if (sigaction(SIGPIPE, &action, NULL) < 0)
480 com_err(whoami, errno, "Unable to establish signal handlers.");
484 action.sa_handler = reapchild;
485 sigaddset(&action.sa_mask, SIGCHLD);
486 if (sigaction(SIGCHLD, &action, NULL) < 0)
488 com_err(whoami, errno, "Unable to establish signal handlers.");