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