4 * This file contains housekeeping tasks which periodically
5 * need to be executed. It keeps a nice little queue...
19 #include <sys/types.h>
21 #ifdef HAVE_SYS_SELECT_H
22 #include <sys/select.h>
27 #include "citserver.h"
29 #include "housekeeping.h"
30 #include "sysdep_decls.h"
34 int housepipe[2]; /* This is the queue for housekeeping tasks */
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.
43 void terminate_idle_sessions(void) {
44 struct CitContext *ccptr;
50 begin_critical_section(S_SESSION_TABLE);
51 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
53 && (config.c_sleeping > 0)
54 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
58 end_critical_section(S_SESSION_TABLE);
63 void check_sched_shutdown(void) {
64 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
65 lprintf(3, "Scheduled shutdown initiating.\n");
73 * This is the main loop for the housekeeping thread. It remains active
74 * during the entire run of the server.
76 void housekeeping_loop(void) {
81 char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
84 if (pipe(housepipe) != 0) {
85 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
90 flags = (long) fcntl(housepipe[1], F_GETFL);
92 fcntl(housepipe[1], F_SETFL, flags);
97 tv.tv_sec = HOUSEKEEPING_WAKEUP;
100 FD_SET(housepipe[0], &readfds);
101 select(housepipe[0] + 1, &readfds, NULL, NULL, &tv);
102 if (FD_ISSET(housepipe[0], &readfds)) {
107 read(housepipe[0], house_cmd, 256);
110 memset(house_cmd, 0, 256);
111 strcpy(house_cmd, "MINUTE");
114 extract(cmd, house_cmd, 0);
116 /* Do whatever this cmd requires */
118 /* Once-every-minute housekeeper */
119 if (!strcmp(cmd, "MINUTE")) {
120 terminate_idle_sessions();
123 /* Scheduled shutdown housekeeper */
124 else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
125 check_sched_shutdown();
130 lprintf(7, "Unknown housekeeping command\n");
133 } while (did_something);
142 void enter_housekeeping_cmd(char *cmd) {
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");
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
161 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
164 getfloor(&flbuf, qrbuf->QRfloor);
166 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
167 putfloor(&flbuf, qrbuf->QRfloor);
170 void check_ref_counts(void) {
174 for (a=0; a<MAXFLOORS; ++a) {
176 flbuf.f_ref_count = 0;
177 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
181 ForEachRoom(check_ref_counts_backend, NULL);