From 27014176ee36ef29b80da016f3fd5772189f8377 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 13 Aug 2019 14:37:14 -0400 Subject: [PATCH] Initialize TSD key earlier in the startup process. Newer Linux/Linux distributions were beginning to crash on an uninitialized key. --- citadel/citadel.h | 2 +- citadel/database.c | 22 ++++++++++------------ citadel/debian/changelog | 6 ++++++ citadel/server_main.c | 30 ++++++------------------------ citadel/sysdep.c | 14 +++++++------- citadel/threads.c | 30 ++++++++++-------------------- citadel/threads.h | 1 + citadel/user_ops.c | 7 +++---- 8 files changed, 44 insertions(+), 68 deletions(-) diff --git a/citadel/citadel.h b/citadel/citadel.h index 8f3390089..8f1576218 100644 --- a/citadel/citadel.h +++ b/citadel/citadel.h @@ -35,7 +35,7 @@ extern "C" { */ #define CITADEL PACKAGE_STRING -#define REV_LEVEL 925 // This version +#define REV_LEVEL 927 // This version #define REV_MIN 591 // Oldest compatible database #define EXPORT_REV_MIN 760 // Oldest compatible export files #define LIBCITADEL_MIN 922 // Minimum required version of libcitadel diff --git a/citadel/database.c b/citadel/database.c index 8b5820edb..94d283714 100644 --- a/citadel/database.c +++ b/citadel/database.c @@ -76,7 +76,7 @@ void cdb_verbose_err(const DB_ENV * dbenv, const char *errpfx, const char *msg) /* wrapper for txn_abort() that logs/aborts on error */ -static void txabort(DB_TXN * tid) +static void txabort(DB_TXN *tid) { int ret; @@ -90,7 +90,7 @@ static void txabort(DB_TXN * tid) /* wrapper for txn_commit() that logs/aborts on error */ -static void txcommit(DB_TXN * tid) +static void txcommit(DB_TXN *tid) { int ret; @@ -104,7 +104,7 @@ static void txcommit(DB_TXN * tid) /* wrapper for txn_begin() that logs/aborts on error */ -static void txbegin(DB_TXN ** tid) +static void txbegin(DB_TXN **tid) { int ret; @@ -416,9 +416,8 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) { DBT dkey, ddata; - DB_TXN *tid; + DB_TXN *tid = NULL; int ret = 0; - struct CtdlCompressHeader zheader; char *compressed_data = NULL; int compressing = 0; @@ -472,11 +471,11 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) retry: txbegin(&tid); - if ((ret = dbp[cdb]->put(dbp[cdb], /* db */ - tid, /* transaction ID */ - &dkey, /* key */ - &ddata, /* data */ - 0))) { /* flags */ + if ((ret = dbp[cdb]->put(dbp[cdb], // db + tid, // transaction ID + &dkey, // key + &ddata, // data + 0))) { // flags if (ret == DB_LOCK_DEADLOCK) { txabort(tid); goto retry; @@ -772,8 +771,7 @@ void cdb_trunc(int cdb) } else { syslog(LOG_EMERG, "db: cdb_truncate(%d): %s", cdb, db_strerror(ret)); if (ret == ENOMEM) { - syslog(LOG_EMERG, - "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information."); + syslog(LOG_EMERG, "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information."); } exit(CTDLEXIT_DB); } diff --git a/citadel/debian/changelog b/citadel/debian/changelog index 94917604f..760d93d20 100644 --- a/citadel/debian/changelog +++ b/citadel/debian/changelog @@ -1,3 +1,9 @@ +citadel (927-1) stable; urgency=low + + * new release + + -- Wilfried Goesgens Wed, 19 Dec 2018 16:57:49 -0500 + citadel (925-1) stable; urgency=low * new release diff --git a/citadel/server_main.c b/citadel/server_main.c index 10e366051..8c4f63741 100644 --- a/citadel/server_main.c +++ b/citadel/server_main.c @@ -32,7 +32,6 @@ uid_t ctdluid = 0; const char *CitadelServiceUDS="citadel-UDS"; const char *CitadelServiceTCP="citadel-TCP"; -void go_threading(void); int sanity_diag_mode = 0; @@ -40,7 +39,7 @@ int sanity_diag_mode = 0; * Create or remove a lock file, so we only have one Citadel Server running at a time. */ void ctdl_lockfile(int yo) { - static char lockfilename[SIZ]; + static char lockfilename[PATH_MAX]; static FILE *fp; if (yo) { @@ -251,26 +250,12 @@ int main(int argc, char **argv) #endif ctdl_lockfile(1); - - /* Initialize... */ - init_sysdep(); - - /* - * Do non system dependent startup functions. - */ - master_startup(); - - /* - * Check that the control record is correct and place sensible values if it isn't - */ - check_control(); - - /* - * Run any upgrade entry points - */ - syslog(LOG_INFO, "main: upgrading modules"); + init_sysdep(); // Initialize... + master_startup(); // Do non system dependent startup functions + check_control(); // Check, sanitize, initialize the control record + syslog(LOG_INFO, "main: upgrading modules"); // Run any upgrade entry points upgrade_modules(); - + /* * Load the user for the masterCC or create them if they don't exist */ @@ -313,9 +298,6 @@ int main(int argc, char **argv) do_async_loop, CitadelServiceTCP); - - - /* * Load any server-side extensions available here. */ diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 971d729c4..c547f9772 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -86,14 +86,14 @@ void init_sysdep(void) { init_ssl(); #endif - /* - * Set up a place to put thread-specific data. - * We only need a single pointer per thread - it points to the - * CitContext structure (in the ContextList linked list) of the - * session to which the calling thread is currently bound. - */ - if (pthread_key_create(&MyConKey, NULL) != 0) { + if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads + syslog(LOG_ERR, "pthread_key_create() : %m"); + abort(); + } + + if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions syslog(LOG_CRIT, "sysdep: can't create TSD key: %m"); + abort(); } /* diff --git a/citadel/threads.c b/citadel/threads.c index 4da15e6fb..b0856bf22 100644 --- a/citadel/threads.c +++ b/citadel/threads.c @@ -1,7 +1,7 @@ /* * Thread handling stuff for Citadel server * - * Copyright (c) 1987-2015 by the citadel.org team + * Copyright (c) 1987-2019 by the citadel.org team * * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 3. @@ -23,16 +23,13 @@ #include "context.h" #include "threads.h" - int num_workers = 0; /* Current number of worker threads */ int active_workers = 0; /* Number of ACTIVE worker threads */ pthread_key_t ThreadKey; pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */ struct thread_tsd masterTSD; int server_shutting_down = 0; /* set to nonzero during shutdown */ - -pthread_mutex_t ThreadCountMutex;; - +pthread_mutex_t ThreadCountMutex; void InitializeSemaphores(void) { @@ -45,8 +42,6 @@ void InitializeSemaphores(void) } - - /* * Obtain a semaphore lock to begin a critical section. * but only if no one else has one @@ -83,6 +78,7 @@ void begin_critical_section(int which_one) pthread_mutex_lock(&Critters[which_one]); } + /* * Release a semaphore lock to end a critical section. */ @@ -92,18 +88,18 @@ void end_critical_section(int which_one) } - - /* * Return a pointer to our thread-specific (not session-specific) data. */ struct thread_tsd *MyThread(void) { - register struct thread_tsd *c; - return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c); + struct thread_tsd *c = (struct thread_tsd *) pthread_getspecific(ThreadKey) ; + if (!c) { + return &masterTSD; + } + return c; } - /* * Called by CtdlThreadCreate() * We have to pass through here before starting our thread in order to create a set of data @@ -119,8 +115,7 @@ void *CTC_backend(void *supplied_start_routine) pthread_setspecific(ThreadKey, (const void *) mytsd); start_routine(NULL); - -// free(mytsd); + // free(mytsd); return(NULL); } @@ -134,7 +129,6 @@ void CtdlThreadCreate(void *(*start_routine)(void*)) pthread_attr_t attr; int ret = 0; - ret = pthread_attr_init(&attr); ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE); ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine); @@ -143,6 +137,7 @@ void CtdlThreadCreate(void *(*start_routine)(void*)) void InitializeMasterTSD(void) { + TRACE; memset(&masterTSD, 0, sizeof(struct thread_tsd)); } @@ -152,11 +147,6 @@ void InitializeMasterTSD(void) { */ void go_threading(void) { - if (pthread_key_create(&ThreadKey, NULL) != 0) { - syslog(LOG_ERR, "pthread_key_create() : %m"); - abort(); - } - pthread_mutex_init(&ThreadCountMutex, NULL); /* Second call to module init functions now that threading is up */ diff --git a/citadel/threads.h b/citadel/threads.h index 21e790530..5540d27a8 100644 --- a/citadel/threads.h +++ b/citadel/threads.h @@ -24,6 +24,7 @@ struct thread_tsd { DBC *cursors[MAXCDB]; /* Cursors, for traversals... */ }; +pthread_key_t ThreadKey; extern struct thread_tsd masterTSD; #define TSD MyThread() diff --git a/citadel/user_ops.c b/citadel/user_ops.c index b9a357db9..f8c74445d 100644 --- a/citadel/user_ops.c +++ b/citadel/user_ops.c @@ -1180,10 +1180,9 @@ void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data) // Phase 2 : perform the callback for each username for (i=0; i