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 "citserver.h"
38 #include "housekeeping.h"
39 #include "sysdep_decls.h"
42 #include "dynloader.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);
71 lprintf(9, "Terminated %d idle sessions\n", killed);
76 void check_sched_shutdown(void) {
77 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
78 lprintf(3, "Scheduled shutdown initiating.\n");
86 * Check (and fix) floor reference counts. This doesn't need to be done
87 * very often, since the counts should remain correct during normal operation.
88 * NOTE: this function pair should ONLY be called during startup. It is NOT
91 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
94 getfloor(&flbuf, qrbuf->QRfloor);
96 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
97 putfloor(&flbuf, qrbuf->QRfloor);
100 void check_ref_counts(void) {
104 lprintf(7, "Checking floor reference counts\n");
105 for (a=0; a<MAXFLOORS; ++a) {
107 flbuf.f_ref_count = 0;
108 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
112 ForEachRoom(check_ref_counts_backend, NULL);
116 * This is the housekeeping loop. Worker threads come through here after
117 * processing client requests but before jumping back into the pool. We
118 * only allow housekeeping to execute once per minute, and we only allow one
119 * instance to run at a time.
121 void do_housekeeping(void) {
122 static int housekeeping_in_progress = 0;
123 static time_t last_timer = 0L;
124 int do_housekeeping_now = 0;
127 * We do it this way instead of wrapping the whole loop in an
128 * S_HOUSEKEEPING critical section because it eliminates the need to
129 * potentially have multiple concurrent mutexes in progress.
131 begin_critical_section(S_HOUSEKEEPING);
132 if ( ((time(NULL) - last_timer) > 60L)
133 && (housekeeping_in_progress == 0) ) {
134 do_housekeeping_now = 1;
135 housekeeping_in_progress = 1;
136 last_timer = time(NULL);
138 end_critical_section(S_HOUSEKEEPING);
139 if (do_housekeeping_now == 0) return;
142 * Ok, at this point we've made the decision to run the housekeeping
143 * loop. Everything below this point is real work.
146 cdb_check_handles(); /* suggested by Justin Case */
147 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
152 housekeeping_in_progress = 0;