]> git.sesse.net Git - vlc/blob - bin/override.c
f3eebae5a069ad596a87737bc751750a7b42f9d5
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overriden function calls for VLC media player
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdbool.h>
26
27 void vlc_enable_override (void);
28
29 #if defined (__GNUC__) \
30  && (defined (__ELF__) && !defined (__sun__))
31 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
32
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <dlfcn.h>
37 #include <pthread.h>
38 #ifdef HAVE_EXECINFO_H
39 # include <execinfo.h>
40 #endif
41 #ifdef NDEBUG
42 # undef HAVE_BACKTRACE
43 #endif
44
45 static bool override = false;
46
47 static void vlc_reset_override (void)
48 {
49     override = false;
50 }
51
52 void vlc_enable_override (void)
53 {
54     override = true;
55     pthread_atfork (NULL, NULL, vlc_reset_override);
56 }
57
58 static void vlogbug (const char *level, const char *func, const char *fmt,
59                      va_list ap)
60 {
61 #ifdef HAVE_BACKTRACE
62     const size_t framec = 4;
63     void *framev[framec];
64
65     backtrace (framev, framec);
66 #endif
67     flockfile (stderr);
68     fprintf (stderr, "%s: call to %s(", level, func);
69     vfprintf (stderr, fmt, ap);
70     fputs (")\n", stderr);
71     fflush (stderr);
72 #ifdef HAVE_BACKTRACE
73     backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
74 #endif
75     funlockfile (stderr);
76 }
77
78 static void logbug (const char *level, const char *func, const char *fmt, ...)
79 {
80     va_list ap;
81
82     va_start (ap, fmt);
83     vlogbug (level, func, fmt, ap);
84     va_end (ap);
85 }
86
87 static void *getsym (const char *name)
88 {
89     void *sym = dlsym (RTLD_NEXT, name);
90     if (sym == NULL)
91     {
92         fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
93                  dlerror ());
94         abort ();
95     }
96     return sym;
97 }
98
99 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
100 /* Evil non-standard GNU C macro ;)
101  *  typeof keyword,
102  *  statement-expression,
103  *  nested function...
104  */
105 #define CALL(func, ...) \
106 ({ \
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) \
111     { \
112         sym = getsym ( # func); \
113     } \
114     pthread_once (&once, getsym_once); \
115     sym (__VA_ARGS__); \
116 })
117
118
119 /*** Environment ***
120  *
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.
124  *
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)
128 {
129     if (override)
130     {
131         LOG("Blocked", "\"%s\"", str);
132         return 0;
133     }
134     return CALL(putenv, str);
135 }
136
137 int setenv (const char *name, const char *value, int overwrite)
138 {
139     if (override)
140     {
141         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
142         return 0;
143     }
144     return CALL(setenv, name, value, overwrite);
145 }
146
147 int unsetenv (const char *name)
148 {
149     if (override)
150     {
151         LOG("Blocked", "\"%s\"", name);
152         return 0;
153     }
154     return CALL(unsetenv, name);
155 }
156
157
158 /*** Pseudo random numbers ***
159  *
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
164  * matter).
165  **/
166 static struct
167 {
168     pthread_mutex_t lock;
169     unsigned int seed;
170 } prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
171
172 void srand (unsigned int seed)
173 {
174     pthread_mutex_lock (&prng.lock);
175     LOG("Warning", "%d", seed);
176     prng.seed = seed;
177     pthread_mutex_unlock (&prng.lock);
178 }
179
180 int rand (void)
181 {
182     int ret;
183
184     pthread_mutex_lock (&prng.lock);
185     LOG("Warning", "");
186     ret = rand_r (&prng.seed);
187     pthread_mutex_unlock (&prng.lock);
188     return ret;
189 }
190
191
192 /** Signals **/
193 #include <signal.h>
194
195 static bool blocked_signal (int num)
196 {
197     switch (num)
198     {
199         case SIGINT:
200         case SIGHUP:
201         case SIGQUIT:
202         case SIGTERM:
203         case SIGPIPE:
204         case SIGCHLD:
205             return true;
206     }
207     return false;
208 }
209
210 void (*signal (int signum, void (*handler) (int))) (int)
211 {
212     if (override)
213     {
214         if (handler != SIG_IGN && handler != SIG_DFL)
215             goto error;
216         if (!blocked_signal (signum))
217             goto error;
218         /* For our blocked signals, the handler won't matter much... */
219         if (handler == SIG_DFL)
220             LOG("Warning", "%d, SIG_DFL", signum, handler);
221     }
222     return CALL(signal, signum, handler);
223 error:
224     LOG("Blocked", "%d, %p", signum, handler);
225     return SIG_DFL;
226 }
227
228 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
229 {
230     if (override && act != NULL)
231     {
232         if ((act->sa_flags & SA_SIGINFO)
233          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
234             goto error;
235         if (act->sa_handler == SIG_DFL)
236             LOG("Warning", "%d, %p, SIG_DFL", signum, act);
237     }
238     return CALL(sigaction, signum, act, old);
239 error:
240     LOG("Blocked", "%d, %p, %p", signum, act, old);
241     return -1;
242 }
243
244
245 /*** Locales ***
246  * setlocale() is not thread-safe and has a tendency to crash other threads as
247  * quite many libc and libintl calls depend on the locale.
248  * Use uselocale() instead for thread-safety.
249  */
250 #include <locale.h>
251
252 char *setlocale (int cat, const char *locale)
253 {
254     if (override && locale != NULL)
255     {
256         LOG("Blocked", "%d, \"%s\"", cat, locale);
257         return NULL;
258     }
259     return CALL(setlocale, cat, locale);
260 }
261
262
263 /*** Xlib ****/
264 #ifdef HAVE_X11_XLIB_H
265 # include <X11/Xlib.h>
266
267 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
268
269 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
270      (Display *, XErrorEvent *)
271 {
272     if (override)
273     {
274         int (*ret) (Display *, XErrorEvent *);
275
276         pthread_mutex_lock (&xlib_lock);
277         LOG("Error", "%p", handler);
278         ret = CALL(XSetErrorHandler, handler);
279         pthread_mutex_unlock (&xlib_lock);
280         return ret;
281     }
282     return CALL(XSetErrorHandler, handler);
283 }
284
285 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
286 {
287     if (override)
288     {
289         int (*ret) (Display *);
290
291         pthread_mutex_lock (&xlib_lock);
292         LOG("Error", "%p", handler);
293         ret = CALL(XSetIOErrorHandler, handler);
294         pthread_mutex_unlock (&xlib_lock);
295         return ret;
296     }
297     return CALL(XSetIOErrorHandler, handler);
298 }
299 #endif
300 #else
301 void vlc_enable_override (void)
302 {
303 }
304 #endif