From 4d9a00d2846d49086faf8dfefb832925be9e19b3 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Thu, 28 Oct 1999 05:08:50 +0000 Subject: [PATCH] * Removed all of the thread cancellation cruft that is no longer necessary * Moved the now non-system-dependent RemoveContext() out of sysdep.c (now it's part of cleanup() in citserver.c) * Removed all references to pthread_* from all modules except sysdep.c --- citadel/ChangeLog | 7 ++++ citadel/citserver.c | 80 +++++++++++++++++++++++++++---------- citadel/citserver.h | 2 +- citadel/control.c | 3 -- citadel/database.c | 3 -- citadel/dynloader.c | 3 -- citadel/file_ops.c | 3 -- citadel/housekeeping.c | 3 -- citadel/html.c | 3 -- citadel/locate_host.c | 3 -- citadel/logging.c | 3 -- citadel/mime_parser.c | 3 -- citadel/msgbase.c | 3 -- citadel/policy.c | 3 -- citadel/room_ops.c | 3 -- citadel/serv_chat.c | 3 -- citadel/serv_expire.c | 3 -- citadel/serv_icq.c | 3 -- citadel/serv_test.c | 4 -- citadel/serv_upgrade.c | 4 -- citadel/serv_vcard.c | 4 -- citadel/server.h | 1 - citadel/support.c | 3 -- citadel/sysconfig.h | 7 +++- citadel/sysdep.c | 91 +++++++++--------------------------------- citadel/sysdep_decls.h | 1 - citadel/user_ops.c | 3 -- 27 files changed, 91 insertions(+), 161 deletions(-) diff --git a/citadel/ChangeLog b/citadel/ChangeLog index 03fd32230..7069b2c48 100644 --- a/citadel/ChangeLog +++ b/citadel/ChangeLog @@ -1,4 +1,10 @@ $Log$ +Revision 1.400 1999/10/28 05:08:49 ajc +* Removed all of the thread cancellation cruft that is no longer necessary +* Moved the now non-system-dependent RemoveContext() out of sysdep.c (now + it's part of cleanup() in citserver.c) +* Removed all references to pthread_* from all modules except sysdep.c + Revision 1.399 1999/10/28 03:20:17 ajc * Fixed the problem of worker threads waking up prematurely. * 'QUIT'-terminated sessions now exit properly. Still need to fix code for @@ -1374,3 +1380,4 @@ Sat Jul 11 00:20:48 EDT 1998 Nathan Bryant Fri Jul 10 1998 Art Cancro * Initial CVS import + diff --git a/citadel/citserver.c b/citadel/citserver.c index 0e5c9e908..c9e9c1af3 100644 --- a/citadel/citserver.c +++ b/citadel/citserver.c @@ -10,9 +10,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include #include @@ -116,34 +113,72 @@ void deallocate_user_data(struct CitContext *con) /* - * Gracefully terminate the session and thread. - * (This is called as a cleanup handler by the thread library.) - * - * All NON-system-dependent stuff is done in this function. - * System-dependent session/thread cleanup is in cleanup() in sysdep.c + * Gracefully terminate a session which is marked as CON_DYING. */ -void cleanup(int exit_code) +void cleanup(struct CitContext *con) { - lprintf(9, "cleanup(%d) called\n", exit_code); + struct CitContext *ptr = NULL; + struct CitContext *ToFree = NULL; + + lprintf(9, "cleanup() called\n"); + if (con==NULL) { + lprintf(5, "WARNING: cleanup() called with NULL!\n"); + return; + } - lprintf(7, "Calling logout(%d)\n", CC->cs_pid); - logout(CC); + lprintf(7, "Calling logout(%d)\n", con->cs_pid); + logout(con); - rec_log(CL_TERMINATE,CC->curr_user); - unlink(CC->temp); - lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid); + rec_log(CL_TERMINATE, con->curr_user); + unlink(con->temp); + lprintf(3, "citserver[%3d]: ended.\n", con->cs_pid); /* Run any cleanup routines registered by loadable modules */ PerformSessionHooks(EVT_STOP); - syslog(LOG_NOTICE,"session %d ended", CC->cs_pid); + syslog(LOG_NOTICE,"session %d ended", con->cs_pid); /* Deallocate any user-data attached to this session */ - deallocate_user_data(CC); + deallocate_user_data(con); + + /* And flag the context as in need of being killed. + * (Probably done already, but just in case) + */ + con->state = CON_DYING; + + /* delete context */ + + lprintf(7, "Removing context\n"); + + begin_critical_section(S_SESSION_TABLE); + if (ContextList == con) { + ToFree = ContextList; + ContextList = ContextList->next; + } + else { + for (ptr = ContextList; ptr != NULL; ptr = ptr->next) { + if (ptr->next == con) { + ToFree = ptr->next; + ptr->next = ptr->next->next; + } + } + } + end_critical_section(S_SESSION_TABLE); + + lprintf(7, "Closing socket %d\n", ToFree->client_socket); + close(ToFree->client_socket); + + /* Tell the housekeeping thread to check to see if this is the time + * to initiate a scheduled shutdown event. + */ + enter_housekeeping_cmd("SCHED_SHUTDOWN"); + + /* Free up the memory used by this context */ + phree(ToFree); + + lprintf(7, "Done with cleanup()\n"); +} - /* And flag the context as in need of being killed */ - CC->state = CON_DYING; - } /* @@ -866,7 +901,8 @@ void do_command_loop(void) { memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ if (client_gets(cmdbuf) < 1) { lprintf(3, "Client socket is broken. Ending session.\n"); - cleanup(EXIT_NULL); + CC->state = CON_DYING; + return; } lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf); @@ -896,7 +932,7 @@ void do_command_loop(void) { else if (!strncasecmp(cmdbuf,"QUIT",4)) { cprintf("%d Goodbye.\n",OK); - cleanup(0); + CC->state = CON_DYING; } else if (!strncasecmp(cmdbuf,"LOUT",4)) { diff --git a/citadel/citserver.h b/citadel/citserver.h index 45ac8d519..c206bd72a 100644 --- a/citadel/citserver.h +++ b/citadel/citserver.h @@ -1,7 +1,7 @@ /* $Id$ */ void master_startup (void); void master_cleanup (void); -void cleanup (int); +void cleanup (struct CitContext *); void set_wtmpsupp (char *newtext); void set_wtmpsupp_to_current_room(void); void cmd_info (void); diff --git a/citadel/control.c b/citadel/control.c index 1455c72c7..e71c5bbe6 100644 --- a/citadel/control.c +++ b/citadel/control.c @@ -18,9 +18,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include "citadel.h" #include "server.h" diff --git a/citadel/database.c b/citadel/database.c index 674ef3f0b..4267b4f83 100644 --- a/citadel/database.c +++ b/citadel/database.c @@ -23,9 +23,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #ifdef HAVE_GDBM_H #include #endif diff --git a/citadel/dynloader.c b/citadel/dynloader.c index d11ca6790..259c6d3dd 100644 --- a/citadel/dynloader.c +++ b/citadel/dynloader.c @@ -17,9 +17,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include #include "citadel.h" diff --git a/citadel/file_ops.c b/citadel/file_ops.c index 17bc5dcda..dac1dd466 100644 --- a/citadel/file_ops.c +++ b/citadel/file_ops.c @@ -10,9 +10,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include "config.h" diff --git a/citadel/housekeeping.c b/citadel/housekeeping.c index 726c92815..72c1f4aa7 100644 --- a/citadel/housekeeping.c +++ b/citadel/housekeeping.c @@ -20,9 +20,6 @@ #ifdef HAVE_SYS_SELECT_H #include #endif -#ifdef HAVE_PTHREAD_H -#include -#endif #include "tools.h" #include "citadel.h" #include "server.h" diff --git a/citadel/html.c b/citadel/html.c index 32065bd49..a4d679295 100644 --- a/citadel/html.c +++ b/citadel/html.c @@ -14,9 +14,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include "citadel.h" #include "server.h" diff --git a/citadel/locate_host.c b/citadel/locate_host.c index bb1162bb3..b4f5cdbad 100644 --- a/citadel/locate_host.c +++ b/citadel/locate_host.c @@ -14,9 +14,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include "locate_host.h" diff --git a/citadel/logging.c b/citadel/logging.c index 2b29a6bdf..0f5eef211 100644 --- a/citadel/logging.c +++ b/citadel/logging.c @@ -13,9 +13,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include "citadel.h" #include "server.h" diff --git a/citadel/mime_parser.c b/citadel/mime_parser.c index 55f1f4d7f..ecd54358e 100644 --- a/citadel/mime_parser.c +++ b/citadel/mime_parser.c @@ -21,9 +21,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "mime_parser.h" #include "sysdep_decls.h" diff --git a/citadel/msgbase.c b/citadel/msgbase.c index 5296502a5..c959b691b 100644 --- a/citadel/msgbase.c +++ b/citadel/msgbase.c @@ -8,9 +8,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include "citadel.h" #include "server.h" diff --git a/citadel/policy.c b/citadel/policy.c index fa6ec318c..f3df450b5 100644 --- a/citadel/policy.c +++ b/citadel/policy.c @@ -5,9 +5,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include #include "citadel.h" diff --git a/citadel/room_ops.c b/citadel/room_ops.c index 9dd7ed0ed..b1823ad09 100644 --- a/citadel/room_ops.c +++ b/citadel/room_ops.c @@ -5,9 +5,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include #include #include diff --git a/citadel/serv_chat.c b/citadel/serv_chat.c index 5b23aedd3..88d343e0b 100644 --- a/citadel/serv_chat.c +++ b/citadel/serv_chat.c @@ -19,9 +19,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include diff --git a/citadel/serv_expire.c b/citadel/serv_expire.c index c566bba04..6ba24723e 100644 --- a/citadel/serv_expire.c +++ b/citadel/serv_expire.c @@ -36,9 +36,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include diff --git a/citadel/serv_icq.c b/citadel/serv_icq.c index ae2ecb8c4..30f3dff55 100644 --- a/citadel/serv_icq.c +++ b/citadel/serv_icq.c @@ -33,9 +33,6 @@ #include #include #include "sysdep.h" -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include "dynloader.h" diff --git a/citadel/serv_test.c b/citadel/serv_test.c index 7601cf215..0d3ce30bc 100644 --- a/citadel/serv_test.c +++ b/citadel/serv_test.c @@ -12,12 +12,8 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" -#include #include #include "sysdep_decls.h" #include "citserver.h" diff --git a/citadel/serv_upgrade.c b/citadel/serv_upgrade.c index eeb997b80..1f31a3b59 100644 --- a/citadel/serv_upgrade.c +++ b/citadel/serv_upgrade.c @@ -12,12 +12,8 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" -#include #include #include "sysdep_decls.h" #include "citserver.h" diff --git a/citadel/serv_vcard.c b/citadel/serv_vcard.c index 6c3d523ec..40e7727f7 100644 --- a/citadel/serv_vcard.c +++ b/citadel/serv_vcard.c @@ -23,12 +23,8 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" -#include #include #include "sysdep_decls.h" #include "citserver.h" diff --git a/citadel/server.h b/citadel/server.h index b693c275d..72dc9f4d6 100644 --- a/citadel/server.h +++ b/citadel/server.h @@ -1,5 +1,4 @@ /* $Id$ */ -typedef pthread_t THREAD; /* Uncomment this if you want to track memory leaks. * This incurs some overhead, so don't use it unless you're debugging the code! diff --git a/citadel/support.c b/citadel/support.c index e9d04a078..cc4ac677b 100644 --- a/citadel/support.c +++ b/citadel/support.c @@ -5,9 +5,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #include "citadel.h" #include "server.h" #include "support.h" diff --git a/citadel/sysconfig.h b/citadel/sysconfig.h index bda55ed28..ff875ee39 100644 --- a/citadel/sysconfig.h +++ b/citadel/sysconfig.h @@ -51,9 +51,14 @@ * editor installed, and you want to make it the default, set this to 46 * to make it use your editor by default. */ -#define DEFAULT_ENTRY 4 +#define DEFAULT_ENTRY 4 +/* + * Logging level to use if none is specified on the command line. + */ +#define DEFAULT_VERBOSITY 9 + /* * HOUSEKEEPING_WAKEUP is the number of seconds which pass between each pass diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 367cc7922..7eea262a3 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -67,7 +67,7 @@ pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */ pthread_key_t MyConKey; /* TSD key for MyContext() */ int msock; /* master listening socket */ -int verbosity = 9; /* Logging level */ +int verbosity = DEFAULT_VERBOSITY; /* Logging level */ struct CitContext masterCC; int rescan[2]; /* The Rescan Pipe */ @@ -175,17 +175,6 @@ void dump_tracked() { } #endif -static pthread_t main_thread_id; - -#ifndef HAVE_PTHREAD_CANCEL -/* - * signal handler to fake thread cancellation; only required on BSDI as far - * as I know. - */ -static RETSIGTYPE cancel_thread(int signum) { - pthread_exit(NULL); - } -#endif /* * we used to use master_cleanup() as a signal handler to shut down the server. @@ -231,11 +220,13 @@ void init_sysdep(void) { signal(SIGQUIT, signal_cleanup); signal(SIGHUP, signal_cleanup); signal(SIGTERM, signal_cleanup); + + /* + * Do not shut down the server on broken pipe signals, otherwise the + * whole Citadel service would come down whenever a single client + * socket breaks. + */ signal(SIGPIPE, SIG_IGN); - main_thread_id = pthread_self(); -#ifndef HAVE_PTHREAD_CANCEL /* fake it - only BSDI afaik */ - signal(SIGUSR1, cancel_thread); -#endif } @@ -305,7 +296,9 @@ int ig_tcp_server(int port_number, int queue_len) /* - * Return a pointer to a thread's own CitContext structure (new) + * Return a pointer to the CitContext structure bound to the thread which + * called this function. If there's no such binding (for example, if it's + * called by the housekeeper thread) then a generic 'master' CC is returned. */ struct CitContext *MyContext(void) { struct CitContext *retCC; @@ -316,7 +309,7 @@ struct CitContext *MyContext(void) { /* - * Wedge our way into the context list. + * Initialize a new context and place it in the list. */ struct CitContext *CreateNewContext(void) { struct CitContext *me; @@ -341,55 +334,6 @@ struct CitContext *CreateNewContext(void) { return(me); } -/* - * Remove a context from the context list. - */ -void RemoveContext(struct CitContext *con) -{ - struct CitContext *ptr = NULL; - struct CitContext *ToFree = NULL; - - lprintf(7, "Starting RemoveContext()\n"); - if (con==NULL) { - lprintf(5, "WARNING: RemoveContext() called with NULL!\n"); - return; - } - - /* - * session_count() starts its own S_SESSION_TABLE critical section; - * so do not call it from within this loop. - */ - begin_critical_section(S_SESSION_TABLE); - - if (ContextList == con) { - ToFree = ContextList; - ContextList = ContextList->next; - } - else { - for (ptr = ContextList; ptr != NULL; ptr = ptr->next) { - if (ptr->next == con) { - ToFree = ptr->next; - ptr->next = ptr->next->next; - } - } - } - - - end_critical_section(S_SESSION_TABLE); - - lprintf(7, "Closing socket %d\n", ToFree->client_socket); - close(ToFree->client_socket); - - /* Tell the housekeeping thread to check to see if this is the time - * to initiate a scheduled shutdown event. - */ - enter_housekeeping_cmd("SCHED_SHUTDOWN"); - - /* Free up the memory used by this context */ - phree(ToFree); - - lprintf(7, "Done with RemoveContext\n"); - } /* @@ -423,7 +367,7 @@ void client_write(char *buf, int nbytes) if (retval < 1) { lprintf(2, "client_write() failed: %s\n", strerror(errno)); - cleanup(errno); + CC->state = CON_DYING; } bytes_written = bytes_written + retval; } @@ -479,7 +423,7 @@ int client_read_to(char *buf, int bytes, int timeout) if (rlen<1) { lprintf(2, "client_read() failed: %s\n", strerror(errno)); - cleanup(errno); + CC->state = CON_DYING; } len = len + rlen; } @@ -541,6 +485,8 @@ void sysdep_master_cleanup(void) { /* * Terminate another session. + * FIX ... now we need some way to wake that session up so it knows it + * needs to terminate. */ void kill_session(int session_to_kill) { struct CitContext *ptr; @@ -672,7 +618,7 @@ int convert_login(char NameToConvert[]) { */ int main(int argc, char **argv) { - THREAD HousekeepingThread; /* Thread descriptor */ + pthread_t HousekeepingThread; /* Thread descriptor */ pthread_attr_t attr; /* Thread attributes */ char tracefile[128]; /* Name of file to log traces to */ int a, i; /* General-purpose variables */ @@ -743,6 +689,7 @@ int main(int argc, char **argv) /* Initialize... */ init_sysdep(); openlog("citserver",LOG_PID,LOG_USER); + /* Load site-specific parameters */ lprintf(7, "Loading citadel.config\n"); get_config(); @@ -853,7 +800,7 @@ void worker_thread(void) { struct CitContext *con= NULL; /* Temporary context pointer */ struct sockaddr_in fsin; /* Data for master socket */ int alen; /* Data for master socket */ - int ssock; /* Descriptor for master socket */ + int ssock; /* Descriptor for client socket */ while (!time_to_die) { @@ -972,7 +919,7 @@ SETUP_FD: FD_ZERO(&readfds); do_command_loop(); pthread_setspecific(MyConKey, (void *)NULL); if (bind_me->state == CON_DYING) { - RemoveContext(bind_me); + cleanup(bind_me); } else { bind_me->state = CON_IDLE; diff --git a/citadel/sysdep_decls.h b/citadel/sysdep_decls.h index ef5b9e700..e748522f4 100644 --- a/citadel/sysdep_decls.h +++ b/citadel/sysdep_decls.h @@ -7,7 +7,6 @@ int ig_tcp_server (int port_number, int queue_len); struct CitContext *MyContext (void); struct CitContext *CreateNewContext (void); void InitMyContext (struct CitContext *con); -void RemoveContext (struct CitContext *con); int session_count (void); void client_write (char *buf, int nbytes); void cprintf (const char *format, ...); diff --git a/citadel/user_ops.c b/citadel/user_ops.c index 608d4a584..5cf386354 100644 --- a/citadel/user_ops.c +++ b/citadel/user_ops.c @@ -14,9 +14,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_H -#include -#endif #ifndef ENABLE_CHKPWD #include "auth.h" #endif -- 2.30.2