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