]> andersk Git - moira.git/blame - server/mr_main.c
Build shared libmoira via libtool.
[moira.git] / server / mr_main.c
CommitLineData
7ac48069 1/* $Id$
0fa91a0a 2 *
7ac48069 3 * Moira server process.
0fa91a0a 4 *
7ac48069 5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
5eaef520 8 *
0fa91a0a 9 */
10
c801de4c 11#include <mit-copyright.h>
7ac48069 12#include "mr_server.h"
13
14#include <sys/socket.h>
15#include <sys/stat.h>
589993be 16#include <sys/types.h>
7ac48069 17#include <sys/utsname.h>
554776b1 18#include <sys/wait.h>
7ac48069 19
20#include <netinet/in.h>
03c05291 21#include <arpa/inet.h>
70d54e43 22
7ac48069 23#include <ctype.h>
24#include <errno.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
cb974713 31#ifdef HAVE_KRB4
85330553 32#include <krb.h>
cb974713 33#endif
991417e4 34#include <krb5.h>
85330553 35
7ac48069 36RCSID("$Header$");
37
85330553 38client *cur_client;
0fa91a0a 39
85330553 40char *whoami;
41char *takedown;
42FILE *journal;
0fa91a0a 43
85330553 44time_t now;
0fa91a0a 45
85330553 46char *host;
991417e4 47krb5_context context = NULL;
cb974713 48char *krb_realm = NULL;
70d54e43 49
85330553 50/* Client array and associated data. This needs to be global for _list_users */
51client **clients;
52int nclients, clientssize;
0eb9f52f 53
85330553 54int dormant;
9af63f7e 55int child_exited_abnormally = 0;
56int child_pid, child_signal, child_status;
3d0d0f07 57
7ac48069 58void reapchild(int x);
59void godormant(int x);
60void gowakeup(int x);
03c05291 61void clist_append(client *cp);
03c05291 62void mr_setup_signals(void);
e448f08d 63
0ec57336 64/*
59ec8dae 65 * Main Moira server loop.
0ec57336 66 *
67 * Initialize the world, then start accepting connections and
68 * making progress on current connections.
69 */
0fa91a0a 70
5eaef520 71int main(int argc, char **argv)
0fa91a0a 72{
85330553 73 int status, i, listener;
5eaef520 74 time_t tardy;
75 char *port, *p;
76 extern char *database;
77 struct stat stbuf;
7ac48069 78 struct utsname uts;
85330553 79 fd_set readfds, writefds, xreadfds, xwritefds;
80 int nfds, counter = 0;
5eaef520 81
82 whoami = argv[0];
83 /*
84 * Error handler init.
85 */
85330553 86 mr_init();
5eaef520 87 set_com_err_hook(mr_com_err);
88 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
89
90 port = strchr(MOIRA_SERVER, ':') + 1;
91
92 for (i = 1; i < argc; i++)
93 {
94 if (!strcmp(argv[i], "-db") && i + 1 < argc)
95 {
96 database = argv[i + 1];
97 i++;
5dbd09a0 98 }
5eaef520 99 else if (!strcmp(argv[i], "-p") && i + 1 < argc)
100 {
101 port = argv[i + 1];
102 i++;
f6cc38de 103 }
5eaef520 104 else
105 {
106 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
3d0d0f07 107 exit(1);
108 }
5eaef520 109 }
110
991417e4 111 status = krb5_init_context(&context);
112 if (status)
113 {
114 com_err(whoami, status, "Initializing krb5 context.");
115 exit(1);
116 }
117
cb974713 118 status = krb5_get_default_realm(context, &krb_realm);
119 if (status)
120 {
121 com_err(whoami, status, "Getting default Kerberos realm.");
122 exit(1);
123 }
124
5eaef520 125 /*
126 * Database initialization. Only init if database should be open.
127 */
128
129 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0)
130 {
131 if ((status = mr_open_database()))
132 {
85330553 133 com_err(whoami, status, "trying to open database.");
5eaef520 134 exit(1);
2f4ff9cd 135 }
5eaef520 136 sanity_check_database();
137 }
138 else
139 {
140 dormant = ASLEEP;
141 com_err(whoami, 0, "sleeping, not opening database");
142 }
2f4ff9cd 143
5eaef520 144 sanity_check_queries();
145
146 /*
147 * Get moira server hostname for authentication
148 */
7ac48069 149 if (uname(&uts) < 0)
5eaef520 150 {
151 com_err(whoami, errno, "Unable to get local hostname");
152 exit(1);
153 }
e688520a 154 host = canonicalize_hostname(xstrdup(uts.nodename));
5eaef520 155 for (p = host; *p && *p != '.'; p++)
156 {
157 if (isupper(*p))
158 *p = tolower(*p);
159 }
160 *p = '\0';
161
162 /*
163 * Set up client array handler.
164 */
165 nclients = 0;
85330553 166 clientssize = 10;
e688520a 167 clients = xmalloc(clientssize * sizeof(client *));
5eaef520 168
169 mr_setup_signals();
170
171 journal = fopen(JOURNAL, "a");
172 if (!journal)
173 {
85330553 174 com_err(whoami, errno, "opening journal file");
5eaef520 175 exit(1);
176 }
177
178 /*
179 * Establish template connection.
180 */
da6a4096 181 listener = mr_listen(port);
182 if (listener == -1)
5eaef520 183 {
93ba0dea 184 com_err(whoami, MR_ABORTED, "trying to create listening connection");
5eaef520 185 exit(1);
186 }
85330553 187 FD_ZERO(&xreadfds);
188 FD_ZERO(&xwritefds);
189 FD_SET(listener, &xreadfds);
190 nfds = listener + 1;
5eaef520 191
192 com_err(whoami, 0, "started (pid %d)", getpid());
7ac48069 193 com_err(whoami, 0, rcsid);
5eaef520 194 if (dormant != ASLEEP)
195 send_zgram("MOIRA", "server started");
196 else
197 send_zgram("MOIRA", "server started, but database closed");
198
199 /*
200 * Run until shut down.
201 */
202 while (!takedown)
203 {
44d12d58 204 int i;
9294e804 205 struct timeval timeout = {60, 0}; /* 1 minute */
85330553 206
207 /* If we're supposed to go down and we can, do it */
59ad5a76 208 if (((dormant == AWAKE) && (nclients == 0) &&
209 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) ||
210 (dormant == SLEEPY))
5eaef520 211 {
212 mr_close_database();
213 com_err(whoami, 0, "database closed");
214 mr_setup_signals();
215 send_zgram("MOIRA", "database closed");
216 dormant = ASLEEP;
217 }
589993be 218
85330553 219 /* Block until something happens. */
220 memcpy(&readfds, &xreadfds, sizeof(readfds));
221 memcpy(&writefds, &xwritefds, sizeof(writefds));
9294e804 222 if (select(nfds, &readfds, &writefds, NULL, &timeout) == -1)
5eaef520 223 {
224 if (errno != EINTR)
85330553 225 com_err(whoami, errno, "in select");
5eaef520 226 if (!inc_running || now - inc_started > INC_TIMEOUT)
227 next_incremental();
228 continue;
229 }
85330553 230
5eaef520 231 if (takedown)
232 break;
9af63f7e 233
234 if (child_exited_abnormally)
235 {
a816420b 236 critical_alert(whoami, "moirad", "%d: child exits with signal %d status %d",
9af63f7e 237 child_pid, child_signal, child_status);
238 child_exited_abnormally = 0;
239 }
240
5eaef520 241 time(&now);
242 if (!inc_running || now - inc_started > INC_TIMEOUT)
243 next_incremental();
85330553 244 tardy = now - 30 * 60;
245
246 /* If we're asleep and we should wake up, do it */
247 if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
248 (errno == ENOENT))
249 {
250 mr_open_database();
251 com_err(whoami, 0, "database open");
252 mr_setup_signals();
253 send_zgram("MOIRA", "database open again");
254 dormant = AWAKE;
255 }
5eaef520 256
85330553 257 /* Handle any new connections */
258 if (FD_ISSET(listener, &readfds))
5eaef520 259 {
8691463b 260 int newconn, addrlen = sizeof(struct sockaddr_in);
85330553 261 struct sockaddr_in addr;
262 client *cp;
263
8691463b 264 newconn = accept(listener, (struct sockaddr *)&addr, &addrlen);
85330553 265 if (newconn == -1)
266 com_err(whoami, errno, "accepting new connection");
267 else if (newconn > 0)
5eaef520 268 {
85330553 269 if (newconn + 1 > nfds)
270 nfds = newconn + 1;
271 FD_SET(newconn, &xreadfds);
272
273 /* Add a new client to the array */
274 nclients++;
275 if (nclients > clientssize)
5eaef520 276 {
85330553 277 clientssize = 2 * clientssize;
278 clients = xrealloc(clients, clientssize * sizeof(client *));
0fa91a0a 279 }
85330553 280
281 clients[nclients - 1] = cp = xmalloc(sizeof(client));
282 memset(cp, 0, sizeof(client));
283 cp->con = newconn;
284 cp->id = counter++;
285 cp->last_time_used = now;
286 cp->haddr = addr;
287 cp->tuplessize = 1;
288 cp->tuples = xmalloc(sizeof(mr_params));
289 memset(cp->tuples, 0, sizeof(mr_params));
8691463b 290 cp->state = CL_ACCEPTING;
c44ddfa7 291 cp->version = 2;
85330553 292
293 cur_client = cp;
294 com_err(whoami, 0,
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" : "");
5eaef520 299 }
0fa91a0a 300 }
5eaef520 301
85330553 302 /* Handle any existing connections. */
5eaef520 303 for (i = 0; i < nclients; i++)
304 {
305 cur_client = clients[i];
85330553 306
307 if (FD_ISSET(clients[i]->con, &writefds))
308 {
309 client_write(clients[i]);
310 if (!clients[i]->ntuples)
311 {
312 FD_CLR(clients[i]->con, &xwritefds);
85330553 313 FD_SET(clients[i]->con, &xreadfds);
314 }
315 clients[i]->last_time_used = now;
316 }
317
318 if (FD_ISSET(clients[i]->con, &readfds))
5eaef520 319 {
8691463b 320 if (clients[i]->state == CL_ACCEPTING)
321 {
322 switch(mr_cont_accept(clients[i]->con,
323 &clients[i]->hsbuf,
324 &clients[i]->hslen))
325 {
326 case -1:
327 break;
328
329 case 0:
330 clients[i]->state = CL_CLOSING;
331 break;
332
333 default:
334 clients[i]->state = CL_ACTIVE;
335 clients[i]->hsbuf = NULL;
336 break;
337 }
338 }
339 else
340 {
341 client_read(clients[i]);
342 if (clients[i]->ntuples)
343 {
344 FD_CLR(clients[i]->con, &xreadfds);
345 FD_SET(clients[i]->con, &xwritefds);
346 }
347 clients[i]->last_time_used = now;
348 }
5eaef520 349 }
85330553 350
351 if (clients[i]->last_time_used < tardy)
5eaef520 352 {
353 com_err(whoami, 0, "Shutting down connection due to inactivity");
8691463b 354 clients[i]->state = CL_CLOSING;
85330553 355 }
356
8691463b 357 if (clients[i]->state == CL_CLOSING)
85330553 358 {
359 client *old;
360
361 com_err(whoami, 0, "Closed connection (now %d client%s, "
362 "%d queries)", nclients - 1, nclients != 2 ? "s" : "",
363 newqueries);
364
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);
92ced3ed 369 free_rtn_tuples(clients[i]);
85330553 370 free(clients[i]->tuples);
8691463b 371 free(clients[i]->hsbuf);
85330553 372 old = clients[i];
373 clients[i] = clients[--nclients];
374 free(old);
5eaef520 375 }
85330553 376
5eaef520 377 cur_client = NULL;
378 if (takedown)
379 break;
380 }
381 }
85330553 382
5eaef520 383 com_err(whoami, 0, "%s", takedown);
384 if (dormant != ASLEEP)
385 mr_close_database();
386 send_zgram("MOIRA", takedown);
387 return 0;
70d54e43 388}
389
5eaef520 390void reapchild(int x)
554776b1 391{
5eaef520 392 int status, pid;
393
394 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
395 {
396 if (pid == inc_pid)
397 inc_running = 0;
398 if (!takedown && (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0))
71c89283 399 {
9af63f7e 400 child_exited_abnormally = 1;
401 child_pid = pid;
402 child_signal = WTERMSIG(status);
403 child_status = WEXITSTATUS(status);
71c89283 404 }
554776b1 405 }
406}
589993be 407
408
5eaef520 409void godormant(int x)
589993be 410{
5eaef520 411 switch (dormant)
412 {
589993be 413 case AWAKE:
414 case GROGGY:
5eaef520 415 com_err(whoami, 0, "requested to go dormant");
416 break;
589993be 417 case ASLEEP:
5eaef520 418 com_err(whoami, 0, "already asleep");
419 break;
589993be 420 case SLEEPY:
5eaef520 421 break;
589993be 422 }
5eaef520 423 dormant = SLEEPY;
589993be 424}
425
426
5eaef520 427void gowakeup(int x)
589993be 428{
5eaef520 429 switch (dormant)
430 {
589993be 431 case ASLEEP:
432 case SLEEPY:
5eaef520 433 com_err(whoami, 0, "Good morning");
434 break;
589993be 435 case AWAKE:
5eaef520 436 com_err(whoami, 0, "already awake");
437 break;
589993be 438 case GROGGY:
5eaef520 439 break;
589993be 440 }
5eaef520 441 dormant = GROGGY;
589993be 442}
083a6e94 443
5eaef520 444
03c05291 445void mr_setup_signals(void)
083a6e94 446{
5eaef520 447 struct sigaction action;
448
449 action.sa_flags = 0;
450 sigemptyset(&action.sa_mask);
03c05291 451
5eaef520 452 /* There should probably be a few more of these. */
03c05291 453
5eaef520 454 action.sa_handler = sigshut;
455 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
456 (sigaction(SIGINT, &action, NULL) < 0) ||
457 (sigaction(SIGHUP, &action, NULL) < 0))
458 {
03c05291 459 com_err(whoami, errno, "Unable to establish signal handlers.");
460 exit(1);
083a6e94 461 }
e448f08d 462
5eaef520 463 action.sa_handler = godormant;
464 if (sigaction(SIGUSR1, &action, NULL) < 0)
465 {
03c05291 466 com_err(whoami, errno, "Unable to establish signal handlers.");
467 exit(1);
468 }
e448f08d 469
5eaef520 470 action.sa_handler = gowakeup;
471 if (sigaction(SIGUSR2, &action, NULL) < 0)
472 {
03c05291 473 com_err(whoami, errno, "Unable to establish signal handlers.");
474 exit(1);
475 }
e448f08d 476
44762a3f 477 action.sa_handler = SIG_IGN;
478 if (sigaction(SIGPIPE, &action, NULL) < 0)
479 {
480 com_err(whoami, errno, "Unable to establish signal handlers.");
481 exit(1);
482 }
483
5eaef520 484 action.sa_handler = reapchild;
485 sigaddset(&action.sa_mask, SIGCHLD);
486 if (sigaction(SIGCHLD, &action, NULL) < 0)
487 {
03c05291 488 com_err(whoami, errno, "Unable to establish signal handlers.");
489 exit(1);
490 }
491}
This page took 0.192257 seconds and 5 git commands to generate.