2 * This file contains miscellaneous housekeeping tasks.
4 * Copyright (c) 1987-2017 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.
16 #include <libcitadel.h>
18 #include "ctdl_module.h"
19 #include "serv_extensions.h"
21 #include "internet_addressing.h"
22 #include "journaling.h"
24 void check_sched_shutdown(void) {
25 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
26 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
27 server_shutting_down = 1;
33 * Check (and fix) floor reference counts. This doesn't need to be done
34 * very often, since the counts should remain correct during normal operation.
36 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
40 new_refcounts = (int *) data;
42 ++new_refcounts[(int)qrbuf->QRfloor];
46 void check_ref_counts(void) {
50 int new_refcounts[MAXFLOORS];
52 syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
53 for (a=0; a<MAXFLOORS; ++a) {
57 cdb_begin_transaction();
58 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
59 cdb_end_transaction();
61 for (a=0; a<MAXFLOORS; ++a) {
63 flbuf.f_ref_count = new_refcounts[a];
64 if (new_refcounts[a] > 0) {
65 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
68 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
71 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
77 * This is the housekeeping loop. Worker threads come through here after
78 * processing client requests but before jumping back into the pool. We
79 * only allow housekeeping to execute once per minute, and we only allow one
80 * instance to run at a time.
82 static int housekeeping_in_progress = 0;
83 static time_t last_timer = 0L;
84 void do_housekeeping(void) {
85 int do_housekeeping_now = 0;
86 int do_perminute_housekeeping_now = 0;
90 * We do it this way instead of wrapping the whole loop in an
91 * S_HOUSEKEEPING critical section because it eliminates the need to
92 * potentially have multiple concurrent mutexes in progress.
94 begin_critical_section(S_HOUSEKEEPING);
95 if (housekeeping_in_progress == 0) {
96 do_housekeeping_now = 1;
97 housekeeping_in_progress = 1;
99 end_critical_section(S_HOUSEKEEPING);
101 if (do_housekeeping_now == 0) {
106 * Ok, at this point we've made the decision to run the housekeeping
107 * loop. Everything below this point is real work.
111 if ( (now - last_timer) > (time_t)60 ) {
112 do_perminute_housekeeping_now = 1;
113 last_timer = time(NULL);
116 /* First, do the "as often as needed" stuff... */
118 PerformSessionHooks(EVT_HOUSE);
120 /* Then, do the "once per minute" stuff... */
121 if (do_perminute_housekeeping_now) {
122 cdb_check_handles(); /* suggested by Justin Case */
123 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
129 begin_critical_section(S_HOUSEKEEPING);
130 housekeeping_in_progress = 0;
131 end_critical_section(S_HOUSEKEEPING);
135 void CtdlDisableHouseKeeping(void)
137 int ActiveBackgroundJobs;
138 int do_housekeeping_now = 0;
139 struct CitContext *nptr;
142 retry_block_housekeeping:
143 syslog(LOG_INFO, "housekeeping: trying to disable services");
144 begin_critical_section(S_HOUSEKEEPING);
145 if (housekeeping_in_progress == 0) {
146 do_housekeeping_now = 1;
147 housekeeping_in_progress = 1;
149 end_critical_section(S_HOUSEKEEPING);
150 if (do_housekeeping_now == 0) {
152 goto retry_block_housekeeping;
155 syslog(LOG_INFO, "housekeeping: checking for running server jobs");
157 retry_wait_for_contexts:
158 /* So that we don't keep the context list locked for a long time
159 * we create a copy of it first
161 ActiveBackgroundJobs = 0;
162 nptr = CtdlGetContextArray(&nContexts) ;
165 for (i=0; i<nContexts; i++)
167 if ((nptr[i].state != CON_SYS) || (nptr[i].lastcmd == 0))
169 ActiveBackgroundJobs ++;
170 syslog(LOG_INFO, "jousekeeping: job CC[%d] active; use TERM if you don't want to wait for it", nptr[i].cs_pid);
177 if (ActiveBackgroundJobs != 0) {
178 syslog(LOG_INFO, "housekeeping: found %d running jobs, need to wait", ActiveBackgroundJobs);
180 goto retry_wait_for_contexts;
182 syslog(LOG_INFO, "housekeeping: disabled now.");
186 void CtdlEnableHouseKeeping(void)
188 begin_critical_section(S_HOUSEKEEPING);
189 housekeeping_in_progress = 0;
190 end_critical_section(S_HOUSEKEEPING);