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;
48 #define JOURNAL "/u1/sms/journal"
50 extern char *malloc();
52 extern char *inet_ntoa();
53 extern void sms_com_err();
54 extern void do_client();
59 void reapchild(), godormant(), gowakeup();
64 * Main SMS server loop.
66 * Initialize the world, then start accepting connections and
67 * making progress on current connections.
84 initialize_sms_error_table();
85 initialize_krb_error_table();
86 set_com_err_hook(sms_com_err);
90 com_err(whoami, 0, "Usage: smsd");
94 /* Profiling implies that getting rid of one level of call
95 * indirection here wins us maybe 1% on the VAX.
101 * GDB initialization.
103 if(gdb_init() != 0) {
104 com_err(whoami, 0, "GDB initialization failed.");
107 gdb_debug(0); /* this can be patched, if necessary, to enable */
108 /* GDB level debugging .. */
109 krb_realm = malloc(REALM_SZ);
110 krb_get_lrealm(krb_realm, 1);
113 * Database initialization. Only init if database should be open.
116 if (stat(SMS_MOTD_FILE, &stbuf) != 0) {
117 if ((status = sms_open_database()) != 0) {
118 com_err(whoami, status, " when trying to open database.");
121 sanity_check_database();
124 com_err(whoami, 0, "sleeping, not opening database");
127 sanity_check_queries();
130 * Set up client array handler.
133 clients = (client **) malloc(0);
137 journal = fopen(JOURNAL, "a");
138 if (journal == NULL) {
139 com_err(whoami, errno, " while opening journal file");
144 * Establish template connection.
146 if ((status = do_listen()) != 0) {
147 com_err(whoami, status,
148 " while trying to create listening connection");
152 op_list = create_list_of_operations(1, listenop);
154 com_err(whoami, 0, "started (pid %d)", getpid());
155 com_err(whoami, 0, rcsid_sms_main_c);
156 if (dormant != ASLEEP)
157 send_zgram("SMS", "server started");
159 send_zgram("SMS", "server started, but database closed");
162 * Run until shut down.
167 * Block until something happens.
170 com_err(whoami, 0, "tick");
172 if (dormant == SLEEPY) {
173 sms_close_database();
174 com_err(whoami, 0, "database closed");
176 send_zgram("SMS", "database closed");
178 } else if (dormant == GROGGY) {
180 com_err(whoami, 0, "database open");
182 send_zgram("SMS", "database open again");
187 status = op_select_any(op_list, 0,
188 (fd_set *)NULL, (fd_set *)NULL,
189 (fd_set *)NULL, (struct timeval *)NULL);
193 com_err(whoami, errno, " error from op_select");
195 } else if (status != -2) {
196 com_err(whoami, 0, " wrong return from op_select_any");
202 fprintf(stderr, " tick\n");
205 * Handle any new connections; this comes first so
206 * errno isn't tromped on.
208 if (OP_DONE(listenop)) {
209 if (OP_STATUS(listenop) == OP_CANCELLED) {
210 if (errno == EWOULDBLOCK) {
213 com_err(whoami, errno,
217 } else if ((status = new_connection()) != 0) {
218 com_err(whoami, errno,
219 " Error on listening operation.");
221 * Sleep here to prevent hosing?
224 /* if the new connection is our only connection,
225 * and the server is supposed to be down, then go
228 if ((dormant == AWAKE) && (nclients == 1) &&
229 (stat(SMS_MOTD_FILE, &stbuf) == 0)) {
230 com_err(whoami, 0, "motd file exists, slumbertime");
233 /* on new connection, if we are no longer supposed
234 * to be down, then wake up.
236 if ((dormant == ASLEEP) &&
237 (stat(SMS_MOTD_FILE, &stbuf) == -1) &&
239 com_err(whoami, 0, "motd file no longer exists, waking up");
245 * Handle any existing connections.
249 for (i=0; i<nclients; i++) {
250 cur_client = clients[i];
251 if (OP_DONE(clients[i]->pending_op)) {
252 cur_client->last_time_used = now;
253 do_client(cur_client);
254 } else if (clients[i]->last_time_used < tardy) {
255 com_err(whoami, 0, "Shutting down connection due to inactivity");
256 shutdown(cur_client->con->in.fd, 0);
262 com_err(whoami, 0, "%s", takedown);
263 sms_close_database();
264 send_zgram("SMS", takedown);
269 * Set up the template connection and queue the first accept.
275 char *service = index(SMS_GDB_SERV, ':') + 1;
277 listencon = create_listening_connection(service);
279 if (listencon == NULL)
282 listenop = create_operation();
283 client_addrlen = sizeof(client_addr);
285 start_accepting_client(listencon, listenop, &newconn,
286 (char *)&client_addr,
287 &client_addrlen, &client_tuple);
294 client_addrlen = sizeof(client_addr);
295 start_accepting_client(listencon, listenop, &newconn,
296 (char *)&client_addr,
297 &client_addrlen, &client_tuple);
301 * This routine is called when a new connection comes in.
303 * It sets up a new client and adds it to the list of currently active clients.
312 * Make sure there's been no error
314 if(OP_STATUS(listenop) != OP_COMPLETE) {
318 if (newconn == NULL) {
319 return SMS_NOT_CONNECTED;
323 * Set up the new connection and reply to the client
325 cp = (client *)malloc(sizeof *cp);
326 bzero(cp, sizeof(*cp));
327 cp->action = CL_ACCEPT;
331 cp->clname[0] = NULL;
332 cp->reply.sms_argv = NULL;
335 cp->last_time_used = now;
338 cp->pending_op = create_operation();
339 reset_operation(cp->pending_op);
340 oplist_append(&op_list, cp->pending_op);
344 * Add a new client to the array..
349 * Let him know we heard him.
351 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
354 cp->haddr = client_addr;
357 * Log new connection.
360 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
361 inet_ntoa(cp->haddr.sin_addr),
362 (int)ntohs(cp->haddr.sin_port),
367 * Get ready to accept the next connection.
369 reset_operation(listenop);
370 client_addrlen = sizeof(client_addr);
372 start_accepting_client(listencon, listenop, &newconn,
373 (char *)&client_addr,
374 &client_addrlen, &client_tuple);
379 * Add a new client to the known clients.
388 clients_n = (client **)malloc
389 ((unsigned)(nclients * sizeof(client *)));
390 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
391 clients_n[nclients-1] = cp;
392 free((char *)clients);
402 client **clients_n, **scpp, **dcpp; /* source and dest client */
407 clients_n = (client **)malloc
408 ((unsigned)((nclients - 1)* sizeof(client *)));
409 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
414 if (found_it) abort();
419 free((char *)clients);
422 oplist_delete(op_list, cp->pending_op);
423 reset_operation(cp->pending_op);
424 delete_operation(cp->pending_op);
425 sever_connection(cp->con);
430 * Add a new operation to a list of operations.
432 * This should be rewritten to use realloc instead, since in most
433 * cases it won't have to copy the array.
437 oplist_append(oplp, op)
438 LIST_OF_OPERATIONS *oplp;
441 int count = (*oplp)->count+1;
442 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
443 db_alloc(size_of_list_of_operations(count));
444 bcopy((char *)(*oplp), (char *)newlist,
445 size_of_list_of_operations((*oplp)->count));
447 newlist->op[count-1] = op;
448 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
453 oplist_delete(oplp, op)
454 LIST_OF_OPERATIONS oplp;
455 register OPERATION op;
457 register OPERATION *s;
460 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
480 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
481 if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0))
482 com_err(whoami, 0, "%d: child exits with signal %d status %d",
483 pid, status.w_termsig, status.w_retcode);
493 com_err(whoami, 0, "requested to go dormant");
496 com_err(whoami, 0, "already asleep");
510 com_err(whoami, 0, "Good morning");
513 com_err(whoami, 0, "already awake");
524 /* There should probably be a few more of these. */
526 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
527 (((int)signal (SIGCHLD, reapchild)) < 0) ||
528 (((int)signal (SIGUSR1, godormant)) < 0) ||
529 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
530 (((int)signal (SIGHUP, sigshut)) < 0)) {
531 com_err(whoami, errno, " Unable to establish signal handlers.");