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