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.8 1987-06-09 18:44:45 wesommer
18 * modified error handling.
20 * Revision 1.7 87/06/08 02:44:44 wesommer
23 * Revision 1.6 87/06/03 17:41:00 wesommer
24 * Added startup support.
26 * Revision 1.5 87/06/03 16:07:17 wesommer
29 * Revision 1.4 87/06/02 20:05:11 wesommer
30 * Bug fixes on memory allocation.
32 * Revision 1.3 87/06/01 04:34:27 wesommer
33 * Changed returned error code.
35 * Revision 1.2 87/06/01 03:34:53 wesommer
36 * Added shutdown, logging.
38 * Revision 1.1 87/05/31 22:06:56 wesommer
43 static char *rcsid_sms_main_c = "$Header$";
46 #include <sys/errno.h>
47 #include <sys/signal.h>
48 #include "sms_private.h"
49 #include "sms_server.h"
51 extern CONNECTION newconn, listencon;
54 extern client **clients, *cur_client;
56 extern OPERATION listenop;
57 extern LIST_OF_OPERATIONS op_list;
59 extern struct sockaddr_in client_addr;
60 extern int client_addrlen;
61 extern TUPLE client_tuple;
64 extern char buf1[BUFSIZ];
65 extern char *takedown;
69 extern char *malloc();
70 extern char *inet_ntoa();
71 extern void sms_com_err();
72 extern void do_client();
77 extern u_short ntohs();
80 * Main SMS server loop.
82 * Initialize the world, then start accepting connections and
83 * making progress on current connections.
100 set_com_err_hook(sms_com_err);
103 com_err(whoami, 0, "Usage: smsd");
108 * GDB initialization.
110 if(gdb_init() != 0) {
111 com_err(whoami, 0, "GDB initialization failed.");
114 gdb_debug(0); /* this can be patched, if necessary, to enable */
115 /* GDB level debugging .. */
118 * Database initialization.
121 if ((status = sms_open_database()) != 0) {
122 com_err(whoami, status, "when trying to open database.");
127 * Set up client array handler.
130 clients = (client **) malloc(0);
134 * There should probably be a few more of these.
137 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
138 (((int)signal (SIGHUP, sigshut)) < 0)) {
139 com_err(whoami, errno, "Unable to establish signal handler.");
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 (void) sprintf(buf1, "started (pid %d)", getpid());
155 com_err(whoami, 0, buf1);
156 com_err(whoami, 0, rcsid_sms_main_c);
159 * Run until shut down.
164 * Block until something happens.
167 com_err(whoami, 0, "tick");
170 status = op_select_any(op_list, 0,
171 (fd_set *)NULL, (fd_set *)NULL,
172 (fd_set *)NULL, (struct timeval *)NULL);
175 com_err(whoami, errno, "error from op_select");
177 } else if (status != -2) {
178 com_err(whoami, 0, "wrong return from op_select_any");
183 fprintf(stderr, " tick\n");
186 * Handle any existing connections.
188 for (i=0; i<nclients; i++) {
189 if (OP_DONE(clients[i]->pending_op)) {
190 cur_client = clients[i];
191 do_client(cur_client);
197 * Handle any new connections.
199 if (OP_DONE(listenop)) {
200 if (OP_STATUS(listenop) == OP_CANCELLED) {
201 com_err(whoami, errno, "Error on listen");
204 } else if ((status = new_connection()) != 0) {
205 com_err(whoami, errno,
206 "Error on listening operation.");
208 * Sleep here to prevent hosing?
213 com_err(whoami, 0, takedown);
214 sms_close_database();
219 * Set up the template connection and queue the first accept.
225 char *service = index(SMS_GDB_SERV, ':') + 1;
227 listencon = create_listening_connection(service);
229 if (listencon == NULL)
232 listenop = create_operation();
233 client_addrlen = sizeof(client_addr);
235 start_accepting_client(listencon, listenop, &newconn,
236 (char *)&client_addr,
237 &client_addrlen, &client_tuple);
242 * This routine is called when a new connection comes in.
244 * It sets up a new client and adds it to the list of currently active clients.
253 * Make sure there's been no error
255 if(OP_STATUS(listenop) != OP_COMPLETE ||
264 * Set up the new connection and reply to the client
266 cp = (client *)malloc(sizeof *cp);
267 cp->state = CL_STARTING;
268 cp->action = CL_ACCEPT;
273 cp->reply.sms_argv = NULL;
277 cp->pending_op = create_operation();
278 reset_operation(cp->pending_op);
279 oplist_append(&op_list, cp->pending_op);
283 * Add a new client to the array..
288 * Let him know we heard him.
290 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
293 cp->haddr = client_addr;
296 * Log new connection.
300 "New connection from %s port %d (now %d client%s)",
301 inet_ntoa(cp->haddr.sin_addr),
302 (int)ntohs(cp->haddr.sin_port),
305 com_err(whoami, 0, buf1);
308 * Get ready to accept the next connection.
310 reset_operation(listenop);
311 client_addrlen = sizeof(client_addr);
313 start_accepting_client(listencon, listenop, &newconn,
314 (char *)&client_addr,
315 &client_addrlen, &client_tuple);
320 * Add a new client to the known clients.
329 clients_n = (client **)malloc
330 ((unsigned)(nclients * sizeof(client *)));
331 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
332 clients_n[nclients-1] = cp;
333 free((char *)clients);
343 client **clients_n, **scpp, **dcpp; /* source and dest client */
348 clients_n = (client **)malloc
349 ((unsigned)((nclients - 1)* sizeof(client *)));
350 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
355 if (found_it) abort();
360 free((char *)clients);
364 reset_operation(cp->pending_op);
365 delete_operation(cp->pending_op);
366 sever_connection(cp->con);
371 * Add a new operation to a list of operations.
373 * This should be rewritten to use realloc instead, since in most
374 * cases it won't have to copy the array.
378 oplist_append(oplp, op)
379 LIST_OF_OPERATIONS *oplp;
382 int count = (*oplp)->count+1;
383 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
384 db_alloc(size_of_list_of_operations(count));
385 bcopy((char *)(*oplp), (char *)newlist,
386 size_of_list_of_operations((*oplp)->count));
388 newlist->op[count-1] = op;
389 db_free((char *)(*oplp), size_of_list_of_operations(count-1));