]>
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> |
32 | ||
7ac48069 | 33 | RCSID("$Header$"); |
34 | ||
35 | extern char *krb_get_lrealm(char *, int); | |
36 | ||
85330553 | 37 | client *cur_client; |
0fa91a0a | 38 | |
85330553 | 39 | char *whoami; |
40 | char *takedown; | |
41 | FILE *journal; | |
0fa91a0a | 42 | |
85330553 | 43 | time_t now; |
0fa91a0a | 44 | |
85330553 | 45 | char *host; |
46 | char krb_realm[REALM_SZ]; | |
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 | ||
109 | /* | |
110 | * Database initialization. Only init if database should be open. | |
111 | */ | |
112 | ||
113 | if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) | |
114 | { | |
115 | if ((status = mr_open_database())) | |
116 | { | |
85330553 | 117 | com_err(whoami, status, "trying to open database."); |
5eaef520 | 118 | exit(1); |
2f4ff9cd | 119 | } |
5eaef520 | 120 | sanity_check_database(); |
121 | } | |
122 | else | |
123 | { | |
124 | dormant = ASLEEP; | |
125 | com_err(whoami, 0, "sleeping, not opening database"); | |
126 | } | |
2f4ff9cd | 127 | |
5eaef520 | 128 | sanity_check_queries(); |
129 | ||
130 | /* | |
131 | * Get moira server hostname for authentication | |
132 | */ | |
7ac48069 | 133 | if (uname(&uts) < 0) |
5eaef520 | 134 | { |
135 | com_err(whoami, errno, "Unable to get local hostname"); | |
136 | exit(1); | |
137 | } | |
e688520a | 138 | host = canonicalize_hostname(xstrdup(uts.nodename)); |
5eaef520 | 139 | for (p = host; *p && *p != '.'; p++) |
140 | { | |
141 | if (isupper(*p)) | |
142 | *p = tolower(*p); | |
143 | } | |
144 | *p = '\0'; | |
145 | ||
146 | /* | |
147 | * Set up client array handler. | |
148 | */ | |
149 | nclients = 0; | |
85330553 | 150 | clientssize = 10; |
e688520a | 151 | clients = xmalloc(clientssize * sizeof(client *)); |
5eaef520 | 152 | |
153 | mr_setup_signals(); | |
154 | ||
155 | journal = fopen(JOURNAL, "a"); | |
156 | if (!journal) | |
157 | { | |
85330553 | 158 | com_err(whoami, errno, "opening journal file"); |
5eaef520 | 159 | exit(1); |
160 | } | |
161 | ||
162 | /* | |
163 | * Establish template connection. | |
164 | */ | |
85330553 | 165 | if (!(listener = mr_listen(port))) |
5eaef520 | 166 | { |
85330553 | 167 | com_err(whoami, status, "trying to create listening connection"); |
5eaef520 | 168 | exit(1); |
169 | } | |
85330553 | 170 | FD_ZERO(&xreadfds); |
171 | FD_ZERO(&xwritefds); | |
172 | FD_SET(listener, &xreadfds); | |
173 | nfds = listener + 1; | |
5eaef520 | 174 | |
175 | com_err(whoami, 0, "started (pid %d)", getpid()); | |
7ac48069 | 176 | com_err(whoami, 0, rcsid); |
5eaef520 | 177 | if (dormant != ASLEEP) |
178 | send_zgram("MOIRA", "server started"); | |
179 | else | |
180 | send_zgram("MOIRA", "server started, but database closed"); | |
181 | ||
182 | /* | |
183 | * Run until shut down. | |
184 | */ | |
185 | while (!takedown) | |
186 | { | |
44d12d58 | 187 | int i; |
85330553 | 188 | struct timeval timeout; |
189 | ||
190 | /* If we're supposed to go down and we can, do it */ | |
59ad5a76 | 191 | if (((dormant == AWAKE) && (nclients == 0) && |
192 | (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) || | |
193 | (dormant == SLEEPY)) | |
5eaef520 | 194 | { |
195 | mr_close_database(); | |
196 | com_err(whoami, 0, "database closed"); | |
197 | mr_setup_signals(); | |
198 | send_zgram("MOIRA", "database closed"); | |
199 | dormant = ASLEEP; | |
200 | } | |
589993be | 201 | |
85330553 | 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) | |
5eaef520 | 207 | { |
208 | if (errno != EINTR) | |
85330553 | 209 | com_err(whoami, errno, "in select"); |
5eaef520 | 210 | if (!inc_running || now - inc_started > INC_TIMEOUT) |
211 | next_incremental(); | |
212 | continue; | |
213 | } | |
85330553 | 214 | |
5eaef520 | 215 | if (takedown) |
216 | break; | |
217 | time(&now); | |
218 | if (!inc_running || now - inc_started > INC_TIMEOUT) | |
219 | next_incremental(); | |
85330553 | 220 | tardy = now - 30 * 60; |
221 | ||
222 | /* If we're asleep and we should wake up, do it */ | |
223 | if ((dormant == ASLEEP) && (stat(MOIRA_MOTD_FILE, &stbuf) == -1) && | |
224 | (errno == ENOENT)) | |
225 | { | |
226 | mr_open_database(); | |
227 | com_err(whoami, 0, "database open"); | |
228 | mr_setup_signals(); | |
229 | send_zgram("MOIRA", "database open again"); | |
230 | dormant = AWAKE; | |
231 | } | |
5eaef520 | 232 | |
85330553 | 233 | /* Handle any new connections */ |
234 | if (FD_ISSET(listener, &readfds)) | |
5eaef520 | 235 | { |
85330553 | 236 | int newconn; |
237 | struct sockaddr_in addr; | |
238 | client *cp; | |
239 | ||
240 | newconn = mr_accept(listener, &addr); | |
241 | if (newconn == -1) | |
242 | com_err(whoami, errno, "accepting new connection"); | |
243 | else if (newconn > 0) | |
5eaef520 | 244 | { |
85330553 | 245 | if (newconn + 1 > nfds) |
246 | nfds = newconn + 1; | |
247 | FD_SET(newconn, &xreadfds); | |
248 | ||
249 | /* Add a new client to the array */ | |
250 | nclients++; | |
251 | if (nclients > clientssize) | |
5eaef520 | 252 | { |
85330553 | 253 | clientssize = 2 * clientssize; |
254 | clients = xrealloc(clients, clientssize * sizeof(client *)); | |
0fa91a0a | 255 | } |
85330553 | 256 | |
257 | clients[nclients - 1] = cp = xmalloc(sizeof(client)); | |
258 | memset(cp, 0, sizeof(client)); | |
259 | cp->con = newconn; | |
260 | cp->id = counter++; | |
261 | cp->last_time_used = now; | |
262 | cp->haddr = addr; | |
263 | cp->tuplessize = 1; | |
264 | cp->tuples = xmalloc(sizeof(mr_params)); | |
265 | memset(cp->tuples, 0, sizeof(mr_params)); | |
266 | ||
267 | cur_client = cp; | |
268 | com_err(whoami, 0, | |
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" : ""); | |
5eaef520 | 273 | } |
0fa91a0a | 274 | } |
5eaef520 | 275 | |
85330553 | 276 | /* Handle any existing connections. */ |
5eaef520 | 277 | for (i = 0; i < nclients; i++) |
278 | { | |
279 | cur_client = clients[i]; | |
85330553 | 280 | |
281 | if (FD_ISSET(clients[i]->con, &writefds)) | |
282 | { | |
283 | client_write(clients[i]); | |
284 | if (!clients[i]->ntuples) | |
285 | { | |
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); | |
289 | } | |
290 | clients[i]->last_time_used = now; | |
291 | } | |
292 | ||
293 | if (FD_ISSET(clients[i]->con, &readfds)) | |
5eaef520 | 294 | { |
85330553 | 295 | client_read(clients[i]); |
296 | if (clients[i]->ntuples) | |
297 | FD_SET(clients[i]->con, &xwritefds); | |
298 | clients[i]->last_time_used = now; | |
5eaef520 | 299 | } |
85330553 | 300 | |
301 | if (clients[i]->last_time_used < tardy) | |
5eaef520 | 302 | { |
303 | com_err(whoami, 0, "Shutting down connection due to inactivity"); | |
85330553 | 304 | clients[i]->done = 1; |
305 | } | |
306 | ||
307 | if (clients[i]->done) | |
308 | { | |
309 | client *old; | |
310 | ||
311 | com_err(whoami, 0, "Closed connection (now %d client%s, " | |
312 | "%d queries)", nclients - 1, nclients != 2 ? "s" : "", | |
313 | newqueries); | |
314 | ||
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); | |
92ced3ed | 319 | free_rtn_tuples(clients[i]); |
85330553 | 320 | free(clients[i]->tuples); |
321 | old = clients[i]; | |
322 | clients[i] = clients[--nclients]; | |
323 | free(old); | |
5eaef520 | 324 | } |
85330553 | 325 | |
5eaef520 | 326 | cur_client = NULL; |
327 | if (takedown) | |
328 | break; | |
329 | } | |
330 | } | |
85330553 | 331 | |
5eaef520 | 332 | com_err(whoami, 0, "%s", takedown); |
333 | if (dormant != ASLEEP) | |
334 | mr_close_database(); | |
335 | send_zgram("MOIRA", takedown); | |
336 | return 0; | |
70d54e43 | 337 | } |
338 | ||
5eaef520 | 339 | void reapchild(int x) |
554776b1 | 340 | { |
5eaef520 | 341 | int status, pid; |
342 | ||
343 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
344 | { | |
345 | if (pid == inc_pid) | |
346 | inc_running = 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)); | |
554776b1 | 350 | } |
351 | } | |
589993be | 352 | |
353 | ||
5eaef520 | 354 | void godormant(int x) |
589993be | 355 | { |
5eaef520 | 356 | switch (dormant) |
357 | { | |
589993be | 358 | case AWAKE: |
359 | case GROGGY: | |
5eaef520 | 360 | com_err(whoami, 0, "requested to go dormant"); |
361 | break; | |
589993be | 362 | case ASLEEP: |
5eaef520 | 363 | com_err(whoami, 0, "already asleep"); |
364 | break; | |
589993be | 365 | case SLEEPY: |
5eaef520 | 366 | break; |
589993be | 367 | } |
5eaef520 | 368 | dormant = SLEEPY; |
589993be | 369 | } |
370 | ||
371 | ||
5eaef520 | 372 | void gowakeup(int x) |
589993be | 373 | { |
5eaef520 | 374 | switch (dormant) |
375 | { | |
589993be | 376 | case ASLEEP: |
377 | case SLEEPY: | |
5eaef520 | 378 | com_err(whoami, 0, "Good morning"); |
379 | break; | |
589993be | 380 | case AWAKE: |
5eaef520 | 381 | com_err(whoami, 0, "already awake"); |
382 | break; | |
589993be | 383 | case GROGGY: |
5eaef520 | 384 | break; |
589993be | 385 | } |
5eaef520 | 386 | dormant = GROGGY; |
589993be | 387 | } |
083a6e94 | 388 | |
5eaef520 | 389 | |
03c05291 | 390 | void mr_setup_signals(void) |
083a6e94 | 391 | { |
5eaef520 | 392 | struct sigaction action; |
393 | ||
394 | action.sa_flags = 0; | |
395 | sigemptyset(&action.sa_mask); | |
03c05291 | 396 | |
5eaef520 | 397 | /* There should probably be a few more of these. */ |
03c05291 | 398 | |
5eaef520 | 399 | action.sa_handler = sigshut; |
400 | if ((sigaction(SIGTERM, &action, NULL) < 0) || | |
401 | (sigaction(SIGINT, &action, NULL) < 0) || | |
402 | (sigaction(SIGHUP, &action, NULL) < 0)) | |
403 | { | |
03c05291 | 404 | com_err(whoami, errno, "Unable to establish signal handlers."); |
405 | exit(1); | |
083a6e94 | 406 | } |
e448f08d | 407 | |
5eaef520 | 408 | action.sa_handler = godormant; |
409 | if (sigaction(SIGUSR1, &action, NULL) < 0) | |
410 | { | |
03c05291 | 411 | com_err(whoami, errno, "Unable to establish signal handlers."); |
412 | exit(1); | |
413 | } | |
e448f08d | 414 | |
5eaef520 | 415 | action.sa_handler = gowakeup; |
416 | if (sigaction(SIGUSR2, &action, NULL) < 0) | |
417 | { | |
03c05291 | 418 | com_err(whoami, errno, "Unable to establish signal handlers."); |
419 | exit(1); | |
420 | } | |
e448f08d | 421 | |
44762a3f | 422 | action.sa_handler = SIG_IGN; |
423 | if (sigaction(SIGPIPE, &action, NULL) < 0) | |
424 | { | |
425 | com_err(whoami, errno, "Unable to establish signal handlers."); | |
426 | exit(1); | |
427 | } | |
428 | ||
5eaef520 | 429 | action.sa_handler = reapchild; |
430 | sigaddset(&action.sa_mask, SIGCHLD); | |
431 | if (sigaction(SIGCHLD, &action, NULL) < 0) | |
432 | { | |
03c05291 | 433 | com_err(whoami, errno, "Unable to establish signal handlers."); |
434 | exit(1); | |
435 | } | |
436 | } |