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