7eee008ae05911837a9d1f092f69392e5172d498
[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 "tools.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
44
45
46
47 /*
48  * Terminate idle sessions.  This function pounds through the session table
49  * comparing the current time to each session's time-of-last-command.  If an
50  * idle session is found it is terminated, then the search restarts at the
51  * beginning because the pointer to our place in the list becomes invalid.
52  */
53 void terminate_idle_sessions(void) {
54         struct CitContext *ccptr;
55         time_t now;
56         int session_to_kill;
57         int killed = 0;
58
59         now = time(NULL);
60         session_to_kill = 0;
61         begin_critical_section(S_SESSION_TABLE);
62         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
63                 if (  (ccptr!=CC)
64                 && (config.c_sleeping > 0)
65                 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
66                         ccptr->kill_me = 1;
67                         ++killed;
68                 }
69         }
70         end_critical_section(S_SESSION_TABLE);
71         if (killed > 0)
72                 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
73 }
74
75
76
77 void check_sched_shutdown(void) {
78         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
79                 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
80                 time_to_die = 1;
81                 master_cleanup(0);
82         }
83 }
84
85
86
87 /*
88  * Check (and fix) floor reference counts.  This doesn't need to be done
89  * very often, since the counts should remain correct during normal operation.
90  */
91 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
92
93         int *new_refcounts;
94
95         new_refcounts = (int *) data;
96
97         ++new_refcounts[(int)qrbuf->QRfloor];
98 }
99
100 void check_ref_counts(void) {
101         struct floor flbuf;
102         int a;
103
104         int new_refcounts[MAXFLOORS];
105
106         lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
107         for (a=0; a<MAXFLOORS; ++a) {
108                 new_refcounts[a] = 0;
109         }
110
111         cdb_begin_transaction();
112         ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
113         cdb_end_transaction();
114
115         for (a=0; a<MAXFLOORS; ++a) {
116                 lgetfloor(&flbuf, a);
117                 flbuf.f_ref_count = new_refcounts[a];
118                 if (new_refcounts[a] > 0) {
119                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
120                 }
121                 else {
122                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
123                 }
124                 lputfloor(&flbuf, a);
125                 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
126         }
127 }       
128
129 /*
130  * This is the housekeeping loop.  Worker threads come through here after
131  * processing client requests but before jumping back into the pool.  We
132  * only allow housekeeping to execute once per minute, and we only allow one
133  * instance to run at a time.
134  */
135 void do_housekeeping(void) {
136         static int housekeeping_in_progress = 0;
137         static time_t last_timer = 0L;
138         int do_housekeeping_now = 0;
139         time_t now;
140
141         /*
142          * We do it this way instead of wrapping the whole loop in an
143          * S_HOUSEKEEPING critical section because it eliminates the need to
144          * potentially have multiple concurrent mutexes in progress.
145          */
146         begin_critical_section(S_HOUSEKEEPING);
147         now = time(NULL);
148         if ( (now - last_timer) > (time_t)60 ) {
149                 if (housekeeping_in_progress == 0) {
150                         do_housekeeping_now = 1;
151                         housekeeping_in_progress = 1;
152                         last_timer = time(NULL);
153                 }
154         }
155         end_critical_section(S_HOUSEKEEPING);
156
157         if (do_housekeeping_now == 0) {
158                 return;
159         }
160
161         /*
162          * Ok, at this point we've made the decision to run the housekeeping
163          * loop.  Everything below this point is real work.
164          */
165
166         cdb_check_handles();                    /* suggested by Justin Case */
167         PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
168
169         /*
170          * All done.
171          */
172         housekeeping_in_progress = 0;
173 }