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"
47 * Terminate idle sessions. This function pounds through the session table
48 * comparing the current time to each session's time-of-last-command. If an
49 * idle session is found it is terminated, then the search restarts at the
50 * beginning because the pointer to our place in the list becomes invalid.
52 void terminate_idle_sessions(void) {
53 struct CitContext *ccptr;
60 begin_critical_section(S_SESSION_TABLE);
61 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
63 && (config.c_sleeping > 0)
64 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
69 end_critical_section(S_SESSION_TABLE);
70 lprintf(9, "Terminated %d idle sessions\n", killed);
75 void check_sched_shutdown(void) {
76 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
77 lprintf(3, "Scheduled shutdown initiating.\n");
85 * Check (and fix) floor reference counts. This doesn't need to be done
86 * very often, since the counts should remain correct during normal operation.
87 * NOTE: this function pair should ONLY be called during startup. It is NOT
90 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
93 getfloor(&flbuf, qrbuf->QRfloor);
95 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
96 putfloor(&flbuf, qrbuf->QRfloor);
99 void check_ref_counts(void) {
103 lprintf(7, "Checking floor reference counts\n");
104 for (a=0; a<MAXFLOORS; ++a) {
106 flbuf.f_ref_count = 0;
107 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
111 ForEachRoom(check_ref_counts_backend, NULL);
115 * This is the housekeeping loop. Worker threads come through here after
116 * processing client requests but before jumping back into the pool. We
117 * only allow housekeeping to execute once per minute, and we only allow one
118 * instance to run at a time.
120 void do_housekeeping(void) {
121 static int housekeeping_in_progress = 0;
122 time_t last_timer = 0L;
123 int do_housekeeping_now = 0;
126 * We do it this way instead of wrapping the whole loop in an
127 * S_HOUSEKEEPING critical section because it eliminates the need to
128 * potentially have multiple concurrent mutexes in progress.
130 begin_critical_section(S_HOUSEKEEPING);
131 if ( ((time(NULL) - last_timer) > 60L)
132 && (housekeeping_in_progress == 0) ) {
133 do_housekeeping_now = 1;
134 housekeeping_in_progress = 1;
135 last_timer = time(NULL);
137 end_critical_section(S_HOUSEKEEPING);
138 if (do_housekeeping_now == 0) return;
141 * Ok, at this point we've made the decision to run the housekeeping
142 * loop. Everything below this point is real work.
145 cdb_check_handles(); /* suggested by Justin Case */
146 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
151 housekeeping_in_progress = 0;