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"
29 extern CONNECTION newconn, listencon;
32 extern client **clients, *cur_client;
34 extern OPERATION listenop;
35 extern LIST_OF_OPERATIONS op_list;
37 extern struct sockaddr_in client_addr;
38 extern int client_addrlen;
39 extern TUPLE client_tuple;
42 extern char buf1[BUFSIZ];
43 extern char *takedown;
46 #define JOURNAL "/u1/sms/journal"
48 extern char *malloc();
50 extern char *inet_ntoa();
51 extern void sms_com_err();
52 extern void do_client();
62 * Main SMS server loop.
64 * Initialize the world, then start accepting connections and
65 * making progress on current connections.
81 initialize_sms_error_table();
82 initialize_krb_error_table();
83 set_com_err_hook(sms_com_err);
87 com_err(whoami, 0, "Usage: smsd");
91 /* Profiling implies that getting rid of one level of call
92 * indirection here wins us maybe 1% on the VAX.
100 if(gdb_init() != 0) {
101 com_err(whoami, 0, "GDB initialization failed.");
104 gdb_debug(0); /* this can be patched, if necessary, to enable */
105 /* GDB level debugging .. */
106 krb_realm = malloc(REALM_SZ);
107 get_krbrlm(krb_realm, 1);
110 * Database initialization.
113 if ((status = sms_open_database()) != 0) {
114 com_err(whoami, status, " when trying to open database.");
118 sanity_check_queries();
119 sanity_check_database();
122 * Set up client array handler.
125 clients = (client **) malloc(0);
129 * There should probably be a few more of these.
132 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
133 (((int)signal (SIGCHLD, reapchild)) < 0) ||
134 (((int)signal (SIGHUP, sigshut)) < 0)) {
135 com_err(whoami, errno, " Unable to establish signal handler.");
139 journal = fopen(JOURNAL, "a");
140 if (journal == NULL) {
141 com_err(whoami, errno, " while opening journal file");
146 * Establish template connection.
148 if ((status = do_listen()) != 0) {
149 com_err(whoami, status,
150 " while trying to create listening connection");
154 op_list = create_list_of_operations(1, listenop);
156 com_err(whoami, 0, "started (pid %d)", getpid());
157 com_err(whoami, 0, rcsid_sms_main_c);
158 send_zgram("SMS", "server started");
161 * Run until shut down.
166 * Block until something happens.
169 com_err(whoami, 0, "tick");
172 status = op_select_any(op_list, 0,
173 (fd_set *)NULL, (fd_set *)NULL,
174 (fd_set *)NULL, (struct timeval *)NULL);
177 com_err(whoami, errno, " error from op_select");
179 } else if (status != -2) {
180 com_err(whoami, 0, " wrong return from op_select_any");
186 fprintf(stderr, " tick\n");
189 * Handle any new connections; this comes first so
190 * errno isn't tromped on.
192 if (OP_DONE(listenop)) {
193 if (OP_STATUS(listenop) == OP_CANCELLED) {
194 if (errno == EWOULDBLOCK) {
197 com_err(whoami, errno,
201 } else if ((status = new_connection()) != 0) {
202 com_err(whoami, errno,
203 " Error on listening operation.");
205 * Sleep here to prevent hosing?
210 * Handle any existing connections.
214 for (i=0; i<nclients; i++) {
215 cur_client = clients[i];
216 if (OP_DONE(clients[i]->pending_op)) {
217 cur_client->last_time_used = now;
218 do_client(cur_client);
219 } else if (clients[i]->last_time_used < tardy) {
220 com_err(whoami, 0, "Shutting down connection due to inactivity");
221 shutdown(cur_client->con->in.fd, 0);
227 com_err(whoami, 0, "%s", takedown);
228 sms_close_database();
229 send_zgram("SMS", takedown);
234 * Set up the template connection and queue the first accept.
240 char *service = index(SMS_GDB_SERV, ':') + 1;
242 listencon = create_listening_connection(service);
244 if (listencon == NULL)
247 listenop = create_operation();
248 client_addrlen = sizeof(client_addr);
250 start_accepting_client(listencon, listenop, &newconn,
251 (char *)&client_addr,
252 &client_addrlen, &client_tuple);
259 client_addrlen = sizeof(client_addr);
260 start_accepting_client(listencon, listenop, &newconn,
261 (char *)&client_addr,
262 &client_addrlen, &client_tuple);
266 * This routine is called when a new connection comes in.
268 * It sets up a new client and adds it to the list of currently active clients.
277 * Make sure there's been no error
279 if(OP_STATUS(listenop) != OP_COMPLETE) {
283 if (newconn == NULL) {
284 return SMS_NOT_CONNECTED;
288 * Set up the new connection and reply to the client
290 cp = (client *)malloc(sizeof *cp);
291 bzero(cp, sizeof(*cp));
292 cp->action = CL_ACCEPT;
297 cp->reply.sms_argv = NULL;
300 cp->last_time_used = now;
303 cp->pending_op = create_operation();
304 reset_operation(cp->pending_op);
305 oplist_append(&op_list, cp->pending_op);
309 * Add a new client to the array..
314 * Let him know we heard him.
316 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
319 cp->haddr = client_addr;
322 * Log new connection.
325 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
326 inet_ntoa(cp->haddr.sin_addr),
327 (int)ntohs(cp->haddr.sin_port),
332 * Get ready to accept the next connection.
334 reset_operation(listenop);
335 client_addrlen = sizeof(client_addr);
337 start_accepting_client(listencon, listenop, &newconn,
338 (char *)&client_addr,
339 &client_addrlen, &client_tuple);
344 * Add a new client to the known clients.
353 clients_n = (client **)malloc
354 ((unsigned)(nclients * sizeof(client *)));
355 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
356 clients_n[nclients-1] = cp;
357 free((char *)clients);
367 client **clients_n, **scpp, **dcpp; /* source and dest client */
372 clients_n = (client **)malloc
373 ((unsigned)((nclients - 1)* sizeof(client *)));
374 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
379 if (found_it) abort();
384 free((char *)clients);
387 oplist_delete(op_list, cp->pending_op);
388 reset_operation(cp->pending_op);
389 delete_operation(cp->pending_op);
390 sever_connection(cp->con);
395 * Add a new operation to a list of operations.
397 * This should be rewritten to use realloc instead, since in most
398 * cases it won't have to copy the array.
402 oplist_append(oplp, op)
403 LIST_OF_OPERATIONS *oplp;
406 int count = (*oplp)->count+1;
407 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
408 db_alloc(size_of_list_of_operations(count));
409 bcopy((char *)(*oplp), (char *)newlist,
410 size_of_list_of_operations((*oplp)->count));
412 newlist->op[count-1] = op;
413 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
418 oplist_delete(oplp, op)
419 LIST_OF_OPERATIONS oplp;
420 register OPERATION op;
422 register OPERATION *s;
425 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
447 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
448 if (status.w_termsig == 0 && status.w_retcode == 0)
449 com_err(whoami, 0, "dcm started successfully");
451 com_err(whoami, 0, "%d: startdcm exits with signal %d status %d",
452 pid, status.w_termsig, status.w_retcode);