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