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