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/types.h>
24 #include <sys/errno.h>
25 #include <sys/signal.h>
28 #include "sms_server.h"
31 extern CONNECTION newconn, listencon;
34 extern client **clients, *cur_client;
36 extern OPERATION listenop;
37 extern LIST_OF_OPERATIONS op_list;
39 extern struct sockaddr_in client_addr;
40 extern int client_addrlen;
41 extern TUPLE client_tuple;
44 extern char buf1[BUFSIZ];
45 extern char *takedown;
48 #define JOURNAL "/u1/sms/journal"
50 extern char *malloc();
52 extern char *inet_ntoa();
53 extern void sms_com_err();
54 extern void do_client();
59 void reapchild(), godormant(), gowakeup();
64 * Main SMS server loop.
66 * Initialize the world, then start accepting connections and
67 * making progress on current connections.
84 initialize_sms_error_table();
85 initialize_krb_error_table();
86 set_com_err_hook(sms_com_err);
90 com_err(whoami, 0, "Usage: smsd");
94 /* Profiling implies that getting rid of one level of call
95 * indirection here wins us maybe 1% on the VAX.
101 * GDB initialization.
103 if(gdb_init() != 0) {
104 com_err(whoami, 0, "GDB initialization failed.");
107 gdb_debug(0); /* this can be patched, if necessary, to enable */
108 /* GDB level debugging .. */
109 krb_realm = malloc(REALM_SZ);
110 get_krbrlm(krb_realm, 1);
113 * Database initialization.
116 if ((status = sms_open_database()) != 0) {
117 com_err(whoami, status, " when trying to open database.");
121 sanity_check_queries();
122 sanity_check_database();
125 * Set up client array handler.
128 clients = (client **) malloc(0);
132 * There should probably be a few more of these. This is
133 * duplicated on the next page, be sure to also add any
134 * additional handlers there.
137 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
138 (((int)signal (SIGCHLD, reapchild)) < 0) ||
139 (((int)signal (SIGUSR1, godormant)) < 0) ||
140 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
141 (((int)signal (SIGHUP, sigshut)) < 0)) {
142 com_err(whoami, errno, " Unable to establish signal handlers.");
146 journal = fopen(JOURNAL, "a");
147 if (journal == NULL) {
148 com_err(whoami, errno, " while opening journal file");
153 * Establish template connection.
155 if ((status = do_listen()) != 0) {
156 com_err(whoami, status,
157 " while trying to create listening connection");
161 op_list = create_list_of_operations(1, listenop);
163 com_err(whoami, 0, "started (pid %d)", getpid());
164 com_err(whoami, 0, rcsid_sms_main_c);
165 send_zgram("SMS", "server started");
168 * Run until shut down.
173 * Block until something happens.
176 com_err(whoami, 0, "tick");
178 if (dormant == SLEEPY) {
179 sms_close_database();
180 com_err(whoami, 0, "database closed");
181 send_zgram("SMS", "database closed");
183 } else if (dormant == GROGGY) {
185 com_err(whoami, 0, "database open");
186 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
187 (((int)signal (SIGCHLD, reapchild)) < 0) ||
188 (((int)signal (SIGUSR1, godormant)) < 0) ||
189 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
190 (((int)signal (SIGHUP, sigshut)) < 0)) {
191 com_err(whoami, errno,
192 " Unable to reestablish signal handlers.");
195 send_zgram("SMS", "database open again");
200 status = op_select_any(op_list, 0,
201 (fd_set *)NULL, (fd_set *)NULL,
202 (fd_set *)NULL, (struct timeval *)NULL);
205 com_err(whoami, errno, " error from op_select");
207 } else if (status != -2) {
208 com_err(whoami, 0, " wrong return from op_select_any");
214 fprintf(stderr, " tick\n");
217 * Handle any new connections; this comes first so
218 * errno isn't tromped on.
220 if (OP_DONE(listenop)) {
221 if (OP_STATUS(listenop) == OP_CANCELLED) {
222 if (errno == EWOULDBLOCK) {
225 com_err(whoami, errno,
229 } else if ((status = new_connection()) != 0) {
230 com_err(whoami, errno,
231 " Error on listening operation.");
233 * Sleep here to prevent hosing?
236 /* if the new connection is our only connection,
237 * and the server is supposed to be down, then go
240 if ((dormant == AWAKE) && (nclients == 1) &&
241 (stat(SMS_MOTD_FILE, &stbuf) == 0)) {
242 com_err(whoami, 0, "motd file exists, slumbertime");
245 /* on new connection, if we are no longer supposed
246 * to be down, then wake up.
248 if ((dormant == ASLEEP) &&
249 (stat(SMS_MOTD_FILE, &stbuf) == -1) &&
251 com_err(whoami, 0, "motd file no longer exists, waking up");
257 * Handle any existing connections.
261 for (i=0; i<nclients; i++) {
262 cur_client = clients[i];
263 if (OP_DONE(clients[i]->pending_op)) {
264 cur_client->last_time_used = now;
265 do_client(cur_client);
266 } else if (clients[i]->last_time_used < tardy) {
267 com_err(whoami, 0, "Shutting down connection due to inactivity");
268 shutdown(cur_client->con->in.fd, 0);
274 com_err(whoami, 0, "%s", takedown);
275 sms_close_database();
276 send_zgram("SMS", takedown);
281 * Set up the template connection and queue the first accept.
287 char *service = index(SMS_GDB_SERV, ':') + 1;
289 listencon = create_listening_connection(service);
291 if (listencon == NULL)
294 listenop = create_operation();
295 client_addrlen = sizeof(client_addr);
297 start_accepting_client(listencon, listenop, &newconn,
298 (char *)&client_addr,
299 &client_addrlen, &client_tuple);
306 client_addrlen = sizeof(client_addr);
307 start_accepting_client(listencon, listenop, &newconn,
308 (char *)&client_addr,
309 &client_addrlen, &client_tuple);
313 * This routine is called when a new connection comes in.
315 * It sets up a new client and adds it to the list of currently active clients.
324 * Make sure there's been no error
326 if(OP_STATUS(listenop) != OP_COMPLETE) {
330 if (newconn == NULL) {
331 return SMS_NOT_CONNECTED;
335 * Set up the new connection and reply to the client
337 cp = (client *)malloc(sizeof *cp);
338 bzero(cp, sizeof(*cp));
339 cp->action = CL_ACCEPT;
344 cp->reply.sms_argv = NULL;
347 cp->last_time_used = now;
350 cp->pending_op = create_operation();
351 reset_operation(cp->pending_op);
352 oplist_append(&op_list, cp->pending_op);
356 * Add a new client to the array..
361 * Let him know we heard him.
363 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
366 cp->haddr = client_addr;
369 * Log new connection.
372 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
373 inet_ntoa(cp->haddr.sin_addr),
374 (int)ntohs(cp->haddr.sin_port),
379 * Get ready to accept the next connection.
381 reset_operation(listenop);
382 client_addrlen = sizeof(client_addr);
384 start_accepting_client(listencon, listenop, &newconn,
385 (char *)&client_addr,
386 &client_addrlen, &client_tuple);
391 * Add a new client to the known clients.
400 clients_n = (client **)malloc
401 ((unsigned)(nclients * sizeof(client *)));
402 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
403 clients_n[nclients-1] = cp;
404 free((char *)clients);
414 client **clients_n, **scpp, **dcpp; /* source and dest client */
419 clients_n = (client **)malloc
420 ((unsigned)((nclients - 1)* sizeof(client *)));
421 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
426 if (found_it) abort();
431 free((char *)clients);
434 oplist_delete(op_list, cp->pending_op);
435 reset_operation(cp->pending_op);
436 delete_operation(cp->pending_op);
437 sever_connection(cp->con);
442 * Add a new operation to a list of operations.
444 * This should be rewritten to use realloc instead, since in most
445 * cases it won't have to copy the array.
449 oplist_append(oplp, op)
450 LIST_OF_OPERATIONS *oplp;
453 int count = (*oplp)->count+1;
454 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
455 db_alloc(size_of_list_of_operations(count));
456 bcopy((char *)(*oplp), (char *)newlist,
457 size_of_list_of_operations((*oplp)->count));
459 newlist->op[count-1] = op;
460 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
465 oplist_delete(oplp, op)
466 LIST_OF_OPERATIONS oplp;
467 register OPERATION op;
469 register OPERATION *s;
472 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
492 if (takedown || dormant == ASLEEP)
494 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
495 if (status.w_termsig == 0 && status.w_retcode == 0)
496 com_err(whoami, 0, "child exited successfully");
498 com_err(whoami, 0, "%d: child exits with signal %d status %d",
499 pid, status.w_termsig, status.w_retcode);
509 com_err(whoami, 0, "requested to go dormant");
512 com_err(whoami, 0, "already asleep");
526 com_err(whoami, 0, "Good morning");
529 com_err(whoami, 0, "already awake");