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 as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
47 #include <libcitadel.h>
50 #include "serv_extensions.h"
51 #include "citserver.h"
53 #include "housekeeping.h"
54 #include "sysdep_decls.h"
58 #include "journaling.h"
60 #include "ctdl_module.h"
63 void check_sched_shutdown(void) {
64 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
65 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
66 server_shutting_down = 1;
73 * Check (and fix) floor reference counts. This doesn't need to be done
74 * very often, since the counts should remain correct during normal operation.
76 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
80 new_refcounts = (int *) data;
82 ++new_refcounts[(int)qrbuf->QRfloor];
85 void check_ref_counts(void) {
89 int new_refcounts[MAXFLOORS];
91 syslog(LOG_DEBUG, "Checking floor reference counts\n");
92 for (a=0; a<MAXFLOORS; ++a) {
96 cdb_begin_transaction();
97 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
98 cdb_end_transaction();
100 for (a=0; a<MAXFLOORS; ++a) {
101 lgetfloor(&flbuf, a);
102 flbuf.f_ref_count = new_refcounts[a];
103 if (new_refcounts[a] > 0) {
104 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
107 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
109 lputfloor(&flbuf, a);
110 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
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 static time_t last_timer = 0L;
123 int do_housekeeping_now = 0;
124 int do_perminute_housekeeping_now = 0;
128 * We do it this way instead of wrapping the whole loop in an
129 * S_HOUSEKEEPING critical section because it eliminates the need to
130 * potentially have multiple concurrent mutexes in progress.
132 begin_critical_section(S_HOUSEKEEPING);
133 if (housekeeping_in_progress == 0) {
134 do_housekeeping_now = 1;
135 housekeeping_in_progress = 1;
137 if ( (now - last_timer) > (time_t)60 ) {
138 do_perminute_housekeeping_now = 1;
139 last_timer = time(NULL);
142 end_critical_section(S_HOUSEKEEPING);
144 if (do_housekeeping_now == 0) {
149 * Ok, at this point we've made the decision to run the housekeeping
150 * loop. Everything below this point is real work.
153 /* First, do the "as often as needed" stuff... */
155 PerformSessionHooks(EVT_HOUSE);
157 /* Then, do the "once per minute" stuff... */
158 if (do_perminute_housekeeping_now) {
159 cdb_check_handles(); /* suggested by Justin Case */
160 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
166 housekeeping_in_progress = 0;