a97ea47799e35c7c7ee824da1ca1dc40397aae44
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains housekeeping tasks which periodically
3  * need to be executed.
4  *
5  * $Id$
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <limits.h>
17 #ifdef HAVE_PTHREAD_H
18 #include <pthread.h>
19 #endif
20 #include "citadel.h"
21 #include "server.h"
22 #include "citserver.h"
23 #include "config.h"
24 #include "housekeeping.h"
25 #include "sysdep_decls.h"
26 #include "room_ops.h"
27
28 /*
29  * Terminate idle sessions.  This function pounds through the session table
30  * comparing the current time to each session's time-of-last-command.  If an
31  * idle session is found it is terminated, then the search restarts at the
32  * beginning because the pointer to our place in the list becomes invalid.
33  */
34 void terminate_idle_sessions(void) {
35         struct CitContext *ccptr;
36         time_t now;
37         int session_to_kill;
38
39         do {
40                 now = time(NULL);
41                 session_to_kill = 0;
42                 lprintf(9, "Scanning for timed out sessions...\n");
43                 begin_critical_section(S_SESSION_TABLE);
44                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
45                         if (  (ccptr!=CC)
46                         && (config.c_sleeping > 0)
47                         && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
48                                 session_to_kill = ccptr->cs_pid;
49                                 }
50                         }
51                 end_critical_section(S_SESSION_TABLE);
52                 lprintf(9, "...done scanning.\n");
53                 if (session_to_kill > 0) {
54                         lprintf(3, "Session %d timed out.  Terminating it...\n",
55                                 session_to_kill);
56                         kill_session(session_to_kill);
57                         lprintf(9, "...done terminating it.\n");
58                         }
59                 } while(session_to_kill > 0);
60         }
61
62
63 /*
64  * Main housekeeping function.  This gets run whenever a session terminates.
65  */
66 void do_housekeeping(void) {
67
68         lprintf(9, "--- begin housekeeping ---\n");
69         begin_critical_section(S_HOUSEKEEPING);
70         /*
71          * Terminate idle sessions.
72          */
73         lprintf(7, "Calling terminate_idle_sessions()\n");
74         terminate_idle_sessions();
75         lprintf(9, "Done with terminate_idle_sessions()\n");
76
77         /*
78          * If the server is scheduled to shut down the next time all
79          * users are logged out, now's the time to do it.
80          */
81         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
82                 lprintf(3, "Scheduled shutdown initiating.\n");
83                 master_cleanup();
84                 }
85         end_critical_section(S_HOUSEKEEPING);
86         lprintf(9, "--- end housekeeping ---\n");
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  * NOTE: this function pair should ONLY be called during startup.  It is NOT
95  * thread safe.
96  */
97 void check_ref_counts_backend(struct quickroom *qrbuf) {
98         struct floor flbuf;
99
100         lgetfloor(&flbuf, qrbuf->QRfloor);
101         ++flbuf.f_ref_count;
102         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
103         lputfloor(&flbuf, qrbuf->QRfloor);
104         }
105
106 void check_ref_counts(void) {
107         struct floor flbuf;
108         int a;
109
110         for (a=0; a<MAXFLOORS; ++a) {
111                 lgetfloor(&flbuf, a);
112                 flbuf.f_ref_count = 0;
113                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
114                 lputfloor(&flbuf, a);
115                 }
116
117         ForEachRoom(check_ref_counts_backend);
118         }       
119