]> git.sesse.net Git - vlc/blob - bin/override.c
Provide a variant of the CALL macro which doesn't use the nested functions GNU C...
[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 #ifdef __clang__
113
114 #define CALL(func, ...) \
115 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
116
117 #else
118
119 /* Evil non-standard GNU C macro ;)
120  *  typeof keyword,
121  *  statement-expression,
122  *  nested function...
123  */
124 #define CALL(func, ...) \
125 ({ \
126     static typeof (func) *sym = NULL; \
127     static pthread_once_t once = PTHREAD_ONCE_INIT; \
128     auto void getsym_once (void); \
129     void getsym_once (void) \
130     { \
131         sym = getsym ( # func); \
132     } \
133     pthread_once (&once, getsym_once); \
134     sym (__VA_ARGS__); \
135 })
136
137 #endif
138
139 /*** Environment ***
140  *
141  * "Conforming multi-threaded applications shall not use the environ variable
142  *  to access or modify any environment variable while any other thread is
143  *  concurrently modifying any environment variable." -- POSIX.
144  *
145  * Some evil libraries modify the environment. We currently ignore the calls as
146  * they could crash the process. This may cause funny behaviour though. */
147 int putenv (char *str)
148 {
149     if (override)
150     {
151         LOG("Blocked", "\"%s\"", str);
152         return 0;
153     }
154     return CALL(putenv, str);
155 }
156
157 int setenv (const char *name, const char *value, int overwrite)
158 {
159     if (override)
160     {
161         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
162         return 0;
163     }
164     return CALL(setenv, name, value, overwrite);
165 }
166
167 int unsetenv (const char *name)
168 {
169     if (override)
170     {
171         LOG("Blocked", "\"%s\"", name);
172         return 0;
173     }
174     return CALL(unsetenv, name);
175 }
176
177
178 /*** Pseudo random numbers ***
179  *
180  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
181  * is much better as a reproducible non-secure PRNG). To work around this, we
182  * force evil callers to serialize. This makes the call safe, but fails to
183  * preserve reproducibility of the number sequence (which usually does not
184  * matter).
185  **/
186 static struct
187 {
188     pthread_mutex_t lock;
189     unsigned int seed;
190 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
191
192 void srand (unsigned int seed)
193 {
194     pthread_mutex_lock (&prng.lock);
195     LOG("Warning", "%d", seed);
196     prng.seed = seed;
197     pthread_mutex_unlock (&prng.lock);
198 }
199
200 int rand (void)
201 {
202     int ret;
203
204     pthread_mutex_lock (&prng.lock);
205     LOG("Warning", "");
206     ret = rand_r (&prng.seed);
207     pthread_mutex_unlock (&prng.lock);
208     return ret;
209 }
210
211
212 #if 0
213 /** Signals **/
214 #include <signal.h>
215
216 static bool blocked_signal (int num)
217 {
218     switch (num)
219     {
220         case SIGINT:
221         case SIGHUP:
222         case SIGQUIT:
223         case SIGTERM:
224         case SIGPIPE:
225         case SIGCHLD:
226             return true;
227     }
228     return false;
229 }
230
231 void (*signal (int signum, void (*handler) (int))) (int)
232 {
233     if (override)
234     {
235         if (handler != SIG_IGN && handler != SIG_DFL)
236             goto error;
237         if (!blocked_signal (signum))
238             goto error;
239         /* For our blocked signals, the handler won't matter much... */
240         if (handler == SIG_DFL)
241             LOG("Warning", "%d, SIG_DFL", signum, handler);
242     }
243     return CALL(signal, signum, handler);
244 error:
245     LOG("Blocked", "%d, %p", signum, handler);
246     return SIG_DFL;
247 }
248
249 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
250 {
251     if (override && act != NULL)
252     {
253         if ((act->sa_flags & SA_SIGINFO)
254          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
255             goto error;
256         if (act->sa_handler == SIG_DFL)
257             LOG("Warning", "%d, %p, SIG_DFL", signum, act);
258     }
259     return CALL(sigaction, signum, act, old);
260 error:
261     LOG("Blocked", "%d, %p, %p", signum, act, old);
262     return -1;
263 }
264 #endif
265
266
267 /*** Locales ***
268  * setlocale() is not thread-safe and has a tendency to crash other threads as
269  * quite many libc and libintl calls depend on the locale.
270  * Use uselocale() instead for thread-safety.
271  */
272 #include <locale.h>
273
274 char *setlocale (int cat, const char *locale)
275 {
276     if (override && locale != NULL)
277     {
278         LOG("Blocked", "%d, \"%s\"", cat, locale);
279         return NULL;
280     }
281     return CALL(setlocale, cat, locale);
282 }
283
284
285 /* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
286  * This caused quite nasty crashes in the history of VLC/Linux. */
287 char *strerror (int val)
288 {
289     if (override)
290     {
291         static const char msg[] =
292             "Error message unavailable (use strerror_r instead of strerror)!";
293         LOG("Blocked", "%d", val);
294         return (char *)msg;
295     }
296     return CALL(strerror, val);
297 }
298
299 /*** Xlib ****/
300 #ifdef HAVE_X11_XLIB_H
301 # include <X11/Xlib.h>
302
303 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
304
305 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
306      (Display *, XErrorEvent *)
307 {
308     if (override)
309     {
310         int (*ret) (Display *, XErrorEvent *);
311
312         pthread_mutex_lock (&xlib_lock);
313         LOG("Error", "%p", handler);
314         ret = CALL(XSetErrorHandler, handler);
315         pthread_mutex_unlock (&xlib_lock);
316         return ret;
317     }
318     return CALL(XSetErrorHandler, handler);
319 }
320
321 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
322 {
323     if (override)
324     {
325         int (*ret) (Display *);
326
327         pthread_mutex_lock (&xlib_lock);
328         LOG("Error", "%p", handler);
329         ret = CALL(XSetIOErrorHandler, handler);
330         pthread_mutex_unlock (&xlib_lock);
331         return ret;
332     }
333     return CALL(XSetIOErrorHandler, handler);
334 }
335 #endif
336 #else
337 void vlc_enable_override (void)
338 {
339 }
340 #endif