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