From 9e8f57a4bb2ccb727d54fa7e05204b1e10443fdf Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Fri, 1 Jun 2007 18:16:36 +0000 Subject: [PATCH] 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. --- jbsockets.c | 15 ++++++++++----- jcc.c | 35 ++++++++++++++++++----------------- jcc.h | 15 +++++++-------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/jbsockets.c b/jbsockets.c index 4925b7fa..c3f4be67 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.41 2006/11/13 19:05:51 fabiankeil Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.42 2007/04/01 17:37:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $ @@ -35,6 +35,11 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.41 2006/11/13 19:05:51 fabian * * Revisions : * $Log: jbsockets.c,v $ + * Revision 1.42 2007/04/01 17:37:07 fabiankeil + * - Add DNS retries for Solaris and other systems + * whose gethostbyname_r version takes five arguments. + * - Move maximum number of DNS retries into a macro. + * * Revision 1.41 2006/11/13 19:05:51 fabiankeil * Make pthread mutex locking more generic. Instead of * checking for OSX and OpenBSD, check for FEATURE_PTHREAD @@ -769,10 +774,10 @@ int accept_connection(struct client_state * csp, jb_socket fd) host = NULL; } #elif FEATURE_PTHREAD - pthread_mutex_lock(&gethostbyaddr_mutex); + pthread_mutex_lock(&resolver_mutex); host = gethostbyaddr((const char *)&server.sin_addr, sizeof(server.sin_addr), AF_INET); - pthread_mutex_unlock(&gethostbyaddr_mutex); + pthread_mutex_unlock(&resolver_mutex); #else host = gethostbyaddr((const char *)&server.sin_addr, sizeof(server.sin_addr), AF_INET); @@ -865,7 +870,7 @@ unsigned long resolve_hostname_to_ip(const char *host) hostp = NULL; } #elif FEATURE_PTHREAD - pthread_mutex_lock(&gethostbyname_mutex); + pthread_mutex_lock(&resolver_mutex); while (NULL == (hostp = gethostbyname(host)) && (h_errno == TRY_AGAIN) && (dns_retries++ < MAX_DNS_RETRIES)) { @@ -873,7 +878,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(&gethostbyname_mutex); + pthread_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 0e83a68f..9da99139 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.135 2007/05/24 17:03:50 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.136 2007/06/01 16:41:11 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,12 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.135 2007/05/24 17:03:50 fabiankeil Exp $" * * Revisions : * $Log: jcc.c,v $ + * Revision 1.136 2007/06/01 16:41:11 fabiankeil + * Add forward-override{} to change the forwarding settings through + * action sections. This is mainly interesting to forward different + * clients differently (for example based on User-Agent or request + * origin). + * * Revision 1.135 2007/05/24 17:03:50 fabiankeil * - Let usage() mention the --chroot parameter. * - Use read_socket() consistently and always leave @@ -945,6 +951,10 @@ static int32 server_thread(void *data); pthread_mutex_t log_mutex; pthread_mutex_t log_init_mutex; +#if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) +pthread_mutex_t resolver_mutex; +#endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */ + #ifndef HAVE_GMTIME_R pthread_mutex_t gmtime_mutex; #endif /* ndef HAVE_GMTIME_R */ @@ -953,14 +963,6 @@ pthread_mutex_t gmtime_mutex; pthread_mutex_t localtime_mutex; #endif /* ndef HAVE_GMTIME_R */ -#ifndef HAVE_GETHOSTBYADDR_R -pthread_mutex_t gethostbyaddr_mutex; -#endif /* ndef HAVE_GETHOSTBYADDR_R */ - -#ifndef HAVE_GETHOSTBYNAME_R -pthread_mutex_t gethostbyname_mutex; -#endif /* ndef HAVE_GETHOSTBYNAME_R */ - #ifndef HAVE_RANDOM pthread_mutex_t rand_mutex; #endif /* ndef HAVE_RANDOM */ @@ -2711,6 +2713,13 @@ void initialize_mutexes() * have no gethostbyname_r, but gethostbyname is * thread safe. */ +#if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) + if (!err) err = pthread_mutex_init(&resolver_mutex, 0); +#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); #endif /* ndef HAVE_GMTIME_R */ @@ -2719,14 +2728,6 @@ void initialize_mutexes() if (!err) err = pthread_mutex_init(&localtime_mutex, 0); #endif /* ndef HAVE_GMTIME_R */ -#ifndef HAVE_GETHOSTBYADDR_R - if (!err) err = pthread_mutex_init(&gethostbyaddr_mutex, 0); -#endif /* ndef HAVE_GETHOSTBYADDR_R */ - -#ifndef HAVE_GETHOSTBYNAME_R - if (!err) err = pthread_mutex_init(&gethostbyname_mutex, 0); -#endif /* ndef HAVE_GETHOSTBYNAME_R */ - #ifndef HAVE_RANDOM if (!err) err = pthread_mutex_init(&rand_mutex, 0); #endif /* ndef HAVE_RANDOM */ diff --git a/jcc.h b/jcc.h index 0b9bfc0f..4c263c77 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.20 2006/12/26 17:31:41 fabiankeil Exp $" +#define JCC_H_VERSION "$Id: jcc.h,v 1.21 2007/04/22 13:18:06 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.h,v $ @@ -35,6 +35,9 @@ * * Revisions : * $Log: jcc.h,v $ + * Revision 1.21 2007/04/22 13:18:06 fabiankeil + * Keep the HTTP snippets local. + * * Revision 1.20 2006/12/26 17:31:41 fabiankeil * Mutex protect rand() if POSIX threading * is used, warn the user if that's not possible @@ -190,13 +193,9 @@ extern pthread_mutex_t gmtime_mutex; extern pthread_mutex_t localtime_mutex; #endif /* ndef HAVE_GMTIME_R */ -#ifndef HAVE_GETHOSTBYADDR_R -extern pthread_mutex_t gethostbyaddr_mutex; -#endif /* ndef HAVE_GETHOSTBYADDR_R */ - -#ifndef HAVE_GETHOSTBYNAME_R -extern pthread_mutex_t gethostbyname_mutex; -#endif /* ndef HAVE_GETHOSTBYNAME_R */ +#if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) +extern pthread_mutex_t resolver_mutex; +#endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */ #ifndef HAVE_RANDOM extern pthread_mutex_t rand_mutex; -- 2.39.2