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 "journaling.h"
54 #include "ctdl_module.h"
57 void check_sched_shutdown(void) {
58 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
59 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
60 server_shutting_down = 1;
67 * Check (and fix) floor reference counts. This doesn't need to be done
68 * very often, since the counts should remain correct during normal operation.
70 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
74 new_refcounts = (int *) data;
76 ++new_refcounts[(int)qrbuf->QRfloor];
79 void check_ref_counts(void) {
83 int new_refcounts[MAXFLOORS];
85 syslog(LOG_DEBUG, "Checking floor reference counts\n");
86 for (a=0; a<MAXFLOORS; ++a) {
90 cdb_begin_transaction();
91 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
92 cdb_end_transaction();
94 for (a=0; a<MAXFLOORS; ++a) {
96 flbuf.f_ref_count = new_refcounts[a];
97 if (new_refcounts[a] > 0) {
98 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
101 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
103 lputfloor(&flbuf, a);
104 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
109 * This is the housekeeping loop. Worker threads come through here after
110 * processing client requests but before jumping back into the pool. We
111 * only allow housekeeping to execute once per minute, and we only allow one
112 * instance to run at a time.
114 static int housekeeping_in_progress = 0;
115 static time_t last_timer = 0L;
116 void do_housekeeping(void) {
117 int do_housekeeping_now = 0;
118 int do_perminute_housekeeping_now = 0;
122 * We do it this way instead of wrapping the whole loop in an
123 * S_HOUSEKEEPING critical section because it eliminates the need to
124 * potentially have multiple concurrent mutexes in progress.
126 begin_critical_section(S_HOUSEKEEPING);
127 if (housekeeping_in_progress == 0) {
128 do_housekeeping_now = 1;
129 housekeeping_in_progress = 1;
131 end_critical_section(S_HOUSEKEEPING);
133 if (do_housekeeping_now == 0) {
138 * Ok, at this point we've made the decision to run the housekeeping
139 * loop. Everything below this point is real work.
143 if ( (now - last_timer) > (time_t)60 ) {
144 do_perminute_housekeeping_now = 1;
145 last_timer = time(NULL);
148 /* First, do the "as often as needed" stuff... */
150 PerformSessionHooks(EVT_HOUSE);
152 /* Then, do the "once per minute" stuff... */
153 if (do_perminute_housekeeping_now) {
154 cdb_check_handles(); /* suggested by Justin Case */
155 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
161 begin_critical_section(S_HOUSEKEEPING);
162 housekeeping_in_progress = 0;
163 end_critical_section(S_HOUSEKEEPING);