6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
11 * Most of this is stolen from ../gdb/tsr.c
13 * You are in a maze of twisty little finite automata, all different.
14 * Let the reader beware.
18 static char *rcsid_sms_main_c = "$Header$";
21 #include <sys/errno.h>
22 #include <sys/signal.h>
23 #include "sms_server.h"
25 extern CONNECTION newconn, listencon;
28 extern client **clients, *cur_client;
30 extern OPERATION listenop;
31 extern LIST_OF_OPERATIONS op_list;
33 extern struct sockaddr_in client_addr;
34 extern int client_addrlen;
35 extern TUPLE client_tuple;
38 extern char buf1[BUFSIZ];
39 extern char *takedown;
42 #define JOURNAL "/u1/sms/journal"
44 extern char *malloc();
46 extern char *inet_ntoa();
47 extern void sms_com_err();
48 extern void do_client();
57 * Main SMS server loop.
59 * Initialize the world, then start accepting connections and
60 * making progress on current connections.
78 set_com_err_hook(sms_com_err);
82 com_err(whoami, 0, "Usage: smsd");
86 /* Profiling implies that getting rid of one level of call
87 * indirection here wins us maybe 1% on the VAX.
96 com_err(whoami, 0, "GDB initialization failed.");
99 gdb_debug(0); /* this can be patched, if necessary, to enable */
100 /* GDB level debugging .. */
101 krb_realm = malloc(REALM_SZ);
102 get_krbrlm(krb_realm, 1);
105 * Database initialization.
108 if ((status = sms_open_database()) != 0) {
109 com_err(whoami, status, " when trying to open database.");
113 sanity_check_queries();
114 sanity_check_database();
117 * Set up client array handler.
120 clients = (client **) malloc(0);
124 * There should probably be a few more of these.
127 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
128 (((int)signal (SIGHUP, sigshut)) < 0)) {
129 com_err(whoami, errno, " Unable to establish signal handler.");
133 journal = fopen(JOURNAL, "a");
134 if (journal == NULL) {
135 com_err(whoami, errno, " while opening journal file");
140 * Establish template connection.
142 if ((status = do_listen()) != 0) {
143 com_err(whoami, status,
144 " while trying to create listening connection");
148 op_list = create_list_of_operations(1, listenop);
150 com_err(whoami, 0, "started (pid %d)", getpid());
151 com_err(whoami, 0, rcsid_sms_main_c);
152 send_zgram("SMS", "server started");
155 * Run until shut down.
160 * Block until something happens.
163 com_err(whoami, 0, "tick");
166 status = op_select_any(op_list, 0,
167 (fd_set *)NULL, (fd_set *)NULL,
168 (fd_set *)NULL, (struct timeval *)NULL);
171 com_err(whoami, errno, " error from op_select");
173 } else if (status != -2) {
174 com_err(whoami, 0, " wrong return from op_select_any");
180 fprintf(stderr, " tick\n");
183 * Handle any new connections; this comes first so
184 * errno isn't tromped on.
186 if (OP_DONE(listenop)) {
187 if (OP_STATUS(listenop) == OP_CANCELLED) {
188 if (errno == EWOULDBLOCK) {
191 com_err(whoami, errno,
195 } else if ((status = new_connection()) != 0) {
196 com_err(whoami, errno,
197 " Error on listening operation.");
199 * Sleep here to prevent hosing?
204 * Handle any existing connections.
208 for (i=0; i<nclients; i++) {
209 cur_client = clients[i];
210 if (OP_DONE(clients[i]->pending_op)) {
211 cur_client->last_time_used = now;
212 do_client(cur_client);
213 } else if (clients[i]->last_time_used < tardy) {
214 com_err(whoami, 0, "Shutting down connection due to inactivity");
215 shutdown(cur_client->con->in.fd, 0);
221 com_err(whoami, 0, "%s", takedown);
222 sms_close_database();
223 send_zgram("SMS", takedown);
228 * Set up the template connection and queue the first accept.
234 char *service = index(SMS_GDB_SERV, ':') + 1;
236 listencon = create_listening_connection(service);
238 if (listencon == NULL)
241 listenop = create_operation();
242 client_addrlen = sizeof(client_addr);
244 start_accepting_client(listencon, listenop, &newconn,
245 (char *)&client_addr,
246 &client_addrlen, &client_tuple);
253 client_addrlen = sizeof(client_addr);
254 start_accepting_client(listencon, listenop, &newconn,
255 (char *)&client_addr,
256 &client_addrlen, &client_tuple);
260 * This routine is called when a new connection comes in.
262 * It sets up a new client and adds it to the list of currently active clients.
271 * Make sure there's been no error
273 if(OP_STATUS(listenop) != OP_COMPLETE) {
277 if (newconn == NULL) {
278 return SMS_NOT_CONNECTED;
282 * Set up the new connection and reply to the client
284 cp = (client *)malloc(sizeof *cp);
285 bzero(cp, sizeof(*cp));
286 cp->state = CL_STARTING;
287 cp->action = CL_ACCEPT;
292 cp->reply.sms_argv = NULL;
295 cp->last_time_used = now;
298 cp->pending_op = create_operation();
299 reset_operation(cp->pending_op);
300 oplist_append(&op_list, cp->pending_op);
304 * Add a new client to the array..
309 * Let him know we heard him.
311 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
314 cp->haddr = client_addr;
317 * Log new connection.
320 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
321 inet_ntoa(cp->haddr.sin_addr),
322 (int)ntohs(cp->haddr.sin_port),
327 * Get ready to accept the next connection.
329 reset_operation(listenop);
330 client_addrlen = sizeof(client_addr);
332 start_accepting_client(listencon, listenop, &newconn,
333 (char *)&client_addr,
334 &client_addrlen, &client_tuple);
339 * Add a new client to the known clients.
348 clients_n = (client **)malloc
349 ((unsigned)(nclients * sizeof(client *)));
350 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
351 clients_n[nclients-1] = cp;
352 free((char *)clients);
362 client **clients_n, **scpp, **dcpp; /* source and dest client */
367 clients_n = (client **)malloc
368 ((unsigned)((nclients - 1)* sizeof(client *)));
369 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
374 if (found_it) abort();
379 free((char *)clients);
382 oplist_delete(op_list, cp->pending_op);
383 reset_operation(cp->pending_op);
384 delete_operation(cp->pending_op);
385 sever_connection(cp->con);
390 * Add a new operation to a list of operations.
392 * This should be rewritten to use realloc instead, since in most
393 * cases it won't have to copy the array.
397 oplist_append(oplp, op)
398 LIST_OF_OPERATIONS *oplp;
401 int count = (*oplp)->count+1;
402 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
403 db_alloc(size_of_list_of_operations(count));
404 bcopy((char *)(*oplp), (char *)newlist,
405 size_of_list_of_operations((*oplp)->count));
407 newlist->op[count-1] = op;
408 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
413 oplist_delete(oplp, op)
414 LIST_OF_OPERATIONS oplp;
415 register OPERATION op;
417 register OPERATION *s;
420 for (s = oplp->op, c=oplp->count; c; --c, ++s) {