Changeover to new room structure. See ChangeLog for details.
[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 <limits.h>
15 #include <pthread.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include "citserver.h"
19 #include "config.h"
20 #include "housekeeping.h"
21 #include "sysdep_decls.h"
22 #include "room_ops.h"
23
24 /*
25  * Terminate idle sessions.  This function pounds through the session table
26  * comparing the current time to each session's time-of-last-command.  If an
27  * idle session is found it is terminated, then the search restarts at the
28  * beginning because the pointer to our place in the list becomes invalid.
29  */
30 void terminate_idle_sessions(void) {
31         struct CitContext *ccptr;
32         time_t now;
33         
34         time(&now);
35         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
36                 if (  (ccptr!=CC)
37                    && (config.c_sleeping > 0)
38                    && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
39                         lprintf(3, "Session %d timed out\n", ccptr->cs_pid);
40                         kill_session(ccptr->cs_pid);
41                         ccptr = ContextList;
42                         }
43                 }
44         }
45
46
47 /*
48  * Main housekeeping function.  This gets run whenever a session terminates.
49  */
50 void do_housekeeping(void) {
51
52         begin_critical_section(S_HOUSEKEEPING);
53         /*
54          * Terminate idle sessions.
55          */
56         lprintf(7, "Calling terminate_idle_sessions()\n");
57         terminate_idle_sessions();
58
59         /*
60          * If the server is scheduled to shut down the next time all
61          * users are logged out, now's the time to do it.
62          */
63         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
64                 lprintf(3, "Scheduled shutdown initiating.\n");
65                 master_cleanup();
66                 }
67         end_critical_section(S_HOUSEKEEPING);
68         }
69
70
71
72 /*
73  * Check (and fix) floor reference counts.  This doesn't need to be done
74  * very often, since the counts should remain correct during normal operation.
75  * NOTE: this function pair should ONLY be called during startup.  It is NOT
76  * thread safe.
77  */
78 void check_ref_counts_backend(struct quickroom *qrbuf) {
79         struct floor flbuf;
80
81         lgetfloor(&flbuf, qrbuf->QRfloor);
82         ++flbuf.f_ref_count;
83         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
84         lputfloor(&flbuf, qrbuf->QRfloor);
85         }
86
87 void check_ref_counts(void) {
88         struct floor flbuf;
89         int a;
90
91         for (a=0; a<MAXFLOORS; ++a) {
92                 lgetfloor(&flbuf, a);
93                 flbuf.f_ref_count = 0;
94                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
95                 lputfloor(&flbuf, a);
96                 }
97
98         ForEachRoom(check_ref_counts_backend);
99         }       
100