4 * This file contains miscellaneous housekeeping tasks.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
40 #include "serv_extensions.h"
41 #include "citserver.h"
43 #include "housekeeping.h"
44 #include "sysdep_decls.h"
52 * Terminate idle sessions. This function pounds through the session table
53 * comparing the current time to each session's time-of-last-command. If an
54 * idle session is found it is terminated, then the search restarts at the
55 * beginning because the pointer to our place in the list becomes invalid.
57 void terminate_idle_sessions(void) {
58 struct CitContext *ccptr;
65 begin_critical_section(S_SESSION_TABLE);
66 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
68 && (config.c_sleeping > 0)
69 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
74 end_critical_section(S_SESSION_TABLE);
76 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
81 void check_sched_shutdown(void) {
82 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
83 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(9, "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;
145 * We do it this way instead of wrapping the whole loop in an
146 * S_HOUSEKEEPING critical section because it eliminates the need to
147 * potentially have multiple concurrent mutexes in progress.
149 begin_critical_section(S_HOUSEKEEPING);
151 if ( (now - last_timer) > (time_t)60 ) {
152 if (housekeeping_in_progress == 0) {
153 do_housekeeping_now = 1;
154 housekeeping_in_progress = 1;
155 last_timer = time(NULL);
158 end_critical_section(S_HOUSEKEEPING);
160 if (do_housekeeping_now == 0) {
165 * Ok, at this point we've made the decision to run the housekeeping
166 * loop. Everything below this point is real work.
169 cdb_check_handles(); /* suggested by Justin Case */
170 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
175 housekeeping_in_progress = 0;