Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
[privoxy.git] / debian / patches / 08_log_pthread.dpatch
1 #! /bin/sh /usr/share/dpatch/dpatch-run
2 ## 08_log_pthread.dpatch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
3 ##
4 ## All lines beginning with `## DP:' are a description of the patch.
5 ## DP: Fix race conditions in log writing functions (Closes: #299662).
6
7 @DPATCH@
8 diff -urNad privoxy-dpatch~/errlog.c privoxy-dpatch/errlog.c
9 --- privoxy-dpatch~/errlog.c    2006-02-05 16:17:43.000000000 +0100
10 +++ privoxy-dpatch/errlog.c     2006-02-05 16:18:06.000000000 +0100
11 @@ -292,6 +292,30 @@
12  static char *os2_socket_strerr(int errcode, char *tmp_buf);
13  #endif
14  
15 +#ifdef FEATURE_PTHREAD
16 +static inline void lock_logfile()
17 +{
18 +    pthread_mutex_lock(&log_mutex);
19 +}
20 +static inline void unlock_logfile()
21 +{
22 +    pthread_mutex_unlock(&log_mutex);
23 +}
24 +static inline void lock_loginit()
25 +{
26 +    pthread_mutex_lock(&log_init_mutex);
27 +}
28 +static inline void unlock_loginit()
29 +{
30 +    pthread_mutex_unlock(&log_init_mutex);
31 +}
32 +#else /* ! FEATURE_PTHREAD */
33 +static inline void lock_logfile() {}
34 +static inline void unlock_logfile() {}
35 +static inline void lock_loginit() {}
36 +static inline void unlock_loginit() {}
37 +#endif
38 +
39  /*********************************************************************
40   *
41   * Function    :  fatal_error
42 @@ -346,7 +370,7 @@
43  {
44     FILE *fp;
45  
46 -   /* FIXME RACE HAZARD: should start critical section error_log_use here */
47 +   lock_loginit();
48  
49     /* set the logging detail level */
50     debug = debuglevel | LOG_LEVEL_MINIMUM;
51 @@ -354,9 +378,13 @@
52     if ((logfp != NULL) && (logfp != stderr))
53     {
54        log_error(LOG_LEVEL_INFO, "(Re-)Open logfile %s", logfname ? logfname : "none");
55 +      lock_logfile();
56        fclose(logfp);
57 +   } else {
58 +      lock_logfile();
59     }
60     logfp = stderr;
61 +   unlock_logfile();
62  
63     /* set the designated log file */
64     if( logfname )
65 @@ -369,7 +397,9 @@
66        /* set logging to be completely unbuffered */
67        setbuf(fp, NULL);
68  
69 +      lock_logfile();
70        logfp = fp;
71 +      unlock_logfile();
72     }
73  
74     log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION);
75 @@ -378,7 +408,7 @@
76        log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
77     }
78  
79 -   /* FIXME RACE HAZARD: should end critical section error_log_use here */
80 +   unlock_loginit();
81  
82  } /* init_error_log */
83  
84 @@ -428,6 +458,9 @@
85        return;
86     }
87  
88 +   /* protect the whole function because of the static buffer (outbuf) */
89 +   lock_logfile();
90 +
91     /* FIXME get current thread id */
92  #ifdef FEATURE_PTHREAD
93     this_thread = (long)pthread_self();
94 @@ -480,41 +513,41 @@
95     switch (loglevel)
96     {
97        case LOG_LEVEL_ERROR:
98 -         outc = sprintf(outbuf, "Privoxy(%05ld) Error: ", this_thread);
99 +         outc = sprintf(outbuf, "Privoxy(%08lx) Error: ", this_thread);
100           break;
101        case LOG_LEVEL_FATAL:
102 -         outc = sprintf(outbuf, "Privoxy(%05ld) Fatal error: ", this_thread);
103 +         outc = sprintf(outbuf, "Privoxy(%08lx) Fatal error: ", this_thread);
104           break;
105        case LOG_LEVEL_GPC:
106 -         outc = sprintf(outbuf, "Privoxy(%05ld) Request: ", this_thread);
107 +         outc = sprintf(outbuf, "Privoxy(%08lx) Request: ", this_thread);
108           break;
109        case LOG_LEVEL_CONNECT:
110 -         outc = sprintf(outbuf, "Privoxy(%05ld) Connect: ", this_thread);
111 +         outc = sprintf(outbuf, "Privoxy(%08lx) Connect: ", this_thread);
112           break;
113        case LOG_LEVEL_LOG:
114 -         outc = sprintf(outbuf, "Privoxy(%05ld) Writing: ", this_thread);
115 +         outc = sprintf(outbuf, "Privoxy(%08lx) Writing: ", this_thread);
116           break;
117        case LOG_LEVEL_HEADER:
118 -         outc = sprintf(outbuf, "Privoxy(%05ld) Header: ", this_thread);
119 +         outc = sprintf(outbuf, "Privoxy(%08lx) Header: ", this_thread);
120           break;
121        case LOG_LEVEL_INFO:
122 -         outc = sprintf(outbuf, "Privoxy(%05ld) Info: ", this_thread);
123 +         outc = sprintf(outbuf, "Privoxy(%08lx) Info: ", this_thread);
124           break;
125        case LOG_LEVEL_RE_FILTER:
126 -         outc = sprintf(outbuf, "Privoxy(%05ld) Re-Filter: ", this_thread);
127 +         outc = sprintf(outbuf, "Privoxy(%08lx) Re-Filter: ", this_thread);
128           break;
129  #ifdef FEATURE_FORCE_LOAD
130        case LOG_LEVEL_FORCE:
131 -         outc = sprintf(outbuf, "Privoxy(%05ld) Force: ", this_thread);
132 +         outc = sprintf(outbuf, "Privoxy(%08lx) Force: ", this_thread);
133           break;
134  #endif /* def FEATURE_FORCE_LOAD */
135  #ifdef FEATURE_FAST_REDIRECTS
136        case LOG_LEVEL_REDIRECTS:
137 -         outc = sprintf(outbuf, "Privoxy(%05ld) Redirect: ", this_thread);
138 +         outc = sprintf(outbuf, "Privoxy(%08lx) Redirect: ", this_thread);
139           break;
140  #endif /* def FEATURE_FAST_REDIRECTS */
141        case LOG_LEVEL_DEANIMATE:
142 -         outc = sprintf(outbuf, "Privoxy(%05ld) Gif-Deanimate: ", this_thread);
143 +         outc = sprintf(outbuf, "Privoxy(%08lx) Gif-Deanimate: ", this_thread);
144           break;
145        case LOG_LEVEL_CLF:
146           outbuf = outbuf_save;
147 @@ -523,14 +556,14 @@
148           break;
149  #ifdef FEATURE_KILL_POPUPS
150        case LOG_LEVEL_POPUPS:
151 -         outc = sprintf(outbuf, "Privoxy(%05ld) Kill-Popups: ", this_thread);
152 +         outc = sprintf(outbuf, "Privoxy(%08lx) Kill-Popups: ", this_thread);
153           break;
154  #endif /* def FEATURE_KILL_POPUPS */
155        case LOG_LEVEL_CGI:
156 -         outc = sprintf(outbuf, "Privoxy(%05ld) CGI: ", this_thread);
157 +         outc = sprintf(outbuf, "Privoxy(%08lx) CGI: ", this_thread);
158           break;
159        default:
160 -         outc = sprintf(outbuf, "Privoxy(%05ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
161 +         outc = sprintf(outbuf, "Privoxy(%08lx) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
162           break;
163     }
164     
165 @@ -605,16 +638,14 @@
166              else
167              {
168                 /* Error */
169 -               sprintf(outbuf, "Privoxy(%ld) Error: log_error(): Bad format string:\n"
170 +               sprintf(outbuf, "Privoxy(%08lx) Error: log_error(): Bad format string:\n"
171                                 "Format = \"%s\"\n"
172                                 "Exiting.", this_thread, fmt);
173 -               /* FIXME RACE HAZARD: should start critical section error_log_use here */
174                 if( !logfp )
175                 {
176                    logfp = stderr;
177                 }
178                 fputs(outbuf, logfp);
179 -               /* FIXME RACE HAZARD: should end critical section error_log_use here */
180                 fatal_error(outbuf);
181                 /* Never get here */
182                 break;
183 @@ -768,16 +799,15 @@
184              }
185              break;
186           default:
187 -            sprintf(outbuf, "Privoxy(%ld) Error: log_error(): Bad format string:\n"
188 +            sprintf(outbuf, "Privoxy(%08lx) Error: log_error(): Bad format string:\n"
189                              "Format = \"%s\"\n"
190                              "Exiting.", this_thread, fmt);
191 -            /* FIXME RACE HAZARD: should start critical section error_log_use here */
192              if( !logfp )
193              {
194                 logfp = stderr;
195              }
196              fputs(outbuf_save, logfp);
197 -            /* FIXME RACE HAZARD: should end critical section error_log_use here */
198 +            unlock_logfile();
199              fatal_error(outbuf_save);
200              /* Never get here */
201              break;
202 @@ -814,8 +844,6 @@
203        outbuf[outc] = '\0';
204     }
205  
206 -   /* FIXME RACE HAZARD: should start critical section error_log_use here */
207 -
208     /* deal with glibc stupidity - it won't let you initialize logfp */
209     if( !logfp )
210     {
211 @@ -830,7 +858,7 @@
212        /* Never get here */
213     }
214  
215 -   /* FIXME RACE HAZARD: should end critical section error_log_use here */
216 +   unlock_logfile();
217  
218  #if defined(_WIN32) && !defined(_WIN_CONSOLE)
219     /* Write to display */
220 diff -urNad privoxy-dpatch~/jcc.c privoxy-dpatch/jcc.c
221 --- privoxy-dpatch~/jcc.c       2006-02-05 16:17:43.000000000 +0100
222 +++ privoxy-dpatch/jcc.c        2006-02-05 16:18:06.000000000 +0100
223 @@ -722,6 +722,10 @@
224  pthread_mutex_t gethostbyaddr_mutex;
225  pthread_mutex_t gethostbyname_mutex;
226  #endif /* def OSX_DARWIN */
227 +#ifdef FEATURE_PTHREAD
228 +pthread_mutex_t log_mutex;
229 +pthread_mutex_t log_init_mutex;
230 +#endif /* FEATURE_PTHREAD */
231  
232  #if defined(unix) || defined(__EMX__)
233  const char *basedir = NULL;
234 @@ -1923,7 +1927,10 @@
235     pthread_mutex_init(&gethostbyaddr_mutex,0);
236     pthread_mutex_init(&gethostbyname_mutex,0);
237  #endif /* def OSX_DARWIN */
238 -
239 +#ifdef FEATURE_PTHREAD
240 +       pthread_mutex_init(&log_mutex,0);
241 +       pthread_mutex_init(&log_init_mutex,0);
242 +#endif /* FEATURE_PTHREAD */
243     /*
244      * Unix signal handling
245      *
246 diff -urNad privoxy-dpatch~/jcc.h privoxy-dpatch/jcc.h
247 --- privoxy-dpatch~/jcc.h       2006-02-05 16:17:43.000000000 +0100
248 +++ privoxy-dpatch/jcc.h        2006-02-05 16:18:06.000000000 +0100
249 @@ -127,6 +127,10 @@
250  extern pthread_mutex_t gethostbyaddr_mutex;
251  extern pthread_mutex_t gethostbyname_mutex;
252  #endif /* def OSX_DARWIN */
253 +#ifdef FEATURE_PTHREAD
254 +extern pthread_mutex_t log_mutex;
255 +extern pthread_mutex_t log_init_mutex;
256 +#endif /* FEATURE_PTHREAD */
257  
258  /* Functions */
259