f3577c0cf60956090fee39c2747b2e9a0a6b4ec0
[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 "journaling.h"
23 #include "citadel_ldap.h"
24
25 void check_sched_shutdown(void) {
26         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
27                 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
28                 server_shutting_down = 1;
29         }
30 }
31
32
33 /*
34  * Check (and fix) floor reference counts.  This doesn't need to be done
35  * very often, since the counts should remain correct during normal operation.
36  */
37 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
38
39         int *new_refcounts;
40
41         new_refcounts = (int *) data;
42
43         ++new_refcounts[(int)qrbuf->QRfloor];
44 }
45
46
47 void check_ref_counts(void) {
48         struct floor flbuf;
49         int a;
50
51         int new_refcounts[MAXFLOORS];
52
53         syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
54         for (a=0; a<MAXFLOORS; ++a) {
55                 new_refcounts[a] = 0;
56         }
57
58         cdb_begin_transaction();
59         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
60         cdb_end_transaction();
61
62         for (a=0; a<MAXFLOORS; ++a) {
63                 lgetfloor(&flbuf, a);
64                 flbuf.f_ref_count = new_refcounts[a];
65                 if (new_refcounts[a] > 0) {
66                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
67                 }
68                 else {
69                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
70                 }
71                 lputfloor(&flbuf, a);
72                 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
73         }
74 }       
75
76
77 /*
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  */
83 static int housekeeping_in_progress = 0;
84 static time_t last_timer = 0L;
85 void do_housekeeping(void) {
86         int do_housekeeping_now = 0;
87         int do_perminute_housekeeping_now = 0;
88         time_t now;
89
90         /*
91          * We do it this way instead of wrapping the whole loop in an
92          * S_HOUSEKEEPING critical section because it eliminates the need to
93          * potentially have multiple concurrent mutexes in progress.
94          */
95         begin_critical_section(S_HOUSEKEEPING);
96         if (housekeeping_in_progress == 0) {
97                 do_housekeeping_now = 1;
98                 housekeeping_in_progress = 1;
99         }
100         end_critical_section(S_HOUSEKEEPING);
101
102         now = time(NULL);
103         if (do_housekeeping_now == 0) {
104                 if ( (now - last_timer) > (time_t)300 ) {
105                         syslog(LOG_WARNING,
106                                 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes.  Is something stuck?",
107                                 ((now - last_timer) / 60)
108                         );
109                 }
110                 return;
111         }
112
113         /*
114          * Ok, at this point we've made the decision to run the housekeeping
115          * loop.  Everything below this point is real work.
116          */
117
118         if ( (now - last_timer) > (time_t)60 ) {
119                 do_perminute_housekeeping_now = 1;
120                 last_timer = time(NULL);
121         }
122
123         /* First, do the "as often as needed" stuff... */
124         JournalRunQueue();
125         PerformSessionHooks(EVT_HOUSE);
126
127         /* Then, do the "once per minute" stuff... */
128         if (do_perminute_housekeeping_now) {
129                 cdb_check_handles();
130 #ifdef HAVE_LDAP
131                 CtdlSynchronizeUsersFromLDAP();         // This one isn't from a module so we put it here
132 #endif
133                 PerformSessionHooks(EVT_TIMER);         // Run all registered TIMER hooks
134         }
135
136         /*
137          * All done.
138          */
139         begin_critical_section(S_HOUSEKEEPING);
140         housekeeping_in_progress = 0;
141         end_critical_section(S_HOUSEKEEPING);
142 }
143
144
145 void CtdlDisableHouseKeeping(void)
146 {
147         int ActiveBackgroundJobs;
148         int do_housekeeping_now = 0;
149         struct CitContext *nptr;
150         int nContexts, i;
151
152 retry_block_housekeeping:
153         syslog(LOG_INFO, "housekeeping: trying to disable services");
154         begin_critical_section(S_HOUSEKEEPING);
155         if (housekeeping_in_progress == 0) {
156                 do_housekeeping_now = 1;
157                 housekeeping_in_progress = 1;
158         }
159         end_critical_section(S_HOUSEKEEPING);
160         if (do_housekeeping_now == 0) {
161                 usleep(1000000);
162                 goto retry_block_housekeeping;
163         }
164         
165         syslog(LOG_INFO, "housekeeping: checking for running server jobs");
166
167 retry_wait_for_contexts:
168         /* So that we don't keep the context list locked for a long time
169          * we create a copy of it first
170          */
171         ActiveBackgroundJobs = 0;
172         nptr = CtdlGetContextArray(&nContexts) ;
173         if (nptr)
174         {
175                 for (i=0; i<nContexts; i++) 
176                 {
177                         if ((nptr[i].state != CON_SYS) || (nptr[i].lastcmd == 0))
178                                 continue;
179                         ActiveBackgroundJobs ++;
180                         syslog(LOG_INFO, "jousekeeping: job CC[%d] active; use TERM if you don't want to wait for it", nptr[i].cs_pid);
181                 
182                 }
183         
184                 free(nptr);
185
186         }
187         if (ActiveBackgroundJobs != 0) {
188                 syslog(LOG_INFO, "housekeeping: found %d running jobs, need to wait", ActiveBackgroundJobs);
189                 usleep(5000000);
190                 goto retry_wait_for_contexts;
191         }
192         syslog(LOG_INFO, "housekeeping: disabled now.");
193 }
194
195
196 void CtdlEnableHouseKeeping(void)
197 {
198         begin_critical_section(S_HOUSEKEEPING);
199         housekeeping_in_progress = 0;
200         end_critical_section(S_HOUSEKEEPING);
201 }