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.14 1987-09-12 20:41:50 wesommer
20 * Revision 1.14 87/09/12 20:08:01 wesommer
21 * Add some defensive programming to defend against people who don't
22 * understand what the contents of the cl struct mean.
24 * Revision 1.13 87/08/04 02:40:30 wesommer
25 * Do end run around minor hotspot.
27 * Revision 1.12 87/08/04 01:50:00 wesommer
28 * Rearranged messages.
30 * Revision 1.11 87/07/29 16:04:54 wesommer
31 * Add keepalive feature.
33 * Revision 1.10 87/06/30 20:02:26 wesommer
34 * Added returned tuple chain to client structure.
35 * Added local realm global variable.
37 * Revision 1.9 87/06/21 16:39:54 wesommer
38 * Performance work, rearrangement of include files.
40 * Revision 1.8 87/06/09 18:44:45 wesommer
41 * modified error handling.
43 * Revision 1.7 87/06/08 02:44:44 wesommer
46 * Revision 1.6 87/06/03 17:41:00 wesommer
47 * Added startup support.
49 * Revision 1.5 87/06/03 16:07:17 wesommer
52 * Revision 1.4 87/06/02 20:05:11 wesommer
53 * Bug fixes on memory allocation.
55 * Revision 1.3 87/06/01 04:34:27 wesommer
56 * Changed returned error code.
58 * Revision 1.2 87/06/01 03:34:53 wesommer
59 * Added shutdown, logging.
61 * Revision 1.1 87/05/31 22:06:56 wesommer
66 static char *rcsid_sms_main_c = "$Header$";
69 #include <sys/errno.h>
70 #include <sys/signal.h>
71 #include "sms_server.h"
73 extern CONNECTION newconn, listencon;
76 extern client **clients, *cur_client;
78 extern OPERATION listenop;
79 extern LIST_OF_OPERATIONS op_list;
81 extern struct sockaddr_in client_addr;
82 extern int client_addrlen;
83 extern TUPLE client_tuple;
86 extern char buf1[BUFSIZ];
87 extern char *takedown;
91 extern char *malloc();
93 extern char *inet_ntoa();
94 extern void sms_com_err();
95 extern void do_client();
100 extern u_short ntohs();
105 * Main SMS server loop.
107 * Initialize the world, then start accepting connections and
108 * making progress on current connections.
122 * Error handler init.
126 set_com_err_hook(sms_com_err);
130 com_err(whoami, 0, "Usage: smsd");
134 /* Profiling implies that getting rid of one level of call
135 * indirection here wins us maybe 1% on the VAX.
141 * GDB initialization.
143 if(gdb_init() != 0) {
144 com_err(whoami, 0, "GDB initialization failed.");
147 gdb_debug(0); /* this can be patched, if necessary, to enable */
148 /* GDB level debugging .. */
149 krb_realm = malloc(REALM_SZ);
150 get_krbrlm(krb_realm, 1);
153 * Database initialization.
156 if ((status = sms_open_database()) != 0) {
157 com_err(whoami, status, "when trying to open database.");
162 * Set up client array handler.
165 clients = (client **) malloc(0);
169 * There should probably be a few more of these.
172 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
173 (((int)signal (SIGHUP, sigshut)) < 0)) {
174 com_err(whoami, errno, "Unable to establish signal handler.");
179 * Establish template connection.
181 if ((status = do_listen()) != 0) {
182 com_err(whoami, status,
183 "while trying to create listening connection");
187 op_list = create_list_of_operations(1, listenop);
189 com_err(whoami, 0, "started (pid %d)", getpid());
190 com_err(whoami, 0, rcsid_sms_main_c);
193 * Run until shut down.
198 * Block until something happens.
201 com_err(whoami, 0, "tick");
204 status = op_select_any(op_list, 0,
205 (fd_set *)NULL, (fd_set *)NULL,
206 (fd_set *)NULL, (struct timeval *)NULL);
209 com_err(whoami, errno, "error from op_select");
211 } else if (status != -2) {
212 com_err(whoami, 0, "wrong return from op_select_any");
218 fprintf(stderr, " tick\n");
221 * Handle any new connections; this comes first so
222 * errno isn't tromped on.
224 if (OP_DONE(listenop)) {
225 if (OP_STATUS(listenop) == OP_CANCELLED) {
226 if (errno == EWOULDBLOCK) {
229 com_err(whoami, errno,
233 } else if ((status = new_connection()) != 0) {
234 com_err(whoami, errno,
235 "Error on listening operation.");
237 * Sleep here to prevent hosing?
242 * Handle any existing connections.
246 for (i=0; i<nclients; i++) {
247 cur_client = clients[i];
248 if (OP_DONE(clients[i]->pending_op)) {
249 cur_client->last_time_used = now;
250 do_client(cur_client);
251 } else if (clients[i]->last_time_used < tardy) {
252 com_err(whoami, 0, "Shutting down connection due to inactivity");
253 shutdown(cur_client->con->in.fd, 0);
259 com_err(whoami, 0, "%s", takedown);
260 sms_close_database();
265 * Set up the template connection and queue the first accept.
271 char *service = index(SMS_GDB_SERV, ':') + 1;
273 listencon = create_listening_connection(service);
275 if (listencon == NULL)
278 listenop = create_operation();
279 client_addrlen = sizeof(client_addr);
281 start_accepting_client(listencon, listenop, &newconn,
282 (char *)&client_addr,
283 &client_addrlen, &client_tuple);
290 client_addrlen = sizeof(client_addr);
291 start_accepting_client(listencon, listenop, &newconn,
292 (char *)&client_addr,
293 &client_addrlen, &client_tuple);
297 * This routine is called when a new connection comes in.
299 * It sets up a new client and adds it to the list of currently active clients.
308 * Make sure there's been no error
310 if(OP_STATUS(listenop) != OP_COMPLETE) {
314 if (newconn == NULL) {
315 return SMS_NOT_CONNECTED;
319 * Set up the new connection and reply to the client
321 cp = (client *)malloc(sizeof *cp);
322 bzero(cp, sizeof(*cp));
323 cp->state = CL_STARTING;
324 cp->action = CL_ACCEPT;
329 cp->reply.sms_argv = NULL;
332 cp->last_time_used = now;
335 cp->pending_op = create_operation();
336 reset_operation(cp->pending_op);
337 oplist_append(&op_list, cp->pending_op);
341 * Add a new client to the array..
346 * Let him know we heard him.
348 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
351 cp->haddr = client_addr;
354 * Log new connection.
357 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
358 inet_ntoa(cp->haddr.sin_addr),
359 (int)ntohs(cp->haddr.sin_port),
364 * Get ready to accept the next connection.
366 reset_operation(listenop);
367 client_addrlen = sizeof(client_addr);
369 start_accepting_client(listencon, listenop, &newconn,
370 (char *)&client_addr,
371 &client_addrlen, &client_tuple);
376 * Add a new client to the known clients.
385 clients_n = (client **)malloc
386 ((unsigned)(nclients * sizeof(client *)));
387 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
388 clients_n[nclients-1] = cp;
389 free((char *)clients);
399 client **clients_n, **scpp, **dcpp; /* source and dest client */
404 clients_n = (client **)malloc
405 ((unsigned)((nclients - 1)* sizeof(client *)));
406 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
411 if (found_it) abort();
416 free((char *)clients);
419 oplist_delete(op_list, cp->pending_op);
420 reset_operation(cp->pending_op);
421 delete_operation(cp->pending_op);
422 sever_connection(cp->con);
427 * Add a new operation to a list of operations.
429 * This should be rewritten to use realloc instead, since in most
430 * cases it won't have to copy the array.
434 oplist_append(oplp, op)
435 LIST_OF_OPERATIONS *oplp;
438 int count = (*oplp)->count+1;
439 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
440 db_alloc(size_of_list_of_operations(count));
441 bcopy((char *)(*oplp), (char *)newlist,
442 size_of_list_of_operations((*oplp)->count));
444 newlist->op[count-1] = op;
445 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
450 oplist_delete(oplp, op)
451 LIST_OF_OPERATIONS oplp;
452 register OPERATION op;
454 register OPERATION *s;
457 for (s = oplp->op, c=oplp->count; c; --c, ++s) {