4 * This file contains miscellaneous housekeeping tasks.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 #include <sys/select.h>
36 #include "serv_extensions.h"
37 #include "citserver.h"
39 #include "housekeeping.h"
40 #include "sysdep_decls.h"
44 #include "journaling.h"
47 # include "serv_sieve.h"
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.
56 void terminate_idle_sessions(void) {
57 struct CitContext *ccptr;
64 begin_critical_section(S_SESSION_TABLE);
65 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
67 && (config.c_sleeping > 0)
68 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
73 end_critical_section(S_SESSION_TABLE);
75 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
80 void check_sched_shutdown(void) {
81 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
82 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
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.
94 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
98 new_refcounts = (int *) data;
100 ++new_refcounts[(int)qrbuf->QRfloor];
103 void check_ref_counts(void) {
107 int new_refcounts[MAXFLOORS];
109 lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
110 for (a=0; a<MAXFLOORS; ++a) {
111 new_refcounts[a] = 0;
114 cdb_begin_transaction();
115 ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
116 cdb_end_transaction();
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;
125 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
127 lputfloor(&flbuf, a);
128 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
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.
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;
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.
150 begin_critical_section(S_HOUSEKEEPING);
151 if (housekeeping_in_progress == 0) {
152 do_housekeeping_now = 1;
153 housekeeping_in_progress = 1;
155 if ( (now - last_timer) > (time_t)60 ) {
156 do_perminute_housekeeping_now = 1;
157 last_timer = time(NULL);
160 end_critical_section(S_HOUSEKEEPING);
162 if (do_housekeeping_now == 0) {
167 * Ok, at this point we've made the decision to run the housekeeping
168 * loop. Everything below this point is real work.
171 /* First, do the "as often as needed" stuff... */
174 perform_sieve_processing();
177 /* Then, do the "once per minute" stuff... */
178 if (do_perminute_housekeeping_now) {
179 cdb_check_handles(); /* suggested by Justin Case */
180 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
186 housekeeping_in_progress = 0;