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"
44 #include "journaling.h"
49 * Terminate idle sessions. This function pounds through the session table
50 * comparing the current time to each session's time-of-last-command. If an
51 * idle session is found it is terminated, then the search restarts at the
52 * beginning because the pointer to our place in the list becomes invalid.
54 void terminate_idle_sessions(void) {
55 struct CitContext *ccptr;
62 begin_critical_section(S_SESSION_TABLE);
63 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
65 && (config.c_sleeping > 0)
66 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
71 end_critical_section(S_SESSION_TABLE);
73 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
78 void check_sched_shutdown(void) {
79 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
80 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
89 * Check (and fix) floor reference counts. This doesn't need to be done
90 * very often, since the counts should remain correct during normal operation.
92 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
96 new_refcounts = (int *) data;
98 ++new_refcounts[(int)qrbuf->QRfloor];
101 void check_ref_counts(void) {
105 int new_refcounts[MAXFLOORS];
107 lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
108 for (a=0; a<MAXFLOORS; ++a) {
109 new_refcounts[a] = 0;
112 cdb_begin_transaction();
113 ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
114 cdb_end_transaction();
116 for (a=0; a<MAXFLOORS; ++a) {
117 lgetfloor(&flbuf, a);
118 flbuf.f_ref_count = new_refcounts[a];
119 if (new_refcounts[a] > 0) {
120 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
123 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
125 lputfloor(&flbuf, a);
126 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
131 * This is the housekeeping loop. Worker threads come through here after
132 * processing client requests but before jumping back into the pool. We
133 * only allow housekeeping to execute once per minute, and we only allow one
134 * instance to run at a time.
136 void do_housekeeping(void) {
137 static int housekeeping_in_progress = 0;
138 static time_t last_timer = 0L;
139 int do_housekeeping_now = 0;
140 int do_perminute_housekeeping_now = 0;
144 * We do it this way instead of wrapping the whole loop in an
145 * S_HOUSEKEEPING critical section because it eliminates the need to
146 * potentially have multiple concurrent mutexes in progress.
148 begin_critical_section(S_HOUSEKEEPING);
149 if (housekeeping_in_progress == 0) {
150 do_housekeeping_now = 1;
151 housekeeping_in_progress = 1;
153 if ( (now - last_timer) > (time_t)60 ) {
154 do_perminute_housekeeping_now = 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 /* First, do the "as often as needed" stuff... */
172 /* Then, do the "once per minute" stuff... */
173 if (do_perminute_housekeeping_now) {
174 cdb_check_handles(); /* suggested by Justin Case */
175 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
181 housekeeping_in_progress = 0;