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