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.
17 * Revision 1.13 1987-08-04 02:40:30 wesommer
18 * Do end run around minor hotspot.
20 * Revision 1.12 87/08/04 01:50:00 wesommer
21 * Rearranged messages.
23 * Revision 1.11 87/07/29 16:04:54 wesommer
24 * Add keepalive feature.
26 * Revision 1.10 87/06/30 20:02:26 wesommer
27 * Added returned tuple chain to client structure.
28 * Added local realm global variable.
30 * Revision 1.9 87/06/21 16:39:54 wesommer
31 * Performance work, rearrangement of include files.
33 * Revision 1.8 87/06/09 18:44:45 wesommer
34 * modified error handling.
36 * Revision 1.7 87/06/08 02:44:44 wesommer
39 * Revision 1.6 87/06/03 17:41:00 wesommer
40 * Added startup support.
42 * Revision 1.5 87/06/03 16:07:17 wesommer
45 * Revision 1.4 87/06/02 20:05:11 wesommer
46 * Bug fixes on memory allocation.
48 * Revision 1.3 87/06/01 04:34:27 wesommer
49 * Changed returned error code.
51 * Revision 1.2 87/06/01 03:34:53 wesommer
52 * Added shutdown, logging.
54 * Revision 1.1 87/05/31 22:06:56 wesommer
59 static char *rcsid_sms_main_c = "$Header$";
62 #include <sys/errno.h>
63 #include <sys/signal.h>
64 #include "sms_server.h"
66 extern CONNECTION newconn, listencon;
69 extern client **clients, *cur_client;
71 extern OPERATION listenop;
72 extern LIST_OF_OPERATIONS op_list;
74 extern struct sockaddr_in client_addr;
75 extern int client_addrlen;
76 extern TUPLE client_tuple;
79 extern char buf1[BUFSIZ];
80 extern char *takedown;
84 extern char *malloc();
86 extern char *inet_ntoa();
87 extern void sms_com_err();
88 extern void do_client();
93 extern u_short ntohs();
98 * Main SMS server loop.
100 * Initialize the world, then start accepting connections and
101 * making progress on current connections.
115 * Error handler init.
119 set_com_err_hook(sms_com_err);
123 com_err(whoami, 0, "Usage: smsd");
127 /* Profiling implies that getting rid of one level of call
128 * indirection here wins us maybe 1% on the VAX.
134 * GDB initialization.
136 if(gdb_init() != 0) {
137 com_err(whoami, 0, "GDB initialization failed.");
140 gdb_debug(0); /* this can be patched, if necessary, to enable */
141 /* GDB level debugging .. */
142 krb_realm = malloc(REALM_SZ);
143 get_krbrlm(krb_realm, 1);
146 * Database initialization.
149 if ((status = sms_open_database()) != 0) {
150 com_err(whoami, status, "when trying to open database.");
155 * Set up client array handler.
158 clients = (client **) malloc(0);
162 * There should probably be a few more of these.
165 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
166 (((int)signal (SIGHUP, sigshut)) < 0)) {
167 com_err(whoami, errno, "Unable to establish signal handler.");
172 * Establish template connection.
174 if ((status = do_listen()) != 0) {
175 com_err(whoami, status,
176 "while trying to create listening connection");
180 op_list = create_list_of_operations(1, listenop);
182 com_err(whoami, 0, "started (pid %d)", getpid());
183 com_err(whoami, 0, rcsid_sms_main_c);
186 * Run until shut down.
191 * Block until something happens.
194 com_err(whoami, 0, "tick");
197 status = op_select_any(op_list, 0,
198 (fd_set *)NULL, (fd_set *)NULL,
199 (fd_set *)NULL, (struct timeval *)NULL);
202 com_err(whoami, errno, "error from op_select");
204 } else if (status != -2) {
205 com_err(whoami, 0, "wrong return from op_select_any");
211 fprintf(stderr, " tick\n");
214 * Handle any new connections; this comes first so
215 * errno isn't tromped on.
217 if (OP_DONE(listenop)) {
218 if (OP_STATUS(listenop) == OP_CANCELLED) {
219 if (errno == EWOULDBLOCK) {
222 com_err(whoami, errno,
226 } else if ((status = new_connection()) != 0) {
227 com_err(whoami, errno,
228 "Error on listening operation.");
230 * Sleep here to prevent hosing?
235 * Handle any existing connections.
239 for (i=0; i<nclients; i++) {
240 cur_client = clients[i];
241 if (OP_DONE(clients[i]->pending_op)) {
242 cur_client->last_time_used = now;
243 do_client(cur_client);
244 } else if (clients[i]->last_time_used < tardy) {
245 com_err(whoami, 0, "Shutting down connection due to inactivity");
246 shutdown(cur_client->con->in.fd, 0);
252 com_err(whoami, 0, "%s", takedown);
253 sms_close_database();
258 * Set up the template connection and queue the first accept.
264 char *service = index(SMS_GDB_SERV, ':') + 1;
266 listencon = create_listening_connection(service);
268 if (listencon == NULL)
271 listenop = create_operation();
272 client_addrlen = sizeof(client_addr);
274 start_accepting_client(listencon, listenop, &newconn,
275 (char *)&client_addr,
276 &client_addrlen, &client_tuple);
283 client_addrlen = sizeof(client_addr);
284 start_accepting_client(listencon, listenop, &newconn,
285 (char *)&client_addr,
286 &client_addrlen, &client_tuple);
290 * This routine is called when a new connection comes in.
292 * It sets up a new client and adds it to the list of currently active clients.
301 * Make sure there's been no error
303 if(OP_STATUS(listenop) != OP_COMPLETE) {
307 if (newconn == NULL) {
308 return SMS_NOT_CONNECTED;
312 * Set up the new connection and reply to the client
314 cp = (client *)malloc(sizeof *cp);
315 cp->state = CL_STARTING;
316 cp->action = CL_ACCEPT;
321 cp->reply.sms_argv = NULL;
324 cp->last_time_used = now;
327 cp->pending_op = create_operation();
328 reset_operation(cp->pending_op);
329 oplist_append(&op_list, cp->pending_op);
333 * Add a new client to the array..
338 * Let him know we heard him.
340 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
343 cp->haddr = client_addr;
346 * Log new connection.
349 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
350 inet_ntoa(cp->haddr.sin_addr),
351 (int)ntohs(cp->haddr.sin_port),
356 * Get ready to accept the next connection.
358 reset_operation(listenop);
359 client_addrlen = sizeof(client_addr);
361 start_accepting_client(listencon, listenop, &newconn,
362 (char *)&client_addr,
363 &client_addrlen, &client_tuple);
368 * Add a new client to the known clients.
377 clients_n = (client **)malloc
378 ((unsigned)(nclients * sizeof(client *)));
379 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
380 clients_n[nclients-1] = cp;
381 free((char *)clients);
391 client **clients_n, **scpp, **dcpp; /* source and dest client */
396 clients_n = (client **)malloc
397 ((unsigned)((nclients - 1)* sizeof(client *)));
398 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
403 if (found_it) abort();
408 free((char *)clients);
411 oplist_delete(op_list, cp->pending_op);
412 reset_operation(cp->pending_op);
413 delete_operation(cp->pending_op);
414 sever_connection(cp->con);
419 * Add a new operation to a list of operations.
421 * This should be rewritten to use realloc instead, since in most
422 * cases it won't have to copy the array.
426 oplist_append(oplp, op)
427 LIST_OF_OPERATIONS *oplp;
430 int count = (*oplp)->count+1;
431 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
432 db_alloc(size_of_list_of_operations(count));
433 bcopy((char *)(*oplp), (char *)newlist,
434 size_of_list_of_operations((*oplp)->count));
436 newlist->op[count-1] = op;
437 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
442 oplist_delete(oplp, op)
443 LIST_OF_OPERATIONS oplp;
444 register OPERATION op;
446 register OPERATION *s;
449 for (s = oplp->op, c=oplp->count; c; --c, ++s) {