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.
18 static char *rcsid_sms_main_c = "$Header$";
21 #include <sys/errno.h>
22 #include <sys/signal.h>
24 #include "sms_server.h"
26 extern CONNECTION newconn, listencon;
29 extern client **clients, *cur_client;
31 extern OPERATION listenop;
32 extern LIST_OF_OPERATIONS op_list;
34 extern struct sockaddr_in client_addr;
35 extern int client_addrlen;
36 extern TUPLE client_tuple;
39 extern char buf1[BUFSIZ];
40 extern char *takedown;
43 #define JOURNAL "/u1/sms/journal"
45 extern char *malloc();
47 extern char *inet_ntoa();
48 extern void sms_com_err();
49 extern void do_client();
59 * Main SMS server loop.
61 * Initialize the world, then start accepting connections and
62 * making progress on current connections.
80 set_com_err_hook(sms_com_err);
84 com_err(whoami, 0, "Usage: smsd");
88 /* Profiling implies that getting rid of one level of call
89 * indirection here wins us maybe 1% on the VAX.
98 com_err(whoami, 0, "GDB initialization failed.");
101 gdb_debug(0); /* this can be patched, if necessary, to enable */
102 /* GDB level debugging .. */
103 krb_realm = malloc(REALM_SZ);
104 get_krbrlm(krb_realm, 1);
107 * Database initialization.
110 if ((status = sms_open_database()) != 0) {
111 com_err(whoami, status, " when trying to open database.");
115 sanity_check_queries();
116 sanity_check_database();
119 * Set up client array handler.
122 clients = (client **) malloc(0);
126 * There should probably be a few more of these.
129 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
130 (((int)signal (SIGHUP, sigshut)) < 0)) {
131 com_err(whoami, errno, " Unable to establish signal handler.");
135 journal = fopen(JOURNAL, "a");
136 if (journal == NULL) {
137 com_err(whoami, errno, " while opening journal file");
142 * Establish template connection.
144 if ((status = do_listen()) != 0) {
145 com_err(whoami, status,
146 " while trying to create listening connection");
150 op_list = create_list_of_operations(1, listenop);
152 com_err(whoami, 0, "started (pid %d)", getpid());
153 com_err(whoami, 0, rcsid_sms_main_c);
154 send_zgram("SMS", "server started");
157 * Run until shut down.
162 * Block until something happens.
165 com_err(whoami, 0, "tick");
168 status = op_select_any(op_list, 0,
169 (fd_set *)NULL, (fd_set *)NULL,
170 (fd_set *)NULL, (struct timeval *)NULL);
173 com_err(whoami, errno, " error from op_select");
175 } else if (status != -2) {
176 com_err(whoami, 0, " wrong return from op_select_any");
182 fprintf(stderr, " tick\n");
185 * Handle any new connections; this comes first so
186 * errno isn't tromped on.
188 if (OP_DONE(listenop)) {
189 if (OP_STATUS(listenop) == OP_CANCELLED) {
190 if (errno == EWOULDBLOCK) {
193 com_err(whoami, errno,
197 } else if ((status = new_connection()) != 0) {
198 com_err(whoami, errno,
199 " Error on listening operation.");
201 * Sleep here to prevent hosing?
206 * Handle any existing connections.
210 for (i=0; i<nclients; i++) {
211 cur_client = clients[i];
212 if (OP_DONE(clients[i]->pending_op)) {
213 cur_client->last_time_used = now;
214 do_client(cur_client);
215 } else if (clients[i]->last_time_used < tardy) {
216 com_err(whoami, 0, "Shutting down connection due to inactivity");
217 shutdown(cur_client->con->in.fd, 0);
223 com_err(whoami, 0, "%s", takedown);
224 sms_close_database();
225 send_zgram("SMS", takedown);
230 * Set up the template connection and queue the first accept.
236 char *service = index(SMS_GDB_SERV, ':') + 1;
238 listencon = create_listening_connection(service);
240 if (listencon == NULL)
243 listenop = create_operation();
244 client_addrlen = sizeof(client_addr);
246 start_accepting_client(listencon, listenop, &newconn,
247 (char *)&client_addr,
248 &client_addrlen, &client_tuple);
255 client_addrlen = sizeof(client_addr);
256 start_accepting_client(listencon, listenop, &newconn,
257 (char *)&client_addr,
258 &client_addrlen, &client_tuple);
262 * This routine is called when a new connection comes in.
264 * It sets up a new client and adds it to the list of currently active clients.
273 * Make sure there's been no error
275 if(OP_STATUS(listenop) != OP_COMPLETE) {
279 if (newconn == NULL) {
280 return SMS_NOT_CONNECTED;
284 * Set up the new connection and reply to the client
286 cp = (client *)malloc(sizeof *cp);
287 bzero(cp, sizeof(*cp));
288 cp->state = CL_STARTING;
289 cp->action = CL_ACCEPT;
294 cp->reply.sms_argv = NULL;
297 cp->last_time_used = now;
300 cp->pending_op = create_operation();
301 reset_operation(cp->pending_op);
302 oplist_append(&op_list, cp->pending_op);
306 * Add a new client to the array..
311 * Let him know we heard him.
313 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
316 cp->haddr = client_addr;
319 * Log new connection.
322 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
323 inet_ntoa(cp->haddr.sin_addr),
324 (int)ntohs(cp->haddr.sin_port),
329 * Get ready to accept the next connection.
331 reset_operation(listenop);
332 client_addrlen = sizeof(client_addr);
334 start_accepting_client(listencon, listenop, &newconn,
335 (char *)&client_addr,
336 &client_addrlen, &client_tuple);
341 * Add a new client to the known clients.
350 clients_n = (client **)malloc
351 ((unsigned)(nclients * sizeof(client *)));
352 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
353 clients_n[nclients-1] = cp;
354 free((char *)clients);
364 client **clients_n, **scpp, **dcpp; /* source and dest client */
369 clients_n = (client **)malloc
370 ((unsigned)((nclients - 1)* sizeof(client *)));
371 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
376 if (found_it) abort();
381 free((char *)clients);
384 oplist_delete(op_list, cp->pending_op);
385 reset_operation(cp->pending_op);
386 delete_operation(cp->pending_op);
387 sever_connection(cp->con);
392 * Add a new operation to a list of operations.
394 * This should be rewritten to use realloc instead, since in most
395 * cases it won't have to copy the array.
399 oplist_append(oplp, op)
400 LIST_OF_OPERATIONS *oplp;
403 int count = (*oplp)->count+1;
404 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
405 db_alloc(size_of_list_of_operations(count));
406 bcopy((char *)(*oplp), (char *)newlist,
407 size_of_list_of_operations((*oplp)->count));
409 newlist->op[count-1] = op;
410 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
415 oplist_delete(oplp, op)
416 LIST_OF_OPERATIONS oplp;
417 register OPERATION op;
419 register OPERATION *s;
422 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
442 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
443 if (status.w_termsig == 0 && status.w_retcode == 0)
444 com_err(whoami, 0, "dcm started successfully");
446 com_err(whoami, 0, "%d: startdcm exits with signal %d status %d",
447 pid, status.w_termsig, status.w_retcode);