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"
23 #include "journaling.h"
24 #include "citadel_ldap.h"
26 void check_sched_shutdown(void) {
27 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
28 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
29 server_shutting_down = 1;
35 * Check (and fix) floor reference counts. This doesn't need to be done
36 * very often, since the counts should remain correct during normal operation.
38 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
42 new_refcounts = (int *) data;
44 ++new_refcounts[(int)qrbuf->QRfloor];
48 void check_ref_counts(void) {
52 int new_refcounts[MAXFLOORS];
54 syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
55 for (a=0; a<MAXFLOORS; ++a) {
59 cdb_begin_transaction();
60 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
61 cdb_end_transaction();
63 for (a=0; a<MAXFLOORS; ++a) {
65 flbuf.f_ref_count = new_refcounts[a];
66 if (new_refcounts[a] > 0) {
67 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
70 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
73 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
79 * This is the housekeeping loop. Worker threads come through here after
80 * processing client requests but before jumping back into the pool. We
81 * only allow housekeeping to execute once per minute, and we only allow one
82 * instance to run at a time.
84 static int housekeeping_in_progress = 0;
85 static time_t last_timer = 0L;
86 void do_housekeeping(void) {
87 int do_housekeeping_now = 0;
88 int do_perminute_housekeeping_now = 0;
92 * We do it this way instead of wrapping the whole loop in an
93 * S_HOUSEKEEPING critical section because it eliminates the need to
94 * potentially have multiple concurrent mutexes in progress.
96 begin_critical_section(S_HOUSEKEEPING);
97 if (housekeeping_in_progress == 0) {
98 do_housekeeping_now = 1;
99 housekeeping_in_progress = 1;
101 end_critical_section(S_HOUSEKEEPING);
104 if (do_housekeeping_now == 0) {
105 if ( (now - last_timer) > (time_t)300 ) {
107 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes. Is something stuck?",
108 ((now - last_timer) / 60)
115 * Ok, at this point we've made the decision to run the housekeeping
116 * loop. Everything below this point is real work.
119 if ( (now - last_timer) > (time_t)60 ) {
120 do_perminute_housekeeping_now = 1;
121 last_timer = time(NULL);
124 /* First, do the "as often as needed" stuff... */
126 PerformSessionHooks(EVT_HOUSE);
128 /* Then, do the "once per minute" stuff... */
129 if (do_perminute_housekeeping_now) {
131 PerformSessionHooks(EVT_TIMER); // Run all registered TIMER hooks
133 #ifdef HAVE_LDAP // LDAP sync isn't in a module so we can put it here
134 static time_t last_ldap_sync = 0L;
135 if ( (now - last_ldap_sync) > (time_t)CtdlGetConfigLong("c_ldap_sync_freq") ) {
136 CtdlSynchronizeUsersFromLDAP();
137 last_ldap_sync = time(NULL);
146 begin_critical_section(S_HOUSEKEEPING);
147 housekeeping_in_progress = 0;
148 end_critical_section(S_HOUSEKEEPING);
152 void CtdlDisableHouseKeeping(void)
154 int ActiveBackgroundJobs;
155 int do_housekeeping_now = 0;
156 struct CitContext *nptr;
159 retry_block_housekeeping:
160 syslog(LOG_INFO, "housekeeping: trying to disable services");
161 begin_critical_section(S_HOUSEKEEPING);
162 if (housekeeping_in_progress == 0) {
163 do_housekeeping_now = 1;
164 housekeeping_in_progress = 1;
166 end_critical_section(S_HOUSEKEEPING);
167 if (do_housekeeping_now == 0) {
169 goto retry_block_housekeeping;
172 syslog(LOG_INFO, "housekeeping: checking for running server jobs");
174 retry_wait_for_contexts:
175 /* So that we don't keep the context list locked for a long time
176 * we create a copy of it first
178 ActiveBackgroundJobs = 0;
179 nptr = CtdlGetContextArray(&nContexts) ;
182 for (i=0; i<nContexts; i++)
184 if ((nptr[i].state != CON_SYS) || (nptr[i].lastcmd == 0))
186 ActiveBackgroundJobs ++;
187 syslog(LOG_INFO, "jousekeeping: job CC[%d] active; use TERM if you don't want to wait for it", nptr[i].cs_pid);
194 if (ActiveBackgroundJobs != 0) {
195 syslog(LOG_INFO, "housekeeping: found %d running jobs, need to wait", ActiveBackgroundJobs);
197 goto retry_wait_for_contexts;
199 syslog(LOG_INFO, "housekeeping: disabled now.");
203 void CtdlEnableHouseKeeping(void)
205 begin_critical_section(S_HOUSEKEEPING);
206 housekeeping_in_progress = 0;
207 end_critical_section(S_HOUSEKEEPING);