5cfea179ae84809a1bfb41c9345dd676dbcb213a
[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 "citserver.h"
37 #include "config.h"
38 #include "housekeeping.h"
39 #include "sysdep_decls.h"
40 #include "room_ops.h"
41 #include "database.h"
42 #include "dynloader.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         lprintf(9, "Terminated %d idle sessions\n", killed);
72 }
73
74
75
76 void check_sched_shutdown(void) {
77         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
78                 lprintf(3, "Scheduled shutdown initiating.\n");
79                 time_to_die = 1;
80         }
81 }
82
83
84
85 /*
86  * Check (and fix) floor reference counts.  This doesn't need to be done
87  * very often, since the counts should remain correct during normal operation.
88  * NOTE: this function pair should ONLY be called during startup.  It is NOT
89  * thread safe.
90  */
91 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
92         struct floor flbuf;
93
94         getfloor(&flbuf, qrbuf->QRfloor);
95         ++flbuf.f_ref_count;
96         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
97         putfloor(&flbuf, qrbuf->QRfloor);
98 }
99
100 void check_ref_counts(void) {
101         struct floor flbuf;
102         int a;
103
104         lprintf(7, "Checking floor reference counts\n");
105         for (a=0; a<MAXFLOORS; ++a) {
106                 getfloor(&flbuf, a);
107                 flbuf.f_ref_count = 0;
108                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
109                 putfloor(&flbuf, a);
110         }
111
112         ForEachRoom(check_ref_counts_backend, NULL);
113 }       
114
115 /*
116  * This is the housekeeping loop.  Worker threads come through here after
117  * processing client requests but before jumping back into the pool.  We
118  * only allow housekeeping to execute once per minute, and we only allow one
119  * instance to run at a time.
120  */
121 void do_housekeeping(void) {
122         static int housekeeping_in_progress = 0;
123         static time_t last_timer = 0L;
124         int do_housekeeping_now = 0;
125
126         /*
127          * We do it this way instead of wrapping the whole loop in an
128          * S_HOUSEKEEPING critical section because it eliminates the need to
129          * potentially have multiple concurrent mutexes in progress.
130          */
131         begin_critical_section(S_HOUSEKEEPING);
132         if ( ((time(NULL) - last_timer) > 60L)
133            && (housekeeping_in_progress == 0) ) {
134                 do_housekeeping_now = 1;
135                 housekeeping_in_progress = 1;
136                 last_timer = time(NULL);
137         }
138         end_critical_section(S_HOUSEKEEPING);
139         if (do_housekeeping_now == 0) return;
140
141         /*
142          * Ok, at this point we've made the decision to run the housekeeping
143          * loop.  Everything below this point is real work.
144          */
145
146         cdb_check_handles();                    /* suggested by Justin Case */
147         PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
148
149         /*
150          * All done.
151          */
152         housekeeping_in_progress = 0;
153 }