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 static bool override = false;
44 static void vlc_reset_override (void)
49 void vlc_enable_override (void)
52 pthread_atfork (NULL, NULL, vlc_reset_override);
55 static void vlogbug (const char *level, const char *func, const char *fmt,
59 const size_t framec = 8;
62 backtrace (framev, framec);
65 fprintf (stderr, "%s: call to %s(", level, func);
66 vfprintf (stderr, fmt, ap);
67 fputs (")\n", stderr);
70 backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
75 static void logbug (const char *level, const char *func, const char *fmt, ...)
80 vlogbug (level, func, fmt, ap);
84 static void *getsym (const char *name)
86 void *sym = dlsym (RTLD_NEXT, name);
89 fprintf (stderr, "Cannot resolve symbol %s!\n", name);
95 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
96 #define CALL(func, ...) \
97 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
102 * "Conforming multi-threaded applications shall not use the environ variable
103 * to access or modify any environment variable while any other thread is
104 * concurrently modifying any environment variable." -- POSIX.
106 * Some evil libraries modify the environment. We currently ignore the calls as
107 * they could crash the process. This may cause funny behaviour though. */
108 int putenv (char *str)
112 LOG("Blocked", "\"%s\"", str);
115 return CALL(putenv, str);
118 int setenv (const char *name, const char *value, int overwrite)
122 LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
125 return CALL(setenv, name, value, overwrite);
128 int unsetenv (const char *name)
132 LOG("Blocked", "\"%s\"", name);
135 return CALL(unsetenv, name);
139 /*** Pseudo random numbers ***
141 * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
142 * is much better as a reproducible non-secure PRNG). To work around this, we
143 * force evil callers to serialize. This makes the call safe, but fails to
144 * preserve reproducibility of the number sequence (which usually does not
147 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
149 void srand (unsigned int seed)
151 pthread_mutex_lock (&prng_lock);
152 LOG("Warning", "%d", seed);
154 pthread_mutex_unlock (&prng_lock);
161 pthread_mutex_lock (&prng_lock);
164 pthread_mutex_unlock (&prng_lock);
172 void (*signal (int signum, void (*handler) (int))) (int)
176 const char *msg = "Error";
178 if ((signum == SIGPIPE && handler == SIG_IGN)
179 || (signum != SIGPIPE && handler == SIG_DFL))
180 /* Same settings we already use */
182 LOG(msg, "%d, %p", signum, handler);
184 return CALL(signal, signum, handler);
187 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
190 LOG("Error", "%d, %p, %p", signum, act, old);
191 return CALL(sigaction, signum, act, old);
196 #ifdef HAVE_X11_XLIB_H
197 # include <X11/Xlib.h>
199 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
201 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
202 (Display *, XErrorEvent *)
206 int (*ret) (Display *, XErrorEvent *);
208 pthread_mutex_lock (&xlib_lock);
209 LOG("Error", "%p", handler);
210 ret = CALL(XSetErrorHandler, handler);
211 pthread_mutex_unlock (&xlib_lock);
214 return CALL(XSetErrorHandler, handler);
217 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
221 int (*ret) (Display *);
223 pthread_mutex_lock (&xlib_lock);
224 LOG("Error", "%p", handler);
225 ret = CALL(XSetIOErrorHandler, handler);
226 pthread_mutex_unlock (&xlib_lock);
229 return CALL(XSetIOErrorHandler, handler);
233 static void vlc_enable_override (void)