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"
48 * Terminate idle sessions. This function pounds through the session table
49 * comparing the current time to each session's time-of-last-command. If an
50 * idle session is found it is terminated, then the search restarts at the
51 * beginning because the pointer to our place in the list becomes invalid.
53 void terminate_idle_sessions(void) {
54 struct CitContext *ccptr;
61 begin_critical_section(S_SESSION_TABLE);
62 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
64 && (config.c_sleeping > 0)
65 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
70 end_critical_section(S_SESSION_TABLE);
72 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
77 void check_sched_shutdown(void) {
78 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
79 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
88 * Check (and fix) floor reference counts. This doesn't need to be done
89 * very often, since the counts should remain correct during normal operation.
91 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
95 new_refcounts = (int *) data;
97 ++new_refcounts[(int)qrbuf->QRfloor];
100 void check_ref_counts(void) {
104 int new_refcounts[MAXFLOORS];
106 lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
107 for (a=0; a<MAXFLOORS; ++a) {
108 new_refcounts[a] = 0;
111 cdb_begin_transaction();
112 ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
113 cdb_end_transaction();
115 for (a=0; a<MAXFLOORS; ++a) {
116 lgetfloor(&flbuf, a);
117 flbuf.f_ref_count = new_refcounts[a];
118 if (new_refcounts[a] > 0) {
119 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
122 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
124 lputfloor(&flbuf, a);
125 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
130 * This is the housekeeping loop. Worker threads come through here after
131 * processing client requests but before jumping back into the pool. We
132 * only allow housekeeping to execute once per minute, and we only allow one
133 * instance to run at a time.
135 void do_housekeeping(void) {
136 static int housekeeping_in_progress = 0;
137 static time_t last_timer = 0L;
138 int do_housekeeping_now = 0;
142 * We do it this way instead of wrapping the whole loop in an
143 * S_HOUSEKEEPING critical section because it eliminates the need to
144 * potentially have multiple concurrent mutexes in progress.
146 begin_critical_section(S_HOUSEKEEPING);
148 if ( (now - last_timer) > (time_t)60 ) {
149 if (housekeeping_in_progress == 0) {
150 do_housekeeping_now = 1;
151 housekeeping_in_progress = 1;
152 last_timer = time(NULL);
155 end_critical_section(S_HOUSEKEEPING);
157 if (do_housekeeping_now == 0) {
162 * Ok, at this point we've made the decision to run the housekeeping
163 * loop. Everything below this point is real work.
166 cdb_check_handles(); /* suggested by Justin Case */
167 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
172 housekeeping_in_progress = 0;