]> andersk Git - moira.git/blame - server/mr_main.c
added support for server going dormant; and a motd operation
[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>
23#include <sys/errno.h>
70d54e43 24#include <sys/signal.h>
554776b1 25#include <sys/wait.h>
0fa91a0a 26#include "sms_server.h"
40165bd0 27#include <krb_et.h>
70d54e43 28
0ec57336 29extern CONNECTION newconn, listencon;
0fa91a0a 30
0ec57336 31extern int nclients;
32extern client **clients, *cur_client;
0fa91a0a 33
0ec57336 34extern OPERATION listenop;
35extern LIST_OF_OPERATIONS op_list;
0fa91a0a 36
0ec57336 37extern struct sockaddr_in client_addr;
38extern int client_addrlen;
39extern TUPLE client_tuple;
0fa91a0a 40
0ec57336 41extern char *whoami;
42extern char buf1[BUFSIZ];
43extern char *takedown;
44extern int errno;
2f4ff9cd 45extern FILE *journal;
46#define JOURNAL "/u1/sms/journal"
70d54e43 47
0ec57336 48extern char *malloc();
f5c72293 49extern int free();
0ec57336 50extern char *inet_ntoa();
51extern void sms_com_err();
52extern void do_client();
70d54e43 53
0ec57336 54extern int sigshut();
55void clist_append();
56void oplist_append();
554776b1 57void reapchild();
0fa91a0a 58
0eb9f52f 59extern time_t now;
60
0ec57336 61/*
62 * Main SMS server loop.
63 *
64 * Initialize the world, then start accepting connections and
65 * making progress on current connections.
66 */
0fa91a0a 67
0ec57336 68/*ARGSUSED*/
69int
0fa91a0a 70main(argc, argv)
70d54e43 71 int argc;
72 char **argv;
0fa91a0a 73{
0ec57336 74 int status;
0eb9f52f 75 time_t tardy;
0ec57336 76
77 whoami = argv[0];
78 /*
79 * Error handler init.
80 */
40165bd0 81 initialize_sms_error_table();
82 initialize_krb_error_table();
0ec57336 83 set_com_err_hook(sms_com_err);
b4182127 84 setlinebuf(stderr);
85
0ec57336 86 if (argc != 1) {
87 com_err(whoami, 0, "Usage: smsd");
88 exit(1);
89 }
f5c72293 90
91 /* Profiling implies that getting rid of one level of call
92 * indirection here wins us maybe 1% on the VAX.
93 */
94 gdb_amv = malloc;
95 gdb_fmv = free;
0ec57336 96
97 /*
98 * GDB initialization.
99 */
5dbd09a0 100 if(gdb_init() != 0) {
101 com_err(whoami, 0, "GDB initialization failed.");
102 exit(1);
103 }
7f024a70 104 gdb_debug(0); /* this can be patched, if necessary, to enable */
105 /* GDB level debugging .. */
4a06eb54 106 krb_realm = malloc(REALM_SZ);
107 get_krbrlm(krb_realm, 1);
7f024a70 108
f6cc38de 109 /*
110 * Database initialization.
111 */
112
113 if ((status = sms_open_database()) != 0) {
2f4ff9cd 114 com_err(whoami, status, " when trying to open database.");
f6cc38de 115 exit(1);
116 }
117
2f4ff9cd 118 sanity_check_queries();
119 sanity_check_database();
120
0ec57336 121 /*
122 * Set up client array handler.
123 */
0fa91a0a 124 nclients = 0;
125 clients = (client **) malloc(0);
70d54e43 126
0ec57336 127 /*
128 * Signal handlers
f6cc38de 129 * There should probably be a few more of these.
0ec57336 130 */
70d54e43 131
0ec57336 132 if ((((int)signal (SIGTERM, sigshut)) < 0) ||
ce81c728 133 (((int)signal (SIGCHLD, reapchild)) < 0) ||
0ec57336 134 (((int)signal (SIGHUP, sigshut)) < 0)) {
2f4ff9cd 135 com_err(whoami, errno, " Unable to establish signal handler.");
0ec57336 136 exit(1);
137 }
138
2f4ff9cd 139 journal = fopen(JOURNAL, "a");
140 if (journal == NULL) {
141 com_err(whoami, errno, " while opening journal file");
142 exit(1);
143 }
144
0ec57336 145 /*
146 * Establish template connection.
147 */
148 if ((status = do_listen()) != 0) {
149 com_err(whoami, status,
2f4ff9cd 150 " while trying to create listening connection");
0ec57336 151 exit(1);
152 }
0fa91a0a 153
0fa91a0a 154 op_list = create_list_of_operations(1, listenop);
0fa91a0a 155
b4182127 156 com_err(whoami, 0, "started (pid %d)", getpid());
0ec57336 157 com_err(whoami, 0, rcsid_sms_main_c);
49700f8b 158 send_zgram("SMS", "server started");
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
171 errno = 0;
172 status = op_select_any(op_list, 0,
173 (fd_set *)NULL, (fd_set *)NULL,
174 (fd_set *)NULL, (struct timeval *)NULL);
175
176 if (status == -1) {
2f4ff9cd 177 com_err(whoami, errno, " error from op_select");
5dbd09a0 178 continue;
179 } else if (status != -2) {
2f4ff9cd 180 com_err(whoami, 0, " wrong return from op_select_any");
5dbd09a0 181 continue;
182 }
70d54e43 183 if (takedown) break;
0eb9f52f 184 time(&now);
70d54e43 185#ifdef notdef
186 fprintf(stderr, " tick\n");
187#endif notdef
b4182127 188 /*
189 * Handle any new connections; this comes first so
190 * errno isn't tromped on.
191 */
192 if (OP_DONE(listenop)) {
193 if (OP_STATUS(listenop) == OP_CANCELLED) {
194 if (errno == EWOULDBLOCK) {
195 do_reset_listen();
196 } else {
197 com_err(whoami, errno,
2f4ff9cd 198 " error on listen");
b4182127 199 exit(1);
200 }
201 } else if ((status = new_connection()) != 0) {
202 com_err(whoami, errno,
2f4ff9cd 203 " Error on listening operation.");
b4182127 204 /*
205 * Sleep here to prevent hosing?
206 */
207 }
208 }
0ec57336 209 /*
210 * Handle any existing connections.
211 */
0eb9f52f 212 tardy = now - 30*60;
213
0fa91a0a 214 for (i=0; i<nclients; i++) {
0eb9f52f 215 cur_client = clients[i];
0fa91a0a 216 if (OP_DONE(clients[i]->pending_op)) {
0eb9f52f 217 cur_client->last_time_used = now;
70d54e43 218 do_client(cur_client);
0eb9f52f 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);
0fa91a0a 222 }
0eb9f52f 223 cur_client = NULL;
224 if (takedown) break;
0fa91a0a 225 }
0fa91a0a 226 }
b4182127 227 com_err(whoami, 0, "%s", takedown);
f6cc38de 228 sms_close_database();
49700f8b 229 send_zgram("SMS", takedown);
0ec57336 230 return 0;
70d54e43 231}
232
0ec57336 233/*
234 * Set up the template connection and queue the first accept.
235 */
236
237int
238do_listen()
70d54e43 239{
0ec57336 240 char *service = index(SMS_GDB_SERV, ':') + 1;
241
242 listencon = create_listening_connection(service);
243
244 if (listencon == NULL)
245 return errno;
246
247 listenop = create_operation();
248 client_addrlen = sizeof(client_addr);
249
250 start_accepting_client(listencon, listenop, &newconn,
251 (char *)&client_addr,
252 &client_addrlen, &client_tuple);
253 return 0;
0fa91a0a 254}
255
b4182127 256
257do_reset_listen()
258{
259 client_addrlen = sizeof(client_addr);
260 start_accepting_client(listencon, listenop, &newconn,
261 (char *)&client_addr,
262 &client_addrlen, &client_tuple);
263}
264
0ec57336 265/*
266 * This routine is called when a new connection comes in.
267 *
268 * It sets up a new client and adds it to the list of currently active clients.
269 */
270int
5dbd09a0 271new_connection()
0fa91a0a 272{
7f024a70 273 register client *cp;
70d54e43 274 static counter = 0;
0fa91a0a 275
0fa91a0a 276 /*
277 * Make sure there's been no error
278 */
c27b3454 279 if(OP_STATUS(listenop) != OP_COMPLETE) {
0ec57336 280 return errno;
c27b3454 281 }
282
283 if (newconn == NULL) {
284 return SMS_NOT_CONNECTED;
0fa91a0a 285 }
286
0fa91a0a 287 /*
288 * Set up the new connection and reply to the client
289 */
7f024a70 290 cp = (client *)malloc(sizeof *cp);
23e476e8 291 bzero(cp, sizeof(*cp));
0fa91a0a 292 cp->action = CL_ACCEPT;
293 cp->con = newconn;
70d54e43 294 cp->id = counter++;
0ec57336 295 cp->args = NULL;
296 cp->clname = NULL;
297 cp->reply.sms_argv = NULL;
4a06eb54 298 cp->first = NULL;
299 cp->last = NULL;
0eb9f52f 300 cp->last_time_used = now;
0fa91a0a 301 newconn = NULL;
302
303 cp->pending_op = create_operation();
304 reset_operation(cp->pending_op);
305 oplist_append(&op_list, cp->pending_op);
70d54e43 306 cur_client = cp;
307
0ec57336 308 /*
309 * Add a new client to the array..
310 */
311 clist_append(cp);
312
313 /*
314 * Let him know we heard him.
315 */
0fa91a0a 316 start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
317 "", "");
0ec57336 318
319 cp->haddr = client_addr;
320
321 /*
322 * Log new connection.
323 */
324
b4182127 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),
328 nclients,
329 nclients!=1?"s":"");
0fa91a0a 330
331 /*
0ec57336 332 * Get ready to accept the next connection.
0fa91a0a 333 */
334 reset_operation(listenop);
0ec57336 335 client_addrlen = sizeof(client_addr);
0fa91a0a 336
337 start_accepting_client(listencon, listenop, &newconn,
0ec57336 338 (char *)&client_addr,
339 &client_addrlen, &client_tuple);
340 return 0;
0fa91a0a 341}
342
0fa91a0a 343/*
344 * Add a new client to the known clients.
345 */
0ec57336 346void
0fa91a0a 347clist_append(cp)
348 client *cp;
349{
350 client **clients_n;
351
352 nclients++;
0ec57336 353 clients_n = (client **)malloc
354 ((unsigned)(nclients * sizeof(client *)));
0fa91a0a 355 bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp));
356 clients_n[nclients-1] = cp;
357 free((char *)clients);
358 clients = clients_n;
359 clients_n = NULL;
360}
361
70d54e43 362
0ec57336 363void
70d54e43 364clist_delete(cp)
365 client *cp;
366{
70d54e43 367 client **clients_n, **scpp, **dcpp; /* source and dest client */
368 /* ptr ptr */
369
70d54e43 370 int found_it = 0;
371
0ec57336 372 clients_n = (client **)malloc
373 ((unsigned)((nclients - 1)* sizeof(client *)));
70d54e43 374 for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) {
375 if (*scpp != cp) {
376 *dcpp++ = *scpp++;
377 } else {
378 scpp++;
379 if (found_it) abort();
380 found_it = 1;
381 }
382 }
383 --nclients;
384 free((char *)clients);
385 clients = clients_n;
386 clients_n = NULL;
c27b3454 387 oplist_delete(op_list, cp->pending_op);
70d54e43 388 reset_operation(cp->pending_op);
389 delete_operation(cp->pending_op);
7f024a70 390 sever_connection(cp->con);
0ec57336 391 free((char *)cp);
70d54e43 392}
0ec57336 393
0fa91a0a 394/*
0ec57336 395 * Add a new operation to a list of operations.
396 *
397 * This should be rewritten to use realloc instead, since in most
398 * cases it won't have to copy the array.
0fa91a0a 399 */
400
0ec57336 401void
0fa91a0a 402oplist_append(oplp, op)
403 LIST_OF_OPERATIONS *oplp;
404 OPERATION op;
405{
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));
0fa91a0a 411 newlist->count++;
412 newlist->op[count-1] = op;
467f5982 413 db_free((char *)(*oplp), size_of_list_of_operations(count-1));
0fa91a0a 414 (*oplp) = newlist;
415}
416
c27b3454 417
418oplist_delete(oplp, op)
419 LIST_OF_OPERATIONS oplp;
420 register OPERATION op;
421{
422 register OPERATION *s;
423 register int c;
424
425 for (s = oplp->op, c=oplp->count; c; --c, ++s) {
426 if (*s == op) {
427 while (c > 0) {
428 *s = *(s+1);
429 ++s;
430 --c;
431 }
432 oplp->count--;
433 return;
434 }
435 }
436 abort();
437}
554776b1 438
439
440void reapchild()
441{
442 union wait status;
443 int pid;
444
beebc379 445 if (takedown)
446 return;
554776b1 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");
450 else
451 com_err(whoami, 0, "%d: startdcm exits with signal %d status %d",
452 pid, status.w_termsig, status.w_retcode);
453 }
454}
This page took 0.187279 seconds and 5 git commands to generate.