From d90684bd0e048383d4b5d4cb5475b69e497b72b7 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Thu, 4 Sep 2008 08:13:58 +0000 Subject: [PATCH] Prepare for critical sections on Windows by adding a layer of indirection before the pthread mutex functions. --- cgi.c | 14 +++--- errlog.c | 27 +++++++----- jbsockets.c | 13 +++--- jcc.c | 122 ++++++++++++++++++++++++++++++++++++++++++---------- jcc.h | 26 ++++++++--- miscutil.c | 10 +++-- parsers.c | 17 +++++--- 7 files changed, 168 insertions(+), 61 deletions(-) diff --git a/cgi.c b/cgi.c index 964c7628..a1b71943 100644 --- a/cgi.c +++ b/cgi.c @@ -1,4 +1,4 @@ -const char cgi_rcs[] = "$Id: cgi.c,v 1.111 2008/08/31 15:59:02 fabiankeil Exp $"; +const char cgi_rcs[] = "$Id: cgi.c,v 1.112 2008/08/31 16:08:12 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $ @@ -38,6 +38,10 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.111 2008/08/31 15:59:02 fabiankeil Exp $" * * Revisions : * $Log: cgi.c,v $ + * Revision 1.112 2008/08/31 16:08:12 fabiankeil + * "View the request headers" isn't more equal than the other + * menu items and thus doesn't need a trailing dot either. + * * Revision 1.111 2008/08/31 15:59:02 fabiankeil * There's no reason to let remote toggling support depend * on FEATURE_CGI_EDIT_ACTIONS, so make sure it doesn't. @@ -2001,9 +2005,9 @@ void get_http_time(int time_offset, char *buf, size_t buffer_size) #if HAVE_GMTIME_R t = gmtime_r(¤t_time, &dummy); #elif FEATURE_PTHREAD - pthread_mutex_lock(&gmtime_mutex); + privoxy_mutex_lock(&gmtime_mutex); t = gmtime(¤t_time); - pthread_mutex_unlock(&gmtime_mutex); + privoxy_mutex_unlock(&gmtime_mutex); #else t = gmtime(¤t_time); #endif @@ -2057,9 +2061,9 @@ static void get_locale_time(char *buf, size_t buffer_size) #if HAVE_LOCALTIME_R timeptr = localtime_r(¤t_time, &dummy); #elif FEATURE_PTHREAD - pthread_mutex_lock(&localtime_mutex); + privoxy_mutex_lock(&localtime_mutex); timeptr = localtime(¤t_time); - pthread_mutex_unlock(&localtime_mutex); + privoxy_mutex_unlock(&localtime_mutex); #else timeptr = localtime(¤t_time); #endif diff --git a/errlog.c b/errlog.c index 047b171e..d9f72ed0 100644 --- a/errlog.c +++ b/errlog.c @@ -1,4 +1,4 @@ -const char errlog_rcs[] = "$Id: errlog.c,v 1.73 2008/08/04 19:06:55 fabiankeil Exp $"; +const char errlog_rcs[] = "$Id: errlog.c,v 1.74 2008/08/06 18:33:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $ @@ -33,6 +33,11 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.73 2008/08/04 19:06:55 fabiankeil E * * Revisions : * $Log: errlog.c,v $ + * Revision 1.74 2008/08/06 18:33:36 fabiankeil + * If the "close fd first" workaround doesn't work, + * the fatal error message will be lost, so we better + * explain the consequences while we still can. + * * Revision 1.73 2008/08/04 19:06:55 fabiankeil * Add a lame workaround for the "can't open an already open * logfile on OS/2" problem reported by Maynard in #2028842 @@ -434,19 +439,19 @@ static char *os2_socket_strerr(int errcode, char *tmp_buf); #ifdef FEATURE_PTHREAD static inline void lock_logfile(void) { - pthread_mutex_lock(&log_mutex); + privoxy_mutex_lock(&log_mutex); } static inline void unlock_logfile(void) { - pthread_mutex_unlock(&log_mutex); + privoxy_mutex_unlock(&log_mutex); } static inline void lock_loginit(void) { - pthread_mutex_lock(&log_init_mutex); + privoxy_mutex_lock(&log_init_mutex); } static inline void unlock_loginit(void) { - pthread_mutex_unlock(&log_init_mutex); + privoxy_mutex_unlock(&log_init_mutex); } #else /* ! FEATURE_PTHREAD */ /* @@ -765,9 +770,9 @@ static inline size_t get_log_timestamp(char *buffer, size_t buffer_size) #ifdef HAVE_LOCALTIME_R tm_now = *localtime_r(&now, &tm_now); #elif FEATURE_PTHREAD - pthread_mutex_lock(&localtime_mutex); + privoxy_mutex_lock(&localtime_mutex); tm_now = *localtime(&now); - pthread_mutex_unlock(&localtime_mutex); + privoxy_mutex_unlock(&localtime_mutex); #else tm_now = *localtime(&now); #endif @@ -823,18 +828,18 @@ static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size) #ifdef HAVE_GMTIME_R gmt = *gmtime_r(&now, &gmt); #elif FEATURE_PTHREAD - pthread_mutex_lock(&gmtime_mutex); + privoxy_mutex_lock(&gmtime_mutex); gmt = *gmtime(&now); - pthread_mutex_unlock(&gmtime_mutex); + privoxy_mutex_unlock(&gmtime_mutex); #else gmt = *gmtime(&now); #endif #ifdef HAVE_LOCALTIME_R tm_now = localtime_r(&now, &dummy); #elif FEATURE_PTHREAD - pthread_mutex_lock(&localtime_mutex); + privoxy_mutex_lock(&localtime_mutex); tm_now = localtime(&now); - pthread_mutex_unlock(&localtime_mutex); + privoxy_mutex_unlock(&localtime_mutex); #else tm_now = localtime(&now); #endif diff --git a/jbsockets.c b/jbsockets.c index 2949c31e..d601fb2d 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.46 2008/03/21 11:13:57 fabiankeil Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.47 2008/03/26 18:07:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $ @@ -35,6 +35,9 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.46 2008/03/21 11:13:57 fabian * * Revisions : * $Log: jbsockets.c,v $ + * Revision 1.47 2008/03/26 18:07:07 fabiankeil + * Add hostname directive. Closes PR#1918189. + * * Revision 1.46 2008/03/21 11:13:57 fabiankeil * Only gather host information if it's actually needed. * Also move the code out of accept_connection() so it's less likely @@ -795,10 +798,10 @@ void get_host_information(jb_socket afd, char **ip_address, char **hostname) host = NULL; } #elif FEATURE_PTHREAD - pthread_mutex_lock(&resolver_mutex); + privoxy_mutex_lock(&resolver_mutex); host = gethostbyaddr((const char *)&server.sin_addr, sizeof(server.sin_addr), AF_INET); - pthread_mutex_unlock(&resolver_mutex); + privoxy_mutex_unlock(&resolver_mutex); #else host = gethostbyaddr((const char *)&server.sin_addr, sizeof(server.sin_addr), AF_INET); @@ -941,7 +944,7 @@ unsigned long resolve_hostname_to_ip(const char *host) hostp = NULL; } #elif FEATURE_PTHREAD - pthread_mutex_lock(&resolver_mutex); + privoxy_mutex_lock(&resolver_mutex); while (NULL == (hostp = gethostbyname(host)) && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES)) { @@ -949,7 +952,7 @@ unsigned long resolve_hostname_to_ip(const char *host) "Timeout #%u while trying to resolve %s. Trying again.", dns_retries, host); } - pthread_mutex_unlock(&resolver_mutex); + privoxy_mutex_unlock(&resolver_mutex); #else while (NULL == (hostp = gethostbyname(host)) && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES)) diff --git a/jcc.c b/jcc.c index e76358ba..d56c815f 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.184 2008/08/22 15:34:45 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.185 2008/08/30 12:03:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,9 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.184 2008/08/22 15:34:45 fabiankeil Exp $" * * Revisions : * $Log: jcc.c,v $ + * Revision 1.185 2008/08/30 12:03:07 fabiankeil + * Remove FEATURE_COOKIE_JAR. + * * Revision 1.184 2008/08/22 15:34:45 fabiankeil * - Silence LLVM/Clang complaint. * - Make received_hup_signal static. @@ -1168,23 +1171,26 @@ static int32 server_thread(void *data); #endif #ifdef FEATURE_PTHREAD -pthread_mutex_t log_mutex; -pthread_mutex_t log_init_mutex; +/* + * XXX: Does the locking stuff really belong in this file? + */ +privoxy_mutex_t log_mutex; +privoxy_mutex_t log_init_mutex; #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) -pthread_mutex_t resolver_mutex; +privoxy_mutex_t resolver_mutex; #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */ #ifndef HAVE_GMTIME_R -pthread_mutex_t gmtime_mutex; +privoxy_mutex_t gmtime_mutex; #endif /* ndef HAVE_GMTIME_R */ #ifndef HAVE_LOCALTIME_R -pthread_mutex_t localtime_mutex; +privoxy_mutex_t localtime_mutex; #endif /* ndef HAVE_GMTIME_R */ #ifndef HAVE_RANDOM -pthread_mutex_t rand_mutex; +privoxy_mutex_t rand_mutex; #endif /* ndef HAVE_RANDOM */ #endif /* FEATURE_PTHREAD */ @@ -2952,6 +2958,86 @@ static void usage(const char *myname) #endif /* #if !defined(_WIN32) || defined(_WIN_CONSOLE) */ +#ifdef FEATURE_PTHREAD +/********************************************************************* + * + * Function : privoxy_mutex_lock + * + * Description : Locks a mutex using pthread_mutex_lock. + * + * Parameters : + * 1 : mutex = The mutex to lock. + * + * Returns : Void, exits in case of errors. + * + *********************************************************************/ +void privoxy_mutex_lock(privoxy_mutex_t *mutex) +{ + int err = pthread_mutex_lock(mutex); + if (err) + { + if (mutex != &log_mutex) + { + log_error(LOG_LEVEL_FATAL, + "Mutex locking failed: %s.\n", strerror(err)); + } + exit(1); + } +} + + +/********************************************************************* + * + * Function : privoxy_mutex_unlock + * + * Description : Unlocks a mutex using pthread_mutex_unlock. + * + * Parameters : + * 1 : mutex = The mutex to unlock. + * + * Returns : Void, exits in case of errors. + * + *********************************************************************/ +void privoxy_mutex_unlock(privoxy_mutex_t *mutex) +{ + int err = pthread_mutex_unlock(mutex); + if (err) + { + if (mutex != &log_mutex) + { + log_error(LOG_LEVEL_FATAL, + "Mutex unlocking failed: %s.\n", strerror(err)); + } + exit(1); + } +} + + +/********************************************************************* + * + * Function : privoxy_mutex_init + * + * Description : Prepares a mutex using pthread_mutex_init. + * + * Parameters : + * 1 : mutex = The mutex to initialize. + * + * Returns : Void, exits in case of errors. + * + *********************************************************************/ +static void privoxy_mutex_init(privoxy_mutex_t *mutex) +{ + int err = pthread_mutex_init(mutex, 0); + if (err) + { + printf("Fatal error. Mutex initialization failed: %s.\n", + strerror(err)); + exit(1); + } +} +#endif /* def FEATURE_PTHREAD */ + + /********************************************************************* * * Function : initialize_mutexes @@ -2965,15 +3051,13 @@ static void usage(const char *myname) *********************************************************************/ static void initialize_mutexes(void) { - int err = 0; - #ifdef FEATURE_PTHREAD /* * Prepare global mutex semaphores */ - err = pthread_mutex_init(&log_mutex, 0); + privoxy_mutex_init(&log_mutex); - if (!err) err = pthread_mutex_init(&log_init_mutex, 0); + privoxy_mutex_init(&log_init_mutex); /* * XXX: The assumptions below are a bit naive @@ -2984,22 +3068,22 @@ static void initialize_mutexes(void) * thread safe. */ #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) - if (!err) err = pthread_mutex_init(&resolver_mutex, 0); + privoxy_mutex_init(&resolver_mutex); #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */ /* * XXX: should we use a single mutex for * localtime() and gmtime() as well? */ #ifndef HAVE_GMTIME_R - if (!err) err = pthread_mutex_init(&gmtime_mutex, 0); + privoxy_mutex_init(&gmtime_mutex); #endif /* ndef HAVE_GMTIME_R */ #ifndef HAVE_LOCALTIME_R - if (!err) err = pthread_mutex_init(&localtime_mutex, 0); + privoxy_mutex_init(&localtime_mutex); #endif /* ndef HAVE_GMTIME_R */ #ifndef HAVE_RANDOM - if (!err) err = pthread_mutex_init(&rand_mutex, 0); + privoxy_mutex_init(&rand_mutex); #endif /* ndef HAVE_RANDOM */ #endif /* FEATURE_PTHREAD */ @@ -3007,14 +3091,6 @@ static void initialize_mutexes(void) * TODO: mutex support for mingw32 would be swell. */ - if (err) - { - printf("Fatal error. Mutex initialization failed: %s.\n", - strerror(err)); - exit(1); - } - - return; } diff --git a/jcc.h b/jcc.h index 4c263c77..03482e89 100644 --- a/jcc.h +++ b/jcc.h @@ -1,6 +1,6 @@ #ifndef JCC_H_INCLUDED #define JCC_H_INCLUDED -#define JCC_H_VERSION "$Id: jcc.h,v 1.21 2007/04/22 13:18:06 fabiankeil Exp $" +#define JCC_H_VERSION "$Id: jcc.h,v 1.22 2007/06/01 18:16:36 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.h,v $ @@ -35,6 +35,12 @@ * * Revisions : * $Log: jcc.h,v $ + * Revision 1.22 2007/06/01 18:16:36 fabiankeil + * Use the same mutex for gethostbyname() and gethostbyaddr() to prevent + * deadlocks and crashes on OpenBSD and possibly other OS with neither + * gethostbyname_r() nor gethostaddr_r(). Closes BR#1729174. + * Thanks to Ralf Horstmann for report and solution. + * * Revision 1.21 2007/04/22 13:18:06 fabiankeil * Keep the HTTP snippets local. * @@ -182,23 +188,29 @@ extern int g_terminate; #ifdef FEATURE_PTHREAD #include -extern pthread_mutex_t log_mutex; -extern pthread_mutex_t log_init_mutex; + +typedef pthread_mutex_t privoxy_mutex_t; + +extern void privoxy_mutex_lock(privoxy_mutex_t *mutex); +extern void privoxy_mutex_unlock(privoxy_mutex_t *mutex); + +extern privoxy_mutex_t log_mutex; +extern privoxy_mutex_t log_init_mutex; #ifndef HAVE_GMTIME_R -extern pthread_mutex_t gmtime_mutex; +extern privoxy_mutex_t gmtime_mutex; #endif /* ndef HAVE_GMTIME_R */ #ifndef HAVE_LOCALTIME_R -extern pthread_mutex_t localtime_mutex; +extern privoxy_mutex_t localtime_mutex; #endif /* ndef HAVE_GMTIME_R */ #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) -extern pthread_mutex_t resolver_mutex; +extern privoxy_mutex_t resolver_mutex; #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */ #ifndef HAVE_RANDOM -extern pthread_mutex_t rand_mutex; +extern privoxy_mutex_t rand_mutex; #endif /* ndef HAVE_RANDOM */ #endif /* FEATURE_PTHREAD */ diff --git a/miscutil.c b/miscutil.c index 3eb707ca..4e5e3330 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.57 2008/03/24 15:29:51 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.58 2008/04/17 14:53:30 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -44,6 +44,10 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.57 2008/03/24 15:29:51 fabianke * * Revisions : * $Log: miscutil.c,v $ + * Revision 1.58 2008/04/17 14:53:30 fabiankeil + * Move simplematch() into urlmatch.c as it's only + * used to match (old-school) domain patterns. + * * Revision 1.57 2008/03/24 15:29:51 fabiankeil * Pet gcc43. * @@ -969,9 +973,9 @@ long int pick_from_range(long int range) #ifdef HAVE_RANDOM number = random() % range + 1; #elif defined(FEATURE_PTHREAD) - pthread_mutex_lock(&rand_mutex); + privoxy_mutex_lock(&rand_mutex); number = rand() % (long int)(range + 1); - pthread_mutex_unlock(&rand_mutex); + privoxy_mutex_unlock(&rand_mutex); #else #ifdef _WIN32 /* diff --git a/parsers.c b/parsers.c index c5d0a9b3..17e85019 100644 --- a/parsers.c +++ b/parsers.c @@ -1,4 +1,4 @@ -const char parsers_rcs[] = "$Id: parsers.c,v 1.137 2008/05/30 15:50:08 fabiankeil Exp $"; +const char parsers_rcs[] = "$Id: parsers.c,v 1.138 2008/08/30 12:03:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $ @@ -44,6 +44,9 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.137 2008/05/30 15:50:08 fabiankei * * Revisions : * $Log: parsers.c,v $ + * Revision 1.138 2008/08/30 12:03:07 fabiankeil + * Remove FEATURE_COOKIE_JAR. + * * Revision 1.137 2008/05/30 15:50:08 fabiankeil * Remove questionable micro-optimizations * whose usefulness has never been measured. @@ -2860,9 +2863,9 @@ static jb_err server_last_modified(struct client_state *csp, char **header) #ifdef HAVE_GMTIME_R timeptr = gmtime_r(&now, &gmt); #elif FEATURE_PTHREAD - pthread_mutex_lock(&gmtime_mutex); + privoxy_mutex_lock(&gmtime_mutex); timeptr = gmtime(&now); - pthread_mutex_unlock(&gmtime_mutex); + privoxy_mutex_unlock(&gmtime_mutex); #else timeptr = gmtime(&now); #endif @@ -2890,9 +2893,9 @@ static jb_err server_last_modified(struct client_state *csp, char **header) #ifdef HAVE_GMTIME_R timeptr = gmtime_r(&last_modified, &gmt); #elif FEATURE_PTHREAD - pthread_mutex_lock(&gmtime_mutex); + privoxy_mutex_lock(&gmtime_mutex); timeptr = gmtime(&last_modified); - pthread_mutex_unlock(&gmtime_mutex); + privoxy_mutex_unlock(&gmtime_mutex); #else timeptr = gmtime(&last_modified); #endif @@ -3586,9 +3589,9 @@ static jb_err client_if_modified_since(struct client_state *csp, char **header) #ifdef HAVE_GMTIME_R timeptr = gmtime_r(&tm, &gmt); #elif FEATURE_PTHREAD - pthread_mutex_lock(&gmtime_mutex); + privoxy_mutex_lock(&gmtime_mutex); timeptr = gmtime(&tm); - pthread_mutex_unlock(&gmtime_mutex); + privoxy_mutex_unlock(&gmtime_mutex); #else timeptr = gmtime(&tm); #endif -- 2.39.2