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.12 1987-08-04 01:50:00 wesommer
18 * Rearranged messages.
20 * Revision 1.11 87/07/29 16:04:54 wesommer
21 * Add keepalive feature.
23 * Revision 1.10 87/06/30 20:02:26 wesommer
24 * Added returned tuple chain to client structure.
25 * Added local realm global variable.
27 * Revision 1.9 87/06/21 16:39:54 wesommer
28 * Performance work, rearrangement of include files.
30 * Revision 1.8 87/06/09 18:44:45 wesommer
31 * modified error handling.
33 * Revision 1.7 87/06/08 02:44:44 wesommer
36 * Revision 1.6 87/06/03 17:41:00 wesommer
37 * Added startup support.
39 * Revision 1.5 87/06/03 16:07:17 wesommer
42 * Revision 1.4 87/06/02 20:05:11 wesommer
43 * Bug fixes on memory allocation.
45 * Revision 1.3 87/06/01 04:34:27 wesommer
46 * Changed returned error code.
48 * Revision 1.2 87/06/01 03:34:53 wesommer
49 * Added shutdown, logging.
51 * Revision 1.1 87/05/31 22:06:56 wesommer
56 static char *rcsid_sms_main_c = "$Header$";
59 #include <sys/errno.h>
60 #include <sys/signal.h>
61 #include "sms_server.h"
63 extern CONNECTION newconn, listencon;
66 extern client **clients, *cur_client;
68 extern OPERATION listenop;
69 extern LIST_OF_OPERATIONS op_list;
71 extern struct sockaddr_in client_addr;
72 extern int client_addrlen;
73 extern TUPLE client_tuple;
76 extern char buf1[BUFSIZ];
77 extern char *takedown;
81 extern char *malloc();
82 extern char *inet_ntoa();
83 extern void sms_com_err();
84 extern void do_client();
89 extern u_short ntohs();
94 * Main SMS server loop.
96 * Initialize the world, then start accepting connections and
97 * making progress on current connections.
111 * Error handler init.
115 set_com_err_hook(sms_com_err);
119 com_err(whoami, 0, "Usage: smsd");
124 * GDB initialization.
126 if(gdb_init() != 0) {
127 com_err(whoami, 0, "GDB initialization failed.");
130 gdb_debug(0); /* this can be patched, if necessary, to enable */
131 /* GDB level debugging .. */
132 krb_realm = malloc(REALM_SZ);
133 get_krbrlm(krb_realm, 1);
136 * Database initialization.
139 if ((status = sms_open_database()) != 0) {
140 com_err(whoami, status, "when trying to open database.");
145 * Set up client array handler.
148 clients = (client **) malloc(0);
152 * There should probably be a few more of these.
155 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
156 (((int)signal (SIGHUP, sigshut)) < 0)) {
157 com_err(whoami, errno, "Unable to establish signal handler.");
162 * Establish template connection.
164 if ((status = do_listen()) != 0) {
165 com_err(whoami, status,
166 "while trying to create listening connection");
170 op_list = create_list_of_operations(1, listenop);
172 com_err(whoami, 0, "started (pid %d)", getpid());
173 com_err(whoami, 0, rcsid_sms_main_c);
176 * Run until shut down.
181 * Block until something happens.
184 com_err(whoami, 0, "tick");
187 status = op_select_any(op_list, 0,
188 (fd_set *)NULL, (fd_set *)NULL,
189 (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?
225 * Handle any existing connections.
229 for (i=0; i<nclients; i++) {
230 cur_client = clients[i];
231 if (OP_DONE(clients[i]->pending_op)) {
232 cur_client->last_time_used = now;
233 do_client(cur_client);
234 } else if (clients[i]->last_time_used < tardy) {
235 com_err(whoami, 0, "Shutting down connection due to inactivity");
236 shutdown(cur_client->con->in.fd, 0);
242 com_err(whoami, 0, "%s", takedown);
243 sms_close_database();
248 * Set up the template connection and queue the first accept.
254 char *service = index(SMS_GDB_SERV, ':') + 1;
256 listencon = create_listening_connection(service);
258 if (listencon == NULL)
261 listenop = create_operation();
262 client_addrlen = sizeof(client_addr);
264 start_accepting_client(listencon, listenop, &newconn,
265 (char *)&client_addr,
266 &client_addrlen, &client_tuple);
273 client_addrlen = sizeof(client_addr);
274 start_accepting_client(listencon, listenop, &newconn,
275 (char *)&client_addr,
276 &client_addrlen, &client_tuple);
280 * This routine is called when a new connection comes in.
282 * It sets up a new client and adds it to the list of currently active clients.
291 * Make sure there's been no error
293 if(OP_STATUS(listenop) != OP_COMPLETE) {
297 if (newconn == NULL) {
298 return SMS_NOT_CONNECTED;
302 * Set up the new connection and reply to the client
304 cp = (client *)malloc(sizeof *cp);
305 cp->state = CL_STARTING;
306 cp->action = CL_ACCEPT;
311 cp->reply.sms_argv = NULL;
314 cp->last_time_used = now;
317 cp->pending_op = create_operation();
318 reset_operation(cp->pending_op);
319 oplist_append(&op_list, cp->pending_op);
323 * Add a new client to the array..
328 * Let him know we heard him.
330 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
333 cp->haddr = client_addr;
336 * Log new connection.
339 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
340 inet_ntoa(cp->haddr.sin_addr),
341 (int)ntohs(cp->haddr.sin_port),
346 * Get ready to accept the next connection.
348 reset_operation(listenop);
349 client_addrlen = sizeof(client_addr);
351 start_accepting_client(listencon, listenop, &newconn,
352 (char *)&client_addr,
353 &client_addrlen, &client_tuple);
358 * Add a new client to the known clients.
367 clients_n = (client **)malloc
368 ((unsigned)(nclients * sizeof(client *)));
369 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
370 clients_n[nclients-1] = cp;
371 free((char *)clients);
381 client **clients_n, **scpp, **dcpp; /* source and dest client */
386 clients_n = (client **)malloc
387 ((unsigned)((nclients - 1)* sizeof(client *)));
388 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
393 if (found_it) abort();
398 free((char *)clients);
401 oplist_delete(op_list, cp->pending_op);
402 reset_operation(cp->pending_op);
403 delete_operation(cp->pending_op);
404 sever_connection(cp->con);
409 * Add a new operation to a list of operations.
411 * This should be rewritten to use realloc instead, since in most
412 * cases it won't have to copy the array.
416 oplist_append(oplp, op)
417 LIST_OF_OPERATIONS *oplp;
420 int count = (*oplp)->count+1;
421 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
422 db_alloc(size_of_list_of_operations(count));
423 bcopy((char *)(*oplp), (char *)newlist,
424 size_of_list_of_operations((*oplp)->count));
426 newlist->op[count-1] = op;
427 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
432 oplist_delete(oplp, op)
433 LIST_OF_OPERATIONS oplp;
434 register OPERATION op;
436 register OPERATION *s;
439 for (s = oplp->op, c=oplp->count; c; --c, ++s) {