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