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