]> git.sesse.net Git - vlc/blob - bin/override.c
Remove the --user-agent hack
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overriden function calls for VLC media player
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdbool.h>
26 #define MAX_ERRORS 5
27
28 void vlc_enable_override (void);
29
30 #if defined (__GNUC__) \
31  && (defined (__ELF__) && !defined (__sun__))
32 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
33
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <dlfcn.h>
39 #include <pthread.h>
40 #ifdef HAVE_EXECINFO_H
41 # include <execinfo.h>
42 #endif
43 #ifdef NDEBUG
44 # undef HAVE_BACKTRACE
45 #endif
46
47 static bool override = false;
48
49 static void vlc_reset_override (void)
50 {
51     override = false;
52 }
53
54 void vlc_enable_override (void)
55 {
56     override = true;
57     pthread_atfork (NULL, NULL, vlc_reset_override);
58 }
59
60 static void vlogbug (unsigned *pc, const char *level, const char *func,
61                      const char *fmt, va_list ap)
62 {
63 #ifdef HAVE_BACKTRACE
64     const size_t framec = 5;
65     void *framev[framec];
66
67     backtrace (framev, framec);
68 #endif
69     flockfile (stderr);
70     if (*pc < MAX_ERRORS)
71     {
72         (*pc)++;
73         fprintf (stderr, "%s: call to %s(", level, func);
74         vfprintf (stderr, fmt, ap);
75         fputs (")\n", stderr);
76         fflush (stderr);
77 #ifdef HAVE_BACKTRACE
78         backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
79 #endif
80     }
81     funlockfile (stderr);
82 }
83
84 static void logbug (unsigned *pc, const char *level, const char *func,
85                     const char *fmt, ...)
86 {
87     va_list ap;
88
89     va_start (ap, fmt);
90     vlogbug (pc, level, func, fmt, ap);
91     va_end (ap);
92 }
93
94 static void *getsym (const char *name)
95 {
96     void *sym = dlsym (RTLD_NEXT, name);
97     if (sym == NULL)
98     {
99         fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
100                  dlerror ());
101         abort ();
102     }
103     return sym;
104 }
105
106 #define LOG(level, ...) \
107     do { \
108         static unsigned counter = 0; \
109         logbug(&counter, level, __func__, __VA_ARGS__); \
110     } while (0)
111
112 /* Evil non-standard GNU C macro ;)
113  *  typeof keyword,
114  *  statement-expression,
115  *  nested function...
116  */
117 #define CALL(func, ...) \
118 ({ \
119     static typeof (func) *sym = NULL; \
120     static pthread_once_t once = PTHREAD_ONCE_INIT; \
121     auto void getsym_once (void); \
122     void getsym_once (void) \
123     { \
124         sym = getsym ( # func); \
125     } \
126     pthread_once (&once, getsym_once); \
127     sym (__VA_ARGS__); \
128 })
129
130
131 /*** Environment ***
132  *
133  * "Conforming multi-threaded applications shall not use the environ variable
134  *  to access or modify any environment variable while any other thread is
135  *  concurrently modifying any environment variable." -- POSIX.
136  *
137  * Some evil libraries modify the environment. We currently ignore the calls as
138  * they could crash the process. This may cause funny behaviour though. */
139 int putenv (char *str)
140 {
141     if (override)
142     {
143         LOG("Blocked", "\"%s\"", str);
144         return 0;
145     }
146     return CALL(putenv, str);
147 }
148
149 int setenv (const char *name, const char *value, int overwrite)
150 {
151     if (override)
152     {
153         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
154         return 0;
155     }
156     return CALL(setenv, name, value, overwrite);
157 }
158
159 int unsetenv (const char *name)
160 {
161     if (override)
162     {
163         LOG("Blocked", "\"%s\"", name);
164         return 0;
165     }
166     return CALL(unsetenv, name);
167 }
168
169
170 /*** Pseudo random numbers ***
171  *
172  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
173  * is much better as a reproducible non-secure PRNG). To work around this, we
174  * force evil callers to serialize. This makes the call safe, but fails to
175  * preserve reproducibility of the number sequence (which usually does not
176  * matter).
177  **/
178 static struct
179 {
180     pthread_mutex_t lock;
181     unsigned int seed;
182 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
183
184 void srand (unsigned int seed)
185 {
186     pthread_mutex_lock (&prng.lock);
187     LOG("Warning", "%d", seed);
188     prng.seed = seed;
189     pthread_mutex_unlock (&prng.lock);
190 }
191
192 int rand (void)
193 {
194     int ret;
195
196     pthread_mutex_lock (&prng.lock);
197     LOG("Warning", "");
198     ret = rand_r (&prng.seed);
199     pthread_mutex_unlock (&prng.lock);
200     return ret;
201 }
202
203
204 /** Signals **/
205 #include <signal.h>
206
207 static bool blocked_signal (int num)
208 {
209     switch (num)
210     {
211         case SIGINT:
212         case SIGHUP:
213         case SIGQUIT:
214         case SIGTERM:
215         case SIGPIPE:
216         case SIGCHLD:
217             return true;
218     }
219     return false;
220 }
221
222 void (*signal (int signum, void (*handler) (int))) (int)
223 {
224     if (override)
225     {
226         if (handler != SIG_IGN && handler != SIG_DFL)
227             goto error;
228         if (!blocked_signal (signum))
229             goto error;
230         /* For our blocked signals, the handler won't matter much... */
231         if (handler == SIG_DFL)
232             LOG("Warning", "%d, SIG_DFL", signum, handler);
233     }
234     return CALL(signal, signum, handler);
235 error:
236     LOG("Blocked", "%d, %p", signum, handler);
237     return SIG_DFL;
238 }
239
240 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
241 {
242     if (override && act != NULL)
243     {
244         if ((act->sa_flags & SA_SIGINFO)
245          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
246             goto error;
247         if (act->sa_handler == SIG_DFL)
248             LOG("Warning", "%d, %p, SIG_DFL", signum, act);
249     }
250     return CALL(sigaction, signum, act, old);
251 error:
252     LOG("Blocked", "%d, %p, %p", signum, act, old);
253     return -1;
254 }
255
256
257 /*** Locales ***
258  * setlocale() is not thread-safe and has a tendency to crash other threads as
259  * quite many libc and libintl calls depend on the locale.
260  * Use uselocale() instead for thread-safety.
261  */
262 #include <locale.h>
263
264 char *setlocale (int cat, const char *locale)
265 {
266     if (override && locale != NULL)
267     {
268         LOG("Blocked", "%d, \"%s\"", cat, locale);
269         return NULL;
270     }
271     return CALL(setlocale, cat, locale);
272 }
273
274
275 /* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
276  * This caused quite nasty crashes in the history of VLC/Linux. */
277 char *strerror (int val)
278 {
279     if (override)
280     {
281         static const char msg[] =
282             "Error message unavailable (use strerror_r instead of strerror)!";
283         LOG("Blocked", "%d", val);
284         return (char *)msg;
285     }
286     return CALL(strerror, val);
287 }
288
289 /*** Xlib ****/
290 #ifdef HAVE_X11_XLIB_H
291 # include <X11/Xlib.h>
292
293 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
294
295 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
296      (Display *, XErrorEvent *)
297 {
298     if (override)
299     {
300         int (*ret) (Display *, XErrorEvent *);
301
302         pthread_mutex_lock (&xlib_lock);
303         LOG("Error", "%p", handler);
304         ret = CALL(XSetErrorHandler, handler);
305         pthread_mutex_unlock (&xlib_lock);
306         return ret;
307     }
308     return CALL(XSetErrorHandler, handler);
309 }
310
311 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
312 {
313     if (override)
314     {
315         int (*ret) (Display *);
316
317         pthread_mutex_lock (&xlib_lock);
318         LOG("Error", "%p", handler);
319         ret = CALL(XSetIOErrorHandler, handler);
320         pthread_mutex_unlock (&xlib_lock);
321         return ret;
322     }
323     return CALL(XSetIOErrorHandler, handler);
324 }
325 #endif
326 #else
327 void vlc_enable_override (void)
328 {
329 }
330 #endif