6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
10 * MOIRA server process.
12 * Most of this is stolen from ../gdb/tsr.c
14 * You are in a maze of twisty little finite automata, all different.
15 * Let the reader beware.
19 static char *rcsid_mr_main_c = "$Header$";
21 #include <mit-copyright.h>
24 #include <sys/types.h>
25 #include <sys/errno.h>
26 #include <sys/signal.h>
31 #include "mr_server.h"
34 #include <arpa/inet.h>
36 extern CONNECTION newconn, listencon;
39 extern client **clients, *cur_client;
41 extern OPERATION listenop;
42 extern LIST_OF_OPERATIONS op_list;
44 extern struct sockaddr_in client_addr;
45 extern int client_addrlen;
46 extern TUPLE client_tuple;
49 extern char buf1[BUFSIZ];
50 extern char *takedown;
56 int do_listen(char *port);
57 void do_reset_listen(void);
58 void clist_append(client *cp);
59 void oplist_append(LIST_OF_OPERATIONS *oplp, OPERATION op);
60 void oplist_delete(LIST_OF_OPERATIONS oplp, OPERATION op);
61 void mr_setup_signals(void);
62 int new_connection(void);
65 * Main MOIRA server loop.
67 * Initialize the world, then start accepting connections and
68 * making progress on current connections.
79 extern char *database;
86 initialize_sms_error_table();
87 initialize_krb_error_table();
88 initialize_gdss_error_table();
89 set_com_err_hook(mr_com_err);
90 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
92 port = strchr(MOIRA_SERVER, ':') + 1;
94 for (i = 1; i < argc; i++) {
95 if (!strcmp(argv[i], "-db") && i+1 < argc) {
98 } else if (!strcmp(argv[i], "-p") && i+1 < argc) {
102 com_err(whoami, 0, "Usage: moirad [-db database][-p port]");
108 * GDB initialization.
110 if(gdb_init() != 0) {
111 com_err(whoami, 0, "GDB initialization failed.");
114 gdb_debug(0); /* this can be patched, if necessary, to enable */
115 /* GDB level debugging .. */
116 krb_realm = malloc(REALM_SZ);
117 krb_get_lrealm(krb_realm, 1);
120 * Database initialization. Only init if database should be open.
123 if (stat(MOIRA_MOTD_FILE, &stbuf) != 0) {
124 if ((status = mr_open_database()) != 0) {
125 com_err(whoami, status, " when trying to open database.");
128 sanity_check_database();
131 com_err(whoami, 0, "sleeping, not opening database");
134 sanity_check_queries();
137 * Set up client array handler.
140 clients = (client **) malloc(0);
144 journal = fopen(JOURNAL, "a");
145 if (journal == NULL) {
146 com_err(whoami, errno, " while opening journal file");
151 * Establish template connection.
153 if ((status = do_listen(port)) != 0) {
154 com_err(whoami, status,
155 " while trying to create listening connection");
159 op_list = create_list_of_operations(1, listenop);
161 com_err(whoami, 0, "started (pid %d)", getpid());
162 com_err(whoami, 0, rcsid_mr_main_c);
163 if (dormant != ASLEEP)
164 send_zgram("MOIRA", "server started");
166 send_zgram("MOIRA", "server started, but database closed");
169 * Run until shut down.
174 * Block until something happens.
177 com_err(whoami, 0, "tick");
179 if (dormant == SLEEPY) {
181 com_err(whoami, 0, "database closed");
183 send_zgram("MOIRA", "database closed");
185 } else if (dormant == GROGGY) {
187 com_err(whoami, 0, "database open");
189 send_zgram("MOIRA", "database open again");
194 status = op_select_any(op_list, 0,
195 (fd_set *)NULL, (fd_set *)NULL,
196 (fd_set *)NULL, (struct timeval *)NULL);
200 com_err(whoami, errno, " error from op_select");
201 if (!inc_running || now - inc_started > INC_TIMEOUT)
204 } else if (status != -2) {
205 com_err(whoami, 0, " wrong return from op_select_any");
210 if (!inc_running || now - inc_started > INC_TIMEOUT)
213 fprintf(stderr, " tick\n");
216 * Handle any new connections; this comes first so
217 * errno isn't tromped on.
219 if (OP_DONE(listenop)) {
220 if (OP_STATUS(listenop) == OP_CANCELLED) {
221 if (errno == EWOULDBLOCK) {
224 static int count = 0;
225 com_err(whoami, errno,
226 " error (%d) on listen", count);
230 } else if ((status = new_connection()) != 0) {
231 com_err(whoami, errno,
232 " Error on listening operation.");
234 * Sleep here to prevent hosing?
237 /* if the new connection is our only connection,
238 * and the server is supposed to be down, then go
241 if ((dormant == AWAKE) && (nclients == 1) &&
242 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
243 com_err(whoami, 0, "motd file exists, slumbertime");
246 /* on new connection, if we are no longer supposed
247 * to be down, then wake up.
249 if ((dormant == ASLEEP) &&
250 (stat(MOIRA_MOTD_FILE, &stbuf) == -1) &&
252 com_err(whoami, 0, "motd file no longer exists, waking up");
258 * Handle any existing connections.
262 for (i=0; i<nclients; i++) {
263 cur_client = clients[i];
264 if (OP_DONE(clients[i]->pending_op)) {
265 cur_client->last_time_used = now;
266 do_client(cur_client);
267 } else if (clients[i]->last_time_used < tardy) {
268 com_err(whoami, 0, "Shutting down connection due to inactivity");
269 shutdown(cur_client->con->in.fd, 2);
275 com_err(whoami, 0, "%s", takedown);
277 send_zgram("MOIRA", takedown);
282 * Set up the template connection and queue the first accept.
288 listencon = create_listening_connection(port);
290 if (listencon == NULL)
293 listenop = create_operation();
294 client_addrlen = sizeof(client_addr);
296 start_accepting_client(listencon, listenop, &newconn,
297 (char *)&client_addr,
298 &client_addrlen, &client_tuple);
303 void do_reset_listen(void)
305 client_addrlen = sizeof(client_addr);
306 start_accepting_client(listencon, listenop, &newconn,
307 (char *)&client_addr,
308 &client_addrlen, &client_tuple);
312 * This routine is called when a new connection comes in.
314 * It sets up a new client and adds it to the list of currently active clients.
316 int new_connection(void)
322 * Make sure there's been no error
324 if(OP_STATUS(listenop) != OP_COMPLETE) {
328 if (newconn == NULL) {
329 return MR_NOT_CONNECTED;
333 * Set up the new connection and reply to the client
335 cp = (client *)malloc(sizeof *cp);
336 memset(cp, 0, sizeof(*cp));
337 cp->action = CL_ACCEPT;
341 cp->clname[0] = NULL;
342 cp->reply.mr_argv = NULL;
345 cp->last_time_used = now;
348 cp->pending_op = create_operation();
349 reset_operation(cp->pending_op);
350 oplist_append(&op_list, cp->pending_op);
354 * Add a new client to the array..
359 * Let him know we heard him.
361 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
364 cp->haddr = client_addr;
367 * Log new connection.
370 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
371 inet_ntoa(cp->haddr.sin_addr),
372 (int)ntohs(cp->haddr.sin_port),
377 * Get ready to accept the next connection.
379 reset_operation(listenop);
380 client_addrlen = sizeof(client_addr);
382 start_accepting_client(listencon, listenop, &newconn,
383 (char *)&client_addr,
384 &client_addrlen, &client_tuple);
389 * Add a new client to the known clients.
391 void clist_append(cp)
397 clients_n = (client **)malloc
398 ((unsigned)(nclients * sizeof(client *)));
399 memcpy((char *)clients_n, (char *)clients, (nclients-1)*sizeof(cp));
400 clients_n[nclients-1] = cp;
401 free((char *)clients);
407 void clist_delete(cp)
410 client **clients_n, **scpp, **dcpp; /* source and dest client */
415 clients_n = (client **)malloc
416 ((unsigned)((nclients - 1)* sizeof(client *)));
417 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
422 if (found_it) abort();
427 free((char *)clients);
430 oplist_delete(op_list, cp->pending_op);
431 reset_operation(cp->pending_op);
432 delete_operation(cp->pending_op);
433 sever_connection(cp->con);
438 * Add a new operation to a list of operations.
440 * This should be rewritten to use realloc instead, since in most
441 * cases it won't have to copy the array.
444 void oplist_append(oplp, op)
445 LIST_OF_OPERATIONS *oplp;
448 int count = (*oplp)->count+1;
449 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
450 db_alloc(size_of_list_of_operations(count));
451 memcpy((char *)newlist, (char *)(*oplp),
452 size_of_list_of_operations((*oplp)->count));
454 newlist->op[count-1] = op;
455 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
460 void oplist_delete(oplp, op)
461 LIST_OF_OPERATIONS oplp;
464 register OPERATION *s;
467 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
487 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
489 com_err(whoami, 0, "Reaping %d (inc_pid=%d, inc_running=%d)",
490 pid, inc_pid, inc_running);
494 if (!takedown && (WTERMSIG(status)!= 0 || WEXITSTATUS(status)!= 0))
495 com_err(whoami, 0, "%d: child exits with signal %d status %d",
496 pid, WTERMSIG(status), WEXITSTATUS(status));
507 com_err(whoami, 0, "requested to go dormant");
510 com_err(whoami, 0, "already asleep");
525 com_err(whoami, 0, "Good morning");
528 com_err(whoami, 0, "already awake");
537 void mr_setup_signals(void)
539 struct sigaction action;
542 sigemptyset(&action.sa_mask);
544 /* There should probably be a few more of these. */
546 action.sa_handler = sigshut;
547 if ((sigaction(SIGTERM, &action, NULL) < 0) ||
548 (sigaction(SIGINT, &action, NULL) < 0) ||
549 (sigaction(SIGHUP, &action, NULL) < 0)) {
550 com_err(whoami, errno, "Unable to establish signal handlers.");
554 action.sa_handler = godormant;
555 if (sigaction(SIGUSR1, &action, NULL) < 0) {
556 com_err(whoami, errno, "Unable to establish signal handlers.");
560 action.sa_handler = gowakeup;
561 if (sigaction(SIGUSR2, &action, NULL) < 0) {
562 com_err(whoami, errno, "Unable to establish signal handlers.");
566 action.sa_handler = reapchild;
567 sigaddset(&action.sa_mask, SIGCHLD);
568 if (sigaction(SIGCHLD, &action, NULL) < 0) {
569 com_err(whoami, errno, "Unable to establish signal handlers.");