]> git.sesse.net Git - vlc/blob - bin/override.c
Qt is GPLv2+
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overridden 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 # include <unistd.h>
43 #endif
44 #ifdef NDEBUG
45 # undef HAVE_BACKTRACE
46 #endif
47
48 static bool override = false;
49
50 static void vlc_reset_override (void)
51 {
52     override = false;
53 }
54
55 void vlc_enable_override (void)
56 {
57     override = true;
58     pthread_atfork (NULL, NULL, vlc_reset_override);
59 }
60
61 static void vlogbug (unsigned *pc, const char *level, const char *func,
62                      const char *fmt, va_list ap)
63 {
64 #ifdef HAVE_BACKTRACE
65     const size_t framec = 5;
66     void *framev[framec];
67
68     backtrace (framev, framec);
69 #endif
70     flockfile (stderr);
71     if (*pc < MAX_ERRORS)
72     {
73         (*pc)++;
74         fprintf (stderr, "%s: call to %s(", level, func);
75         vfprintf (stderr, fmt, ap);
76         fputs (")\n", stderr);
77         fflush (stderr);
78 #ifdef HAVE_BACKTRACE
79         backtrace_symbols_fd (framev + 2, framec - 2, STDERR_FILENO);
80 #endif
81     }
82     funlockfile (stderr);
83 }
84
85 static void logbug (unsigned *pc, const char *level, const char *func,
86                     const char *fmt, ...)
87 {
88     va_list ap;
89
90     va_start (ap, fmt);
91     vlogbug (pc, level, func, fmt, ap);
92     va_end (ap);
93 }
94
95 static void *getsym (const char *name)
96 {
97     void *sym = dlsym (RTLD_NEXT, name);
98     if (sym == NULL)
99     {
100         fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
101                  dlerror ());
102         abort ();
103     }
104     return sym;
105 }
106
107 #define LOG(level, ...) \
108     do { \
109         static unsigned counter = 0; \
110         logbug(&counter, level, __func__, __VA_ARGS__); \
111     } while (0)
112
113 /* Evil non-standard GNU C macro ;)
114  *  typeof keyword,
115  *  statement-expression
116  */
117 #define CALL(func, ...) \
118 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
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 #if 0
194 /** Signals **/
195 #include <signal.h>
196
197 static bool blocked_signal (int num)
198 {
199     switch (num)
200     {
201         case SIGINT:
202         case SIGHUP:
203         case SIGQUIT:
204         case SIGTERM:
205         case SIGPIPE:
206         case SIGCHLD:
207             return true;
208     }
209     return false;
210 }
211
212 void (*signal (int signum, void (*handler) (int))) (int)
213 {
214     if (override)
215     {
216         if (handler != SIG_IGN && handler != SIG_DFL)
217             goto error;
218         if (!blocked_signal (signum))
219             goto error;
220         /* For our blocked signals, the handler won't matter much... */
221         if (handler == SIG_DFL)
222             LOG("Warning", "%d, SIG_DFL", signum, handler);
223     }
224     return CALL(signal, signum, handler);
225 error:
226     LOG("Blocked", "%d, %p", signum, handler);
227     return SIG_DFL;
228 }
229
230 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
231 {
232     if (override && act != NULL)
233     {
234         if ((act->sa_flags & SA_SIGINFO)
235          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
236             goto error;
237         if (act->sa_handler == SIG_DFL)
238             LOG("Warning", "%d, %p, SIG_DFL", signum, act);
239     }
240     return CALL(sigaction, signum, act, old);
241 error:
242     LOG("Blocked", "%d, %p, %p", signum, act, old);
243     return -1;
244 }
245 #endif
246
247
248 /*** Locales ***
249  * setlocale() is not thread-safe and has a tendency to crash other threads as
250  * quite many libc and libintl calls depend on the locale.
251  * Use uselocale() instead for thread-safety.
252  */
253 #include <locale.h>
254
255 char *setlocale (int cat, const char *locale)
256 {
257     if (override && locale != NULL)
258     {
259         LOG("Blocked", "%d, \"%s\"", cat, locale);
260         return NULL;
261     }
262     return CALL(setlocale, cat, locale);
263 }
264
265
266 /* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
267  * This caused quite nasty crashes in the history of VLC/Linux. */
268 char *strerror (int val)
269 {
270     if (override)
271     {
272         static const char msg[] =
273             "Error message unavailable (use strerror_r instead of strerror)!";
274         LOG("Blocked", "%d", val);
275         return (char *)msg;
276     }
277     return CALL(strerror, val);
278 }
279
280 /*** Xlib ****/
281 #ifdef HAVE_X11_XLIB_H
282 # include <X11/Xlib.h>
283
284 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
285
286 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
287      (Display *, XErrorEvent *)
288 {
289     if (override)
290     {
291         int (*ret) (Display *, XErrorEvent *);
292
293         pthread_mutex_lock (&xlib_lock);
294         LOG("Error", "%p", handler);
295         ret = CALL(XSetErrorHandler, handler);
296         pthread_mutex_unlock (&xlib_lock);
297         return ret;
298     }
299     return CALL(XSetErrorHandler, handler);
300 }
301
302 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
303 {
304     if (override)
305     {
306         int (*ret) (Display *);
307
308         pthread_mutex_lock (&xlib_lock);
309         LOG("Error", "%p", handler);
310         ret = CALL(XSetIOErrorHandler, handler);
311         pthread_mutex_unlock (&xlib_lock);
312         return ret;
313     }
314     return CALL(XSetIOErrorHandler, handler);
315 }
316 #endif
317 #else
318 void vlc_enable_override (void)
319 {
320 }
321 #endif