7fed1541883efc9f4abdf397554d52a1bb2f9455
[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 <libcitadel.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 #include "ctdl_module.h"
47 #include "threads.h"
48
49 /*
50  * Terminate idle sessions.  This function pounds through the session table
51  * comparing the current time to each session's time-of-last-command.  If an
52  * idle session is found it is terminated, then the search restarts at the
53  * beginning because the pointer to our place in the list becomes invalid.
54  */
55 void terminate_idle_sessions(void) {
56         struct CitContext *ccptr;
57         time_t now;
58         int session_to_kill;
59         int killed = 0;
60         int longrunners = 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                         if (!ccptr->dont_term) {
70                                 ccptr->kill_me = 1;
71                                 ++killed;
72                         }
73                         else 
74                                 longrunners ++;
75                 }
76         }
77         end_critical_section(S_SESSION_TABLE);
78         if (killed > 0)
79                 CtdlLogPrintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
80         if (longrunners > 0)
81                 CtdlLogPrintf(CTDL_INFO, "Didn't terminate %d protected idle sessions;\n", killed);
82 }
83
84
85
86 void check_sched_shutdown(void) {
87         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
88                 CtdlLogPrintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
89                 CtdlThreadStopAll();
90         }
91 }
92
93
94
95 /*
96  * Check (and fix) floor reference counts.  This doesn't need to be done
97  * very often, since the counts should remain correct during normal operation.
98  */
99 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
100
101         int *new_refcounts;
102
103         new_refcounts = (int *) data;
104
105         ++new_refcounts[(int)qrbuf->QRfloor];
106 }
107
108 void check_ref_counts(void) {
109         struct floor flbuf;
110         int a;
111
112         int new_refcounts[MAXFLOORS];
113
114         CtdlLogPrintf(CTDL_DEBUG, "Checking floor reference counts\n");
115         for (a=0; a<MAXFLOORS; ++a) {
116                 new_refcounts[a] = 0;
117         }
118
119         cdb_begin_transaction();
120         ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
121         cdb_end_transaction();
122
123         for (a=0; a<MAXFLOORS; ++a) {
124                 lgetfloor(&flbuf, a);
125                 flbuf.f_ref_count = new_refcounts[a];
126                 if (new_refcounts[a] > 0) {
127                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
128                 }
129                 else {
130                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
131                 }
132                 lputfloor(&flbuf, a);
133                 CtdlLogPrintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
134         }
135 }       
136
137 /*
138  * This is the housekeeping loop.  Worker threads come through here after
139  * processing client requests but before jumping back into the pool.  We
140  * only allow housekeeping to execute once per minute, and we only allow one
141  * instance to run at a time.
142  */
143 void do_housekeeping(void) {
144         static int housekeeping_in_progress = 0;
145         static time_t last_timer = 0L;
146         int do_housekeeping_now = 0;
147         int do_perminute_housekeeping_now = 0;
148         time_t now;
149         const char *old_name;
150
151         /*
152          * We do it this way instead of wrapping the whole loop in an
153          * S_HOUSEKEEPING critical section because it eliminates the need to
154          * potentially have multiple concurrent mutexes in progress.
155          */
156         begin_critical_section(S_HOUSEKEEPING);
157         if (housekeeping_in_progress == 0) {
158                 do_housekeeping_now = 1;
159                 housekeeping_in_progress = 1;
160                 now = time(NULL);
161                 if ( (now - last_timer) > (time_t)60 ) {
162                         do_perminute_housekeeping_now = 1;
163                         last_timer = time(NULL);
164                 }
165         }
166         end_critical_section(S_HOUSEKEEPING);
167
168         if (do_housekeeping_now == 0) {
169                 return;
170         }
171
172         /*
173          * Ok, at this point we've made the decision to run the housekeeping
174          * loop.  Everything below this point is real work.
175          */
176
177         /* First, do the "as often as needed" stuff... */
178         old_name = CtdlThreadName("House Keeping - Journal");
179         JournalRunQueue();
180
181         CtdlThreadName("House Keeping - EVT_HOUSE");
182         PerformSessionHooks(EVT_HOUSE); /* perform as needed housekeeping */
183
184         /* Then, do the "once per minute" stuff... */
185         if (do_perminute_housekeeping_now) {
186                 cdb_check_handles();                    /* suggested by Justin Case */
187                 CtdlThreadName("House Keeping - EVT_TIMER");
188                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
189         }
190
191         /*
192          * All done.
193          */
194         housekeeping_in_progress = 0;
195         CtdlThreadName(old_name);
196 }