* Be more aggressive about shutting down when told to. Getting the
[citadel.git] / citadel / housekeeping.c
1 /*
2  * $Id$
3  *
4  * This file contains miscellaneous housekeeping tasks.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
36 #endif
37 #include "tools.h"
38 #include "citadel.h"
39 #include "server.h"
40 #include "serv_extensions.h"
41 #include "citserver.h"
42 #include "config.h"
43 #include "housekeeping.h"
44 #include "sysdep_decls.h"
45 #include "room_ops.h"
46 #include "database.h"
47
48
49
50
51 /*
52  * Terminate idle sessions.  This function pounds through the session table
53  * comparing the current time to each session's time-of-last-command.  If an
54  * idle session is found it is terminated, then the search restarts at the
55  * beginning because the pointer to our place in the list becomes invalid.
56  */
57 void terminate_idle_sessions(void) {
58         struct CitContext *ccptr;
59         time_t now;
60         int session_to_kill;
61         int killed = 0;
62
63         now = time(NULL);
64         session_to_kill = 0;
65         begin_critical_section(S_SESSION_TABLE);
66         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
67                 if (  (ccptr!=CC)
68                 && (config.c_sleeping > 0)
69                 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
70                         ccptr->kill_me = 1;
71                         ++killed;
72                 }
73         }
74         end_critical_section(S_SESSION_TABLE);
75         if (killed > 0)
76                 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
77 }
78
79
80
81 void check_sched_shutdown(void) {
82         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
83                 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
84                 time_to_die = 1;
85                 master_cleanup();
86         }
87 }
88
89
90
91 /*
92  * Check (and fix) floor reference counts.  This doesn't need to be done
93  * very often, since the counts should remain correct during normal operation.
94  */
95 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
96
97         int *new_refcounts;
98
99         new_refcounts = (int *) data;
100
101         ++new_refcounts[(int)qrbuf->QRfloor];
102 }
103
104 void check_ref_counts(void) {
105         struct floor flbuf;
106         int a;
107
108         int new_refcounts[MAXFLOORS];
109
110         lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
111         for (a=0; a<MAXFLOORS; ++a) {
112                 new_refcounts[a] = 0;
113         }
114
115         cdb_begin_transaction();
116         ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
117         cdb_end_transaction();
118
119         for (a=0; a<MAXFLOORS; ++a) {
120                 lgetfloor(&flbuf, a);
121                 flbuf.f_ref_count = new_refcounts[a];
122                 if (new_refcounts[a] > 0) {
123                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
124                 }
125                 else {
126                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
127                 }
128                 lputfloor(&flbuf, a);
129                 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
130         }
131 }       
132
133 /*
134  * This is the housekeeping loop.  Worker threads come through here after
135  * processing client requests but before jumping back into the pool.  We
136  * only allow housekeeping to execute once per minute, and we only allow one
137  * instance to run at a time.
138  */
139 void do_housekeeping(void) {
140         static int housekeeping_in_progress = 0;
141         static time_t last_timer = 0L;
142         int do_housekeeping_now = 0;
143         time_t now;
144
145         /*
146          * We do it this way instead of wrapping the whole loop in an
147          * S_HOUSEKEEPING critical section because it eliminates the need to
148          * potentially have multiple concurrent mutexes in progress.
149          */
150         begin_critical_section(S_HOUSEKEEPING);
151         now = time(NULL);
152         if ( (now - last_timer) > (time_t)60 ) {
153                 if (housekeeping_in_progress == 0) {
154                         do_housekeeping_now = 1;
155                         housekeeping_in_progress = 1;
156                         last_timer = time(NULL);
157                 }
158         }
159         end_critical_section(S_HOUSEKEEPING);
160
161         if (do_housekeeping_now == 0) {
162                 return;
163         }
164
165         /*
166          * Ok, at this point we've made the decision to run the housekeeping
167          * loop.  Everything below this point is real work.
168          */
169
170         cdb_check_handles();                    /* suggested by Justin Case */
171         PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
172
173         /*
174          * All done.
175          */
176         housekeeping_in_progress = 0;
177 }