2 * This file contains housekeeping tasks which periodically
3 * need to be executed. It keeps a nice little queue...
18 #include <sys/types.h>
20 #ifdef HAVE_SYS_SELECT_H
21 #include <sys/select.h>
26 #include "citserver.h"
28 #include "housekeeping.h"
29 #include "sysdep_decls.h"
33 int housepipe[2]; /* This is the queue for housekeeping tasks */
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.
42 void terminate_idle_sessions(void) {
43 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) ) {
55 session_to_kill = ccptr->cs_pid;
58 end_critical_section(S_SESSION_TABLE);
59 if (session_to_kill > 0) {
60 lprintf(3, "Session %d timed out. Terminating it...\n",
62 kill_session(session_to_kill);
64 } while(session_to_kill > 0);
69 void check_sched_shutdown(void) {
70 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
71 lprintf(3, "Scheduled shutdown initiating.\n");
79 * This is the main loop for the housekeeping thread. It remains active
80 * during the entire run of the server.
82 void housekeeping_loop(void) {
87 char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
90 if (pipe(housepipe) != 0) {
91 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
96 flags = (long) fcntl(housepipe[1], F_GETFL);
98 fcntl(housepipe[1], F_SETFL, flags);
103 tv.tv_sec = HOUSEKEEPING_WAKEUP;
106 FD_SET(housepipe[0], &readfds);
107 select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
108 if (FD_ISSET(housepipe[0], &readfds)) {
113 read(housepipe[0], house_cmd, 256);
116 memset(house_cmd, 0, 256);
117 strcpy(house_cmd, "MINUTE");
120 extract(cmd, house_cmd, 0);
122 /* Do whatever this cmd requires */
124 /* Once-every-minute housekeeper */
125 if (!strcmp(cmd, "MINUTE")) {
126 terminate_idle_sessions();
129 /* Scheduled shutdown housekeeper */
130 else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
131 check_sched_shutdown();
136 lprintf(7, "Unknown housekeeping command\n");
139 } while (did_something);
148 void enter_housekeeping_cmd(char *cmd) {
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");
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
167 void check_ref_counts_backend(struct quickroom *qrbuf) {
170 getfloor(&flbuf, qrbuf->QRfloor);
172 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
173 putfloor(&flbuf, qrbuf->QRfloor);
176 void check_ref_counts(void) {
180 for (a=0; a<MAXFLOORS; ++a) {
182 flbuf.f_ref_count = 0;
183 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
187 ForEachRoom(check_ref_counts_backend);