]> andersk Git - moira.git/blob - server/mr_main.c
2d76d215bd919071416d1a3b68211fb48135a2b7
[moira.git] / server / mr_main.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
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  * 
17  */
18
19 static char *rcsid_sms_main_c = "$Header$";
20
21 #include <mit-copyright.h>
22 #include <strings.h>
23 #include <sys/types.h>
24 #include <sys/errno.h>
25 #include <sys/signal.h>
26 #include <sys/wait.h>
27 #include <sys/stat.h>
28 #include "sms_server.h"
29 #include <krb_et.h>
30
31 extern CONNECTION newconn, listencon;
32
33 extern int nclients;
34 extern client **clients, *cur_client;
35
36 extern OPERATION listenop;
37 extern LIST_OF_OPERATIONS op_list;
38
39 extern struct sockaddr_in client_addr;
40 extern int client_addrlen;
41 extern TUPLE client_tuple;
42
43 extern char *whoami;
44 extern char buf1[BUFSIZ];
45 extern char *takedown;
46 extern int errno;
47 extern FILE *journal;
48 #define JOURNAL "/u1/sms/journal"
49
50 extern char *malloc();
51 extern int free();
52 extern char *inet_ntoa();
53 extern void sms_com_err();
54 extern void do_client();
55
56 extern int sigshut();
57 void clist_append();
58 void oplist_append();
59 void reapchild(), godormant(), gowakeup();
60
61 extern time_t now;
62
63 /*
64  * Main SMS server loop.
65  *
66  * Initialize the world, then start accepting connections and
67  * making progress on current connections.
68  */
69
70 /*ARGSUSED*/
71 int
72 main(argc, argv)
73         int argc;
74         char **argv;
75 {
76         int status;
77         time_t tardy;
78         struct stat stbuf;
79         
80         whoami = argv[0];
81         /*
82          * Error handler init.
83          */
84         initialize_sms_error_table();
85         initialize_krb_error_table();
86         set_com_err_hook(sms_com_err);
87         setlinebuf(stderr);
88         
89         if (argc != 1) {
90                 com_err(whoami, 0, "Usage: smsd");
91                 exit(1);
92         }               
93
94         /* Profiling implies that getting rid of one level of call
95          * indirection here wins us maybe 1% on the VAX.
96          */
97         gdb_amv = malloc;
98         gdb_fmv = free;
99         
100         /*
101          * GDB initialization.
102          */
103         if(gdb_init() != 0) {
104                 com_err(whoami, 0, "GDB initialization failed.");
105                 exit(1);
106         }
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);
111         
112         /*
113          * Database initialization.  Only init if database should be open.
114          */
115
116         if (stat(SMS_MOTD_FILE, &stbuf) != 0) {
117             if ((status = sms_open_database()) != 0) {
118                 com_err(whoami, status, " when trying to open database.");
119                 exit(1);
120             }
121             sanity_check_database();
122         } else {
123             dormant = ASLEEP;
124             com_err(whoami, 0, "sleeping, not opening database");
125         }
126         
127         sanity_check_queries();
128
129         /*
130          * Set up client array handler.
131          */
132         nclients = 0;
133         clients = (client **) malloc(0);
134         
135         sms_setup_signals();
136         
137         journal = fopen(JOURNAL, "a");
138         if (journal == NULL) {
139             com_err(whoami, errno, " while opening journal file");
140             exit(1);
141         }
142
143         /*
144          * Establish template connection.
145          */
146         if ((status = do_listen()) != 0) {
147                 com_err(whoami, status,
148                         " while trying to create listening connection");
149                 exit(1);
150         }
151         
152         op_list = create_list_of_operations(1, listenop);
153         
154         com_err(whoami, 0, "started (pid %d)", getpid());
155         com_err(whoami, 0, rcsid_sms_main_c);
156         if (dormant != ASLEEP)
157           send_zgram("SMS", "server started");
158         else
159           send_zgram("SMS", "server started, but database closed");
160
161         /*
162          * Run until shut down.
163          */
164         while(!takedown) {              
165                 register int i;
166                 /*
167                  * Block until something happens.
168                  */
169 #ifdef notdef
170                 com_err(whoami, 0, "tick");
171 #endif notdef
172                 if (dormant == SLEEPY) {
173                     sms_close_database();
174                     com_err(whoami, 0, "database closed");
175                     sms_setup_signals();
176                     send_zgram("SMS", "database closed");
177                     dormant = ASLEEP;
178                 } else if (dormant == GROGGY) {
179                     sms_open_database();
180                     com_err(whoami, 0, "database open");
181                     sms_setup_signals();
182                     send_zgram("SMS", "database open again");
183                     dormant = AWAKE;
184                 }
185
186                 errno = 0;
187                 status = op_select_any(op_list, 0,
188                                        (fd_set *)NULL, (fd_set *)NULL,
189                                        (fd_set *)NULL, (struct timeval *)NULL);
190
191                 if (status == -1) {
192                         com_err(whoami, errno, " error from op_select");
193                         continue;
194                 } else if (status != -2) {
195                         com_err(whoami, 0, " wrong return from op_select_any");
196                         continue;
197                 }
198                 if (takedown) break;
199                 time(&now);
200 #ifdef notdef
201                 fprintf(stderr, "    tick\n");
202 #endif notdef
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,
213                                                 " error on listen");
214                                         exit(1);
215                                 }
216                         } else if ((status = new_connection()) != 0) {
217                                 com_err(whoami, errno,
218                                         " Error on listening operation.");
219                                 /*
220                                  * Sleep here to prevent hosing?
221                                  */
222                         }
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                           
242                 }
243                 /*
244                  * Handle any existing connections.
245                  */
246                 tardy = now - 30*60;
247                 
248                 for (i=0; i<nclients; i++) {
249                         cur_client = clients[i];
250                         if (OP_DONE(clients[i]->pending_op)) {
251                                 cur_client->last_time_used = now;
252                                 do_client(cur_client);
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);
256                         }
257                         cur_client = NULL;
258                         if (takedown) break;
259                 }
260         }
261         com_err(whoami, 0, "%s", takedown);
262         sms_close_database();
263         send_zgram("SMS", takedown);
264         return 0;
265 }
266
267 /*
268  * Set up the template connection and queue the first accept.
269  */
270
271 int
272 do_listen()
273 {
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;
288 }
289
290
291 do_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
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  */
304 int
305 new_connection()
306 {
307         register client *cp;
308         static counter = 0;
309         
310         /*
311          * Make sure there's been no error
312          */
313         if(OP_STATUS(listenop) != OP_COMPLETE) {
314                 return errno;
315         }
316         
317         if (newconn == NULL) {
318                 return SMS_NOT_CONNECTED;
319         }
320
321         /*
322          * Set up the new connection and reply to the client
323          */
324         cp = (client *)malloc(sizeof *cp);
325         bzero(cp, sizeof(*cp));
326         cp->action = CL_ACCEPT;
327         cp->con = newconn;
328         cp->id = counter++;
329         cp->args = NULL;
330         cp->clname = NULL;
331         cp->reply.sms_argv = NULL;
332         cp->first = NULL;
333         cp->last = NULL;
334         cp->last_time_used = now;
335         newconn = NULL;
336         
337         cp->pending_op = create_operation();
338         reset_operation(cp->pending_op);
339         oplist_append(&op_list, cp->pending_op);
340         cur_client = cp;
341         
342         /*
343          * Add a new client to the array..
344          */
345         clist_append(cp);
346         
347         /*
348          * Let him know we heard him.
349          */
350         start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
351                                  "", "");
352
353         cp->haddr = client_addr;
354         
355         /*
356          * Log new connection.
357          */
358         
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":"");
364         
365         /*
366          * Get ready to accept the next connection.
367          */
368         reset_operation(listenop);
369         client_addrlen = sizeof(client_addr);
370
371         start_accepting_client(listencon, listenop, &newconn,
372                                (char *)&client_addr,
373                                &client_addrlen, &client_tuple);
374         return 0;
375 }
376
377 /*
378  * Add a new client to the known clients.
379  */
380 void
381 clist_append(cp)
382         client *cp;
383 {               
384         client **clients_n;
385         
386         nclients++;
387         clients_n = (client **)malloc
388                 ((unsigned)(nclients * sizeof(client *)));
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
396                 
397 void
398 clist_delete(cp)
399         client *cp;
400 {
401         client **clients_n, **scpp, **dcpp; /* source and dest client */
402                                             /* ptr ptr */
403         
404         int found_it = 0;
405         
406         clients_n = (client **)malloc
407                 ((unsigned)((nclients - 1)* sizeof(client *)));
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;
421         oplist_delete(op_list, cp->pending_op);
422         reset_operation(cp->pending_op);
423         delete_operation(cp->pending_op);
424         sever_connection(cp->con);
425         free((char *)cp);
426 }
427
428 /*
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.
433  */
434
435 void
436 oplist_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));
445         newlist->count++;
446         newlist->op[count-1] = op;
447         db_free((char *)(*oplp), size_of_list_of_operations(count-1));
448         (*oplp) = newlist;
449 }
450
451
452 oplist_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 }
472
473
474 void reapchild()
475 {
476     union wait status;
477     int pid;
478
479     if (takedown || dormant == ASLEEP)
480       return;
481     while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
482         if  (status.w_termsig == 0 && status.w_retcode == 0)
483           com_err(whoami, 0, "child exited successfully");
484         else
485           com_err(whoami, 0, "%d: child exits with signal %d status %d",
486                   pid, status.w_termsig, status.w_retcode);
487     }
488 }
489
490
491 void godormant()
492 {
493     switch (dormant) {
494     case AWAKE:
495     case GROGGY:
496         com_err(whoami, 0, "requested to go dormant");
497         break;
498     case ASLEEP:
499         com_err(whoami, 0, "already asleep");
500         break;
501     case SLEEPY:
502         break;
503     }
504     dormant = SLEEPY;
505 }
506
507
508 void gowakeup()
509 {
510     switch (dormant) {
511     case ASLEEP:
512     case SLEEPY:
513         com_err(whoami, 0, "Good morning");
514         break;
515     case AWAKE:
516         com_err(whoami, 0, "already awake");
517         break;
518     case GROGGY:
519         break;
520     }
521     dormant = GROGGY;
522 }
523
524         
525 sms_setup_signals()
526 {
527     /* There should probably be a few more of these. */
528         
529     if ((((int)signal (SIGTERM, sigshut)) < 0) ||
530         (((int)signal (SIGCHLD, reapchild)) < 0) ||
531         (((int)signal (SIGUSR1, godormant)) < 0) ||
532         (((int)signal (SIGUSR2, gowakeup)) < 0) ||
533         (((int)signal (SIGHUP, sigshut)) < 0)) {
534         com_err(whoami, errno, " Unable to establish signal handlers.");
535         exit(1);
536     }
537 }
This page took 0.089251 seconds and 3 git commands to generate.