* setup.c: default value for maxsessions is now 0 (no limit)
[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 <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <time.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <limits.h>
16 #include <pthread.h>
17 #include "citadel.h"
18 #include "server.h"
19 #include "citserver.h"
20 #include "config.h"
21 #include "housekeeping.h"
22 #include "sysdep_decls.h"
23 #include "room_ops.h"
24
25 /*
26  * Terminate idle sessions.  This function pounds through the session table
27  * comparing the current time to each session's time-of-last-command.  If an
28  * idle session is found it is terminated, then the search restarts at the
29  * beginning because the pointer to our place in the list becomes invalid.
30  */
31 void terminate_idle_sessions(void) {
32         struct CitContext *ccptr;
33         time_t now;
34
35 START_OVER:
36         now = time(NULL);
37         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
38                 if (  (ccptr!=CC)
39                    && (config.c_sleeping > 0)
40                    && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
41                         lprintf(3, "Session %d timed out.  Terminating it...\n",
42                                 ccptr->cs_pid);
43                         kill_session(ccptr->cs_pid);
44                         lprintf(9, "...done terminating it.\n");
45                         goto START_OVER;
46                         }
47                 }
48         }
49
50
51 /*
52  * Main housekeeping function.  This gets run whenever a session terminates.
53  */
54 void do_housekeeping(void) {
55
56         lprintf(9, "--- begin housekeeping ---\n");
57         begin_critical_section(S_HOUSEKEEPING);
58         /*
59          * Terminate idle sessions.
60          */
61         lprintf(7, "Calling terminate_idle_sessions()\n");
62         terminate_idle_sessions();
63         lprintf(9, "Done with terminate_idle_sessions()\n");
64
65         /*
66          * If the server is scheduled to shut down the next time all
67          * users are logged out, now's the time to do it.
68          */
69         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
70                 lprintf(3, "Scheduled shutdown initiating.\n");
71                 master_cleanup();
72                 }
73         end_critical_section(S_HOUSEKEEPING);
74         lprintf(9, "--- end housekeeping ---\n");
75         }
76
77
78
79 /*
80  * Check (and fix) floor reference counts.  This doesn't need to be done
81  * very often, since the counts should remain correct during normal operation.
82  * NOTE: this function pair should ONLY be called during startup.  It is NOT
83  * thread safe.
84  */
85 void check_ref_counts_backend(struct quickroom *qrbuf) {
86         struct floor flbuf;
87
88         lgetfloor(&flbuf, qrbuf->QRfloor);
89         ++flbuf.f_ref_count;
90         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
91         lputfloor(&flbuf, qrbuf->QRfloor);
92         }
93
94 void check_ref_counts(void) {
95         struct floor flbuf;
96         int a;
97
98         for (a=0; a<MAXFLOORS; ++a) {
99                 lgetfloor(&flbuf, a);
100                 flbuf.f_ref_count = 0;
101                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
102                 lputfloor(&flbuf, a);
103                 }
104
105         ForEachRoom(check_ref_counts_backend);
106         }       
107