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"
35 int housepipe[2]; /* This is the queue for housekeeping tasks */
39 * Terminate idle sessions. This function pounds through the session table
40 * comparing the current time to each session's time-of-last-command. If an
41 * idle session is found it is terminated, then the search restarts at the
42 * beginning because the pointer to our place in the list becomes invalid.
44 void terminate_idle_sessions(void) {
45 struct CitContext *ccptr;
51 begin_critical_section(S_SESSION_TABLE);
52 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
54 && (config.c_sleeping > 0)
55 && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
59 end_critical_section(S_SESSION_TABLE);
64 void check_sched_shutdown(void) {
65 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
66 lprintf(3, "Scheduled shutdown initiating.\n");
74 * This is the main loop for the housekeeping thread. It remains active
75 * during the entire run of the server.
77 void housekeeping_loop(void) {
82 char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
85 if (pipe(housepipe) != 0) {
86 lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
91 flags = (long) fcntl(housepipe[1], F_GETFL);
93 fcntl(housepipe[1], F_SETFL, flags);
98 tv.tv_sec = HOUSEKEEPING_WAKEUP;
101 FD_SET(housepipe[0], &readfds);
102 select(housepipe[0] + 1, &readfds, NULL, NULL, &tv);
103 if (FD_ISSET(housepipe[0], &readfds)) {
108 read(housepipe[0], house_cmd, 256);
111 memset(house_cmd, 0, 256);
112 strcpy(house_cmd, "MINUTE");
115 extract(cmd, house_cmd, 0);
116 cdb_begin_transaction();
118 /* Do whatever this cmd requires */
120 /* Once-every-minute housekeeper */
121 if (!strcmp(cmd, "MINUTE")) {
122 terminate_idle_sessions();
125 /* Scheduled shutdown housekeeper */
126 else if (!strcmp(cmd, "SCHED_SHUTDOWN")) {
127 check_sched_shutdown();
132 lprintf(7, "Unknown housekeeping command\n");
135 cdb_end_transaction();
137 } while (did_something);
146 void enter_housekeeping_cmd(char *cmd) {
149 lprintf(9, "enter_housekeeping_cmd(%s)\n", cmd);
150 safestrncpy(cmdbuf, cmd, 256);
151 begin_critical_section(S_HOUSEKEEPING);
152 write(housepipe[1], cmdbuf, 256);
153 end_critical_section(S_HOUSEKEEPING);
154 lprintf(9, "leaving enter_housekeeping_cmd()\n");
160 * Check (and fix) floor reference counts. This doesn't need to be done
161 * very often, since the counts should remain correct during normal operation.
162 * NOTE: this function pair should ONLY be called during startup. It is NOT
165 void check_ref_counts_backend(struct quickroom *qrbuf, void *data) {
168 getfloor(&flbuf, qrbuf->QRfloor);
170 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
171 putfloor(&flbuf, qrbuf->QRfloor);
174 void check_ref_counts(void) {
178 for (a=0; a<MAXFLOORS; ++a) {
180 flbuf.f_ref_count = 0;
181 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
185 ForEachRoom(check_ref_counts_backend, NULL);