726c928158e213e09ce0dc51c0a02f8033eeb9b6
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains housekeeping tasks which periodically
3  * need to be executed.  It keeps a nice little queue...
4  *
5  * $Id$
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 #ifdef HAVE_PTHREAD_H
24 #include <pthread.h>
25 #endif
26 #include "tools.h"
27 #include "citadel.h"
28 #include "server.h"
29 #include "citserver.h"
30 #include "config.h"
31 #include "housekeeping.h"
32 #include "sysdep_decls.h"
33 #include "room_ops.h"
34
35
36 int housepipe[2];       /* This is the queue for housekeeping tasks */
37
38
39 /*
40  * Terminate idle sessions.  This function pounds through the session table
41  * comparing the current time to each session's time-of-last-command.  If an
42  * idle session is found it is terminated, then the search restarts at the
43  * beginning because the pointer to our place in the list becomes invalid.
44  */
45 void terminate_idle_sessions(void) {
46         struct CitContext *ccptr;
47         time_t now;
48         int session_to_kill;
49
50         do {
51                 now = time(NULL);
52                 session_to_kill = 0;
53                 begin_critical_section(S_SESSION_TABLE);
54                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
55                         if (  (ccptr!=CC)
56                         && (config.c_sleeping > 0)
57                         && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
58                                 session_to_kill = ccptr->cs_pid;
59                                 }
60                         }
61                 end_critical_section(S_SESSION_TABLE);
62                 if (session_to_kill > 0) {
63                         lprintf(3, "Session %d timed out.  Terminating it...\n",
64                                 session_to_kill);
65                         kill_session(session_to_kill);
66                         }
67                 } while(session_to_kill > 0);
68         }
69
70
71
72 void check_sched_shutdown(void) {
73         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
74                 lprintf(3, "Scheduled shutdown initiating.\n");
75                 master_cleanup();
76         }
77 }
78
79
80
81 /*
82  * This is the main loop for the housekeeping thread.  It remains active
83  * during the entire run of the server.
84  */
85 void housekeeping_loop(void) {
86         long flags;
87         struct timeval tv;
88         fd_set readfds;
89         int did_something;
90         char house_cmd[256];    /* Housekeep cmds are always 256 bytes long */
91         char cmd[256];
92
93         if (pipe(housepipe) != 0) {
94                 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
95                         strerror(errno));
96                 exit(0);
97         }
98
99         flags = (long) fcntl(housepipe[1], F_GETFL);
100         flags |= O_NONBLOCK;
101         fcntl(housepipe[1], F_SETFL, flags);
102
103         while(1) {
104                 do {
105                         did_something = 0;
106                         tv.tv_sec = HOUSEKEEPING_WAKEUP;
107                         tv.tv_usec = 0;
108                         FD_ZERO(&readfds);
109                         FD_SET(housepipe[0], &readfds);
110                         select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
111                         if (FD_ISSET(housepipe[0], &readfds)) {
112                                 did_something = 1;
113                         }
114
115                         if (did_something) {
116                                 read(housepipe[0], house_cmd, 256);
117                         }
118                         else {
119                                 memset(house_cmd, 0, 256);
120                                 strcpy(house_cmd, "MINUTE");
121                         }
122
123                         extract(cmd, house_cmd, 0);
124
125                         /* Do whatever this cmd requires */
126
127                         /* Once-every-minute housekeeper */
128                         if (!strcmp(cmd, "MINUTE")) {
129                                 terminate_idle_sessions();
130                         }
131
132                         /* Scheduled shutdown housekeeper */
133                         else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
134                                 check_sched_shutdown();
135                         }
136
137                         /* Unknown */
138                         else {
139                                 lprintf(7, "Unknown housekeeping command\n");
140                         }
141
142                 } while (did_something);
143         }
144 }
145
146
147
148
149
150
151 void enter_housekeeping_cmd(char *cmd) {
152         char cmdbuf[256];
153
154         lprintf(9, "enter_housekeeping_cmd(%s)\n", cmd);
155         safestrncpy(cmdbuf, cmd, 256);
156         begin_critical_section(S_HOUSEKEEPING);
157         write(housepipe[1], cmdbuf, 256);
158         end_critical_section(S_HOUSEKEEPING);
159         lprintf(9, "leaving enter_housekeeping_cmd()\n");
160 }
161         
162
163
164 /*
165  * Check (and fix) floor reference counts.  This doesn't need to be done
166  * very often, since the counts should remain correct during normal operation.
167  * NOTE: this function pair should ONLY be called during startup.  It is NOT
168  * thread safe.
169  */
170 void check_ref_counts_backend(struct quickroom *qrbuf) {
171         struct floor flbuf;
172
173         getfloor(&flbuf, qrbuf->QRfloor);
174         ++flbuf.f_ref_count;
175         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
176         putfloor(&flbuf, qrbuf->QRfloor);
177         }
178
179 void check_ref_counts(void) {
180         struct floor flbuf;
181         int a;
182
183         for (a=0; a<MAXFLOORS; ++a) {
184                 getfloor(&flbuf, a);
185                 flbuf.f_ref_count = 0;
186                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
187                 putfloor(&flbuf, a);
188                 }
189
190         ForEachRoom(check_ref_counts_backend);
191         }       
192