$Log$
+Revision 1.377 1999/10/04 03:19:52 ajc
+* We now have a housekeeping thread and a housekeeping queue.
+
Revision 1.376 1999/10/03 21:48:21 ajc
* Added serv_upgrade.h to automagically convert pre-5.55 format user records
to 5.55 format user records and generate vCards.
Fri Jul 10 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
* Initial CVS import
-
lprintf(7, "cleanup_stuff() calling RemoveContext(%d)\n", CC->cs_pid);
RemoveContext(CC);
- /* While we still have an extra thread with no user attached to it,
- * take the opportunity to do some housekeeping before exiting.
- */
- do_housekeeping();
+ /* Wake up the housekeeping thread */
+ enter_housekeeping_cmd("SCHED_SHUTDOWN");
}
void *CtdlGetUserData(unsigned long requested_sym);
void CtdlAllocUserData(unsigned long requested_sym, size_t num_bytes);
int CtdlGetDynamicSymbol(void);
+void enter_housekeeping_cmd(char *);
+
extern int do_defrag;
/*
* This file contains housekeeping tasks which periodically
- * need to be executed.
+ * need to be executed. It keeps a nice little queue...
*
* $Id$
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
+#include <fcntl.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
+#include "tools.h"
#include "citadel.h"
#include "server.h"
#include "citserver.h"
#include "sysdep_decls.h"
#include "room_ops.h"
+
+int housepipe[2]; /* This is the queue for housekeeping tasks */
+
+
/*
* Terminate idle sessions. This function pounds through the session table
* comparing the current time to each session's time-of-last-command. If an
do {
now = time(NULL);
session_to_kill = 0;
- lprintf(9, "Scanning for timed out sessions...\n");
begin_critical_section(S_SESSION_TABLE);
for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
if ( (ccptr!=CC)
}
}
end_critical_section(S_SESSION_TABLE);
- lprintf(9, "...done scanning.\n");
if (session_to_kill > 0) {
lprintf(3, "Session %d timed out. Terminating it...\n",
session_to_kill);
kill_session(session_to_kill);
- lprintf(9, "...done terminating it.\n");
}
} while(session_to_kill > 0);
}
-/*
- * Main housekeeping function. This gets run whenever a session terminates.
- */
-void do_housekeeping(void) {
- lprintf(9, "--- begin housekeeping ---\n");
- begin_critical_section(S_HOUSEKEEPING);
- /*
- * Terminate idle sessions.
- */
- lprintf(7, "Calling terminate_idle_sessions()\n");
- terminate_idle_sessions();
- lprintf(9, "Done with terminate_idle_sessions()\n");
-
- /*
- * If the server is scheduled to shut down the next time all
- * users are logged out, now's the time to do it.
- */
+void check_sched_shutdown(void) {
if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
lprintf(3, "Scheduled shutdown initiating.\n");
master_cleanup();
- }
- end_critical_section(S_HOUSEKEEPING);
- lprintf(9, "--- end housekeeping ---\n");
}
+}
+
+
+
+/*
+ * This is the main loop for the housekeeping thread. It remains active
+ * during the entire run of the server.
+ */
+void housekeeping_loop(void) {
+ long flags;
+ struct timeval tv;
+ fd_set readfds;
+ int did_something;
+ char house_cmd[256]; /* Housekeep cmds are always 256 bytes long */
+
+ if (pipe(housepipe) != 0) {
+ lprintf(1, "FATAL ERROR: can't create housekeeping pipe: %s\n",
+ strerror(errno));
+ exit(0);
+ }
+
+ flags = (long) fcntl(housepipe[1], F_GETFL);
+ flags |= O_NONBLOCK;
+ fcntl(housepipe[1], F_SETFL, flags);
+ while(1) {
+ do {
+ did_something = 0;
+ tv.tv_sec = HOUSEKEEPING_WAKEUP;
+ tv.tv_usec = 0;
+ FD_ZERO(&readfds);
+ FD_SET(housepipe[0], &readfds);
+ select(housepipe[0] + 1, &readfds, 0L, 0L, &tv);
+ if (FD_ISSET(housepipe[0], &readfds)) {
+ did_something = 1;
+ }
+
+ if (did_something) {
+ read(housepipe[0], house_cmd, 256);
+ }
+ else {
+ memset(house_cmd, 0, 256);
+ strcpy(house_cmd, "MINUTE");
+ }
+
+
+ /* Do whatever this cmd requires */
+ if (!strcmp(house_cmd, "MINUTE")) {
+ terminate_idle_sessions();
+ }
+
+ else if (!strcmp(house_cmd, "SCHED_SHUTDOWN")) {
+ check_sched_shutdown();
+ }
+
+ else {
+ lprintf(7, "Unknown housekeeping command\n");
+ }
+
+ } while (did_something);
+ }
+}
+
+
+
+
+
+
+void enter_housekeeping_cmd(char *cmd) {
+ char cmdbuf[256];
+
+ safestrncpy(cmdbuf, cmd, 256);
+ begin_critical_section(S_HOUSEKEEPING);
+ write(housepipe[1], cmdbuf, 256);
+ end_critical_section(S_HOUSEKEEPING);
+}
+
/*
void terminate_idle_sessions (void);
void do_housekeeping (void);
void check_ref_counts (void);
+void housekeeping_loop (void);
#include "citadel.h"
#include "server.h"
#include <syslog.h>
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
#include "serv_chat.h"
#include "sysdep_decls.h"
#include "citserver.h"
#define DEFAULT_ENTRY 4
+
+/*
+ * HOUSEKEEPING_WAKEUP is the number of seconds which pass between each pass
+ * of the housekeeping thread. This normally happens once per minute and there
+ * isn't any good reason to change it.
+ */
+#define HOUSEKEEPING_WAKEUP 60
+
+
/*** STRUCTURE SIZE VARIABLES ***/
/* You may NOT change this value once you set up your system. */
struct CitContext *CreateNewContext(void) {
struct CitContext *me;
- lprintf(9, "CreateNewContext: calling malloc()\n");
me = (struct CitContext *) mallok(sizeof(struct CitContext));
if (me == NULL) {
lprintf(1, "citserver: can't allocate memory!!\n");
struct CitContext *ptr;
lprintf(7, "Starting RemoveContext()\n");
- lprintf(9, "Session count before RemoveContext is %d\n",
- session_count());
if (con==NULL) {
lprintf(7, "WARNING: RemoveContext() called with null!\n");
return;
lprintf(7, "Closing socket %d\n", con->client_socket);
close(con->client_socket);
- lprintf(9, "Dereferencing session context\n");
if (ContextList==con) {
ContextList = ContextList->next;
}
}
}
- lprintf(9, "Freeing session context...\n");
phree(con);
- lprintf(9, "...done.\n");
end_critical_section(S_SESSION_TABLE);
-
- lprintf(9, "Session count after RemoveContext is %d\n",
- session_count());
-
lprintf(7, "Done with RemoveContext\n");
}
struct CitContext *ptr;
int TheCount = 0;
- lprintf(9, "session_count() starting\n");
begin_critical_section(S_SESSION_TABLE);
for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
++TheCount;
- lprintf(9, "Counted session %3d (%d)\n", ptr->cs_pid, TheCount);
}
end_critical_section(S_SESSION_TABLE);
- lprintf(9, "session_count() finishing\n");
return(TheCount);
}
struct CitContext *ptr;
THREAD killme = 0;
- lprintf(9, "kill_session() scanning for thread to cancel...\n");
begin_critical_section(S_SESSION_TABLE);
for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
if (ptr->cs_pid == session_to_kill) {
}
}
end_critical_section(S_SESSION_TABLE);
- lprintf(9, "kill_session() finished scanning.\n");
if (killme != 0) {
#ifdef HAVE_PTHREAD_CANCEL
- lprintf(9, "calling pthread_cancel()\n");
pthread_cancel(killme);
#else
pthread_kill(killme, SIGUSR1);
int alen; /* Data for master socket */
int ssock; /* Descriptor for master socket */
THREAD SessThread; /* Thread descriptor */
+ THREAD HousekeepingThread; /* Thread descriptor */
pthread_attr_t attr; /* Thread attributes */
struct CitContext *con; /* Temporary context pointer */
char tracefile[128]; /* Name of file to log traces to */
DLoader_Init(moddir);
free(moddir);
}
- lprintf(9, "Modules done initializing.\n");
+
+ lprintf(7, "Starting housekeeper thread\n");
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ if (pthread_create(&HousekeepingThread, &attr,
+ (void* (*)(void*)) housekeeping_loop, NULL) != 0) {
+ lprintf(1, "Can't create housekeeping thead: %s\n",
+ strerror(errno));
+ }
/*
* Endless loop. Listen on the master socket. When a connection
}
else {
lprintf(7, "citserver: Client socket %d\n", ssock);
- lprintf(9, "creating context\n");
con = CreateNewContext();
con->client_socket = ssock;
/* Set the SO_REUSEADDR socket option */
- lprintf(9, "setting socket options\n");
i = 1;
setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
&i, sizeof(i));
/* set attributes for the new thread */
- lprintf(9, "setting thread attributes\n");
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_DETACHED);
/* now create the thread */
- lprintf(9, "creating thread\n");
if (pthread_create(&SessThread, &attr,
(void* (*)(void*)) sd_context_loop,
con)
strerror(errno));
}
- /* detach the thread
- * (defunct -- now done at thread creation time)
- * if (pthread_detach(&SessThread) != 0) {
- * lprintf(1,
- * "citserver: can't detach thread: %s\n",
- * strerror(errno));
- * }
- */
- lprintf(9, "done!\n");
}
}
master_cleanup();