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;
49 begin_critical_section(S_SESSION_TABLE);
50 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
52 && (config.c_sleeping > 0)
53 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
57 end_critical_section(S_SESSION_TABLE);
62 void check_sched_shutdown(void) {
63 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
64 lprintf(3, "Scheduled shutdown initiating.\n");
72 * This is the main loop for the housekeeping thread. It remains active
73 * during the entire run of the server.
75 void housekeeping_loop(void) {
80 char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
83 if (pipe(housepipe) != 0) {
84 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
89 flags = (long) fcntl(housepipe[1], F_GETFL);
91 fcntl(housepipe[1], F_SETFL, flags);
96 tv.tv_sec = HOUSEKEEPING_WAKEUP;
99 FD_SET(housepipe[0], &readfds);
100 select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
101 if (FD_ISSET(housepipe[0], &readfds)) {
106 read(housepipe[0], house_cmd, 256);
109 memset(house_cmd, 0, 256);
110 strcpy(house_cmd, "MINUTE");
113 extract(cmd, house_cmd, 0);
115 /* Do whatever this cmd requires */
117 /* Once-every-minute housekeeper */
118 if (!strcmp(cmd, "MINUTE")) {
119 terminate_idle_sessions();
122 /* Scheduled shutdown housekeeper */
123 else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
124 check_sched_shutdown();
129 lprintf(7, "Unknown housekeeping command\n");
132 } while (did_something);
141 void enter_housekeeping_cmd(char *cmd) {
144 lprintf(9, "enter_housekeeping_cmd(%s)\n", cmd);
145 safestrncpy(cmdbuf, cmd, 256);
146 begin_critical_section(S_HOUSEKEEPING);
147 write(housepipe[1], cmdbuf, 256);
148 end_critical_section(S_HOUSEKEEPING);
149 lprintf(9, "leaving enter_housekeeping_cmd()\n");
155 * Check (and fix) floor reference counts. This doesn't need to be done
156 * very often, since the counts should remain correct during normal operation.
157 * NOTE: this function pair should ONLY be called during startup. It is NOT
160 void check_ref_counts_backend(struct quickroom *qrbuf) {
163 getfloor(&flbuf, qrbuf->QRfloor);
165 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
166 putfloor(&flbuf, qrbuf->QRfloor);
169 void check_ref_counts(void) {
173 for (a=0; a<MAXFLOORS; ++a) {
175 flbuf.f_ref_count = 0;
176 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
180 ForEachRoom(check_ref_counts_backend);