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