more whittling away ... plus memory leak tests
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains miscellaneous housekeeping tasks.
3  *
4  * Copyright (c) 1987-2017 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <libcitadel.h>
17
18 #include "ctdl_module.h"
19 #include "serv_extensions.h"
20 #include "room_ops.h"
21 #include "internet_addressing.h"
22 #include "config.h"
23 #include "journaling.h"
24 #include "citadel_ldap.h"
25
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;
30         }
31 }
32
33
34 /*
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.
37  */
38 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
39
40         int *new_refcounts;
41
42         new_refcounts = (int *) data;
43
44         ++new_refcounts[(int)qrbuf->QRfloor];
45 }
46
47
48 void check_ref_counts(void) {
49         struct floor flbuf;
50         int a;
51
52         int new_refcounts[MAXFLOORS];
53
54         syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
55         for (a=0; a<MAXFLOORS; ++a) {
56                 new_refcounts[a] = 0;
57         }
58
59         cdb_begin_transaction();
60         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
61         cdb_end_transaction();
62
63         for (a=0; a<MAXFLOORS; ++a) {
64                 lgetfloor(&flbuf, a);
65                 flbuf.f_ref_count = new_refcounts[a];
66                 if (new_refcounts[a] > 0) {
67                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
68                 }
69                 else {
70                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
71                 }
72                 lputfloor(&flbuf, a);
73                 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
74         }
75 }       
76
77
78 /*
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.
83  */
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;
89         time_t now;
90         static void *original_brk = NULL;
91
92         /*
93          * We do it this way instead of wrapping the whole loop in an
94          * S_HOUSEKEEPING critical section because it eliminates the need to
95          * potentially have multiple concurrent mutexes in progress.
96          */
97         begin_critical_section(S_HOUSEKEEPING);
98         if (housekeeping_in_progress == 0) {
99                 do_housekeeping_now = 1;
100                 housekeeping_in_progress = 1;
101         }
102         end_critical_section(S_HOUSEKEEPING);
103
104         if (!original_brk) original_brk = sbrk(0);      // Remember the original program break so we can test for leaks
105         syslog(LOG_DEBUG, "original_brk=%x, current_brk=%x, addl=%d", (int)original_brk, (int)sbrk(0), (int)(sbrk(0)-original_brk));    // FIXME not so noisy please
106
107         now = time(NULL);
108         if (do_housekeeping_now == 0) {
109                 if ( (now - last_timer) > (time_t)300 ) {
110                         syslog(LOG_WARNING,
111                                 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes.  Is something stuck?",
112                                 ((now - last_timer) / 60)
113                         );
114                 }
115                 return;
116         }
117
118         /*
119          * Ok, at this point we've made the decision to run the housekeeping
120          * loop.  Everything below this point is real work.
121          */
122
123         if ( (now - last_timer) > (time_t)60 ) {
124                 do_perminute_housekeeping_now = 1;
125                 last_timer = time(NULL);
126         }
127
128         /* First, do the "as often as needed" stuff... */
129         JournalRunQueue();
130         PerformSessionHooks(EVT_HOUSE);
131
132         /* Then, do the "once per minute" stuff... */
133         if (do_perminute_housekeeping_now) {
134                 cdb_check_handles();
135                 PerformSessionHooks(EVT_TIMER);         // Run all registered TIMER hooks
136
137 #ifdef HAVE_LDAP                                        // LDAP sync isn't in a module so we can put it here
138                 static time_t last_ldap_sync = 0L;
139                 if ( (now - last_ldap_sync) > (time_t)CtdlGetConfigLong("c_ldap_sync_freq") ) {
140                         CtdlSynchronizeUsersFromLDAP();
141                         last_ldap_sync = time(NULL);
142                 }
143 #endif
144
145         }
146
147         /*
148          * All done.
149          */
150         begin_critical_section(S_HOUSEKEEPING);
151         housekeeping_in_progress = 0;
152         end_critical_section(S_HOUSEKEEPING);
153 }
154
155
156 void CtdlDisableHouseKeeping(void)
157 {
158         int ActiveBackgroundJobs;
159         int do_housekeeping_now = 0;
160         struct CitContext *nptr;
161         int nContexts, i;
162
163 retry_block_housekeeping:
164         syslog(LOG_INFO, "housekeeping: trying to disable services");
165         begin_critical_section(S_HOUSEKEEPING);
166         if (housekeeping_in_progress == 0) {
167                 do_housekeeping_now = 1;
168                 housekeeping_in_progress = 1;
169         }
170         end_critical_section(S_HOUSEKEEPING);
171         if (do_housekeeping_now == 0) {
172                 usleep(1000000);
173                 goto retry_block_housekeeping;
174         }
175         
176         syslog(LOG_INFO, "housekeeping: checking for running server jobs");
177
178 retry_wait_for_contexts:
179         /* So that we don't keep the context list locked for a long time
180          * we create a copy of it first
181          */
182         ActiveBackgroundJobs = 0;
183         nptr = CtdlGetContextArray(&nContexts) ;
184         if (nptr)
185         {
186                 for (i=0; i<nContexts; i++) 
187                 {
188                         if ((nptr[i].state != CON_SYS) || (nptr[i].lastcmd == 0))
189                                 continue;
190                         ActiveBackgroundJobs ++;
191                         syslog(LOG_INFO, "jousekeeping: job CC[%d] active; use TERM if you don't want to wait for it", nptr[i].cs_pid);
192                 
193                 }
194         
195                 free(nptr);
196
197         }
198         if (ActiveBackgroundJobs != 0) {
199                 syslog(LOG_INFO, "housekeeping: found %d running jobs, need to wait", ActiveBackgroundJobs);
200                 usleep(5000000);
201                 goto retry_wait_for_contexts;
202         }
203         syslog(LOG_INFO, "housekeeping: disabled now.");
204 }
205
206
207 void CtdlEnableHouseKeeping(void)
208 {
209         begin_critical_section(S_HOUSEKEEPING);
210         housekeeping_in_progress = 0;
211         end_critical_section(S_HOUSEKEEPING);
212 }