* We now have a housekeeping thread and a housekeeping queue.
[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
92         if (pipe(housepipe) != 0) {
93                 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
94                         strerror(errno));
95                 exit(0);
96         }
97
98         flags = (long) fcntl(housepipe[1], F_GETFL);
99         flags |= O_NONBLOCK;
100         fcntl(housepipe[1], F_SETFL, flags);
101
102         while(1) {
103                 do {
104                         did_something = 0;
105                         tv.tv_sec = HOUSEKEEPING_WAKEUP;
106                         tv.tv_usec = 0;
107                         FD_ZERO(&readfds);
108                         FD_SET(housepipe[0], &readfds);
109                         select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
110                         if (FD_ISSET(housepipe[0], &readfds)) {
111                                 did_something = 1;
112                         }
113
114                         if (did_something) {
115                                 read(housepipe[0], house_cmd, 256);
116                         }
117                         else {
118                                 memset(house_cmd, 0, 256);
119                                 strcpy(house_cmd, "MINUTE");
120                         }
121
122
123                         /* Do whatever this cmd requires */
124                         if (!strcmp(house_cmd, "MINUTE")) {
125                                 terminate_idle_sessions();
126                         }
127
128                         else if (!strcmp(house_cmd, "SCHED_SHUTDOWN")) {
129                                 check_sched_shutdown();
130                         }
131
132                         else {
133                                 lprintf(7, "Unknown housekeeping command\n");
134                         }
135
136                 } while (did_something);
137         }
138 }
139
140
141
142
143
144
145 void enter_housekeeping_cmd(char *cmd) {
146         char cmdbuf[256];
147
148         safestrncpy(cmdbuf, cmd, 256);
149         begin_critical_section(S_HOUSEKEEPING);
150         write(housepipe[1], cmdbuf, 256);
151         end_critical_section(S_HOUSEKEEPING);
152 }
153         
154
155
156 /*
157  * Check (and fix) floor reference counts.  This doesn't need to be done
158  * very often, since the counts should remain correct during normal operation.
159  * NOTE: this function pair should ONLY be called during startup.  It is NOT
160  * thread safe.
161  */
162 void check_ref_counts_backend(struct quickroom *qrbuf) {
163         struct floor flbuf;
164
165         getfloor(&flbuf, qrbuf->QRfloor);
166         ++flbuf.f_ref_count;
167         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
168         putfloor(&flbuf, qrbuf->QRfloor);
169         }
170
171 void check_ref_counts(void) {
172         struct floor flbuf;
173         int a;
174
175         for (a=0; a<MAXFLOORS; ++a) {
176                 getfloor(&flbuf, a);
177                 flbuf.f_ref_count = 0;
178                 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
179                 putfloor(&flbuf, a);
180                 }
181
182         ForEachRoom(check_ref_counts_backend);
183         }       
184