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>
29 #include "citserver.h"
31 #include "housekeeping.h"
32 #include "sysdep_decls.h"
36 int housepipe[2]; /* This is the queue for housekeeping tasks */
40 * Terminate idle sessions. This function pounds through the session table
41 * comparing the current time to each session's time-of-last-command. If an
42 * idle session is found it is terminated, then the search restarts at the
43 * beginning because the pointer to our place in the list becomes invalid.
45 void terminate_idle_sessions(void) {
46 struct CitContext *ccptr;
53 begin_critical_section(S_SESSION_TABLE);
54 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
56 && (config.c_sleeping > 0)
57 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
58 session_to_kill = ccptr->cs_pid;
61 end_critical_section(S_SESSION_TABLE);
62 if (session_to_kill > 0) {
63 lprintf(3, "Session %d timed out. Terminating it...\n",
65 kill_session(session_to_kill);
67 } while(session_to_kill > 0);
72 void check_sched_shutdown(void) {
73 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
74 lprintf(3, "Scheduled shutdown initiating.\n");
82 * This is the main loop for the housekeeping thread. It remains active
83 * during the entire run of the server.
85 void housekeeping_loop(void) {
90 char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
92 if (pipe(housepipe) != 0) {
93 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
98 flags = (long) fcntl(housepipe[1], F_GETFL);
100 fcntl(housepipe[1], F_SETFL, flags);
105 tv.tv_sec = HOUSEKEEPING_WAKEUP;
108 FD_SET(housepipe[0], &readfds);
109 select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
110 if (FD_ISSET(housepipe[0], &readfds)) {
115 read(housepipe[0], house_cmd, 256);
118 memset(house_cmd, 0, 256);
119 strcpy(house_cmd, "MINUTE");
123 /* Do whatever this cmd requires */
124 if (!strcmp(house_cmd, "MINUTE")) {
125 terminate_idle_sessions();
128 else if (!strcmp(house_cmd, "SCHED_SHUTDOWN")) {
129 check_sched_shutdown();
133 lprintf(7, "Unknown housekeeping command\n");
136 } while (did_something);
145 void enter_housekeeping_cmd(char *cmd) {
148 safestrncpy(cmdbuf, cmd, 256);
149 begin_critical_section(S_HOUSEKEEPING);
150 write(housepipe[1], cmdbuf, 256);
151 end_critical_section(S_HOUSEKEEPING);
157 * Check (and fix) floor reference counts. This doesn't need to be done
158 * very often, since the counts should remain correct during normal operation.
159 * NOTE: this function pair should ONLY be called during startup. It is NOT
162 void check_ref_counts_backend(struct quickroom *qrbuf) {
165 getfloor(&flbuf, qrbuf->QRfloor);
167 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
168 putfloor(&flbuf, qrbuf->QRfloor);
171 void check_ref_counts(void) {
175 for (a=0; a<MAXFLOORS; ++a) {
177 flbuf.f_ref_count = 0;
178 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
182 ForEachRoom(check_ref_counts_backend);