6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
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_sms_main_c = "$Header$";
21 #include <mit-copyright.h>
23 #include <sys/types.h>
24 #include <sys/errno.h>
25 #include <sys/signal.h>
28 #include "sms_server.h"
31 extern CONNECTION newconn, listencon;
34 extern client **clients, *cur_client;
36 extern OPERATION listenop;
37 extern LIST_OF_OPERATIONS op_list;
39 extern struct sockaddr_in client_addr;
40 extern int client_addrlen;
41 extern TUPLE client_tuple;
44 extern char buf1[BUFSIZ];
45 extern char *takedown;
49 extern char *malloc();
51 extern char *inet_ntoa();
52 extern void sms_com_err();
53 extern void do_client();
58 void reapchild(), godormant(), gowakeup();
63 * Main SMS server loop.
65 * Initialize the world, then start accepting connections and
66 * making progress on current connections.
83 initialize_sms_error_table();
84 initialize_krb_error_table();
85 set_com_err_hook(sms_com_err);
89 com_err(whoami, 0, "Usage: smsd");
93 /* Profiling implies that getting rid of one level of call
94 * indirection here wins us maybe 1% on the VAX.
100 * GDB initialization.
102 if(gdb_init() != 0) {
103 com_err(whoami, 0, "GDB initialization failed.");
106 gdb_debug(0); /* this can be patched, if necessary, to enable */
107 /* GDB level debugging .. */
108 krb_realm = malloc(REALM_SZ);
109 krb_get_lrealm(krb_realm, 1);
112 * Database initialization. Only init if database should be open.
115 if (stat(SMS_MOTD_FILE, &stbuf) != 0) {
116 if ((status = sms_open_database()) != 0) {
117 com_err(whoami, status, " when trying to open database.");
120 sanity_check_database();
123 com_err(whoami, 0, "sleeping, not opening database");
126 sanity_check_queries();
129 * Set up client array handler.
132 clients = (client **) malloc(0);
136 journal = fopen(JOURNAL, "a");
137 if (journal == NULL) {
138 com_err(whoami, errno, " while opening journal file");
143 * Establish template connection.
145 if ((status = do_listen()) != 0) {
146 com_err(whoami, status,
147 " while trying to create listening connection");
151 op_list = create_list_of_operations(1, listenop);
153 com_err(whoami, 0, "started (pid %d)", getpid());
154 com_err(whoami, 0, rcsid_sms_main_c);
155 if (dormant != ASLEEP)
156 send_zgram("SMS", "server started");
158 send_zgram("SMS", "server started, but database closed");
161 * Run until shut down.
166 * Block until something happens.
169 com_err(whoami, 0, "tick");
171 if (dormant == SLEEPY) {
172 sms_close_database();
173 com_err(whoami, 0, "database closed");
175 send_zgram("SMS", "database closed");
177 } else if (dormant == GROGGY) {
179 com_err(whoami, 0, "database open");
181 send_zgram("SMS", "database open again");
186 status = op_select_any(op_list, 0,
187 (fd_set *)NULL, (fd_set *)NULL,
188 (fd_set *)NULL, (struct timeval *)NULL);
192 com_err(whoami, errno, " error from op_select");
194 } else if (status != -2) {
195 com_err(whoami, 0, " wrong return from op_select_any");
201 fprintf(stderr, " tick\n");
204 * Handle any new connections; this comes first so
205 * errno isn't tromped on.
207 if (OP_DONE(listenop)) {
208 if (OP_STATUS(listenop) == OP_CANCELLED) {
209 if (errno == EWOULDBLOCK) {
212 com_err(whoami, errno,
216 } else if ((status = new_connection()) != 0) {
217 com_err(whoami, errno,
218 " Error on listening operation.");
220 * Sleep here to prevent hosing?
223 /* if the new connection is our only connection,
224 * and the server is supposed to be down, then go
227 if ((dormant == AWAKE) && (nclients == 1) &&
228 (stat(SMS_MOTD_FILE, &stbuf) == 0)) {
229 com_err(whoami, 0, "motd file exists, slumbertime");
232 /* on new connection, if we are no longer supposed
233 * to be down, then wake up.
235 if ((dormant == ASLEEP) &&
236 (stat(SMS_MOTD_FILE, &stbuf) == -1) &&
238 com_err(whoami, 0, "motd file no longer exists, waking up");
244 * Handle any existing connections.
248 for (i=0; i<nclients; i++) {
249 cur_client = clients[i];
250 if (OP_DONE(clients[i]->pending_op)) {
251 cur_client->last_time_used = now;
252 do_client(cur_client);
253 } else if (clients[i]->last_time_used < tardy) {
254 com_err(whoami, 0, "Shutting down connection due to inactivity");
255 shutdown(cur_client->con->in.fd, 0);
261 com_err(whoami, 0, "%s", takedown);
262 sms_close_database();
263 send_zgram("SMS", takedown);
268 * Set up the template connection and queue the first accept.
274 char *service = index(SMS_GDB_SERV, ':') + 1;
276 listencon = create_listening_connection(service);
278 if (listencon == NULL)
281 listenop = create_operation();
282 client_addrlen = sizeof(client_addr);
284 start_accepting_client(listencon, listenop, &newconn,
285 (char *)&client_addr,
286 &client_addrlen, &client_tuple);
293 client_addrlen = sizeof(client_addr);
294 start_accepting_client(listencon, listenop, &newconn,
295 (char *)&client_addr,
296 &client_addrlen, &client_tuple);
300 * This routine is called when a new connection comes in.
302 * It sets up a new client and adds it to the list of currently active clients.
311 * Make sure there's been no error
313 if(OP_STATUS(listenop) != OP_COMPLETE) {
317 if (newconn == NULL) {
318 return SMS_NOT_CONNECTED;
322 * Set up the new connection and reply to the client
324 cp = (client *)malloc(sizeof *cp);
325 bzero(cp, sizeof(*cp));
326 cp->action = CL_ACCEPT;
330 cp->clname[0] = NULL;
331 cp->reply.sms_argv = NULL;
334 cp->last_time_used = now;
337 cp->pending_op = create_operation();
338 reset_operation(cp->pending_op);
339 oplist_append(&op_list, cp->pending_op);
343 * Add a new client to the array..
348 * Let him know we heard him.
350 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
353 cp->haddr = client_addr;
356 * Log new connection.
359 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
360 inet_ntoa(cp->haddr.sin_addr),
361 (int)ntohs(cp->haddr.sin_port),
366 * Get ready to accept the next connection.
368 reset_operation(listenop);
369 client_addrlen = sizeof(client_addr);
371 start_accepting_client(listencon, listenop, &newconn,
372 (char *)&client_addr,
373 &client_addrlen, &client_tuple);
378 * Add a new client to the known clients.
387 clients_n = (client **)malloc
388 ((unsigned)(nclients * sizeof(client *)));
389 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
390 clients_n[nclients-1] = cp;
391 free((char *)clients);
401 client **clients_n, **scpp, **dcpp; /* source and dest client */
406 clients_n = (client **)malloc
407 ((unsigned)((nclients - 1)* sizeof(client *)));
408 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
413 if (found_it) abort();
418 free((char *)clients);
421 oplist_delete(op_list, cp->pending_op);
422 reset_operation(cp->pending_op);
423 delete_operation(cp->pending_op);
424 sever_connection(cp->con);
429 * Add a new operation to a list of operations.
431 * This should be rewritten to use realloc instead, since in most
432 * cases it won't have to copy the array.
436 oplist_append(oplp, op)
437 LIST_OF_OPERATIONS *oplp;
440 int count = (*oplp)->count+1;
441 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
442 db_alloc(size_of_list_of_operations(count));
443 bcopy((char *)(*oplp), (char *)newlist,
444 size_of_list_of_operations((*oplp)->count));
446 newlist->op[count-1] = op;
447 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
452 oplist_delete(oplp, op)
453 LIST_OF_OPERATIONS oplp;
454 register OPERATION op;
456 register OPERATION *s;
459 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
479 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
480 if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0))
481 com_err(whoami, 0, "%d: child exits with signal %d status %d",
482 pid, status.w_termsig, status.w_retcode);
492 com_err(whoami, 0, "requested to go dormant");
495 com_err(whoami, 0, "already asleep");
509 com_err(whoami, 0, "Good morning");
512 com_err(whoami, 0, "already awake");
523 /* There should probably be a few more of these. */
525 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
526 (((int)signal (SIGCHLD, reapchild)) < 0) ||
527 (((int)signal (SIGUSR1, godormant)) < 0) ||
528 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
529 (((int)signal (SIGHUP, sigshut)) < 0)) {
530 com_err(whoami, errno, " Unable to establish signal handlers.");