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