6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
12 * Most of this is stolen from ../gdb/tsr.c
14 * You are in a maze of twisty little finite automata, all different.
15 * Let the reader beware.
19 static char *rcsid_sms_main_c = "$Header$";
21 #include <mit-copyright.h>
23 #include <sys/errno.h>
24 #include <sys/signal.h>
26 #include "sms_server.h"
28 extern CONNECTION newconn, listencon;
31 extern client **clients, *cur_client;
33 extern OPERATION listenop;
34 extern LIST_OF_OPERATIONS op_list;
36 extern struct sockaddr_in client_addr;
37 extern int client_addrlen;
38 extern TUPLE client_tuple;
41 extern char buf1[BUFSIZ];
42 extern char *takedown;
45 #define JOURNAL "/u1/sms/journal"
47 extern char *malloc();
49 extern char *inet_ntoa();
50 extern void sms_com_err();
51 extern void do_client();
61 * Main SMS server loop.
63 * Initialize the world, then start accepting connections and
64 * making progress on current connections.
82 set_com_err_hook(sms_com_err);
86 com_err(whoami, 0, "Usage: smsd");
90 /* Profiling implies that getting rid of one level of call
91 * indirection here wins us maybe 1% on the VAX.
100 com_err(whoami, 0, "GDB initialization failed.");
103 gdb_debug(0); /* this can be patched, if necessary, to enable */
104 /* GDB level debugging .. */
105 krb_realm = malloc(REALM_SZ);
106 get_krbrlm(krb_realm, 1);
109 * Database initialization.
112 if ((status = sms_open_database()) != 0) {
113 com_err(whoami, status, " when trying to open database.");
117 sanity_check_queries();
118 sanity_check_database();
121 * Set up client array handler.
124 clients = (client **) malloc(0);
128 * There should probably be a few more of these.
131 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
132 (((int)signal (SIGCHLD, reapchild)) < 0) ||
133 (((int)signal (SIGHUP, sigshut)) < 0)) {
134 com_err(whoami, errno, " Unable to establish signal handler.");
138 journal = fopen(JOURNAL, "a");
139 if (journal == NULL) {
140 com_err(whoami, errno, " while opening journal file");
145 * Establish template connection.
147 if ((status = do_listen()) != 0) {
148 com_err(whoami, status,
149 " while trying to create listening connection");
153 op_list = create_list_of_operations(1, listenop);
155 com_err(whoami, 0, "started (pid %d)", getpid());
156 com_err(whoami, 0, rcsid_sms_main_c);
157 send_zgram("SMS", "server started");
160 * Run until shut down.
165 * Block until something happens.
168 com_err(whoami, 0, "tick");
171 status = op_select_any(op_list, 0,
172 (fd_set *)NULL, (fd_set *)NULL,
173 (fd_set *)NULL, (struct timeval *)NULL);
176 com_err(whoami, errno, " error from op_select");
178 } else if (status != -2) {
179 com_err(whoami, 0, " wrong return from op_select_any");
185 fprintf(stderr, " tick\n");
188 * Handle any new connections; this comes first so
189 * errno isn't tromped on.
191 if (OP_DONE(listenop)) {
192 if (OP_STATUS(listenop) == OP_CANCELLED) {
193 if (errno == EWOULDBLOCK) {
196 com_err(whoami, errno,
200 } else if ((status = new_connection()) != 0) {
201 com_err(whoami, errno,
202 " Error on listening operation.");
204 * Sleep here to prevent hosing?
209 * Handle any existing connections.
213 for (i=0; i<nclients; i++) {
214 cur_client = clients[i];
215 if (OP_DONE(clients[i]->pending_op)) {
216 cur_client->last_time_used = now;
217 do_client(cur_client);
218 } else if (clients[i]->last_time_used < tardy) {
219 com_err(whoami, 0, "Shutting down connection due to inactivity");
220 shutdown(cur_client->con->in.fd, 0);
226 com_err(whoami, 0, "%s", takedown);
227 sms_close_database();
228 send_zgram("SMS", takedown);
233 * Set up the template connection and queue the first accept.
239 char *service = index(SMS_GDB_SERV, ':') + 1;
241 listencon = create_listening_connection(service);
243 if (listencon == NULL)
246 listenop = create_operation();
247 client_addrlen = sizeof(client_addr);
249 start_accepting_client(listencon, listenop, &newconn,
250 (char *)&client_addr,
251 &client_addrlen, &client_tuple);
258 client_addrlen = sizeof(client_addr);
259 start_accepting_client(listencon, listenop, &newconn,
260 (char *)&client_addr,
261 &client_addrlen, &client_tuple);
265 * This routine is called when a new connection comes in.
267 * It sets up a new client and adds it to the list of currently active clients.
276 * Make sure there's been no error
278 if(OP_STATUS(listenop) != OP_COMPLETE) {
282 if (newconn == NULL) {
283 return SMS_NOT_CONNECTED;
287 * Set up the new connection and reply to the client
289 cp = (client *)malloc(sizeof *cp);
290 bzero(cp, sizeof(*cp));
291 cp->action = CL_ACCEPT;
296 cp->reply.sms_argv = NULL;
299 cp->last_time_used = now;
302 cp->pending_op = create_operation();
303 reset_operation(cp->pending_op);
304 oplist_append(&op_list, cp->pending_op);
308 * Add a new client to the array..
313 * Let him know we heard him.
315 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
318 cp->haddr = client_addr;
321 * Log new connection.
324 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
325 inet_ntoa(cp->haddr.sin_addr),
326 (int)ntohs(cp->haddr.sin_port),
331 * Get ready to accept the next connection.
333 reset_operation(listenop);
334 client_addrlen = sizeof(client_addr);
336 start_accepting_client(listencon, listenop, &newconn,
337 (char *)&client_addr,
338 &client_addrlen, &client_tuple);
343 * Add a new client to the known clients.
352 clients_n = (client **)malloc
353 ((unsigned)(nclients * sizeof(client *)));
354 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
355 clients_n[nclients-1] = cp;
356 free((char *)clients);
366 client **clients_n, **scpp, **dcpp; /* source and dest client */
371 clients_n = (client **)malloc
372 ((unsigned)((nclients - 1)* sizeof(client *)));
373 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
378 if (found_it) abort();
383 free((char *)clients);
386 oplist_delete(op_list, cp->pending_op);
387 reset_operation(cp->pending_op);
388 delete_operation(cp->pending_op);
389 sever_connection(cp->con);
394 * Add a new operation to a list of operations.
396 * This should be rewritten to use realloc instead, since in most
397 * cases it won't have to copy the array.
401 oplist_append(oplp, op)
402 LIST_OF_OPERATIONS *oplp;
405 int count = (*oplp)->count+1;
406 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
407 db_alloc(size_of_list_of_operations(count));
408 bcopy((char *)(*oplp), (char *)newlist,
409 size_of_list_of_operations((*oplp)->count));
411 newlist->op[count-1] = op;
412 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
417 oplist_delete(oplp, op)
418 LIST_OF_OPERATIONS oplp;
419 register OPERATION op;
421 register OPERATION *s;
424 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
446 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
447 if (status.w_termsig == 0 && status.w_retcode == 0)
448 com_err(whoami, 0, "dcm started successfully");
450 com_err(whoami, 0, "%d: startdcm exits with signal %d status %d",
451 pid, status.w_termsig, status.w_retcode);