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.10 1987-06-30 20:02:26 wesommer
18 * Added returned tuple chain to client structure.
19 * Added local realm global variable.
21 * Revision 1.9 87/06/21 16:39:54 wesommer
22 * Performance work, rearrangement of include files.
24 * Revision 1.8 87/06/09 18:44:45 wesommer
25 * modified error handling.
27 * Revision 1.7 87/06/08 02:44:44 wesommer
30 * Revision 1.6 87/06/03 17:41:00 wesommer
31 * Added startup support.
33 * Revision 1.5 87/06/03 16:07:17 wesommer
36 * Revision 1.4 87/06/02 20:05:11 wesommer
37 * Bug fixes on memory allocation.
39 * Revision 1.3 87/06/01 04:34:27 wesommer
40 * Changed returned error code.
42 * Revision 1.2 87/06/01 03:34:53 wesommer
43 * Added shutdown, logging.
45 * Revision 1.1 87/05/31 22:06:56 wesommer
50 static char *rcsid_sms_main_c = "$Header$";
53 #include <sys/errno.h>
54 #include <sys/signal.h>
55 #include "sms_server.h"
57 extern CONNECTION newconn, listencon;
60 extern client **clients, *cur_client;
62 extern OPERATION listenop;
63 extern LIST_OF_OPERATIONS op_list;
65 extern struct sockaddr_in client_addr;
66 extern int client_addrlen;
67 extern TUPLE client_tuple;
70 extern char buf1[BUFSIZ];
71 extern char *takedown;
75 extern char *malloc();
76 extern char *inet_ntoa();
77 extern void sms_com_err();
78 extern void do_client();
83 extern u_short ntohs();
86 * Main SMS server loop.
88 * Initialize the world, then start accepting connections and
89 * making progress on current connections.
102 * Error handler init.
106 set_com_err_hook(sms_com_err);
109 com_err(whoami, 0, "Usage: smsd");
114 * GDB initialization.
116 if(gdb_init() != 0) {
117 com_err(whoami, 0, "GDB initialization failed.");
120 gdb_debug(0); /* this can be patched, if necessary, to enable */
121 /* GDB level debugging .. */
122 krb_realm = malloc(REALM_SZ);
123 get_krbrlm(krb_realm, 1);
126 * Database initialization.
129 if ((status = sms_open_database()) != 0) {
130 com_err(whoami, status, "when trying to open database.");
135 * Set up client array handler.
138 clients = (client **) malloc(0);
142 * There should probably be a few more of these.
145 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
146 (((int)signal (SIGHUP, sigshut)) < 0)) {
147 com_err(whoami, errno, "Unable to establish signal handler.");
152 * Establish template connection.
154 if ((status = do_listen()) != 0) {
155 com_err(whoami, status,
156 "while trying to create listening connection");
160 op_list = create_list_of_operations(1, listenop);
162 (void) sprintf(buf1, "started (pid %d)", getpid());
163 com_err(whoami, 0, buf1);
164 com_err(whoami, 0, rcsid_sms_main_c);
167 * Run until shut down.
172 * Block until something happens.
175 com_err(whoami, 0, "tick");
178 status = op_select_any(op_list, 0,
179 (fd_set *)NULL, (fd_set *)NULL,
180 (fd_set *)NULL, (struct timeval *)NULL);
183 com_err(whoami, errno, "error from op_select");
185 } else if (status != -2) {
186 com_err(whoami, 0, "wrong return from op_select_any");
191 fprintf(stderr, " tick\n");
194 * Handle any existing connections.
196 for (i=0; i<nclients; i++) {
197 if (OP_DONE(clients[i]->pending_op)) {
198 cur_client = clients[i];
199 do_client(cur_client);
205 * Handle any new connections.
207 if (OP_DONE(listenop)) {
208 if (OP_STATUS(listenop) == OP_CANCELLED) {
209 com_err(whoami, errno, "Error on listen");
212 } else if ((status = new_connection()) != 0) {
213 com_err(whoami, errno,
214 "Error on listening operation.");
216 * Sleep here to prevent hosing?
221 com_err(whoami, 0, takedown);
222 sms_close_database();
227 * Set up the template connection and queue the first accept.
233 char *service = index(SMS_GDB_SERV, ':') + 1;
235 listencon = create_listening_connection(service);
237 if (listencon == NULL)
240 listenop = create_operation();
241 client_addrlen = sizeof(client_addr);
243 start_accepting_client(listencon, listenop, &newconn,
244 (char *)&client_addr,
245 &client_addrlen, &client_tuple);
250 * This routine is called when a new connection comes in.
252 * It sets up a new client and adds it to the list of currently active clients.
261 * Make sure there's been no error
263 if(OP_STATUS(listenop) != OP_COMPLETE) {
267 if (newconn == NULL) {
268 return SMS_NOT_CONNECTED;
272 * Set up the new connection and reply to the client
274 cp = (client *)malloc(sizeof *cp);
275 cp->state = CL_STARTING;
276 cp->action = CL_ACCEPT;
281 cp->reply.sms_argv = NULL;
287 cp->pending_op = create_operation();
288 reset_operation(cp->pending_op);
289 oplist_append(&op_list, cp->pending_op);
293 * Add a new client to the array..
298 * Let him know we heard him.
300 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
303 cp->haddr = client_addr;
306 * Log new connection.
310 "New connection from %s port %d (now %d client%s)",
311 inet_ntoa(cp->haddr.sin_addr),
312 (int)ntohs(cp->haddr.sin_port),
315 com_err(whoami, 0, buf1);
318 * Get ready to accept the next connection.
320 reset_operation(listenop);
321 client_addrlen = sizeof(client_addr);
323 start_accepting_client(listencon, listenop, &newconn,
324 (char *)&client_addr,
325 &client_addrlen, &client_tuple);
330 * Add a new client to the known clients.
339 clients_n = (client **)malloc
340 ((unsigned)(nclients * sizeof(client *)));
341 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
342 clients_n[nclients-1] = cp;
343 free((char *)clients);
353 client **clients_n, **scpp, **dcpp; /* source and dest client */
358 clients_n = (client **)malloc
359 ((unsigned)((nclients - 1)* sizeof(client *)));
360 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
365 if (found_it) abort();
370 free((char *)clients);
373 oplist_delete(op_list, cp->pending_op);
374 reset_operation(cp->pending_op);
375 delete_operation(cp->pending_op);
376 sever_connection(cp->con);
381 * Add a new operation to a list of operations.
383 * This should be rewritten to use realloc instead, since in most
384 * cases it won't have to copy the array.
388 oplist_append(oplp, op)
389 LIST_OF_OPERATIONS *oplp;
392 int count = (*oplp)->count+1;
393 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
394 db_alloc(size_of_list_of_operations(count));
395 bcopy((char *)(*oplp), (char *)newlist,
396 size_of_list_of_operations((*oplp)->count));
398 newlist->op[count-1] = op;
399 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
404 oplist_delete(oplp, op)
405 LIST_OF_OPERATIONS oplp;
406 register OPERATION op;
408 register OPERATION *s;
411 for (s = oplp->op, c=oplp->count; c; --c, ++s) {