]> git.sesse.net Git - vlc/blob - bin/override.c
Safety checks for X11 error handlers
[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
42 static bool override = false;
43
44 static void vlc_reset_override (void)
45 {
46     override = false;
47 }
48
49 void vlc_enable_override (void)
50 {
51     override = true;
52     pthread_atfork (NULL, NULL, vlc_reset_override);
53 }
54
55 static void vlogbug (const char *level, const char *func, const char *fmt,
56                      va_list ap)
57 {
58 #ifdef HAVE_BACKTRACE
59     const size_t framec = 8;
60     void *framev[framec];
61
62     backtrace (framev, framec);
63 #endif
64     flockfile (stderr);
65     fprintf (stderr, "%s: call to %s(", level, func);
66     vfprintf (stderr, fmt, ap);
67     fputs (")\n", stderr);
68     fflush (stderr);
69 #ifdef HAVE_BACKTRACE
70     backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
71 #endif
72     funlockfile (stderr);
73 }
74
75 static void logbug (const char *level, const char *func, const char *fmt, ...)
76 {
77     va_list ap;
78
79     va_start (ap, fmt);
80     vlogbug (level, func, fmt, ap);
81     va_end (ap);
82 }
83
84 static void *getsym (const char *name)
85 {
86     void *sym = dlsym (RTLD_NEXT, name);
87     if (sym == NULL)
88     {
89         fprintf (stderr, "Cannot resolve symbol %s!\n", name);
90         abort ();
91     }
92     return sym;
93 }
94
95 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
96 #define CALL(func, ...) \
97     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
98
99
100 /*** Environment ***
101  *
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.
105  *
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)
109 {
110     if (override)
111     {
112         LOG("Blocked", "\"%s\"", str);
113         return 0;
114     }
115     return CALL(putenv, str);
116 }
117
118 int setenv (const char *name, const char *value, int overwrite)
119 {
120     if (override)
121     {
122         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
123         return 0;
124     }
125     return CALL(setenv, name, value, overwrite);
126 }
127
128 int unsetenv (const char *name)
129 {
130     if (override)
131     {
132         LOG("Blocked", "\"%s\"", name);
133         return 0;
134     }
135     return CALL(unsetenv, name);
136 }
137
138
139 /*** Pseudo random numbers ***
140  *
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
145  * matter).
146  **/
147 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
148
149 void srand (unsigned int seed)
150 {
151     pthread_mutex_lock (&prng_lock);
152     LOG("Warning", "%d", seed);
153     CALL(srand, seed);
154     pthread_mutex_unlock (&prng_lock);
155 }
156
157 int rand (void)
158 {
159     int ret;
160
161     pthread_mutex_lock (&prng_lock);
162     LOG("Warning", "");
163     ret = CALL(rand);
164     pthread_mutex_unlock (&prng_lock);
165     return ret;
166 }
167
168
169 /** Signals **/
170 #include <signal.h>
171
172 void (*signal (int signum, void (*handler) (int))) (int)
173 {
174     if (override)
175     {
176         const char *msg = "Error";
177
178         if ((signum == SIGPIPE && handler == SIG_IGN)
179          || (signum != SIGPIPE && handler == SIG_DFL))
180             /* Same settings we already use */
181             msg = "Warning";
182         LOG(msg, "%d, %p", signum, handler);
183     }
184     return CALL(signal, signum, handler);
185 }
186
187 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
188 {
189     if (act != NULL)
190         LOG("Error", "%d, %p, %p", signum, act, old);
191     return CALL(sigaction, signum, act, old);
192 }
193
194
195 /*** Xlib ****/
196 #ifdef HAVE_X11_XLIB_H
197 # include <X11/Xlib.h>
198
199 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
200
201 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
202      (Display *, XErrorEvent *)
203 {
204     if (override)
205     {
206         int (*ret) (Display *, XErrorEvent *);
207
208         pthread_mutex_lock (&xlib_lock);
209         LOG("Error", "%p", handler);
210         ret = CALL(XSetErrorHandler, handler);
211         pthread_mutex_unlock (&xlib_lock);
212         return ret;
213     }
214     return CALL(XSetErrorHandler, handler);
215 }
216
217 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
218 {
219     if (override)
220     {
221         int (*ret) (Display *);
222
223         pthread_mutex_lock (&xlib_lock);
224         LOG("Error", "%p", handler);
225         ret = CALL(XSetIOErrorHandler, handler);
226         pthread_mutex_unlock (&xlib_lock);
227         return ret;
228     }
229     return CALL(XSetIOErrorHandler, handler);
230 }
231 #endif
232 #else
233 static void vlc_enable_override (void)
234 {
235 }
236 #endif