42bab2eee3243e26675bdc9d960257c682efdaf7
[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         
35         time(&now);
36         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
37                 if (  (ccptr!=CC)
38                    && (config.c_sleeping > 0)
39                    && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
40                         lprintf(3, "Session %d timed out\n", ccptr->cs_pid);
41                         kill_session(ccptr->cs_pid);
42                         ccptr = ContextList;
43                         }
44                 }
45         }
46
47
48 /*
49  * Main housekeeping function.  This gets run whenever a session terminates.
50  */
51 void do_housekeeping(void) {
52
53         begin_critical_section(S_HOUSEKEEPING);
54         /*
55          * Terminate idle sessions.
56          */
57         lprintf(7, "Calling terminate_idle_sessions()\n");
58         terminate_idle_sessions();
59
60         /*
61          * If the server is scheduled to shut down the next time all
62          * users are logged out, now's the time to do it.
63          */
64         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
65                 lprintf(3, "Scheduled shutdown initiating.\n");
66                 master_cleanup();
67                 }
68         end_critical_section(S_HOUSEKEEPING);
69         }
70
71
72
73 /*
74  * Check (and fix) floor reference counts.  This doesn't need to be done
75  * very often, since the counts should remain correct during normal operation.
76  * NOTE: this function pair should ONLY be called during startup.  It is NOT
77  * thread safe.
78  */
79 void check_ref_counts_backend(struct quickroom *qrbuf) {
80         struct floor flbuf;
81
82         lgetfloor(&flbuf, qrbuf->QRfloor);
83         ++flbuf.f_ref_count;
84         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
85         lputfloor(&flbuf, qrbuf->QRfloor);
86         }
87
88 void check_ref_counts(void) {
89         struct floor flbuf;
90         int a;
91
92         for (a=0; a<MAXFLOORS; ++a) {
93                 lgetfloor(&flbuf, a);
94                 flbuf.f_ref_count = 0;
95                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
96                 lputfloor(&flbuf, a);
97                 }
98
99         ForEachRoom(check_ref_counts_backend);
100         }       
101