]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
Patches from Matt with slight mod from me to do without strlen. Also modified lprintf...
[citadel.git] / citadel / sysdep.c
index 761b225c746ca6620b3a8f3c6ac9029af952c5f5..94029bc68fead4090dc82b1199eaea7990d52945 100644 (file)
@@ -61,8 +61,7 @@
 #include "database.h"
 #include "housekeeping.h"
 #include "tools.h"
-#include "serv_crypto.h"
-#include "serv_fulltext.h"
+#include "modules/crypto/serv_crypto.h"        /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
 
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
@@ -94,12 +93,9 @@ struct CitContext masterCC;
 time_t last_purge = 0;                         /* Last dead session purge */
 static int num_threads = 0;                    /* Current number of threads */
 int num_sessions = 0;                          /* Current number of sessions */
-pthread_t indexer_thread_tid;
-pthread_t checkpoint_thread_tid;
 
 int syslog_facility = LOG_DAEMON;
 int enable_syslog = 0;
-extern int running_as_daemon;
 
 void DestroyWorkerList(void);
 
@@ -107,6 +103,7 @@ void DestroyWorkerList(void);
  * lprintf()  ...   Write logging information
  */
 void lprintf(enum LogLevel loglevel, const char *format, ...) {   
+       char *buf;
        va_list arg_ptr;
 
        if (enable_syslog) {
@@ -116,7 +113,9 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
        }
 
        /* stderr output code */
-       if (enable_syslog || running_as_daemon) return;
+       if (enable_syslog || running_as_daemon) {
+               return;
+       }
 
        /* if we run in forground and syslog is disabled, log to terminal */
        if (loglevel <= verbosity) { 
@@ -128,24 +127,30 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
                /* Promote to time_t; types differ on some OSes (like darwin) */
                unixtime = tv.tv_sec;
                localtime_r(&unixtime, &tim);
+//     begin_critical_section(S_LOG);
+
+               buf = malloc(SIZ+strlen(format));
+
                if (CC->cs_pid != 0) {
-                       fprintf(stderr,
+                       sprintf(buf,
                                "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
                                tim.tm_year + 1900, tim.tm_mon + 1,
                                tim.tm_mday, tim.tm_hour, tim.tm_min,
                                tim.tm_sec, (long)tv.tv_usec,
                                CC->cs_pid);
                } else {
-                       fprintf(stderr,
+                       sprintf(buf,
                                "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
                                tim.tm_year + 1900, tim.tm_mon + 1,
                                tim.tm_mday, tim.tm_hour, tim.tm_min,
                                tim.tm_sec, (long)tv.tv_usec);
                }
-               va_start(arg_ptr, format);   
-                       vfprintf(stderr, format, arg_ptr);   
+               strcat(buf, format);
+               va_start(arg_ptr, buf);   
+               vfprintf(stderr, buf, arg_ptr);   
                va_end(arg_ptr);   
                fflush(stderr);
+//     end_critical_section(S_LOG);
        }
 }   
 
@@ -157,6 +162,8 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
 
 volatile int time_to_die = 0;
 volatile int shutdown_and_halt = 0;
+volatile int restart_server = 0;
+volatile int running_as_daemon = 0;
 
 static RETSIGTYPE signal_cleanup(int signum) {
        lprintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
@@ -164,11 +171,25 @@ static RETSIGTYPE signal_cleanup(int signum) {
        master_cleanup(signum);
 }
 
+
+
+
+void InitialiseSemaphores(void)
+{
+       int i;
+
+       /* Set up a bunch of semaphores to be used for critical sections */
+       for (i=0; i<MAX_SEMAPHORES; ++i) {
+               pthread_mutex_init(&Critters[i], NULL);
+       }
+}
+
+
+
 /*
  * Some initialization stuff...
  */
 void init_sysdep(void) {
-       int i;
        sigset_t set;
 
        /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
@@ -187,11 +208,6 @@ void init_sysdep(void) {
        init_ssl();
 #endif
 
-       /* Set up a bunch of semaphores to be used for critical sections */
-       for (i=0; i<MAX_SEMAPHORES; ++i) {
-               pthread_mutex_init(&Critters[i], NULL);
-       }
-
        /*
         * Set up a place to put thread-specific data.
         * We only need a single pointer per thread - it points to the
@@ -353,7 +369,7 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
        int s;
        int i;
        int actual_queue_len;
-
+       long ret, ret2;
        actual_queue_len = queue_len;
        if (actual_queue_len < 5) actual_queue_len = 5;
 
@@ -409,7 +425,7 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
                return(-1);
        }
 
-       chmod(sockpath, 0777);
+       chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
        return(s);
 }
 
@@ -508,13 +524,14 @@ void flush_output(void) {
        setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
        setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
 }
-#elif HAVE_DARWIN
+#else 
+#ifdef HAVE_DARWIN
 /* Stub functions for Darwin/OS X where TCP buffering isn't liked at all */
 void buffer_output(void) {
-CC->buffering = 0;
+       CC->buffering = 0;
 }
 void unbuffer_output(void) {
-CC->buffering = 0;
+       CC->buffering = 0;
 }
 void flush_output(void) {
 }
@@ -544,7 +561,8 @@ void unbuffer_output(void) {
                CC->output_buffer = NULL;
        }
 }
-#endif
+#endif /* HAVE_DARWIN */
+#endif /* HAVE_TCP_BUFFERING */
 
 
 
@@ -599,6 +617,7 @@ void client_write(char *buf, int nbytes)
                                "client_write(%d bytes) failed: %s (%d)\n",
                                nbytes - bytes_written,
                                strerror(errno), errno);
+                       cit_backtrace();
                        CC->kill_me = 1;
                        return;
                }
@@ -706,7 +725,7 @@ int client_getln(char *buf, int bufsize)
        /* Strip the trailing LF, and the trailing CR if present.
         */
        buf[i] = 0;
-       while ( (strlen(buf) > 0) && ((buf[strlen(buf)-1]==10) || (buf[strlen(buf)-1] == 13)) ) {
+       while ( (!IsEmptyStr(buf)) && ((buf[strlen(buf)-1]==10) || (buf[strlen(buf)-1] == 13)) ) {
                buf[strlen(buf)-1] = 0;
        }
        if (retval < 0) safestrncpy(buf, "000", bufsize);
@@ -979,8 +998,7 @@ void DestroyWorkerList(void)
 }
 
 /*
- * Create the indexer thread and begin its operation.
- * Then create the checkpoint thread and begin its operation.
+ * Create the maintenance threads and begin their operation.
  */
 void create_maintenance_threads(void) {
        int ret;
@@ -1003,17 +1021,23 @@ void create_maintenance_threads(void) {
                pthread_attr_destroy(&attr);
                return;
        }
+       
+       struct MaintenanceThreadHook *fcn;
 
-       if ((ret = pthread_create(&indexer_thread_tid, &attr, indexer_thread, NULL) != 0)) {
-               lprintf(CTDL_ALERT, "Can't create thread: %s\n", strerror(ret));
-       }
+       lprintf(CTDL_DEBUG, "Performing startup of maintenance thread hooks\n");
 
-       if ((ret = pthread_create(&checkpoint_thread_tid, &attr, checkpoint_thread, NULL) != 0)) {
-               lprintf(CTDL_ALERT, "Can't create thread: %s\n", strerror(ret));
+       for (fcn = MaintenanceThreadHookTable; fcn != NULL; fcn = fcn->next) {
+               if ((ret = pthread_create(&(fcn->MaintenanceThread_tid), &attr, fcn->fcn_ptr, NULL) != 0)) {
+                       lprintf(CTDL_ALERT, "Can't create thread: %s\n", strerror(ret));
+               }
+               else
+               {
+                       lprintf(CTDL_NOTICE, "Spawned a new maintenance thread \"%s\" (%ld). \n", fcn->name,
+                               fcn->MaintenanceThread_tid);
+               }
        }
 
-       lprintf(CTDL_NOTICE, "Spawned indexer (%ld) and checkpoint (%ld) thread. \n", 
-               indexer_thread_tid, checkpoint_thread_tid);
+
        pthread_attr_destroy(&attr);
 }