f4d930d64802631a320086be954b50f409255f58
[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 as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include "sysdep.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <ctype.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
45 #endif
46 #include <syslog.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "serv_extensions.h"
51 #include "citserver.h"
52 #include "config.h"
53 #include "housekeeping.h"
54 #include "sysdep_decls.h"
55 #include "room_ops.h"
56 #include "database.h"
57 #include "msgbase.h"
58 #include "journaling.h"
59
60 #include "ctdl_module.h"
61 #include "threads.h"
62
63 void check_sched_shutdown(void) {
64         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
65                 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
66                 server_shutting_down = 1;
67         }
68 }
69
70
71
72 /*
73  * Check (and fix) floor reference counts.  This doesn't need to be done
74  * very often, since the counts should remain correct during normal operation.
75  */
76 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
77
78         int *new_refcounts;
79
80         new_refcounts = (int *) data;
81
82         ++new_refcounts[(int)qrbuf->QRfloor];
83 }
84
85 void check_ref_counts(void) {
86         struct floor flbuf;
87         int a;
88
89         int new_refcounts[MAXFLOORS];
90
91         syslog(LOG_DEBUG, "Checking floor reference counts\n");
92         for (a=0; a<MAXFLOORS; ++a) {
93                 new_refcounts[a] = 0;
94         }
95
96         cdb_begin_transaction();
97         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
98         cdb_end_transaction();
99
100         for (a=0; a<MAXFLOORS; ++a) {
101                 lgetfloor(&flbuf, a);
102                 flbuf.f_ref_count = new_refcounts[a];
103                 if (new_refcounts[a] > 0) {
104                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
105                 }
106                 else {
107                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
108                 }
109                 lputfloor(&flbuf, a);
110                 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
111         }
112 }       
113
114 /*
115  * This is the housekeeping loop.  Worker threads come through here after
116  * processing client requests but before jumping back into the pool.  We
117  * only allow housekeeping to execute once per minute, and we only allow one
118  * instance to run at a time.
119  */
120 void do_housekeeping(void) {
121         static int housekeeping_in_progress = 0;
122         static time_t last_timer = 0L;
123         int do_housekeeping_now = 0;
124         int do_perminute_housekeeping_now = 0;
125         time_t now;
126
127         /*
128          * We do it this way instead of wrapping the whole loop in an
129          * S_HOUSEKEEPING critical section because it eliminates the need to
130          * potentially have multiple concurrent mutexes in progress.
131          */
132         begin_critical_section(S_HOUSEKEEPING);
133         if (housekeeping_in_progress == 0) {
134                 do_housekeeping_now = 1;
135                 housekeeping_in_progress = 1;
136                 now = time(NULL);
137                 if ( (now - last_timer) > (time_t)60 ) {
138                         do_perminute_housekeeping_now = 1;
139                         last_timer = time(NULL);
140                 }
141         }
142         end_critical_section(S_HOUSEKEEPING);
143
144         if (do_housekeeping_now == 0) {
145                 return;
146         }
147
148         /*
149          * Ok, at this point we've made the decision to run the housekeeping
150          * loop.  Everything below this point is real work.
151          */
152
153         /* First, do the "as often as needed" stuff... */
154         JournalRunQueue();
155         PerformSessionHooks(EVT_HOUSE);
156
157         /* Then, do the "once per minute" stuff... */
158         if (do_perminute_housekeeping_now) {
159                 cdb_check_handles();                    /* suggested by Justin Case */
160                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
161         }
162
163         /*
164          * All done.
165          */
166         housekeeping_in_progress = 0;
167 }