]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
The pid file now contains a valid pid (it used to be 0).
[citadel.git] / citadel / sysdep.c
index ae90340fc7ac21f3ab6a3f456063a93ca3d4cebf..55643e8c5eb0ab6b9b6495b5734e7209df0f2ce9 100644 (file)
@@ -54,7 +54,6 @@
 #endif
 #include "citadel.h"
 #include "server.h"
-#include "serv_extensions.h"
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
@@ -62,8 +61,8 @@
 #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 */
+#include "ecrash.h"
 
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
@@ -95,12 +94,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);
 
@@ -109,6 +105,7 @@ void DestroyWorkerList(void);
  */
 void lprintf(enum LogLevel loglevel, const char *format, ...) {   
        va_list arg_ptr;
+       char buf[SIZ], buf2[SIZ];
 
        if (enable_syslog) {
                va_start(arg_ptr, format);
@@ -130,22 +127,24 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
                unixtime = tv.tv_sec;
                localtime_r(&unixtime, &tim);
                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);   
+                       vsprintf(buf2, format, arg_ptr);   
                va_end(arg_ptr);   
+
+               fprintf(stderr, "%s%s", buf, buf2);
                fflush(stderr);
        }
 }   
@@ -158,6 +157,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);
@@ -165,11 +166,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. */
@@ -188,11 +203,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
@@ -410,7 +420,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);
 }
 
@@ -509,13 +519,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) {
 }
@@ -545,7 +556,8 @@ void unbuffer_output(void) {
                CC->output_buffer = NULL;
        }
 }
-#endif
+#endif /* HAVE_DARWIN */
+#endif /* HAVE_TCP_BUFFERING */
 
 
 
@@ -600,6 +612,8 @@ void client_write(char *buf, int nbytes)
                                "client_write(%d bytes) failed: %s (%d)\n",
                                nbytes - bytes_written,
                                strerror(errno), errno);
+                       cit_backtrace();
+                       lprintf(CTDL_DEBUG, "Tried to send: %s",  &buf[bytes_written]);
                        CC->kill_me = 1;
                        return;
                }
@@ -707,7 +721,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);
@@ -758,6 +772,9 @@ void sysdep_master_cleanup(void) {
        CtdlDestroyFixedOutputHooks();  
        CtdlDestroySessionHooks();
        CtdlDestroyServiceHook();
+       #ifdef HAVE_BACKTRACE
+       eCrash_Uninit();
+       #endif
 }
 
 
@@ -837,7 +854,11 @@ void start_daemon(int unused) {
                else {
                        fp = fopen(file_pid_file, "w");
                        if (fp != NULL) {
-                               fprintf(fp, ""F_PID_T"\n", child);
+               /*
+                * NB.. The pid file contains the pid of the actual server.
+                * This is not the pid of the watcher process
+                */
+                               fprintf(fp, ""F_PID_T"\n", current_child);
                                fclose(fp);
                        }
                        waitpid(current_child, &status, 0);
@@ -980,8 +1001,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;
@@ -1004,17 +1024,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);
 }
 
@@ -1137,6 +1163,10 @@ void *worker_thread(void *arg) {
 
        cdb_allocate_tsd();
 
+       // Register for tracing
+       #ifdef HAVE_BACKTRACE
+       eCrash_RegisterThread("WorkerThread", 0);
+       #endif
        while (!time_to_die) {
 
                /* make doubly sure we're not holding any stale db handles
@@ -1241,7 +1271,9 @@ do_select:        force_purge = 0;
                                                serviceptr->h_command_function;
                                        con->h_async_function =
                                                serviceptr->h_async_function;
-
+                                       con->ServiceName =
+                                               serviceptr->ServiceName;
+                                       
                                        /* Determine whether it's a local socket */
                                        if (serviceptr->sockpath != NULL)
                                                con->is_local_socket = 1;
@@ -1314,6 +1346,9 @@ SKIP_SELECT:
        }
        if (con != NULL) free (con);//// TODO: could this harm other threads? 
        /* If control reaches this point, the server is shutting down */        
+       #ifdef HAVE_BACKTRACE
+       eCrash_UnregisterThread();
+       #endif
        return(NULL);
 }