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 static bool override = false;
31 void vlc_enable_override (void)
36 #if defined (__GNUC__) /* typeof and statement-expression */ \
37 && (defined (__ELF__) && !defined (__sun__))
38 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
45 #ifdef HAVE_EXECINFO_H
46 # include <execinfo.h>
49 static void vlogbug (const char *level, const char *func, const char *fmt,
53 const size_t framec = 8;
56 backtrace (framev, framec);
59 fprintf (stderr, "%s: call to %s(", level, func);
60 vfprintf (stderr, fmt, ap);
61 fputs (")\n", stderr);
64 backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
69 static void logbug (const char *level, const char *func, const char *fmt, ...)
74 vlogbug (level, func, fmt, ap);
78 static void *getsym (const char *name)
80 void *sym = dlsym (RTLD_NEXT, name);
83 fprintf (stderr, "Cannot resolve symbol %s!\n", name);
89 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
90 #define CALL(func, ...) \
91 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
96 * "Conforming multi-threaded applications shall not use the environ variable
97 * to access or modify any environment variable while any other thread is
98 * concurrently modifying any environment variable." -- POSIX.
100 * Some evil libraries modify the environment. We currently ignore the calls as
101 * they could crash the process. This may cause funny behaviour though. */
102 int putenv (char *str)
106 LOG("Blocked", "\"%s\"", str);
109 return CALL(putenv, str);
112 int setenv (const char *name, const char *value, int overwrite)
116 LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
119 return CALL(setenv, name, value, overwrite);
122 int unsetenv (const char *name)
126 LOG("Blocked", "\"%s\"", name);
129 return CALL(unsetenv, name);
133 /*** Pseudo random numbers ***
135 * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
136 * is much better as a reproducible non-secure PRNG). To work around this, we
137 * force evil callers to serialize. This makes the call safe, but fails to
138 * preserve reproducibility of the number sequence (which usually does not
141 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
143 void srand (unsigned int seed)
145 pthread_mutex_lock (&prng_lock);
146 LOG("Warning", "%d", seed);
148 pthread_mutex_unlock (&prng_lock);
155 pthread_mutex_lock (&prng_lock);
158 pthread_mutex_unlock (&prng_lock);
166 void (*signal (int signum, void (*handler) (int))) (int)
170 const char *msg = "Error";
172 if ((signum == SIGPIPE && handler == SIG_IGN)
173 || (signum != SIGPIPE && handler == SIG_DFL))
174 /* Same settings we already use */
176 LOG(msg, "%d, %p", signum, handler);
178 return CALL(signal, signum, handler);
181 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
184 LOG("Error", "%d, %p, %p", signum, act, old);
185 return CALL(sigaction, signum, act, old);