*/
+#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
+#ifdef HAVE_PTHREAD_H
#include <pthread.h>
+#endif
#include "citadel.h"
#include "server.h"
#include "sysdep_decls.h"
#include "snprintf.h"
#endif
+#ifdef DEBUG_MEMORY_LEAKS
+struct TheHeap *heap = NULL;
+#endif
+
pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
pthread_key_t MyConKey; /* TSD key for MyContext() */
*/
void lprintf(int loglevel, const char *format, ...) {
va_list arg_ptr;
- char buf[256];
+ char buf[512];
va_start(arg_ptr, format);
vsprintf(buf, format, arg_ptr);
}
+
+#ifdef DEBUG_MEMORY_LEAKS
+void *tracked_malloc(size_t tsize, char *tfile, int tline) {
+ void *ptr;
+ struct TheHeap *hptr;
+
+ ptr = malloc(tsize);
+ if (ptr == NULL) return(NULL);
+
+ hptr = (struct TheHeap *) malloc(sizeof(struct TheHeap));
+ strcpy(hptr->h_file, tfile);
+ hptr->h_line = tline;
+ hptr->next = heap;
+ hptr->h_ptr = ptr;
+ heap = hptr;
+ return ptr;
+ }
+
+
+void tracked_free(void *ptr) {
+ struct TheHeap *hptr, *freeme;
+
+ if (heap->h_ptr == ptr) {
+ hptr = heap->next;
+ free(heap);
+ heap = hptr;
+ }
+ else {
+ for (hptr=heap; hptr->next!=NULL; hptr=hptr->next) {
+ if (hptr->next->h_ptr == ptr) {
+ freeme = hptr->next;
+ hptr->next = hptr->next->next;
+ free(freeme);
+ }
+ }
+ }
+
+ free(ptr);
+ }
+
+void *tracked_realloc(void *ptr, size_t size) {
+ void *newptr;
+ struct TheHeap *hptr;
+
+ newptr = realloc(ptr, size);
+
+ for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
+ if (hptr->h_ptr == ptr) hptr->h_ptr = newptr;
+ }
+
+ return newptr;
+ }
+
+
+void dump_tracked() {
+ struct TheHeap *hptr;
+
+ cprintf("%d Here's what's allocated...\n", LISTING_FOLLOWS);
+ for (hptr=heap; hptr!=NULL; hptr=hptr->next) {
+ cprintf("%20s %5d\n",
+ hptr->h_file, hptr->h_line);
+ }
+ cprintf("000\n");
+ }
+#endif
+
+#ifndef HAVE_PTHREAD_CANCEL
+/*
+ * signal handler to fake thread cancellation; only required on BSDI as far
+ * as I know.
+ */
+
+static pthread_t main_thread_id;
+
+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.
+ * however, master_cleanup() and the functions it calls do some things that
+ * aren't such a good idea to do from a signal handler: acquiring mutexes,
+ * playing with signal masks on BSDI systems, etc. so instead we install the
+ * following signal handler to set a global variable to inform the main loop
+ * that it's time to call master_cleanup() and exit.
+ */
+
+static volatile int time_to_die = 0;
+
+static RETSIGTYPE signal_cleanup(int signum) {
+ time_to_die = 1;
+ }
+
+
/*
* Some initialization stuff...
*/
/*
* The action for unexpected signals and exceptions should be to
- * call master_cleanup() to gracefully shut down the server.
+ * call signal_cleanup() to gracefully shut down the server.
*/
- signal(SIGINT, (void(*)(int))master_cleanup);
- signal(SIGQUIT, (void(*)(int))master_cleanup);
- signal(SIGHUP, (void(*)(int))master_cleanup);
- signal(SIGTERM, (void(*)(int))master_cleanup);
+ signal(SIGINT, signal_cleanup);
+ signal(SIGQUIT, signal_cleanup);
+ signal(SIGHUP, signal_cleanup);
+ signal(SIGTERM, signal_cleanup);
+ signal(SIGPIPE, SIG_IGN);
+#ifndef HAVE_PTHREAD_CANCEL /* fake it - only BSDI afaik */
+ main_thread_id = pthread_self();
+ signal(SIGUSR1, cancel_thread);
+#endif
}
*/
void begin_critical_section(int which_one)
{
+#ifdef HAVE_PTHREAD_CANCEL
int oldval;
+#else
+ sigset_t set;
+#endif
/* lprintf(8, "begin_critical_section(%d)\n", which_one); */
+#ifdef HAVE_PTHREAD_CANCEL
/* Don't get interrupted during the critical section */
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldval);
+#else
+ /* We're faking cancellation with signals. Block SIGUSR1 while we're in
+ * the critical section. */
+ if (!pthread_equal(pthread_self(), main_thread_id)) {
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ pthread_sigmask(SIG_BLOCK, &set, NULL);
+ }
+#endif
/* Obtain a semaphore */
pthread_mutex_lock(&Critters[which_one]);
*/
void end_critical_section(int which_one)
{
+#ifdef HAVE_PTHREAD_CANCEL
int oldval;
+#else
+ sigset_t set;
+#endif
/* lprintf(8, " end_critical_section(%d)\n", which_one); */
/* Let go of the semaphore */
pthread_mutex_unlock(&Critters[which_one]);
+#ifdef HAVE_PTHREAD_CANCEL
/* If a cancel was sent during the critical section, do it now.
* Then re-enable thread cancellation.
*/
pthread_testcancel();
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldval);
pthread_testcancel();
+#else
+ /* We're faking it. Unblock SIGUSR1; signals sent during the critical
+ * section should now be able to kill us. */
+ if (!pthread_equal(pthread_self(), main_thread_id)) {
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ pthread_sigmask(SIG_UNBLOCK, &set, NULL);
+ }
+#endif
}
struct CitContext *me;
lprintf(9, "CreateNewContext: calling malloc()\n");
- me = (struct CitContext *) malloc(sizeof(struct CitContext));
+ me = (struct CitContext *) mallok(sizeof(struct CitContext));
if (me == NULL) {
lprintf(1, "citserver: can't allocate memory!!\n");
pthread_exit(NULL);
*/
void InitMyContext(struct CitContext *con)
{
+#ifdef HAVE_PTHREAD_CANCEL
int oldval;
+#endif
con->mythread = pthread_self();
+#ifdef HAVE_PTHREAD_CANCEL
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldval);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldval);
+#endif
if (pthread_setspecific(MyConKey, (void *)con) != 0) {
lprintf(1, "ERROR! pthread_setspecific() failed: %s\n",
strerror(errno));
}
lprintf(9, "Freeing session context...\n");
- free(con);
+ phree(con);
lprintf(9, "...done.\n");
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);
+#endif
}
}
char tracefile[128]; /* Name of file to log traces to */
int a, i; /* General-purpose variables */
char convbuf[128];
- char modpath[128];
+ fd_set readfds;
+ struct timeval tv;
/* specify default port name and trace file */
strcpy(tracefile, "");
get_config();
lprintf(7, "Initializing loadable modules\n");
- snprintf(modpath, 128, "%s/modules", BBSDIR);
- DLoader_Init(modpath);
+ DLoader_Init("./modules");
lprintf(9, "Modules done initializing.\n");
/* Do non system dependent startup functions */
* Endless loop. Listen on the master socket. When a connection
* comes in, create a socket, a context, and a thread.
*/
- while (1) {
+ while (!time_to_die) {
+ /* we need to check if a signal has been delivered. because
+ * syscalls may be restartable across signals, we call
+ * select with a timeout of 1 second and repeatedly check for
+ * time_to_die... */
+ FD_ZERO(&readfds);
+ FD_SET(msock, &readfds);
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ if (select(msock + 1, &readfds, NULL, NULL, &tv) <= 0)
+ continue;
+ alen = sizeof fsin;
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock < 0) {
lprintf(2, "citserver: accept() failed: %s\n",
lprintf(9, "done!\n");
}
}
+ master_cleanup();
+ return 0;
}