92336277733acaaeaab8a0bd9145dfeea0cf7ee6
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains miscellaneous housekeeping tasks.
3  *
4  * Copyright (c) 1987-2011 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
24 void check_sched_shutdown(void) {
25         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
26                 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
27                 server_shutting_down = 1;
28         }
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 void check_ref_counts(void) {
47         struct floor flbuf;
48         int a;
49
50         int new_refcounts[MAXFLOORS];
51
52         syslog(LOG_DEBUG, "Checking floor reference counts\n");
53         for (a=0; a<MAXFLOORS; ++a) {
54                 new_refcounts[a] = 0;
55         }
56
57         cdb_begin_transaction();
58         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
59         cdb_end_transaction();
60
61         for (a=0; a<MAXFLOORS; ++a) {
62                 lgetfloor(&flbuf, a);
63                 flbuf.f_ref_count = new_refcounts[a];
64                 if (new_refcounts[a] > 0) {
65                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
66                 }
67                 else {
68                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
69                 }
70                 lputfloor(&flbuf, a);
71                 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
72         }
73 }       
74
75 /*
76  * This is the housekeeping loop.  Worker threads come through here after
77  * processing client requests but before jumping back into the pool.  We
78  * only allow housekeeping to execute once per minute, and we only allow one
79  * instance to run at a time.
80  */
81 static int housekeeping_in_progress = 0;
82 static time_t last_timer = 0L;
83 void do_housekeeping(void) {
84         int do_housekeeping_now = 0;
85         int do_perminute_housekeeping_now = 0;
86         time_t now;
87
88         /*
89          * We do it this way instead of wrapping the whole loop in an
90          * S_HOUSEKEEPING critical section because it eliminates the need to
91          * potentially have multiple concurrent mutexes in progress.
92          */
93         begin_critical_section(S_HOUSEKEEPING);
94         if (housekeeping_in_progress == 0) {
95                 do_housekeeping_now = 1;
96                 housekeeping_in_progress = 1;
97         }
98         end_critical_section(S_HOUSEKEEPING);
99
100         if (do_housekeeping_now == 0) {
101                 return;
102         }
103
104         /*
105          * Ok, at this point we've made the decision to run the housekeeping
106          * loop.  Everything below this point is real work.
107          */
108
109         now = time(NULL);
110         if ( (now - last_timer) > (time_t)60 ) {
111                 do_perminute_housekeeping_now = 1;
112                 last_timer = time(NULL);
113         }
114
115         /* First, do the "as often as needed" stuff... */
116         JournalRunQueue();
117         PerformSessionHooks(EVT_HOUSE);
118
119         /* Then, do the "once per minute" stuff... */
120         if (do_perminute_housekeeping_now) {
121                 cdb_check_handles();                    /* suggested by Justin Case */
122                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
123         }
124
125         /*
126          * All done.
127          */
128         begin_critical_section(S_HOUSEKEEPING);
129         housekeeping_in_progress = 0;
130         end_critical_section(S_HOUSEKEEPING);
131 }
132
133 void CtdlDisableHouseKeeping(void)
134 {
135         int ActiveBackgroundJobs;
136         int do_housekeeping_now = 0;
137         struct CitContext *nptr;
138         int nContexts, i;
139
140 retry_block_housekeeping:
141         syslog(LOG_INFO, "trying to disable housekeeping services");
142         begin_critical_section(S_HOUSEKEEPING);
143         if (housekeeping_in_progress == 0) {
144                 do_housekeeping_now = 1;
145                 housekeeping_in_progress = 1;
146         }
147         end_critical_section(S_HOUSEKEEPING);
148         if (do_housekeeping_now == 0) {
149                 usleep(1000000);
150                 goto retry_block_housekeeping;
151         }
152         
153         syslog(LOG_INFO, "checking for running server Jobs");
154
155 retry_wait_for_contexts:
156         /* So that we don't keep the context list locked for a long time
157          * we create a copy of it first
158          */
159         ActiveBackgroundJobs = 0;
160         nptr = CtdlGetContextArray(&nContexts) ;
161         if (nptr)
162         {
163                 for (i=0; i<nContexts; i++) 
164                 {
165                         if ((nptr[i].state != CON_SYS) || (nptr[i].IO == NULL) || (nptr[i].lastcmd == 0))
166                                 continue;
167                         ActiveBackgroundJobs ++;
168                         syslog(LOG_INFO, "Job CC[%d] active; use TERM if you don't want to wait for it",nptr[i].cs_pid);
169                 
170                 }
171         
172                 free(nptr);
173
174         }
175         if (ActiveBackgroundJobs != 0) {
176                 syslog(LOG_INFO, "found %d running jobs, need to wait", ActiveBackgroundJobs);
177                 usleep(5000000);
178                 goto retry_wait_for_contexts;
179         }
180         syslog(LOG_INFO, "Housekeeping disabled now.");
181 }
182
183 void CtdlEnableHouseKeeping(void)
184 {
185         begin_critical_section(S_HOUSEKEEPING);
186         housekeeping_in_progress = 0;
187         end_critical_section(S_HOUSEKEEPING);
188 }