]> git.sesse.net Git - vlc/blob - bin/override.c
2e8919925b02828a00cb47d8b014e39589c28eb9
[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__) /* typeof and statement-expression */ \
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!\n", name);
93         abort ();
94     }
95     return sym;
96 }
97
98 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
99 #define CALL(func, ...) \
100     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
101
102
103 /*** Environment ***
104  *
105  * "Conforming multi-threaded applications shall not use the environ variable
106  *  to access or modify any environment variable while any other thread is
107  *  concurrently modifying any environment variable." -- POSIX.
108  *
109  * Some evil libraries modify the environment. We currently ignore the calls as
110  * they could crash the process. This may cause funny behaviour though. */
111 int putenv (char *str)
112 {
113     if (override)
114     {
115         LOG("Blocked", "\"%s\"", str);
116         return 0;
117     }
118     return CALL(putenv, str);
119 }
120
121 int setenv (const char *name, const char *value, int overwrite)
122 {
123     if (override)
124     {
125         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
126         return 0;
127     }
128     return CALL(setenv, name, value, overwrite);
129 }
130
131 int unsetenv (const char *name)
132 {
133     if (override)
134     {
135         LOG("Blocked", "\"%s\"", name);
136         return 0;
137     }
138     return CALL(unsetenv, name);
139 }
140
141
142 /*** Pseudo random numbers ***
143  *
144  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
145  * is much better as a reproducible non-secure PRNG). To work around this, we
146  * force evil callers to serialize. This makes the call safe, but fails to
147  * preserve reproducibility of the number sequence (which usually does not
148  * matter).
149  **/
150 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
151
152 void srand (unsigned int seed)
153 {
154     pthread_mutex_lock (&prng_lock);
155     LOG("Warning", "%d", seed);
156     CALL(srand, seed);
157     pthread_mutex_unlock (&prng_lock);
158 }
159
160 int rand (void)
161 {
162     int ret;
163
164     pthread_mutex_lock (&prng_lock);
165     LOG("Warning", "");
166     ret = CALL(rand);
167     pthread_mutex_unlock (&prng_lock);
168     return ret;
169 }
170
171
172 /** Signals **/
173 #include <signal.h>
174
175 void (*signal (int signum, void (*handler) (int))) (int)
176 {
177     if (override)
178     {
179         const char *msg = "Error";
180
181         if ((signum == SIGPIPE && handler == SIG_IGN)
182          || (signum != SIGPIPE && handler == SIG_DFL))
183             /* Same settings we already use */
184             msg = "Warning";
185         LOG(msg, "%d, %p", signum, handler);
186     }
187     return CALL(signal, signum, handler);
188 }
189
190 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
191 {
192     if (act != NULL)
193         LOG("Error", "%d, %p, %p", signum, act, old);
194     return CALL(sigaction, signum, act, old);
195 }
196
197
198 /*** Xlib ****/
199 #ifdef HAVE_X11_XLIB_H
200 # include <X11/Xlib.h>
201
202 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
203
204 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
205      (Display *, XErrorEvent *)
206 {
207     if (override)
208     {
209         int (*ret) (Display *, XErrorEvent *);
210
211         pthread_mutex_lock (&xlib_lock);
212         LOG("Error", "%p", handler);
213         ret = CALL(XSetErrorHandler, handler);
214         pthread_mutex_unlock (&xlib_lock);
215         return ret;
216     }
217     return CALL(XSetErrorHandler, handler);
218 }
219
220 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
221 {
222     if (override)
223     {
224         int (*ret) (Display *);
225
226         pthread_mutex_lock (&xlib_lock);
227         LOG("Error", "%p", handler);
228         ret = CALL(XSetIOErrorHandler, handler);
229         pthread_mutex_unlock (&xlib_lock);
230         return ret;
231     }
232     return CALL(XSetIOErrorHandler, handler);
233 }
234 #endif
235 #else
236 void vlc_enable_override (void)
237 {
238 }
239 #endif