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.9 1987-06-21 16:39:54 wesommer
18 * Performance work, rearrangement of include files.
20 * Revision 1.8 87/06/09 18:44:45 wesommer
21 * modified error handling.
23 * Revision 1.7 87/06/08 02:44:44 wesommer
26 * Revision 1.6 87/06/03 17:41:00 wesommer
27 * Added startup support.
29 * Revision 1.5 87/06/03 16:07:17 wesommer
32 * Revision 1.4 87/06/02 20:05:11 wesommer
33 * Bug fixes on memory allocation.
35 * Revision 1.3 87/06/01 04:34:27 wesommer
36 * Changed returned error code.
38 * Revision 1.2 87/06/01 03:34:53 wesommer
39 * Added shutdown, logging.
41 * Revision 1.1 87/05/31 22:06:56 wesommer
46 static char *rcsid_sms_main_c = "$Header$";
49 #include <sys/errno.h>
50 #include <sys/signal.h>
51 #include "sms_server.h"
53 extern CONNECTION newconn, listencon;
56 extern client **clients, *cur_client;
58 extern OPERATION listenop;
59 extern LIST_OF_OPERATIONS op_list;
61 extern struct sockaddr_in client_addr;
62 extern int client_addrlen;
63 extern TUPLE client_tuple;
66 extern char buf1[BUFSIZ];
67 extern char *takedown;
71 extern char *malloc();
72 extern char *inet_ntoa();
73 extern void sms_com_err();
74 extern void do_client();
79 extern u_short ntohs();
82 * Main SMS server loop.
84 * Initialize the world, then start accepting connections and
85 * making progress on current connections.
102 set_com_err_hook(sms_com_err);
105 com_err(whoami, 0, "Usage: smsd");
110 * GDB initialization.
112 if(gdb_init() != 0) {
113 com_err(whoami, 0, "GDB initialization failed.");
116 gdb_debug(0); /* this can be patched, if necessary, to enable */
117 /* GDB level debugging .. */
120 * Database initialization.
123 if ((status = sms_open_database()) != 0) {
124 com_err(whoami, status, "when trying to open database.");
129 * Set up client array handler.
132 clients = (client **) malloc(0);
136 * There should probably be a few more of these.
139 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
140 (((int)signal (SIGHUP, sigshut)) < 0)) {
141 com_err(whoami, errno, "Unable to establish signal handler.");
146 * Establish template connection.
148 if ((status = do_listen()) != 0) {
149 com_err(whoami, status,
150 "while trying to create listening connection");
154 op_list = create_list_of_operations(1, listenop);
156 (void) sprintf(buf1, "started (pid %d)", getpid());
157 com_err(whoami, 0, buf1);
158 com_err(whoami, 0, rcsid_sms_main_c);
161 * Run until shut down.
166 * Block until something happens.
169 com_err(whoami, 0, "tick");
172 status = op_select_any(op_list, 0,
173 (fd_set *)NULL, (fd_set *)NULL,
174 (fd_set *)NULL, (struct timeval *)NULL);
177 com_err(whoami, errno, "error from op_select");
179 } else if (status != -2) {
180 com_err(whoami, 0, "wrong return from op_select_any");
185 fprintf(stderr, " tick\n");
188 * Handle any existing connections.
190 for (i=0; i<nclients; i++) {
191 if (OP_DONE(clients[i]->pending_op)) {
192 cur_client = clients[i];
193 do_client(cur_client);
199 * Handle any new connections.
201 if (OP_DONE(listenop)) {
202 if (OP_STATUS(listenop) == OP_CANCELLED) {
203 com_err(whoami, errno, "Error on listen");
206 } else if ((status = new_connection()) != 0) {
207 com_err(whoami, errno,
208 "Error on listening operation.");
210 * Sleep here to prevent hosing?
215 com_err(whoami, 0, takedown);
216 sms_close_database();
221 * Set up the template connection and queue the first accept.
227 char *service = index(SMS_GDB_SERV, ':') + 1;
229 listencon = create_listening_connection(service);
231 if (listencon == NULL)
234 listenop = create_operation();
235 client_addrlen = sizeof(client_addr);
237 start_accepting_client(listencon, listenop, &newconn,
238 (char *)&client_addr,
239 &client_addrlen, &client_tuple);
244 * This routine is called when a new connection comes in.
246 * It sets up a new client and adds it to the list of currently active clients.
255 * Make sure there's been no error
257 if(OP_STATUS(listenop) != OP_COMPLETE) {
261 if (newconn == NULL) {
262 return SMS_NOT_CONNECTED;
266 * Set up the new connection and reply to the client
268 cp = (client *)malloc(sizeof *cp);
269 cp->state = CL_STARTING;
270 cp->action = CL_ACCEPT;
275 cp->reply.sms_argv = NULL;
279 cp->pending_op = create_operation();
280 reset_operation(cp->pending_op);
281 oplist_append(&op_list, cp->pending_op);
285 * Add a new client to the array..
290 * Let him know we heard him.
292 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
295 cp->haddr = client_addr;
298 * Log new connection.
302 "New connection from %s port %d (now %d client%s)",
303 inet_ntoa(cp->haddr.sin_addr),
304 (int)ntohs(cp->haddr.sin_port),
307 com_err(whoami, 0, buf1);
310 * Get ready to accept the next connection.
312 reset_operation(listenop);
313 client_addrlen = sizeof(client_addr);
315 start_accepting_client(listencon, listenop, &newconn,
316 (char *)&client_addr,
317 &client_addrlen, &client_tuple);
322 * Add a new client to the known clients.
331 clients_n = (client **)malloc
332 ((unsigned)(nclients * sizeof(client *)));
333 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
334 clients_n[nclients-1] = cp;
335 free((char *)clients);
345 client **clients_n, **scpp, **dcpp; /* source and dest client */
350 clients_n = (client **)malloc
351 ((unsigned)((nclients - 1)* sizeof(client *)));
352 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
357 if (found_it) abort();
362 free((char *)clients);
365 oplist_delete(op_list, cp->pending_op);
366 reset_operation(cp->pending_op);
367 delete_operation(cp->pending_op);
368 sever_connection(cp->con);
373 * Add a new operation to a list of operations.
375 * This should be rewritten to use realloc instead, since in most
376 * cases it won't have to copy the array.
380 oplist_append(oplp, op)
381 LIST_OF_OPERATIONS *oplp;
384 int count = (*oplp)->count+1;
385 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
386 db_alloc(size_of_list_of_operations(count));
387 bcopy((char *)(*oplp), (char *)newlist,
388 size_of_list_of_operations((*oplp)->count));
390 newlist->op[count-1] = op;
391 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
396 oplist_delete(oplp, op)
397 LIST_OF_OPERATIONS oplp;
398 register OPERATION op;
400 register OPERATION *s;
403 for (s = oplp->op, c=oplp->count; c; --c, ++s) {