Updated the copyright declaration in several modules, removing any language which...
[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 "journaling.h"
53
54 #include "ctdl_module.h"
55 #include "threads.h"
56
57 void check_sched_shutdown(void) {
58         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
59                 syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
60                 server_shutting_down = 1;
61         }
62 }
63
64
65
66 /*
67  * Check (and fix) floor reference counts.  This doesn't need to be done
68  * very often, since the counts should remain correct during normal operation.
69  */
70 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
71
72         int *new_refcounts;
73
74         new_refcounts = (int *) data;
75
76         ++new_refcounts[(int)qrbuf->QRfloor];
77 }
78
79 void check_ref_counts(void) {
80         struct floor flbuf;
81         int a;
82
83         int new_refcounts[MAXFLOORS];
84
85         syslog(LOG_DEBUG, "Checking floor reference counts\n");
86         for (a=0; a<MAXFLOORS; ++a) {
87                 new_refcounts[a] = 0;
88         }
89
90         cdb_begin_transaction();
91         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
92         cdb_end_transaction();
93
94         for (a=0; a<MAXFLOORS; ++a) {
95                 lgetfloor(&flbuf, a);
96                 flbuf.f_ref_count = new_refcounts[a];
97                 if (new_refcounts[a] > 0) {
98                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
99                 }
100                 else {
101                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
102                 }
103                 lputfloor(&flbuf, a);
104                 syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
105         }
106 }       
107
108 /*
109  * This is the housekeeping loop.  Worker threads come through here after
110  * processing client requests but before jumping back into the pool.  We
111  * only allow housekeeping to execute once per minute, and we only allow one
112  * instance to run at a time.
113  */
114 static int housekeeping_in_progress = 0;
115 static time_t last_timer = 0L;
116 void do_housekeeping(void) {
117         int do_housekeeping_now = 0;
118         int do_perminute_housekeeping_now = 0;
119         time_t now;
120
121         /*
122          * We do it this way instead of wrapping the whole loop in an
123          * S_HOUSEKEEPING critical section because it eliminates the need to
124          * potentially have multiple concurrent mutexes in progress.
125          */
126         begin_critical_section(S_HOUSEKEEPING);
127         if (housekeeping_in_progress == 0) {
128                 do_housekeeping_now = 1;
129                 housekeeping_in_progress = 1;
130         }
131         end_critical_section(S_HOUSEKEEPING);
132
133         if (do_housekeeping_now == 0) {
134                 return;
135         }
136
137         /*
138          * Ok, at this point we've made the decision to run the housekeeping
139          * loop.  Everything below this point is real work.
140          */
141
142         now = time(NULL);
143         if ( (now - last_timer) > (time_t)60 ) {
144                 do_perminute_housekeeping_now = 1;
145                 last_timer = time(NULL);
146         }
147
148         /* First, do the "as often as needed" stuff... */
149         JournalRunQueue();
150         PerformSessionHooks(EVT_HOUSE);
151
152         /* Then, do the "once per minute" stuff... */
153         if (do_perminute_housekeeping_now) {
154                 cdb_check_handles();                    /* suggested by Justin Case */
155                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
156         }
157
158         /*
159          * All done.
160          */
161         begin_critical_section(S_HOUSEKEEPING);
162         housekeeping_in_progress = 0;
163         end_critical_section(S_HOUSEKEEPING);
164 }