2458fa861ff20e128f802bfcbeeb3a1e9fa81d62
[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  * NOTE: this function pair should ONLY be called during startup.  It is NOT
75  * thread safe.
76  */
77 void check_ref_counts_backend(struct quickroom *qrbuf) {
78         struct floor flbuf;
79
80         lgetfloor(&flbuf, qrbuf->QRfloor);
81         ++flbuf.f_ref_count;
82         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
83         lputfloor(&flbuf, qrbuf->QRfloor);
84         }
85
86 void check_ref_counts(void) {
87         struct floor flbuf;
88         int a;
89
90         for (a=0; a<MAXFLOORS; ++a) {
91                 lgetfloor(&flbuf, a);
92                 flbuf.f_ref_count = 0;
93                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
94                 lputfloor(&flbuf, a);
95                 }
96
97         ForEachRoom(check_ref_counts_backend);
98         }       
99