]> git.sesse.net Git - vlc/blob - bin/override.c
winvlc: various cleaning and comments
[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  */
116 #define CALL(func, ...) \
117 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
118
119 /*** Environment ***
120  *
121  * "Conforming multi-threaded applications shall not use the environ variable
122  *  to access or modify any environment variable while any other thread is
123  *  concurrently modifying any environment variable." -- POSIX.
124  *
125  * Some evil libraries modify the environment. We currently ignore the calls as
126  * they could crash the process. This may cause funny behaviour though. */
127 int putenv (char *str)
128 {
129     if (override)
130     {
131         LOG("Blocked", "\"%s\"", str);
132         return 0;
133     }
134     return CALL(putenv, str);
135 }
136
137 int setenv (const char *name, const char *value, int overwrite)
138 {
139     if (override)
140     {
141         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
142         return 0;
143     }
144     return CALL(setenv, name, value, overwrite);
145 }
146
147 int unsetenv (const char *name)
148 {
149     if (override)
150     {
151         LOG("Blocked", "\"%s\"", name);
152         return 0;
153     }
154     return CALL(unsetenv, name);
155 }
156
157
158 /*** Pseudo random numbers ***
159  *
160  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
161  * is much better as a reproducible non-secure PRNG). To work around this, we
162  * force evil callers to serialize. This makes the call safe, but fails to
163  * preserve reproducibility of the number sequence (which usually does not
164  * matter).
165  **/
166 static struct
167 {
168     pthread_mutex_t lock;
169     unsigned int seed;
170 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
171
172 void srand (unsigned int seed)
173 {
174     pthread_mutex_lock (&prng.lock);
175     LOG("Warning", "%d", seed);
176     prng.seed = seed;
177     pthread_mutex_unlock (&prng.lock);
178 }
179
180 int rand (void)
181 {
182     int ret;
183
184     pthread_mutex_lock (&prng.lock);
185     LOG("Warning", "");
186     ret = rand_r (&prng.seed);
187     pthread_mutex_unlock (&prng.lock);
188     return ret;
189 }
190
191
192 #if 0
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 #endif
245
246
247 /*** Locales ***
248  * setlocale() is not thread-safe and has a tendency to crash other threads as
249  * quite many libc and libintl calls depend on the locale.
250  * Use uselocale() instead for thread-safety.
251  */
252 #include <locale.h>
253
254 char *setlocale (int cat, const char *locale)
255 {
256     if (override && locale != NULL)
257     {
258         LOG("Blocked", "%d, \"%s\"", cat, locale);
259         return NULL;
260     }
261     return CALL(setlocale, cat, locale);
262 }
263
264
265 /* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
266  * This caused quite nasty crashes in the history of VLC/Linux. */
267 char *strerror (int val)
268 {
269     if (override)
270     {
271         static const char msg[] =
272             "Error message unavailable (use strerror_r instead of strerror)!";
273         LOG("Blocked", "%d", val);
274         return (char *)msg;
275     }
276     return CALL(strerror, val);
277 }
278
279 /*** Xlib ****/
280 #ifdef HAVE_X11_XLIB_H
281 # include <X11/Xlib.h>
282
283 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
284
285 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
286      (Display *, XErrorEvent *)
287 {
288     if (override)
289     {
290         int (*ret) (Display *, XErrorEvent *);
291
292         pthread_mutex_lock (&xlib_lock);
293         LOG("Error", "%p", handler);
294         ret = CALL(XSetErrorHandler, handler);
295         pthread_mutex_unlock (&xlib_lock);
296         return ret;
297     }
298     return CALL(XSetErrorHandler, handler);
299 }
300
301 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
302 {
303     if (override)
304     {
305         int (*ret) (Display *);
306
307         pthread_mutex_lock (&xlib_lock);
308         LOG("Error", "%p", handler);
309         ret = CALL(XSetIOErrorHandler, handler);
310         pthread_mutex_unlock (&xlib_lock);
311         return ret;
312     }
313     return CALL(XSetIOErrorHandler, handler);
314 }
315 #endif
316 #else
317 void vlc_enable_override (void)
318 {
319 }
320 #endif