* Removed all of the thread cancellation cruft that is no longer necessary
[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         do {
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                                 session_to_kill = ccptr->cs_pid;
56                                 }
57                         }
58                 end_critical_section(S_SESSION_TABLE);
59                 if (session_to_kill > 0) {
60                         lprintf(3, "Session %d timed out.  Terminating it...\n",
61                                 session_to_kill);
62                         kill_session(session_to_kill);
63                         }
64                 } while(session_to_kill > 0);
65         }
66
67
68
69 void check_sched_shutdown(void) {
70         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
71                 lprintf(3, "Scheduled shutdown initiating.\n");
72                 master_cleanup();
73         }
74 }
75
76
77
78 /*
79  * This is the main loop for the housekeeping thread.  It remains active
80  * during the entire run of the server.
81  */
82 void housekeeping_loop(void) {
83         long flags;
84         struct timeval tv;
85         fd_set readfds;
86         int did_something;
87         char house_cmd[256];    /* Housekeep cmds are always 256 bytes long */
88         char cmd[256];
89
90         if (pipe(housepipe) != 0) {
91                 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
92                         strerror(errno));
93                 exit(0);
94         }
95
96         flags = (long) fcntl(housepipe[1], F_GETFL);
97         flags |= O_NONBLOCK;
98         fcntl(housepipe[1], F_SETFL, flags);
99
100         while(1) {
101                 do {
102                         did_something = 0;
103                         tv.tv_sec = HOUSEKEEPING_WAKEUP;
104                         tv.tv_usec = 0;
105                         FD_ZERO(&readfds);
106                         FD_SET(housepipe[0], &readfds);
107                         select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
108                         if (FD_ISSET(housepipe[0], &readfds)) {
109                                 did_something = 1;
110                         }
111
112                         if (did_something) {
113                                 read(housepipe[0], house_cmd, 256);
114                         }
115                         else {
116                                 memset(house_cmd, 0, 256);
117                                 strcpy(house_cmd, "MINUTE");
118                         }
119
120                         extract(cmd, house_cmd, 0);
121
122                         /* Do whatever this cmd requires */
123
124                         /* Once-every-minute housekeeper */
125                         if (!strcmp(cmd, "MINUTE")) {
126                                 terminate_idle_sessions();
127                         }
128
129                         /* Scheduled shutdown housekeeper */
130                         else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
131                                 check_sched_shutdown();
132                         }
133
134                         /* Unknown */
135                         else {
136                                 lprintf(7, "Unknown housekeeping command\n");
137                         }
138
139                 } while (did_something);
140         }
141 }
142
143
144
145
146
147
148 void enter_housekeeping_cmd(char *cmd) {
149         char cmdbuf[256];
150
151         lprintf(9, "enter_housekeeping_cmd(%s)\n", cmd);
152         safestrncpy(cmdbuf, cmd, 256);
153         begin_critical_section(S_HOUSEKEEPING);
154         write(housepipe[1], cmdbuf, 256);
155         end_critical_section(S_HOUSEKEEPING);
156         lprintf(9, "leaving enter_housekeeping_cmd()\n");
157 }
158         
159
160
161 /*
162  * Check (and fix) floor reference counts.  This doesn't need to be done
163  * very often, since the counts should remain correct during normal operation.
164  * NOTE: this function pair should ONLY be called during startup.  It is NOT
165  * thread safe.
166  */
167 void check_ref_counts_backend(struct quickroom *qrbuf) {
168         struct floor flbuf;
169
170         getfloor(&flbuf, qrbuf->QRfloor);
171         ++flbuf.f_ref_count;
172         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
173         putfloor(&flbuf, qrbuf->QRfloor);
174         }
175
176 void check_ref_counts(void) {
177         struct floor flbuf;
178         int a;
179
180         for (a=0; a<MAXFLOORS; ++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);
188         }       
189