2 * This file contains miscellaneous housekeeping tasks.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
41 #include <libcitadel.h>
44 #include "serv_extensions.h"
45 #include "citserver.h"
47 #include "housekeeping.h"
48 #include "sysdep_decls.h"
52 #include "internet_addressing.h"
53 #include "journaling.h"
55 #include "ctdl_module.h"
58 void check_sched_shutdown(void) {
59 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
60 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
61 server_shutting_down = 1;
68 * Check (and fix) floor reference counts. This doesn't need to be done
69 * very often, since the counts should remain correct during normal operation.
71 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
75 new_refcounts = (int *) data;
77 ++new_refcounts[(int)qrbuf->QRfloor];
80 void check_ref_counts(void) {
84 int new_refcounts[MAXFLOORS];
86 syslog(LOG_DEBUG, "Checking floor reference counts\n");
87 for (a=0; a<MAXFLOORS; ++a) {
91 cdb_begin_transaction();
92 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
93 cdb_end_transaction();
95 for (a=0; a<MAXFLOORS; ++a) {
97 flbuf.f_ref_count = new_refcounts[a];
98 if (new_refcounts[a] > 0) {
99 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
102 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
104 lputfloor(&flbuf, a);
105 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
110 * This is the housekeeping loop. Worker threads come through here after
111 * processing client requests but before jumping back into the pool. We
112 * only allow housekeeping to execute once per minute, and we only allow one
113 * instance to run at a time.
115 static int housekeeping_in_progress = 0;
116 static time_t last_timer = 0L;
117 void do_housekeeping(void) {
118 int do_housekeeping_now = 0;
119 int do_perminute_housekeeping_now = 0;
123 * We do it this way instead of wrapping the whole loop in an
124 * S_HOUSEKEEPING critical section because it eliminates the need to
125 * potentially have multiple concurrent mutexes in progress.
127 begin_critical_section(S_HOUSEKEEPING);
128 if (housekeeping_in_progress == 0) {
129 do_housekeeping_now = 1;
130 housekeeping_in_progress = 1;
132 end_critical_section(S_HOUSEKEEPING);
134 if (do_housekeeping_now == 0) {
139 * Ok, at this point we've made the decision to run the housekeeping
140 * loop. Everything below this point is real work.
144 if ( (now - last_timer) > (time_t)60 ) {
145 do_perminute_housekeeping_now = 1;
146 last_timer = time(NULL);
149 /* First, do the "as often as needed" stuff... */
151 PerformSessionHooks(EVT_HOUSE);
153 /* Then, do the "once per minute" stuff... */
154 if (do_perminute_housekeeping_now) {
155 cdb_check_handles(); /* suggested by Justin Case */
156 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
162 begin_critical_section(S_HOUSEKEEPING);
163 housekeeping_in_progress = 0;
164 end_critical_section(S_HOUSEKEEPING);