Mon Aug 24 20:04:04 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains housekeeping tasks which periodically
3  * need to be executed.
4  *
5  */
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <pthread.h>
15 #include "citadel.h"
16 #include "server.h"
17 #include "citserver.h"
18 #include "config.h"
19 #include "housekeeping.h"
20 #include "sysdep_decls.h"
21 #include "room_ops.h"
22
23 /*
24  * Terminate idle sessions.  This function pounds through the session table
25  * comparing the current time to each session's time-of-last-command.  If an
26  * idle session is found it is terminated, then the search restarts at the
27  * beginning because the pointer to our place in the list becomes invalid.
28  */
29 void terminate_idle_sessions(void) {
30         struct CitContext *ccptr;
31         time_t now;
32         
33         time(&now);
34         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
35                 if (  (ccptr!=CC)
36                    && (config.c_sleeping > 0)
37                    && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
38                         lprintf(3, "Session %d timed out\n", ccptr->cs_pid);
39                         kill_session(ccptr->cs_pid);
40                         ccptr = ContextList;
41                         }
42                 }
43         }
44
45
46 /*
47  * Main housekeeping function.  This gets run whenever a session terminates.
48  */
49 void do_housekeeping(void) {
50
51         begin_critical_section(S_HOUSEKEEPING);
52         /*
53          * Terminate idle sessions.
54          */
55         lprintf(7, "Calling terminate_idle_sessions()\n");
56         terminate_idle_sessions();
57
58         /*
59          * If the server is scheduled to shut down the next time all
60          * users are logged out, now's the time to do it.
61          */
62         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
63                 lprintf(3, "Scheduled shutdown initiating.\n");
64                 master_cleanup();
65                 }
66         end_critical_section(S_HOUSEKEEPING);
67         }
68
69
70
71 /*
72  * Check (and fix) floor reference counts.  This doesn't need to be done
73  * very often, since the counts should remain correct during normal operation.
74  */
75 void check_ref_counts(void) {
76         int ref[MAXFLOORS];
77         struct quickroom qrbuf;
78         struct floor flbuf;
79         int a;
80
81         for (a=0; a<MAXFLOORS; ++a) ref[a] = 0;
82                 
83         for (a=0; a<MAXROOMS; ++a) {
84                 getroom(&qrbuf, a);
85                 if (qrbuf.QRflags & QR_INUSE) {
86                         ++ref[(int)qrbuf.QRfloor];
87                         }
88                 }
89
90         for (a=0; a<MAXFLOORS; ++a) {
91                 lgetfloor(&flbuf, a);
92                 flbuf.f_ref_count = ref[a];
93                 if (ref[a] > 0) flbuf.f_flags = flbuf.f_flags | QR_INUSE ;
94                 lputfloor(&flbuf, a);
95                 }
96         }       
97