1 /*****************************************************************************
2 * override.c: overriden function calls for VLC media player
3 *****************************************************************************
4 * Copyright (C) 2010 RĂ©mi Denis-Courmont
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.
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.
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 *****************************************************************************/
28 void vlc_enable_override (void);
30 #if defined (__GNUC__) \
31 && (defined (__ELF__) && !defined (__sun__))
32 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
40 #ifdef HAVE_EXECINFO_H
41 # include <execinfo.h>
44 # undef HAVE_BACKTRACE
47 static bool override
= false;
49 static void vlc_reset_override (void)
54 void vlc_enable_override (void)
57 pthread_atfork (NULL
, NULL
, vlc_reset_override
);
60 static void vlogbug (unsigned *pc
, const char *level
, const char *func
,
61 const char *fmt
, va_list ap
)
64 const size_t framec
= 5;
67 backtrace (framev
, framec
);
73 fprintf (stderr
, "%s: call to %s(", level
, func
);
74 vfprintf (stderr
, fmt
, ap
);
75 fputs (")\n", stderr
);
78 backtrace_symbols_fd (framev
+ 2, framec
- 2, fileno (stderr
));
84 static void logbug (unsigned *pc
, const char *level
, const char *func
,
90 vlogbug (pc
, level
, func
, fmt
, ap
);
94 static void *getsym (const char *name
)
96 void *sym
= dlsym (RTLD_NEXT
, name
);
99 fprintf (stderr
, "Cannot resolve symbol %s: %s\n", name
,
106 #define LOG(level, ...) \
108 static unsigned counter = 0; \
109 logbug(&counter, level, __func__, __VA_ARGS__); \
112 /* Evil non-standard GNU C macro ;)
114 * statement-expression,
117 #define CALL(func, ...) \
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) \
124 sym = getsym ( # func); \
126 pthread_once (&once, getsym_once); \
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.
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
)
143 LOG("Blocked", "\"%s\"", str
);
146 return CALL(putenv
, str
);
149 int setenv (const char *name
, const char *value
, int overwrite
)
153 LOG("Blocked", "\"%s\", \"%s\", %d", name
, value
, overwrite
);
156 return CALL(setenv
, name
, value
, overwrite
);
159 int unsetenv (const char *name
)
163 LOG("Blocked", "\"%s\"", name
);
166 return CALL(unsetenv
, name
);
170 /*** Pseudo random numbers ***
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
180 pthread_mutex_t lock
;
182 } prng
= { PTHREAD_MUTEX_INITIALIZER
, 0, };
184 void srand (unsigned int seed
)
186 pthread_mutex_lock (&prng
.lock
);
187 LOG("Warning", "%d", seed
);
189 pthread_mutex_unlock (&prng
.lock
);
196 pthread_mutex_lock (&prng
.lock
);
198 ret
= rand_r (&prng
.seed
);
199 pthread_mutex_unlock (&prng
.lock
);
208 static bool blocked_signal (int num
)
223 void (*signal (int signum
, void (*handler
) (int))) (int)
227 if (handler
!= SIG_IGN
&& handler
!= SIG_DFL
)
229 if (!blocked_signal (signum
))
231 /* For our blocked signals, the handler won't matter much... */
232 if (handler
== SIG_DFL
)
233 LOG("Warning", "%d, SIG_DFL", signum
, handler
);
235 return CALL(signal
, signum
, handler
);
237 LOG("Blocked", "%d, %p", signum
, handler
);
241 int sigaction (int signum
, const struct sigaction
*act
, struct sigaction
*old
)
243 if (override
&& act
!= NULL
)
245 if ((act
->sa_flags
& SA_SIGINFO
)
246 || (act
->sa_handler
!= SIG_IGN
&& act
->sa_handler
!= SIG_DFL
))
248 if (act
->sa_handler
== SIG_DFL
)
249 LOG("Warning", "%d, %p, SIG_DFL", signum
, act
);
251 return CALL(sigaction
, signum
, act
, old
);
253 LOG("Blocked", "%d, %p, %p", signum
, act
, old
);
260 * setlocale() is not thread-safe and has a tendency to crash other threads as
261 * quite many libc and libintl calls depend on the locale.
262 * Use uselocale() instead for thread-safety.
266 char *setlocale (int cat
, const char *locale
)
268 if (override
&& locale
!= NULL
)
270 LOG("Blocked", "%d, \"%s\"", cat
, locale
);
273 return CALL(setlocale
, cat
, locale
);
277 /* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
278 * This caused quite nasty crashes in the history of VLC/Linux. */
279 char *strerror (int val
)
283 static const char msg
[] =
284 "Error message unavailable (use strerror_r instead of strerror)!";
285 LOG("Blocked", "%d", val
);
288 return CALL(strerror
, val
);
292 #ifdef HAVE_X11_XLIB_H
293 # include <X11/Xlib.h>
295 static pthread_mutex_t xlib_lock
= PTHREAD_MUTEX_INITIALIZER
;
297 int (*XSetErrorHandler (int (*handler
) (Display
*, XErrorEvent
*)))
298 (Display
*, XErrorEvent
*)
302 int (*ret
) (Display
*, XErrorEvent
*);
304 pthread_mutex_lock (&xlib_lock
);
305 LOG("Error", "%p", handler
);
306 ret
= CALL(XSetErrorHandler
, handler
);
307 pthread_mutex_unlock (&xlib_lock
);
310 return CALL(XSetErrorHandler
, handler
);
313 int (*XSetIOErrorHandler (int (*handler
) (Display
*))) (Display
*)
317 int (*ret
) (Display
*);
319 pthread_mutex_lock (&xlib_lock
);
320 LOG("Error", "%p", handler
);
321 ret
= CALL(XSetIOErrorHandler
, handler
);
322 pthread_mutex_unlock (&xlib_lock
);
325 return CALL(XSetIOErrorHandler
, handler
);
329 void vlc_enable_override (void)