Begun implimentation of a really good thread control interface.
[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 <libcitadel.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 #include "msgbase.h"
44 #include "journaling.h"
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         if (killed > 0)
71                 lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
72 }
73
74
75
76 void check_sched_shutdown(void) {
77         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
78                 lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
79                 time_to_die = 1;
80                 master_cleanup(0);
81         }
82 }
83
84
85
86 /*
87  * Check (and fix) floor reference counts.  This doesn't need to be done
88  * very often, since the counts should remain correct during normal operation.
89  */
90 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
91
92         int *new_refcounts;
93
94         new_refcounts = (int *) data;
95
96         ++new_refcounts[(int)qrbuf->QRfloor];
97 }
98
99 void check_ref_counts(void) {
100         struct floor flbuf;
101         int a;
102
103         int new_refcounts[MAXFLOORS];
104
105         lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
106         for (a=0; a<MAXFLOORS; ++a) {
107                 new_refcounts[a] = 0;
108         }
109
110         cdb_begin_transaction();
111         ForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
112         cdb_end_transaction();
113
114         for (a=0; a<MAXFLOORS; ++a) {
115                 lgetfloor(&flbuf, a);
116                 flbuf.f_ref_count = new_refcounts[a];
117                 if (new_refcounts[a] > 0) {
118                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
119                 }
120                 else {
121                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
122                 }
123                 lputfloor(&flbuf, a);
124                 lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
125         }
126 }       
127
128 /*
129  * This is the housekeeping loop.  Worker threads come through here after
130  * processing client requests but before jumping back into the pool.  We
131  * only allow housekeeping to execute once per minute, and we only allow one
132  * instance to run at a time.
133  */
134 void do_housekeeping(void) {
135         static int housekeeping_in_progress = 0;
136         static time_t last_timer = 0L;
137         int do_housekeeping_now = 0;
138         int do_perminute_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         if (housekeeping_in_progress == 0) {
148                 do_housekeeping_now = 1;
149                 housekeeping_in_progress = 1;
150                 now = time(NULL);
151                 if ( (now - last_timer) > (time_t)60 ) {
152                         do_perminute_housekeeping_now = 1;
153                         last_timer = time(NULL);
154                 }
155         }
156         end_critical_section(S_HOUSEKEEPING);
157
158         if (do_housekeeping_now == 0) {
159                 return;
160         }
161
162         /*
163          * Ok, at this point we've made the decision to run the housekeeping
164          * loop.  Everything below this point is real work.
165          */
166
167         /* First, do the "as often as needed" stuff... */
168         JournalRunQueue();
169
170         PerformSessionHooks(EVT_HOUSE); /* perform as needed housekeeping */
171         
172         ctdl_internal_thread_gc(0);
173
174         /* Then, do the "once per minute" stuff... */
175         if (do_perminute_housekeeping_now) {
176                 cdb_check_handles();                    /* suggested by Justin Case */
177                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
178         }
179
180         /*
181          * All done.
182          */
183         housekeeping_in_progress = 0;
184 }