1f2fc70f36e3d0cc40f85e505c23df6ccfba9d84
[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 #include <time.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #ifdef HAVE_SYS_SELECT_H
21 #include <sys/select.h>
22 #endif
23 #include "tools.h"
24 #include "citadel.h"
25 #include "server.h"
26 #include "citserver.h"
27 #include "config.h"
28 #include "housekeeping.h"
29 #include "sysdep_decls.h"
30 #include "room_ops.h"
31 #include "database.h"
32
33
34 int housepipe[2];       /* This is the queue for housekeeping tasks */
35
36
37 /*
38  * Terminate idle sessions.  This function pounds through the session table
39  * comparing the current time to each session's time-of-last-command.  If an
40  * idle session is found it is terminated, then the search restarts at the
41  * beginning because the pointer to our place in the list becomes invalid.
42  */
43 void terminate_idle_sessions(void) {
44         struct CitContext *ccptr;
45         time_t now;
46         int session_to_kill;
47         int killed = 0;
48
49         now = time(NULL);
50         session_to_kill = 0;
51         begin_critical_section(S_SESSION_TABLE);
52         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
53                 if (  (ccptr!=CC)
54                 && (config.c_sleeping > 0)
55                 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
56                         ccptr->kill_me = 1;
57                         ++killed;
58                 }
59         }
60         end_critical_section(S_SESSION_TABLE);
61         lprintf(9, "Terminated %d idle sessions\n", killed);
62 }
63
64
65
66 void check_sched_shutdown(void) {
67         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
68                 lprintf(3, "Scheduled shutdown initiating.\n");
69                 time_to_die = 1;
70         }
71 }
72
73
74
75 /*
76  * Check (and fix) floor reference counts.  This doesn't need to be done
77  * very often, since the counts should remain correct during normal operation.
78  * NOTE: this function pair should ONLY be called during startup.  It is NOT
79  * thread safe.
80  */
81 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
82         struct floor flbuf;
83
84         getfloor(&flbuf, qrbuf->QRfloor);
85         ++flbuf.f_ref_count;
86         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
87         putfloor(&flbuf, qrbuf->QRfloor);
88 }
89
90 void check_ref_counts(void) {
91         struct floor flbuf;
92         int a;
93
94         lprintf(7, "Checking floor reference counts\n");
95         for (a=0; a<MAXFLOORS; ++a) {
96                 getfloor(&flbuf, a);
97                 flbuf.f_ref_count = 0;
98                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
99                 putfloor(&flbuf, a);
100         }
101
102         ForEachRoom(check_ref_counts_backend, NULL);
103 }       
104