]> andersk Git - moira.git/blame - server/mr_main.c
moved directory definitions
[moira.git] / server / mr_main.c
CommitLineData
0fa91a0a 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
c801de4c 7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
0fa91a0a 9 *
10 * SMS server process.
11 *
12 * Most of this is stolen from ../gdb/tsr.c
13 *
14 * You are in a maze of twisty little finite automata, all different.
15 * Let the reader beware.
16 *
0fa91a0a 17 */
18
0fa91a0a 19static char *rcsid_sms_main_c = "$Header$";
0fa91a0a 20
c801de4c 21#include <mit-copyright.h>
70d54e43 22#include <strings.h>
589993be 23#include <sys/types.h>
70d54e43 24#include <sys/errno.h>
70d54e43 25#include <sys/signal.h>
554776b1 26#include <sys/wait.h>
589993be 27#include <sys/stat.h>
0fa91a0a 28#include "sms_server.h"
40165bd0 29#include <krb_et.h>
70d54e43 30
0ec57336 31extern CONNECTION newconn, listencon;
0fa91a0a 32
0ec57336 33extern int nclients;
34extern client **clients, *cur_client;
0fa91a0a 35
0ec57336 36extern OPERATION listenop;
37extern LIST_OF_OPERATIONS op_list;
0fa91a0a 38
0ec57336 39extern struct sockaddr_in client_addr;
40extern int client_addrlen;
41extern TUPLE client_tuple;
0fa91a0a 42
0ec57336 43extern char *whoami;
44extern char buf1[BUFSIZ];
45extern char *takedown;
46extern int errno;
2f4ff9cd 47extern FILE *journal;
70d54e43 48
0ec57336 49extern char *malloc();
f5c72293 50extern int free();
0ec57336 51extern char *inet_ntoa();
52extern void sms_com_err();
53extern void do_client();
70d54e43 54
0ec57336 55extern int sigshut();
56void clist_append();
57void oplist_append();
589993be 58void reapchild(), godormant(), gowakeup();
0fa91a0a 59
0eb9f52f 60extern time_t now;
61
0ec57336 62/*
63 * Main SMS server loop.
64 *
65 * Initialize the world, then start accepting connections and
66 * making progress on current connections.
67 */
0fa91a0a 68
0ec57336 69/*ARGSUSED*/
70int
0fa91a0a 71main(argc, argv)
70d54e43 72 int argc;
73 char **argv;
0fa91a0a 74{
0ec57336 75 int status;
0eb9f52f 76 time_t tardy;
589993be 77 struct stat stbuf;
0ec57336 78
79 whoami = argv[0];
80 /*
81 * Error handler init.
82 */
40165bd0 83 initialize_sms_error_table();
84 initialize_krb_error_table();
0ec57336 85 set_com_err_hook(sms_com_err);
b4182127 86 setlinebuf(stderr);
87
0ec57336 88 if (argc != 1) {
89 com_err(whoami, 0, "Usage: smsd");
90 exit(1);
91 }
f5c72293 92
93 /* Profiling implies that getting rid of one level of call
94 * indirection here wins us maybe 1% on the VAX.
95 */
96 gdb_amv = malloc;
97 gdb_fmv = free;
0ec57336 98
99 /*
100 * GDB initialization.
101 */
5dbd09a0 102 if(gdb_init() != 0) {
103 com_err(whoami, 0, "GDB initialization failed.");
104 exit(1);
105 }
7f024a70 106 gdb_debug(0); /* this can be patched, if necessary, to enable */
107 /* GDB level debugging .. */
4a06eb54 108 krb_realm = malloc(REALM_SZ);
42b1d0ae 109 krb_get_lrealm(krb_realm, 1);
7f024a70 110
f6cc38de 111 /*
083a6e94 112 * Database initialization. Only init if database should be open.
f6cc38de 113 */
114
083a6e94 115 if (stat(SMS_MOTD_FILE, &stbuf) != 0) {
116 if ((status = sms_open_database()) != 0) {
2f4ff9cd 117 com_err(whoami, status, " when trying to open database.");
f6cc38de 118 exit(1);
083a6e94 119 }
120 sanity_check_database();
121 } else {
122 dormant = ASLEEP;
123 com_err(whoami, 0, "sleeping, not opening database");
f6cc38de 124 }
125
2f4ff9cd 126 sanity_check_queries();
2f4ff9cd 127
0ec57336 128 /*
129 * Set up client array handler.
130 */
0fa91a0a 131 nclients = 0;
132 clients = (client **) malloc(0);
70d54e43 133
083a6e94 134 sms_setup_signals();
0ec57336 135
2f4ff9cd 136 journal = fopen(JOURNAL, "a");
137 if (journal == NULL) {
138 com_err(whoami, errno, " while opening journal file");
139 exit(1);
140 }
141
0ec57336 142 /*
143 * Establish template connection.
144 */
145 if ((status = do_listen()) != 0) {
146 com_err(whoami, status,
2f4ff9cd 147 " while trying to create listening connection");
0ec57336 148 exit(1);
149 }
0fa91a0a 150
0fa91a0a 151 op_list = create_list_of_operations(1, listenop);
0fa91a0a 152
b4182127 153 com_err(whoami, 0, "started (pid %d)", getpid());
0ec57336 154 com_err(whoami, 0, rcsid_sms_main_c);
083a6e94 155 if (dormant != ASLEEP)
156 send_zgram("SMS", "server started");
157 else
158 send_zgram("SMS", "server started, but database closed");
0ec57336 159
160 /*
161 * Run until shut down.
162 */
70d54e43 163 while(!takedown) {
0ec57336 164 register int i;
165 /*
166 * Block until something happens.
167 */
5dbd09a0 168#ifdef notdef
169 com_err(whoami, 0, "tick");
170#endif notdef
589993be 171 if (dormant == SLEEPY) {
172 sms_close_database();
173 com_err(whoami, 0, "database closed");
083a6e94 174 sms_setup_signals();
589993be 175 send_zgram("SMS", "database closed");
176 dormant = ASLEEP;
177 } else if (dormant == GROGGY) {
178 sms_open_database();
179 com_err(whoami, 0, "database open");
083a6e94 180 sms_setup_signals();
589993be 181 send_zgram("SMS", "database open again");
182 dormant = AWAKE;
183 }
184
5dbd09a0 185 errno = 0;
186 status = op_select_any(op_list, 0,
187 (fd_set *)NULL, (fd_set *)NULL,
188 (fd_set *)NULL, (struct timeval *)NULL);
189
190 if (status == -1) {
42b1d0ae 191 if (errno != EINTR)
192 com_err(whoami, errno, " error from op_select");
5dbd09a0 193 continue;
194 } else if (status != -2) {
2f4ff9cd 195 com_err(whoami, 0, " wrong return from op_select_any");
5dbd09a0 196 continue;
197 }
70d54e43 198 if (takedown) break;
0eb9f52f 199 time(&now);
70d54e43 200#ifdef notdef
201 fprintf(stderr, " tick\n");
202#endif notdef
b4182127 203 /*
204 * Handle any new connections; this comes first so
205 * errno isn't tromped on.
206 */
207 if (OP_DONE(listenop)) {
208 if (OP_STATUS(listenop) == OP_CANCELLED) {
209 if (errno == EWOULDBLOCK) {
210 do_reset_listen();
211 } else {
212 com_err(whoami, errno,
2f4ff9cd 213 " error on listen");
b4182127 214 exit(1);
215 }
216 } else if ((status = new_connection()) != 0) {
217 com_err(whoami, errno,
2f4ff9cd 218 " Error on listening operation.");
b4182127 219 /*
220 * Sleep here to prevent hosing?
221 */
222 }
589993be 223 /* if the new connection is our only connection,
224 * and the server is supposed to be down, then go
225 * down now.
226 */
227 if ((dormant == AWAKE) && (nclients == 1) &&
228 (stat(SMS_MOTD_FILE, &stbuf) == 0)) {
229 com_err(whoami, 0, "motd file exists, slumbertime");
230 dormant = SLEEPY;
231 }
232 /* on new connection, if we are no longer supposed
233 * to be down, then wake up.
234 */
235 if ((dormant == ASLEEP) &&
236 (stat(SMS_MOTD_FILE, &stbuf) == -1) &&
237 (errno == ENOENT)) {
238 com_err(whoami, 0, "motd file no longer exists, waking up");
239 dormant = GROGGY;
240 }
241
b4182127 242 }
0ec57336 243 /*
244 * Handle any existing connections.
245 */
0eb9f52f 246 tardy = now - 30*60;
247
0fa91a0a 248 for (i=0; i<nclients; i++) {
0eb9f52f 249 cur_client = clients[i];
0fa91a0a 250 if (OP_DONE(clients[i]->pending_op)) {
0eb9f52f 251 cur_client->last_time_used = now;
70d54e43 252 do_client(cur_client);
0eb9f52f 253 } else if (clients[i]->last_time_used < tardy) {
254 com_err(whoami, 0, "Shutting down connection due to inactivity");
255 shutdown(cur_client->con->in.fd, 0);
0fa91a0a 256 }
0eb9f52f 257 cur_client = NULL;
258 if (takedown) break;
0fa91a0a 259 }
0fa91a0a 260 }
b4182127 261 com_err(whoami, 0, "%s", takedown);
f6cc38de 262 sms_close_database();
49700f8b 263 send_zgram("SMS", takedown);
0ec57336 264 return 0;
70d54e43 265}
266
0ec57336 267/*
268 * Set up the template connection and queue the first accept.
269 */
270
271int
272do_listen()
70d54e43 273{
0ec57336 274 char *service = index(SMS_GDB_SERV, ':') + 1;
275
276 listencon = create_listening_connection(service);
277
278 if (listencon == NULL)
279 return errno;
280
281 listenop = create_operation();
282 client_addrlen = sizeof(client_addr);
283
284 start_accepting_client(listencon, listenop, &newconn,
285 (char *)&client_addr,
286 &client_addrlen, &client_tuple);
287 return 0;
0fa91a0a 288}
289
b4182127 290
291do_reset_listen()
292{
293 client_addrlen = sizeof(client_addr);
294 start_accepting_client(listencon, listenop, &newconn,
295 (char *)&client_addr,
296 &client_addrlen, &client_tuple);
297}
298
0ec57336 299/*
300 * This routine is called when a new connection comes in.
301 *
302 * It sets up a new client and adds it to the list of currently active clients.
303 */
304int
5dbd09a0 305new_connection()
0fa91a0a 306{
7f024a70 307 register client *cp;
70d54e43 308 static counter = 0;
0fa91a0a 309
0fa91a0a 310 /*
311 * Make sure there's been no error
312 */
c27b3454 313 if(OP_STATUS(listenop) != OP_COMPLETE) {
0ec57336 314 return errno;
c27b3454 315 }
316
317 if (newconn == NULL) {
318 return SMS_NOT_CONNECTED;
0fa91a0a 319 }
320
0fa91a0a 321 /*
322 * Set up the new connection and reply to the client
323 */
7f024a70 324 cp = (client *)malloc(sizeof *cp);
23e476e8 325 bzero(cp, sizeof(*cp));
0fa91a0a 326 cp->action = CL_ACCEPT;
327 cp->con = newconn;
70d54e43 328 cp->id = counter++;
0ec57336 329 cp->args = NULL;
42b1d0ae 330 cp->clname[0] = NULL;
0ec57336 331 cp->reply.sms_argv = NULL;
4a06eb54 332 cp->first = NULL;
333 cp->last = NULL;
0eb9f52f 334 cp->last_time_used = now;
0fa91a0a 335 newconn = NULL;
336
337 cp->pending_op = create_operation();
338 reset_operation(cp->pending_op);
339 oplist_append(&op_list, cp->pending_op);
70d54e43 340 cur_client = cp;
341
0ec57336 342 /*
343 * Add a new client to the array..
344 */
345 clist_append(cp);
346
347 /*
348 * Let him know we heard him.
349 */
0fa91a0a 350 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
351 "", "");
0ec57336 352
353 cp->haddr = client_addr;
354
355 /*
356 * Log new connection.
357 */
358
b4182127 359 com_err(whoami, 0, "New connection from %s port %d (now %d client%s)",
360 inet_ntoa(cp->haddr.sin_addr),
361 (int)ntohs(cp->haddr.sin_port),
362 nclients,
363 nclients!=1?"s":"");
0fa91a0a 364
365 /*
0ec57336 366 * Get ready to accept the next connection.
0fa91a0a 367 */
368 reset_operation(listenop);
0ec57336 369 client_addrlen = sizeof(client_addr);
0fa91a0a 370
371 start_accepting_client(listencon, listenop, &newconn,
0ec57336 372 (char *)&client_addr,
373 &client_addrlen, &client_tuple);
374 return 0;
0fa91a0a 375}
376
0fa91a0a 377/*
378 * Add a new client to the known clients.
379 */
0ec57336 380void
0fa91a0a 381clist_append(cp)
382 client *cp;
383{
384 client **clients_n;
385
386 nclients++;
0ec57336 387 clients_n = (client **)malloc
388 ((unsigned)(nclients * sizeof(client *)));
0fa91a0a 389 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
390 clients_n[nclients-1] = cp;
391 free((char *)clients);
392 clients = clients_n;
393 clients_n = NULL;
394}
395
70d54e43 396
0ec57336 397void
70d54e43 398clist_delete(cp)
399 client *cp;
400{
70d54e43 401 client **clients_n, **scpp, **dcpp; /* source and dest client */
402 /* ptr ptr */
403
70d54e43 404 int found_it = 0;
405
0ec57336 406 clients_n = (client **)malloc
407 ((unsigned)((nclients - 1)* sizeof(client *)));
70d54e43 408 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
409 if (*scpp != cp) {
410 *dcpp++ = *scpp++;
411 } else {
412 scpp++;
413 if (found_it) abort();
414 found_it = 1;
415 }
416 }
417 --nclients;
418 free((char *)clients);
419 clients = clients_n;
420 clients_n = NULL;
c27b3454 421 oplist_delete(op_list, cp->pending_op);
70d54e43 422 reset_operation(cp->pending_op);
423 delete_operation(cp->pending_op);
7f024a70 424 sever_connection(cp->con);
0ec57336 425 free((char *)cp);
70d54e43 426}
0ec57336 427
0fa91a0a 428/*
0ec57336 429 * Add a new operation to a list of operations.
430 *
431 * This should be rewritten to use realloc instead, since in most
432 * cases it won't have to copy the array.
0fa91a0a 433 */
434
0ec57336 435void
0fa91a0a 436oplist_append(oplp, op)
437 LIST_OF_OPERATIONS *oplp;
438 OPERATION op;
439{
440 int count = (*oplp)->count+1;
441 LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS)
442 db_alloc(size_of_list_of_operations(count));
443 bcopy((char *)(*oplp), (char *)newlist,
444 size_of_list_of_operations((*oplp)->count));
0fa91a0a 445 newlist->count++;
446 newlist->op[count-1] = op;
467f5982 447 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
0fa91a0a 448 (*oplp) = newlist;
449}
450
c27b3454 451
452oplist_delete(oplp, op)
453 LIST_OF_OPERATIONS oplp;
454 register OPERATION op;
455{
456 register OPERATION *s;
457 register int c;
458
459 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
460 if (*s == op) {
461 while (c > 0) {
462 *s = *(s+1);
463 ++s;
464 --c;
465 }
466 oplp->count--;
467 return;
468 }
469 }
470 abort();
471}
554776b1 472
473
474void reapchild()
475{
476 union wait status;
477 int pid;
478
479 while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
42b1d0ae 480 if (!takedown && (status.w_termsig != 0 || status.w_retcode != 0))
589993be 481 com_err(whoami, 0, "%d: child exits with signal %d status %d",
554776b1 482 pid, status.w_termsig, status.w_retcode);
483 }
484}
589993be 485
486
487void godormant()
488{
489 switch (dormant) {
490 case AWAKE:
491 case GROGGY:
492 com_err(whoami, 0, "requested to go dormant");
493 break;
494 case ASLEEP:
495 com_err(whoami, 0, "already asleep");
496 break;
497 case SLEEPY:
498 break;
499 }
500 dormant = SLEEPY;
501}
502
503
504void gowakeup()
505{
506 switch (dormant) {
507 case ASLEEP:
508 case SLEEPY:
509 com_err(whoami, 0, "Good morning");
510 break;
511 case AWAKE:
512 com_err(whoami, 0, "already awake");
513 break;
514 case GROGGY:
515 break;
516 }
517 dormant = GROGGY;
518}
083a6e94 519
520
521sms_setup_signals()
522{
523 /* There should probably be a few more of these. */
524
525 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
526 (((int)signal (SIGCHLD, reapchild)) < 0) ||
527 (((int)signal (SIGUSR1, godormant)) < 0) ||
528 (((int)signal (SIGUSR2, gowakeup)) < 0) ||
529 (((int)signal (SIGHUP, sigshut)) < 0)) {
530 com_err(whoami, errno, " Unable to establish signal handlers.");
531 exit(1);
532 }
533}
This page took 0.145341 seconds and 5 git commands to generate.