99f0d792fa764e6ba488d1b003aa33885b127b75
[citadel.git] / citadel / housekeeping.c
1 /*
2  * $Id$
3  *
4  * This file contains miscellaneous housekeeping tasks.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 #  include <sys/time.h>
20 # else
21 #  include <time.h>
22 # endif
23 #endif
24
25 #include <ctype.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 #include <sys/select.h>
32 #endif
33 #include "tools.h"
34 #include "citadel.h"
35 #include "server.h"
36 #include "serv_extensions.h"
37 #include "citserver.h"
38 #include "config.h"
39 #include "housekeeping.h"
40 #include "sysdep_decls.h"
41 #include "room_ops.h"
42 #include "database.h"
43 #include "msgbase.h"
44 #include "journaling.h"
45
46 #ifdef HAVE_LIBSIEVE
47 # include "serv_sieve.h"
48 #endif
49
50 /*
51  * Terminate idle sessions.  This function pounds through the session table
52  * comparing the current time to each session's time-of-last-command.  If an
53  * idle session is found it is terminated, then the search restarts at the
54  * beginning because the pointer to our place in the list becomes invalid.
55  */
56 void terminate_idle_sessions(void) {
57         struct CitContext *ccptr;
58         time_t now;
59         int session_to_kill;
60         int killed = 0;
61
62         now = time(NULL);
63         session_to_kill = 0;
64         begin_critical_section(S_SESSION_TABLE);
65         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
66                 if (  (ccptr!=CC)
67                 && (config.c_sleeping > 0)
68                 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
69                         ccptr->kill_me = 1;
70                         ++killed;
71                 }
72         }
73         end_critical_section(S_SESSION_TABLE);
74         if (killed > 0)
75                 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
76 }
77
78
79
80 void check_sched_shutdown(void) {
81         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
82                 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
83                 time_to_die = 1;
84                 master_cleanup(0);
85         }
86 }
87
88
89
90 /*
91  * Check (and fix) floor reference counts.  This doesn't need to be done
92  * very often, since the counts should remain correct during normal operation.
93  */
94 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
95
96         int *new_refcounts;
97
98         new_refcounts = (int *) data;
99
100         ++new_refcounts[(int)qrbuf->QRfloor];
101 }
102
103 void check_ref_counts(void) {
104         struct floor flbuf;
105         int a;
106
107         int new_refcounts[MAXFLOORS];
108
109         lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
110         for (a=0; a<MAXFLOORS; ++a) {
111                 new_refcounts[a] = 0;
112         }
113
114         cdb_begin_transaction();
115         ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
116         cdb_end_transaction();
117
118         for (a=0; a<MAXFLOORS; ++a) {
119                 lgetfloor(&flbuf, a);
120                 flbuf.f_ref_count = new_refcounts[a];
121                 if (new_refcounts[a] > 0) {
122                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
123                 }
124                 else {
125                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
126                 }
127                 lputfloor(&flbuf, a);
128                 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
129         }
130 }       
131
132 /*
133  * This is the housekeeping loop.  Worker threads come through here after
134  * processing client requests but before jumping back into the pool.  We
135  * only allow housekeeping to execute once per minute, and we only allow one
136  * instance to run at a time.
137  */
138 void do_housekeeping(void) {
139         static int housekeeping_in_progress = 0;
140         static time_t last_timer = 0L;
141         int do_housekeeping_now = 0;
142         int do_perminute_housekeeping_now = 0;
143         time_t now;
144
145         /*
146          * We do it this way instead of wrapping the whole loop in an
147          * S_HOUSEKEEPING critical section because it eliminates the need to
148          * potentially have multiple concurrent mutexes in progress.
149          */
150         begin_critical_section(S_HOUSEKEEPING);
151         if (housekeeping_in_progress == 0) {
152                 do_housekeeping_now = 1;
153                 housekeeping_in_progress = 1;
154                 now = time(NULL);
155                 if ( (now - last_timer) > (time_t)60 ) {
156                         do_perminute_housekeeping_now = 1;
157                         last_timer = time(NULL);
158                 }
159         }
160         end_critical_section(S_HOUSEKEEPING);
161
162         if (do_housekeeping_now == 0) {
163                 return;
164         }
165
166         /*
167          * Ok, at this point we've made the decision to run the housekeeping
168          * loop.  Everything below this point is real work.
169          */
170
171         /* First, do the "as often as needed" stuff... */
172         JournalRunQueue();
173
174         PerformSessionHooks(EVT_HOUSE); /* perform as needed housekeeping */
175
176         /* Then, do the "once per minute" stuff... */
177         if (do_perminute_housekeeping_now) {
178                 cdb_check_handles();                    /* suggested by Justin Case */
179                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
180         }
181
182         /*
183          * All done.
184          */
185         housekeeping_in_progress = 0;
186 }