citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
housekeeping.c
Go to the documentation of this file.
1// This file contains miscellaneous housekeeping tasks.
2//
3// Copyright (c) 1987-2021 by the citadel.org team
4//
5// This program is open source software. Use, duplication, or disclosure
6// is subject to the terms of the GNU General Public License, version 3.
7
8
9#include <stdio.h>
10#include <libcitadel.h>
11
12#include "ctdl_module.h"
13#include "serv_extensions.h"
14#include "room_ops.h"
15#include "internet_addressing.h"
16#include "config.h"
17#include "journaling.h"
18#include "citadel_ldap.h"
19
21 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
22 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
24 }
25}
26
27
28// Check (and fix) floor reference counts. This doesn't need to be done
29// very often, since the counts should remain correct during normal operation.
30void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
31
32 int *new_refcounts;
33
34 new_refcounts = (int *) data;
35
36 ++new_refcounts[(int)qrbuf->QRfloor];
37}
38
39
40void check_ref_counts(void) {
41 struct floor flbuf;
42 int a;
43
44 int new_refcounts[MAXFLOORS];
45
46 syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
47 for (a=0; a<MAXFLOORS; ++a) {
48 new_refcounts[a] = 0;
49 }
50
52 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
54
55 for (a=0; a<MAXFLOORS; ++a) {
56 lgetfloor(&flbuf, a);
57 flbuf.f_ref_count = new_refcounts[a];
58 if (new_refcounts[a] > 0) {
59 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
60 }
61 else {
62 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
63 }
64 lputfloor(&flbuf, a);
65 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
66 }
67}
68
69
70// Provide hints as to whether we have any memory leaks
72 static void *original_brk = NULL;
73 if (!original_brk) original_brk = sbrk(0); // Remember the original program break so we can test for leaks
74 syslog(LOG_DEBUG, "Additional memory allocated since startup: %ld bytes", (long)(sbrk(0)-original_brk));
75}
76
77
78// This is the housekeeping loop. Worker threads come through here after
79// processing client requests but before jumping back into the pool. We
80// only allow housekeeping to execute once per minute, and we only allow one
81// instance to run at a time.
83static int housekeeping_disabled = 0;
84static time_t last_timer = 0L;
85
86void do_housekeeping(void) {
87 int do_housekeeping_now = 0;
88 int do_perminute_housekeeping_now = 0;
89 time_t now;
90
92 return;
93 }
94
95 // We do it this way instead of wrapping the whole loop in an
96 // S_HOUSEKEEPING critical section because it eliminates the need to
97 // potentially have multiple concurrent mutexes in progress.
99 if (housekeeping_in_progress == 0) {
100 do_housekeeping_now = 1;
102 }
104
105 now = time(NULL);
106 if ( (do_housekeeping_now == 0) && (!CtdlIsSingleUser()) && ((now - last_timer) > (time_t)3600) ) {
107 if ( (now - last_timer) > (time_t)3600 ) {
108 syslog(LOG_WARNING,
109 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes. Is something stuck?",
110 ((now - last_timer) / 60)
111 );
112 }
113 return;
114 }
115
116 if (!do_housekeeping_now) {
117 return;
118 }
119
120 // Ok, at this point we've made the decision to run the housekeeping
121 // loop. Everything below this point is real work.
122
123 if ( (now - last_timer) > (time_t)60 ) {
124 do_perminute_housekeeping_now = 1;
125 last_timer = time(NULL);
126 }
127
128 // First, do the "as often as needed" stuff...
131 cdb_tick();
132
133 // Then, do the "once per minute" stuff...
134 if (do_perminute_housekeeping_now) {
136 PerformSessionHooks(EVT_TIMER); // Run all registered TIMER hooks
137
138 // LDAP sync isn't in a module so we can put it here
139 static time_t last_ldap_sync = 0L;
140 if ( (now - last_ldap_sync) > (time_t)CtdlGetConfigLong("c_ldap_sync_freq") ) {
142 last_ldap_sync = time(NULL);
143 }
144
146 }
147
148 // All done.
152}
153
154
156 syslog(LOG_INFO, "housekeeping: trying to disable");
158
160 sleep(1);
161 }
162 else {
166 }
168 }
169 }
170 syslog(LOG_INFO, "housekeeping: disabled now");
171}
172
173
@ EVT_HOUSE
@ EVT_TIMER
@ S_HOUSEKEEPING
void CtdlSynchronizeUsersFromLDAP(void)
Definition ldap.c:534
int ScheduledShutdown
Definition citserver.c:25
long CtdlGetConfigLong(char *key)
Definition config.c:394
CitContext * ContextList
Definition context.c:20
int CtdlIsSingleUser(void)
Definition context.c:57
void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
Definition room_ops.c:523
void(* cdb_check_handles)(void)
Definition database.c:36
void(* cdb_begin_transaction)(void)
Definition database.c:34
void(* cdb_end_transaction)(void)
Definition database.c:35
void(* cdb_tick)(void)
Definition database.c:42
static time_t last_timer
void keep_an_eye_on_memory_usage(void)
void do_housekeeping(void)
static int housekeeping_disabled
void CtdlDisableHouseKeeping(void)
void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data)
void check_ref_counts(void)
void CtdlEnableHouseKeeping(void)
void check_sched_shutdown(void)
static int housekeeping_in_progress
#define QR_INUSE
Definition ipcdef.h:36
void JournalRunQueue(void)
Definition journaling.c:218
void lgetfloor(struct floor *flbuf, int floor_num)
Definition room_ops.c:453
void lputfloor(struct floor *flbuf, int floor_num)
Definition room_ops.c:517
void PerformSessionHooks(int EventType)
char QRfloor
Definition server.h:183
int f_ref_count
Definition server.h:200
unsigned short f_flags
Definition server.h:198
#define MAXFLOORS
Definition sysconfig.h:16
void begin_critical_section(int which_one)
Definition threads.c:50
void end_critical_section(int which_one)
Definition threads.c:63
int server_shutting_down
Definition threads.c:22