From 712f52070112388ddfcdc059b54a76af3dbdf726 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 25 Sep 2003 01:44:33 +0000 Subject: [PATCH 1/1] Resyncing HEAD with v_3_0_branch for two OSX fixes: Making thread IDs look sane in the logfile for Mach kernels, and fixing multithreading crashes due to thread-unsafe system calls. and --- config | 6 +++--- configure.in | 18 ++++++------------ src/cgi.c | 28 ++++++++++++++++++++++++---- src/errlog.c | 26 +++++++++++++++++++++++++- src/jbsockets.c | 36 ++++++++++++++++++++++++++++++++---- src/jcc.c | 27 +++++++++++++++++++++++++-- src/jcc.h | 14 ++++++++++++-- src/parsers.c | 14 +++++++++++++- 8 files changed, 140 insertions(+), 29 deletions(-) diff --git a/config b/config index de69de6f..5aac5a30 100644 --- a/config +++ b/config @@ -2,7 +2,7 @@ # # Copyright (C) 2001, 2002 Privoxy Developers http://privoxy.org # -# $Id: config,v 1.44 2003/04/20 17:37:28 hal9 Exp $ +# $Id: config,v 1.45 2003/09/22 00:33:01 david__schmidt Exp $ # #################################################################### # # @@ -104,7 +104,7 @@ # flat, except for confdir/templates, where the HTML templates # for CGI output reside (e.g. Privoxy's 404 error page). # -confdir /home/hal/ptmp/etc +confdir . # # 1.2. logdir @@ -131,7 +131,7 @@ confdir /home/hal/ptmp/etc # # No trailing "/", please # -logdir /home/hal/ptmp/var/log/privoxy +logdir . # # 1.3. actionsfile diff --git a/configure.in b/configure.in index e794688c..e36526a0 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. dnl -dnl $Id: configure.in,v 1.87 2002/12/28 04:10:22 david__schmidt Exp $ +dnl $Id: configure.in,v 1.88 2003/03/23 02:15:51 hal9 Exp $ dnl dnl Written by and Copyright (C) 2001, 2002 the SourceForge dnl Privoxy team. http://www.privoxy.org/ @@ -28,6 +28,9 @@ dnl or write to the Free Software Foundation, Inc., 59 dnl Temple Place - Suite 330, Boston, MA 02111-1307, USA. dnl dnl $Log: configure.in,v $ +dnl Revision 1.88 2003/03/23 02:15:51 hal9 +dnl Apply Docbook/FreeBSD patch from a.go at tiscali.nl. Thanks! +dnl dnl Revision 1.87 2002/12/28 04:10:22 david__schmidt dnl Initial drop of dashboard instrumentation - enabled with dnl --enable-activity-console @@ -440,7 +443,7 @@ dnl ================================================================= dnl AutoConf Initialization dnl ================================================================= -AC_REVISION($Revision: 1.87 $) +AC_REVISION($Revision: 1.88 $) AC_INIT(src/jcc.c) if test ! -f src/config.h.in; then echo "You need to run autoheader first. " @@ -925,19 +928,10 @@ dnl Mac OSX specific dnl ================================================================= case "$host" in -*-apple-darwin*) SPECIAL_CFLAGS="-Dunix" +*-apple-darwin*) SPECIAL_CFLAGS="-Dunix -DOSX_DARWIN" ;; esac -dnl ================================================================= -dnl Mac OSX specific -dnl ================================================================= - -case "$host" in -*-apple-darwin*) SPECIAL_CFLAGS="-Dunix" -;; -esac - dnl ================================================================= dnl OpenBSD specific dnl ================================================================= diff --git a/src/cgi.c b/src/cgi.c index 6af40dfb..034f872d 100644 --- a/src/cgi.c +++ b/src/cgi.c @@ -1,7 +1,7 @@ -const char cgi_rcs[] = "$Id: cgi.c,v 2.2 2002/09/04 15:17:28 oes Exp $"; +const char cgi_rcs[] = "$Id: cgi.c,v 2.3 2002/11/12 16:19:18 oes Exp $"; /********************************************************************* * - * File : $Source: /cvsroot/ijbswa//current/src/cgi.c,v $ + * File : $Source: /cvsroot/ijbswa/current/src/cgi.c,v $ * * Purpose : Declares functions to intercept request, generate * html or gif answers, and to compose HTTP resonses. @@ -38,6 +38,9 @@ const char cgi_rcs[] = "$Id: cgi.c,v 2.2 2002/09/04 15:17:28 oes Exp $"; * * Revisions : * $Log: cgi.c,v $ + * Revision 2.3 2002/11/12 16:19:18 oes + * Fix: g_bToggleIJB was used outside #ifdef FEATURE_TOGGLE + * * Revision 2.2 2002/09/04 15:17:28 oes * Synced with the stable branch: * Revision 1.70.2.1 2002/08/05 11:17:46 oes @@ -426,6 +429,11 @@ const char cgi_rcs[] = "$Id: cgi.c,v 2.2 2002/09/04 15:17:28 oes Exp $"; #endif /* def FEATURE_CGI_EDIT_ACTIONS */ #include "loadcfg.h" /* loadcfg.h is for g_bToggleIJB only */ +#ifdef FEATURE_PTHREAD +#include +#include "jcc.h" +/* jcc.h is for mutex semaphore globals only */ +#endif /* def FEATURE_PTHREAD */ const char cgi_h_rcs[] = CGI_H_VERSION; @@ -1442,6 +1450,15 @@ void get_http_time(int time_offset, char *buf) struct tm *t; time_t current_time; +#if defined(HAVE_GMTIME_R) && !defined(OSX_DARWIN) + /* + * Declare dummy up here (instead of inside get/set gmt block) so it + * doesn't go out of scope before it's potentially used in snprintf later. + * Wrapping declaration inside HAVE_GMTIME_R keeps the compiler quiet when + * !defined HAVE_GMTIME_R. + */ + struct tm dummy; +#endif assert(buf); @@ -1451,8 +1468,11 @@ void get_http_time(int time_offset, char *buf) /* get and save the gmt */ { -#ifdef HAVE_GMTIME_R - struct tm dummy; +#ifdef OSX_DARWIN + pthread_mutex_lock(&gmtime_mutex); + t = gmtime(¤t_time); + pthread_mutex_unlock(&gmtime_mutex); +#elif HAVE_GMTIME_R t = gmtime_r(¤t_time, &dummy); #else t = gmtime(¤t_time); diff --git a/src/errlog.c b/src/errlog.c index fa88da45..c3985d3c 100644 --- a/src/errlog.c +++ b/src/errlog.c @@ -1,4 +1,4 @@ -const char errlog_rcs[] = "$Id: errlog.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $"; +const char errlog_rcs[] = "$Id: errlog.c,v 2.1 2002/09/25 12:51:21 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/src/errlog.c,v $ @@ -33,6 +33,9 @@ const char errlog_rcs[] = "$Id: errlog.c,v 2.0 2002/06/04 14:34:21 jongfoster Ex * * Revisions : * $Log: errlog.c,v $ + * Revision 2.1 2002/09/25 12:51:21 oes + * Made log_error safe against NULL string arguments + * * Revision 2.0 2002/06/04 14:34:21 jongfoster * Moving source files to src/ * @@ -423,6 +426,15 @@ void log_error(int loglevel, char *fmt, ...) /* FIXME get current thread id */ #ifdef FEATURE_PTHREAD this_thread = (long)pthread_self(); +#ifdef __MACH__ + /* + * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable + * value at the first 4 bytes of pthread_self()'s return value, a pthread_t. + * pthread_t is supposed to be opaque... but it's fairly random, though, so + * we make it mostly presentable. + */ + this_thread = abs(this_thread % 1000); +#endif /* def __MACH__ */ #elif defined(_WIN32) this_thread = GetCurrentThreadId(); #elif defined(__OS2__) @@ -450,6 +462,10 @@ void log_error(int loglevel, char *fmt, ...) time (&now); #ifdef HAVE_LOCALTIME_R tm_now = *localtime_r(&now, &tm_now); +#elif OSX_DARWIN + pthread_mutex_lock(&localtime_mutex); + tm_now = *localtime (&now); + pthread_mutex_unlock(&localtime_mutex); #else tm_now = *localtime (&now); #endif @@ -713,11 +729,19 @@ void log_error(int loglevel, char *fmt, ...) time (&now); #ifdef HAVE_GMTIME_R gmt = *gmtime_r(&now, &gmt); +#elif OSX_DARWIN + pthread_mutex_lock(&gmtime_mutex); + gmt = *gmtime(&now); + pthread_mutex_unlock(&gmtime_mutex); #else gmt = *gmtime(&now); #endif #ifdef HAVE_LOCALTIME_R tm_now = localtime_r(&now, &dummy); +#elif OSX_DARWIN + pthread_mutex_lock(&localtime_mutex); + tm_now = localtime (&now); + pthread_mutex_unlock(&localtime_mutex); #else tm_now = localtime (&now); #endif diff --git a/src/jbsockets.c b/src/jbsockets.c index dcd1970b..02270368 100644 --- a/src/jbsockets.c +++ b/src/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 2.1 2002/12/30 19:56:16 david__schmidt Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/src/jbsockets.c,v $ @@ -35,6 +35,13 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 2.0 2002/06/04 14:34:21 jongfos * * Revisions : * $Log: jbsockets.c,v $ + * Revision 2.1 2002/12/30 19:56:16 david__schmidt + * End of initial drop of statistics console infrastructure. Data stream + * is transmitted on the stats port every interval, provided the data has + * changed since the last transmission. More work probably needs to be + * done with regard to multiplatform threading; I stole the thread spawning + * code from jcc.c, but haven't been able to test it everywhere. + * * Revision 2.0 2002/06/04 14:34:21 jongfoster * Moving source files to src/ * @@ -233,6 +240,12 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 2.0 2002/06/04 14:34:21 jongfos #endif +#ifdef OSX_DARWIN +#include +#include "jcc.h" +/* jcc.h is for mutex semaphores only */ +#endif /* def OSX_DARWIN */ + #include "project.h" #include "jbsockets.h" #include "filters.h" @@ -593,11 +606,11 @@ int bind_port(const char *hostnam, int portnum, jb_socket *pfd) #ifndef _WIN32 /* * This is not needed for Win32 - in fact, it stops - * duplicate instances of Junkbuster from being caught. + * duplicate instances of Privoxy from being caught. * * On UNIX, we assume the user is sensible enough not * to start Junkbuster multiple times on the same IP. - * Without this, stopping and restarting Junkbuster + * Without this, stopping and restarting Privoxy * from a script fails. * Note: SO_REUSEADDR is meant to only take over * sockets which are *not* in listen state in Linux, @@ -720,6 +733,11 @@ int accept_connection(struct client_state * csp, jb_socket fd) { host = NULL; } +#elif defined(OSX_DARWIN) + pthread_mutex_lock(&gethostbyaddr_mutex); + host = gethostbyaddr((const char *)&server.sin_addr, + sizeof(server.sin_addr), AF_INET); + pthread_mutex_unlock(&gethostbyaddr_mutex); #else host = gethostbyaddr((const char *)&server.sin_addr, sizeof(server.sin_addr), AF_INET); @@ -794,10 +812,20 @@ unsigned long resolve_hostname_to_ip(const char *host) { hostp = NULL; } +#elif OSX_DARWIN + pthread_mutex_lock(&gethostbyname_mutex); + hostp = gethostbyname(host); + pthread_mutex_unlock(&gethostbyname_mutex); #else hostp = gethostbyname(host); #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */ - if (hostp == NULL) + /* + * On Mac OSX, if a domain exists but doesn't have a type A + * record associated with it, the h_addr member of the struct + * hostent returned by gethostbyname is NULL, even if h_length + * is 4. Therefore the second test below. + */ + if (hostp == NULL || hostp->h_addr == NULL) { errno = EINVAL; log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host); diff --git a/src/jcc.c b/src/jcc.c index 4cd21384..29b712e3 100644 --- a/src/jcc.c +++ b/src/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 2.5 2003/01/26 20:24:26 david__schmidt Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 2.6 2003/06/24 12:24:24 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/src/jcc.c,v $ @@ -33,6 +33,9 @@ const char jcc_rcs[] = "$Id: jcc.c,v 2.5 2003/01/26 20:24:26 david__schmidt Exp * * Revisions : * $Log: jcc.c,v $ + * Revision 2.6 2003/06/24 12:24:24 oes + * Added a line plus Fix-me as a reminder to fix broken force handling in trunk. Thanks to lionel for the hint + * * Revision 2.5 2003/01/26 20:24:26 david__schmidt * Updated activity console instrumentation locations * @@ -674,8 +677,18 @@ static int32 server_thread(void *data); #define sleep(N) DosSleep(((N) * 100)) #endif +#ifdef OSX_DARWIN +/* + * Hit OSX over the head with a hammer. Protect all *_r functions. + */ +pthread_mutex_t gmtime_mutex; +pthread_mutex_t localtime_mutex; +pthread_mutex_t gethostbyaddr_mutex; +pthread_mutex_t gethostbyname_mutex; +#endif /* def OSX_DARWIN */ + #if defined(unix) || defined(__EMX__) -const char *basedir; +const char *basedir = NULL; const char *pidfile = NULL; int received_hup_signal = 0; #endif /* defined unix */ @@ -1127,6 +1140,16 @@ int main(int argc, const char *argv[]) InitWin32(); #endif +#ifdef OSX_DARWIN + /* + * Prepare global mutex semaphores + */ + pthread_mutex_init(&gmtime_mutex,0); + pthread_mutex_init(&localtime_mutex,0); + pthread_mutex_init(&gethostbyaddr_mutex,0); + pthread_mutex_init(&gethostbyname_mutex,0); +#endif /* def OSX_DARWIN */ + /* * Unix signal handling * diff --git a/src/jcc.h b/src/jcc.h index 9cdab39a..15c91f24 100644 --- a/src/jcc.h +++ b/src/jcc.h @@ -1,9 +1,9 @@ #ifndef JCC_H_INCLUDED #define JCC_H_INCLUDED -#define JCC_H_VERSION "$Id: jcc.h,v 1.12 2002/03/26 22:29:55 swa Exp $" +#define JCC_H_VERSION "$Id: jcc.h,v 2.0 2002/06/04 14:34:21 jongfoster Exp $" /********************************************************************* * - * File : $Source: /cvsroot/ijbswa/current/jcc.h,v $ + * File : $Source: /cvsroot/ijbswa/current/src/jcc.h,v $ * * Purpose : Main file. Contains main() method, main loop, and * the main connection-handling function. @@ -35,6 +35,9 @@ * * Revisions : * $Log: jcc.h,v $ + * Revision 2.0 2002/06/04 14:34:21 jongfoster + * Moving source files to src/ + * * Revision 1.12 2002/03/26 22:29:55 swa * we have a new homepage! * @@ -118,6 +121,13 @@ extern int no_daemon; extern int g_terminate; #endif +#ifdef OSX_DARWIN +extern pthread_mutex_t gmtime_mutex; +extern pthread_mutex_t localtime_mutex; +extern pthread_mutex_t gethostbyaddr_mutex; +extern pthread_mutex_t gethostbyname_mutex; +#endif /* def OSX_DARWIN */ + /* Functions */ #ifdef __MINGW32__ diff --git a/src/parsers.c b/src/parsers.c index 9ad0341a..c617b564 100644 --- a/src/parsers.c +++ b/src/parsers.c @@ -1,4 +1,4 @@ -const char parsers_rcs[] = "$Id: parsers.c,v 2.3 2002/12/28 03:58:19 david__schmidt Exp $"; +const char parsers_rcs[] = "$Id: parsers.c,v 2.4 2003/01/26 20:24:26 david__schmidt Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/src/parsers.c,v $ @@ -40,6 +40,9 @@ const char parsers_rcs[] = "$Id: parsers.c,v 2.3 2002/12/28 03:58:19 david__schm * * Revisions : * $Log: parsers.c,v $ + * Revision 2.4 2003/01/26 20:24:26 david__schmidt + * Updated activity console instrumentation locations + * * Revision 2.3 2002/12/28 03:58:19 david__schmidt * Initial drop of dashboard instrumentation - enabled with * --enable-activity-console @@ -415,6 +418,11 @@ const char parsers_rcs[] = "$Id: parsers.c,v 2.3 2002/12/28 03:58:19 david__schm #include #endif +#ifdef OSX_DARWIN +#include +#include "jcc.h" +/* jcc.h is for mutex semapores only */ +#endif /* def OSX_DARWIN */ #include "project.h" #include "list.h" #include "parsers.h" @@ -1694,6 +1702,10 @@ jb_err server_set_cookie(struct client_state *csp, char **header) time (&now); #ifdef HAVE_LOCALTIME_R tm_now = *localtime_r(&now, &tm_now); +#elif OSX_DARWIN + pthread_mutex_lock(&localtime_mutex); + tm_now = *localtime (&now); + pthread_mutex_unlock(&localtime_mutex); #else tm_now = *localtime (&now); #endif -- 2.39.2