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 (SIGCHLD, reapchild)) < 0) ||
131 (((int)signal (SIGHUP, sigshut)) < 0)) {
132 com_err(whoami, errno, " Unable to establish signal handler.");
136 journal = fopen(JOURNAL, "a");
137 if (journal == NULL) {
138 com_err(whoami, errno, " while opening journal file");
143 * Establish template connection.
145 if ((status = do_listen()) != 0) {
146 com_err(whoami, status,
147 " while trying to create listening connection");
151 op_list = create_list_of_operations(1, listenop);
153 com_err(whoami, 0, "started (pid %d)", getpid());
154 com_err(whoami, 0, rcsid_sms_main_c);
155 send_zgram("SMS", "server started");
158 * Run until shut down.
163 * Block until something happens.
166 com_err(whoami, 0, "tick");
169 status = op_select_any(op_list, 0,
170 (fd_set *)NULL, (fd_set *)NULL,
171 (fd_set *)NULL, (struct timeval *)NULL);
174 com_err(whoami, errno, " error from op_select");
176 } else if (status != -2) {
177 com_err(whoami, 0, " wrong return from op_select_any");
183 fprintf(stderr, " tick\n");
186 * Handle any new connections; this comes first so
187 * errno isn't tromped on.
189 if (OP_DONE(listenop)) {
190 if (OP_STATUS(listenop) == OP_CANCELLED) {
191 if (errno == EWOULDBLOCK) {
194 com_err(whoami, errno,
198 } else if ((status = new_connection()) != 0) {
199 com_err(whoami, errno,
200 " Error on listening operation.");
202 * Sleep here to prevent hosing?
207 * Handle any existing connections.
211 for (i=0; i<nclients; i++) {
212 cur_client = clients[i];
213 if (OP_DONE(clients[i]->pending_op)) {
214 cur_client->last_time_used = now;
215 do_client(cur_client);
216 } else if (clients[i]->last_time_used < tardy) {
217 com_err(whoami, 0, "Shutting down connection due to inactivity");
218 shutdown(cur_client->con->in.fd, 0);
224 com_err(whoami, 0, "%s", takedown);
225 sms_close_database();
226 send_zgram("SMS", takedown);
231 * Set up the template connection and queue the first accept.
237 char *service = index(SMS_GDB_SERV, ':') + 1;
239 listencon = create_listening_connection(service);
241 if (listencon == NULL)
244 listenop = create_operation();
245 client_addrlen = sizeof(client_addr);
247 start_accepting_client(listencon, listenop, &newconn,
248 (char *)&client_addr,
249 &client_addrlen, &client_tuple);
256 client_addrlen = sizeof(client_addr);
257 start_accepting_client(listencon, listenop, &newconn,
258 (char *)&client_addr,
259 &client_addrlen, &client_tuple);
263 * This routine is called when a new connection comes in.
265 * It sets up a new client and adds it to the list of currently active clients.
274 * Make sure there's been no error
276 if(OP_STATUS(listenop) != OP_COMPLETE) {
280 if (newconn == NULL) {
281 return SMS_NOT_CONNECTED;
285 * Set up the new connection and reply to the client
287 cp = (client *)malloc(sizeof *cp);
288 bzero(cp, sizeof(*cp));
289 cp->state = CL_STARTING;
290 cp->action = CL_ACCEPT;
295 cp->reply.sms_argv = NULL;
298 cp->last_time_used = now;
301 cp->pending_op = create_operation();
302 reset_operation(cp->pending_op);
303 oplist_append(&op_list, cp->pending_op);
307 * Add a new client to the array..
312 * Let him know we heard him.
314 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
317 cp->haddr = client_addr;
320 * Log new connection.
323 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
324 inet_ntoa(cp->haddr.sin_addr),
325 (int)ntohs(cp->haddr.sin_port),
330 * Get ready to accept the next connection.
332 reset_operation(listenop);
333 client_addrlen = sizeof(client_addr);
335 start_accepting_client(listencon, listenop, &newconn,
336 (char *)&client_addr,
337 &client_addrlen, &client_tuple);
342 * Add a new client to the known clients.
351 clients_n = (client **)malloc
352 ((unsigned)(nclients * sizeof(client *)));
353 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
354 clients_n[nclients-1] = cp;
355 free((char *)clients);
365 client **clients_n, **scpp, **dcpp; /* source and dest client */
370 clients_n = (client **)malloc
371 ((unsigned)((nclients - 1)* sizeof(client *)));
372 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
377 if (found_it) abort();
382 free((char *)clients);
385 oplist_delete(op_list, cp->pending_op);
386 reset_operation(cp->pending_op);
387 delete_operation(cp->pending_op);
388 sever_connection(cp->con);
393 * Add a new operation to a list of operations.
395 * This should be rewritten to use realloc instead, since in most
396 * cases it won't have to copy the array.
400 oplist_append(oplp, op)
401 LIST_OF_OPERATIONS *oplp;
404 int count = (*oplp)->count+1;
405 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
406 db_alloc(size_of_list_of_operations(count));
407 bcopy((char *)(*oplp), (char *)newlist,
408 size_of_list_of_operations((*oplp)->count));
410 newlist->op[count-1] = op;
411 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
416 oplist_delete(oplp, op)
417 LIST_OF_OPERATIONS oplp;
418 register OPERATION op;
420 register OPERATION *s;
423 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
443 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
444 if (status.w_termsig == 0 && status.w_retcode == 0)
445 com_err(whoami, 0, "dcm started successfully");
447 com_err(whoami, 0, "%d: startdcm exits with signal %d status %d",
448 pid, status.w_termsig, status.w_retcode);