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__) \
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 /* Evil non-standard GNU C macro ;)
102 * statement-expression,
105 #define CALL(func, ...) \
107 static typeof (func) *sym = NULL; \
108 static pthread_once_t once = PTHREAD_ONCE_INIT; \
109 auto void getsym_once (void); \
110 void getsym_once (void) \
112 sym = getsym ( # func); \
114 pthread_once (&once, getsym_once); \
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.
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)
131 LOG("Blocked", "\"%s\"", str);
134 return CALL(putenv, str);
137 int setenv (const char *name, const char *value, int overwrite)
141 LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
144 return CALL(setenv, name, value, overwrite);
147 int unsetenv (const char *name)
151 LOG("Blocked", "\"%s\"", name);
154 return CALL(unsetenv, name);
158 /*** Pseudo random numbers ***
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
168 pthread_mutex_t lock;
170 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
172 void srand (unsigned int seed)
174 pthread_mutex_lock (&prng.lock);
175 LOG("Warning", "%d", seed);
177 pthread_mutex_unlock (&prng.lock);
184 pthread_mutex_lock (&prng.lock);
186 ret = rand_r (&prng.seed);
187 pthread_mutex_unlock (&prng.lock);
195 static bool blocked_signal (int num)
210 void (*signal (int signum, void (*handler) (int))) (int)
214 if (handler != SIG_IGN && handler != SIG_DFL)
216 if (!blocked_signal (signum))
218 /* For our blocked signals, the handler won't matter much... */
219 LOG("Warning", "%d, %p", signum, handler);
221 return CALL(signal, signum, handler);
223 LOG("Blocked", "%d, %p", signum, handler);
227 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
229 if (override && act != NULL)
231 if ((act->sa_flags & SA_SIGINFO)
232 || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
234 LOG("Warning", "%d, %p, %p", signum, act, old);
236 return CALL(sigaction, signum, act, old);
238 LOG("Blocked", "%d, %p, %p", signum, act, old);
244 * setlocale() is not thread-safe and has a tendency to crash other threads as
245 * quite many libc and libintl calls depend on the locale.
246 * Use uselocale() instead for thread-safety.
250 char *setlocale (int cat, const char *locale)
252 if (override && locale != NULL)
254 LOG("Blocked", "%d, \"%s\"", cat, locale);
257 return CALL(setlocale, cat, locale);
262 #ifdef HAVE_X11_XLIB_H
263 # include <X11/Xlib.h>
265 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
267 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
268 (Display *, XErrorEvent *)
272 int (*ret) (Display *, XErrorEvent *);
274 pthread_mutex_lock (&xlib_lock);
275 LOG("Error", "%p", handler);
276 ret = CALL(XSetErrorHandler, handler);
277 pthread_mutex_unlock (&xlib_lock);
280 return CALL(XSetErrorHandler, handler);
283 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
287 int (*ret) (Display *);
289 pthread_mutex_lock (&xlib_lock);
290 LOG("Error", "%p", handler);
291 ret = CALL(XSetIOErrorHandler, handler);
292 pthread_mutex_unlock (&xlib_lock);
295 return CALL(XSetIOErrorHandler, handler);
299 void vlc_enable_override (void)