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 *****************************************************************************/
27 void vlc_enable_override (void);
29 #if defined (__GNUC__) /* typeof and statement-expression */ \
30 && (defined (__ELF__) && !defined (__sun__))
31 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
38 #ifdef HAVE_EXECINFO_H
39 # include <execinfo.h>
42 # undef HAVE_BACKTRACE
45 static bool override = false;
47 static void vlc_reset_override (void)
52 void vlc_enable_override (void)
55 pthread_atfork (NULL, NULL, vlc_reset_override);
58 static void vlogbug (const char *level, const char *func, const char *fmt,
62 const size_t framec = 4;
65 backtrace (framev, framec);
68 fprintf (stderr, "%s: call to %s(", level, func);
69 vfprintf (stderr, fmt, ap);
70 fputs (")\n", stderr);
73 backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
78 static void logbug (const char *level, const char *func, const char *fmt, ...)
83 vlogbug (level, func, fmt, ap);
87 static void *getsym (const char *name)
89 void *sym = dlsym (RTLD_NEXT, name);
92 fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
99 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
100 #define CALL(func, ...) \
101 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
106 * "Conforming multi-threaded applications shall not use the environ variable
107 * to access or modify any environment variable while any other thread is
108 * concurrently modifying any environment variable." -- POSIX.
110 * Some evil libraries modify the environment. We currently ignore the calls as
111 * they could crash the process. This may cause funny behaviour though. */
112 int putenv (char *str)
116 LOG("Blocked", "\"%s\"", str);
119 return CALL(putenv, str);
122 int setenv (const char *name, const char *value, int overwrite)
126 LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
129 return CALL(setenv, name, value, overwrite);
132 int unsetenv (const char *name)
136 LOG("Blocked", "\"%s\"", name);
139 return CALL(unsetenv, name);
143 /*** Pseudo random numbers ***
145 * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
146 * is much better as a reproducible non-secure PRNG). To work around this, we
147 * force evil callers to serialize. This makes the call safe, but fails to
148 * preserve reproducibility of the number sequence (which usually does not
153 pthread_mutex_t lock;
155 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
157 void srand (unsigned int seed)
159 pthread_mutex_lock (&prng.lock);
160 LOG("Warning", "%d", seed);
162 pthread_mutex_unlock (&prng.lock);
169 pthread_mutex_lock (&prng.lock);
171 ret = rand_r (&prng.seed);
172 pthread_mutex_unlock (&prng.lock);
180 static bool blocked_signal (int num)
195 void (*signal (int signum, void (*handler) (int))) (int)
199 if (handler != SIG_IGN && handler != SIG_DFL)
201 if (!blocked_signal (signum))
203 /* For our blocked signals, the handler won't matter much... */
204 LOG("Warning", "%d, %p", signum, handler);
206 return CALL(signal, signum, handler);
208 LOG("Blocked", "%d, %p", signum, handler);
212 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
214 if (override && act != NULL)
216 if ((act->sa_flags & SA_SIGINFO)
217 || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
219 LOG("Warning", "%d, %p, %p", signum, act, old);
221 return CALL(sigaction, signum, act, old);
223 LOG("Blocked", "%d, %p, %p", signum, act, old);
229 * setlocale() is not thread-safe and has a tendency to crash other threads as
230 * quite many libc and libintl calls depend on the locale.
231 * Use uselocale() instead for thread-safety.
235 char *setlocale (int cat, const char *locale)
237 if (override && locale != NULL)
239 LOG("Blocked", "%d, \"%s\"", cat, locale);
242 return CALL(setlocale, cat, locale);
247 #ifdef HAVE_X11_XLIB_H
248 # include <X11/Xlib.h>
250 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
252 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
253 (Display *, XErrorEvent *)
257 int (*ret) (Display *, XErrorEvent *);
259 pthread_mutex_lock (&xlib_lock);
260 LOG("Error", "%p", handler);
261 ret = CALL(XSetErrorHandler, handler);
262 pthread_mutex_unlock (&xlib_lock);
265 return CALL(XSetErrorHandler, handler);
268 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
272 int (*ret) (Display *);
274 pthread_mutex_lock (&xlib_lock);
275 LOG("Error", "%p", handler);
276 ret = CALL(XSetIOErrorHandler, handler);
277 pthread_mutex_unlock (&xlib_lock);
280 return CALL(XSetIOErrorHandler, handler);
284 void vlc_enable_override (void)