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__) /* typeof and statement-expression */ \
31 && (defined (__ELF__) && !defined (__sun__))
32 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
39 #ifdef HAVE_EXECINFO_H
40 # include <execinfo.h>
43 # undef HAVE_BACKTRACE
46 static bool override
= false;
48 static void vlc_reset_override (void)
53 void vlc_enable_override (void)
56 pthread_atfork (NULL
, NULL
, vlc_reset_override
);
59 static void vlogbug (unsigned *pc
, const char *level
, const char *func
,
60 const char *fmt
, va_list ap
)
63 const size_t framec
= 5;
66 backtrace (framev
, framec
);
72 fprintf (stderr
, "%s: call to %s(", level
, func
);
73 vfprintf (stderr
, fmt
, ap
);
74 fputs (")\n", stderr
);
77 backtrace_symbols_fd (framev
+ 2, framec
- 2, fileno (stderr
));
83 static void logbug (unsigned *pc
, const char *level
, const char *func
,
89 vlogbug (pc
, level
, func
, fmt
, ap
);
93 static void *getsym (const char *name
)
95 void *sym
= dlsym (RTLD_NEXT
, name
);
98 fprintf (stderr
, "Cannot resolve symbol %s: %s\n", name
,
105 #define LOG(level, ...) \
107 static unsigned counter = 0; \
108 logbug(&counter, level, __func__, __VA_ARGS__); \
111 #define CALL(func, ...) \
112 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
117 * "Conforming multi-threaded applications shall not use the environ variable
118 * to access or modify any environment variable while any other thread is
119 * concurrently modifying any environment variable." -- POSIX.
121 * Some evil libraries modify the environment. We currently ignore the calls as
122 * they could crash the process. This may cause funny behaviour though. */
123 int putenv (char *str
)
127 LOG("Blocked", "\"%s\"", str
);
130 return CALL(putenv
, str
);
133 int setenv (const char *name
, const char *value
, int overwrite
)
137 LOG("Blocked", "\"%s\", \"%s\", %d", name
, value
, overwrite
);
140 return CALL(setenv
, name
, value
, overwrite
);
143 int unsetenv (const char *name
)
147 LOG("Blocked", "\"%s\"", name
);
150 return CALL(unsetenv
, name
);
154 /*** Pseudo random numbers ***
156 * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
157 * is much better as a reproducible non-secure PRNG). To work around this, we
158 * force evil callers to serialize. This makes the call safe, but fails to
159 * preserve reproducibility of the number sequence (which usually does not
164 pthread_mutex_t lock
;
166 } prng
= { PTHREAD_MUTEX_INITIALIZER
, 0, };
168 void srand (unsigned int seed
)
170 pthread_mutex_lock (&prng
.lock
);
171 LOG("Warning", "%d", seed
);
173 pthread_mutex_unlock (&prng
.lock
);
180 pthread_mutex_lock (&prng
.lock
);
182 ret
= rand_r (&prng
.seed
);
183 pthread_mutex_unlock (&prng
.lock
);
191 static bool blocked_signal (int num
)
206 void (*signal (int signum
, void (*handler
) (int))) (int)
210 if (handler
!= SIG_IGN
&& handler
!= SIG_DFL
)
212 if (!blocked_signal (signum
))
214 /* For our blocked signals, the handler won't matter much... */
215 LOG("Warning", "%d, %p", signum
, handler
);
217 return CALL(signal
, signum
, handler
);
219 LOG("Blocked", "%d, %p", signum
, handler
);
223 int sigaction (int signum
, const struct sigaction
*act
, struct sigaction
*old
)
225 if (override
&& act
!= NULL
)
227 if ((act
->sa_flags
& SA_SIGINFO
)
228 || (act
->sa_handler
!= SIG_IGN
&& act
->sa_handler
!= SIG_DFL
))
230 LOG("Warning", "%d, %p, %p", signum
, act
, old
);
232 return CALL(sigaction
, signum
, act
, old
);
234 LOG("Blocked", "%d, %p, %p", signum
, act
, old
);
240 * setlocale() is not thread-safe and has a tendency to crash other threads as
241 * quite many libc and libintl calls depend on the locale.
242 * Use uselocale() instead for thread-safety.
246 char *setlocale (int cat
, const char *locale
)
248 if (override
&& locale
!= NULL
)
250 LOG("Blocked", "%d, \"%s\"", cat
, locale
);
253 return CALL(setlocale
, cat
, locale
);
258 #ifdef HAVE_X11_XLIB_H
259 # include <X11/Xlib.h>
261 static pthread_mutex_t xlib_lock
= PTHREAD_MUTEX_INITIALIZER
;
263 int (*XSetErrorHandler (int (*handler
) (Display
*, XErrorEvent
*)))
264 (Display
*, XErrorEvent
*)
268 int (*ret
) (Display
*, XErrorEvent
*);
270 pthread_mutex_lock (&xlib_lock
);
271 LOG("Error", "%p", handler
);
272 ret
= CALL(XSetErrorHandler
, handler
);
273 pthread_mutex_unlock (&xlib_lock
);
276 return CALL(XSetErrorHandler
, handler
);
279 int (*XSetIOErrorHandler (int (*handler
) (Display
*))) (Display
*)
283 int (*ret
) (Display
*);
285 pthread_mutex_lock (&xlib_lock
);
286 LOG("Error", "%p", handler
);
287 ret
= CALL(XSetIOErrorHandler
, handler
);
288 pthread_mutex_unlock (&xlib_lock
);
291 return CALL(XSetIOErrorHandler
, handler
);
295 /*** Buggy shared objects ***/
298 void *dlopen (const char *path
, int flag
)
300 if (path
&& strstr (path
, "/gui_platform/libkde.so"))
301 return NULL
; /* Oh no, not that one! */
302 return CALL(dlopen
, path
, flag
);
306 void vlc_enable_override (void)