Fixed (un)selection of pcr pid in TS demuxer.
[vlc/solaris.git] / bin / override.c
blob5e72918e1d3f7a08a891a90492176995a97eb89c
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 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdbool.h>
26 #define MAX_ERRORS 5
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. */
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <dlfcn.h>
38 #include <pthread.h>
39 #ifdef HAVE_EXECINFO_H
40 # include <execinfo.h>
41 #endif
42 #ifdef NDEBUG
43 # undef HAVE_BACKTRACE
44 #endif
46 static bool override = false;
48 static void vlc_reset_override (void)
50 override = false;
53 void vlc_enable_override (void)
55 override = true;
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)
62 #ifdef HAVE_BACKTRACE
63 const size_t framec = 5;
64 void *framev[framec];
66 backtrace (framev, framec);
67 #endif
68 flockfile (stderr);
69 if (*pc < MAX_ERRORS)
71 (*pc)++;
72 fprintf (stderr, "%s: call to %s(", level, func);
73 vfprintf (stderr, fmt, ap);
74 fputs (")\n", stderr);
75 fflush (stderr);
76 #ifdef HAVE_BACKTRACE
77 backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
78 #endif
80 funlockfile (stderr);
83 static void logbug (unsigned *pc, const char *level, const char *func,
84 const char *fmt, ...)
86 va_list ap;
88 va_start (ap, fmt);
89 vlogbug (pc, level, func, fmt, ap);
90 va_end (ap);
93 static void *getsym (const char *name)
95 void *sym = dlsym (RTLD_NEXT, name);
96 if (sym == NULL)
98 fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
99 dlerror ());
100 abort ();
102 return sym;
105 #define LOG(level, ...) \
106 do { \
107 static unsigned counter = 0; \
108 logbug(&counter, level, __func__, __VA_ARGS__); \
109 } while (0)
111 #define CALL(func, ...) \
112 ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
115 /*** Environment ***
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)
125 if (override)
127 LOG("Blocked", "\"%s\"", str);
128 return 0;
130 return CALL(putenv, str);
133 int setenv (const char *name, const char *value, int overwrite)
135 if (override)
137 LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
138 return 0;
140 return CALL(setenv, name, value, overwrite);
143 int unsetenv (const char *name)
145 if (override)
147 LOG("Blocked", "\"%s\"", name);
148 return 0;
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
160 * matter).
162 static struct
164 pthread_mutex_t lock;
165 unsigned int seed;
166 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
168 void srand (unsigned int seed)
170 pthread_mutex_lock (&prng.lock);
171 LOG("Warning", "%d", seed);
172 prng.seed = seed;
173 pthread_mutex_unlock (&prng.lock);
176 int rand (void)
178 int ret;
180 pthread_mutex_lock (&prng.lock);
181 LOG("Warning", "");
182 ret = rand_r (&prng.seed);
183 pthread_mutex_unlock (&prng.lock);
184 return ret;
188 /** Signals **/
189 #include <signal.h>
191 static bool blocked_signal (int num)
193 switch (num)
195 case SIGINT:
196 case SIGHUP:
197 case SIGQUIT:
198 case SIGTERM:
199 case SIGPIPE:
200 case SIGCHLD:
201 return true;
203 return false;
206 void (*signal (int signum, void (*handler) (int))) (int)
208 if (override)
210 if (handler != SIG_IGN && handler != SIG_DFL)
211 goto error;
212 if (!blocked_signal (signum))
213 goto error;
214 /* For our blocked signals, the handler won't matter much... */
215 LOG("Warning", "%d, %p", signum, handler);
217 return CALL(signal, signum, handler);
218 error:
219 LOG("Blocked", "%d, %p", signum, handler);
220 return SIG_DFL;
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))
229 goto error;
230 LOG("Warning", "%d, %p, %p", signum, act, old);
232 return CALL(sigaction, signum, act, old);
233 error:
234 LOG("Blocked", "%d, %p, %p", signum, act, old);
235 return -1;
239 /*** Locales ***
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.
244 #include <locale.h>
246 char *setlocale (int cat, const char *locale)
248 if (override && locale != NULL)
250 LOG("Blocked", "%d, \"%s\"", cat, locale);
251 return NULL;
253 return CALL(setlocale, cat, locale);
257 /*** Xlib ****/
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 *)
266 if (override)
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);
274 return ret;
276 return CALL(XSetErrorHandler, handler);
279 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
281 if (override)
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);
289 return ret;
291 return CALL(XSetIOErrorHandler, handler);
293 #endif
295 /*** Buggy shared objects ***/
296 #include <string.h>
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);
305 #else
306 void vlc_enable_override (void)
309 #endif