1 // This file contains miscellaneous housekeeping tasks.
3 // Copyright (c) 1987-2021 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
10 #include <libcitadel.h>
12 #include "ctdl_module.h"
13 #include "serv_extensions.h"
15 #include "internet_addressing.h"
17 #include "journaling.h"
18 #include "citadel_ldap.h"
20 void check_sched_shutdown(void) {
21 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
22 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
23 server_shutting_down = 1;
28 // Check (and fix) floor reference counts. This doesn't need to be done
29 // very often, since the counts should remain correct during normal operation.
30 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
34 new_refcounts = (int *) data;
36 ++new_refcounts[(int)qrbuf->QRfloor];
40 void check_ref_counts(void) {
44 int new_refcounts[MAXFLOORS];
46 syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
47 for (a=0; a<MAXFLOORS; ++a) {
51 cdb_begin_transaction();
52 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
53 cdb_end_transaction();
55 for (a=0; a<MAXFLOORS; ++a) {
57 flbuf.f_ref_count = new_refcounts[a];
58 if (new_refcounts[a] > 0) {
59 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
62 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
65 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
70 // Provide hints as to whether we have any memory leaks
71 void keep_an_eye_on_memory_usage(void) {
72 static void *original_brk = NULL;
73 if (!original_brk) original_brk = sbrk(0); // Remember the original program break so we can test for leaks
74 syslog(LOG_DEBUG, "Additional memory allocated since startup: %ld bytes", (long)(sbrk(0)-original_brk));
78 // This is the housekeeping loop. Worker threads come through here after
79 // processing client requests but before jumping back into the pool. We
80 // only allow housekeeping to execute once per minute, and we only allow one
81 // instance to run at a time.
82 static int housekeeping_in_progress = 0;
83 static int housekeeping_disabled = 0;
84 static time_t last_timer = 0L;
86 void do_housekeeping(void) {
87 int do_housekeeping_now = 0;
88 int do_perminute_housekeeping_now = 0;
91 if ( (housekeeping_disabled) || (housekeeping_in_progress) ) {
95 // We do it this way instead of wrapping the whole loop in an
96 // S_HOUSEKEEPING critical section because it eliminates the need to
97 // potentially have multiple concurrent mutexes in progress.
98 begin_critical_section(S_HOUSEKEEPING);
99 if (housekeeping_in_progress == 0) {
100 do_housekeeping_now = 1;
101 housekeeping_in_progress = 1;
103 end_critical_section(S_HOUSEKEEPING);
106 if ( (do_housekeeping_now == 0) && (!CtdlIsSingleUser()) && ((now - last_timer) > (time_t)3600) ) {
107 if ( (now - last_timer) > (time_t)3600 ) {
109 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes. Is something stuck?",
110 ((now - last_timer) / 60)
116 if (!do_housekeeping_now) {
120 // Ok, at this point we've made the decision to run the housekeeping
121 // loop. Everything below this point is real work.
123 if ( (now - last_timer) > (time_t)60 ) {
124 do_perminute_housekeeping_now = 1;
125 last_timer = time(NULL);
128 // First, do the "as often as needed" stuff...
130 PerformSessionHooks(EVT_HOUSE);
133 // Then, do the "once per minute" stuff...
134 if (do_perminute_housekeeping_now) {
136 PerformSessionHooks(EVT_TIMER); // Run all registered TIMER hooks
138 // LDAP sync isn't in a module so we can put it here
139 static time_t last_ldap_sync = 0L;
140 if ( (now - last_ldap_sync) > (time_t)CtdlGetConfigLong("c_ldap_sync_freq") ) {
141 CtdlSynchronizeUsersFromLDAP();
142 last_ldap_sync = time(NULL);
145 keep_an_eye_on_memory_usage();
149 begin_critical_section(S_HOUSEKEEPING);
150 housekeeping_in_progress = 0;
151 end_critical_section(S_HOUSEKEEPING);
155 void CtdlDisableHouseKeeping(void) {
156 syslog(LOG_INFO, "housekeeping: trying to disable");
157 while ( (!housekeeping_disabled) && (!server_shutting_down) && (!housekeeping_in_progress) ) {
159 if (housekeeping_in_progress) {
163 begin_critical_section(S_HOUSEKEEPING);
164 if (!housekeeping_in_progress) {
165 housekeeping_disabled = 1;
167 end_critical_section(S_HOUSEKEEPING);
170 syslog(LOG_INFO, "housekeeping: disabled now");
174 void CtdlEnableHouseKeeping(void) {
175 begin_critical_section(S_HOUSEKEEPING);
176 housekeeping_in_progress = 0;
177 end_critical_section(S_HOUSEKEEPING);