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 "dynloader.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);
75 lprintf(9, "Terminated %d idle sessions\n", killed);
80 void check_sched_shutdown(void) {
81 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
82 lprintf(3, "Scheduled shutdown initiating.\n");
90 * Check (and fix) floor reference counts. This doesn't need to be done
91 * very often, since the counts should remain correct during normal operation.
92 * NOTE: this function pair should ONLY be called during startup. It is NOT
95 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
98 getfloor(&flbuf, qrbuf->QRfloor);
100 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
101 putfloor(&flbuf, qrbuf->QRfloor);
104 void check_ref_counts(void) {
108 lprintf(7, "Checking floor reference counts\n");
109 for (a=0; a<MAXFLOORS; ++a) {
111 flbuf.f_ref_count = 0;
112 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
116 cdb_begin_transaction();
117 ForEachRoom(check_ref_counts_backend, NULL);
118 cdb_end_transaction();
122 * This is the housekeeping loop. Worker threads come through here after
123 * processing client requests but before jumping back into the pool. We
124 * only allow housekeeping to execute once per minute, and we only allow one
125 * instance to run at a time.
127 void do_housekeeping(void) {
128 static int housekeeping_in_progress = 0;
129 static time_t last_timer = 0L;
130 int do_housekeeping_now = 0;
134 * We do it this way instead of wrapping the whole loop in an
135 * S_HOUSEKEEPING critical section because it eliminates the need to
136 * potentially have multiple concurrent mutexes in progress.
138 begin_critical_section(S_HOUSEKEEPING);
140 if ( (now - last_timer) > (time_t)60 ) {
141 if (housekeeping_in_progress == 0) {
142 do_housekeeping_now = 1;
143 housekeeping_in_progress = 1;
144 last_timer = time(NULL);
147 end_critical_section(S_HOUSEKEEPING);
149 if (do_housekeeping_now == 0) {
154 * Ok, at this point we've made the decision to run the housekeeping
155 * loop. Everything below this point is real work.
158 cdb_check_handles(); /* suggested by Justin Case */
159 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
164 housekeeping_in_progress = 0;