]> git.sesse.net Git - vlc/blob - bin/override.c
Improve signal/sigaction wrapper
[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: %s\n", name,
93                  dlerror ());
94         abort ();
95     }
96     return sym;
97 }
98
99 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
100 #define CALL(func, ...) \
101     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
102
103
104 /*** Environment ***
105  *
106  * "Conforming multi-threaded applications shall not use the environ variable
107  *  to access or modify any environment variable while any other thread is
108  *  concurrently modifying any environment variable." -- POSIX.
109  *
110  * Some evil libraries modify the environment. We currently ignore the calls as
111  * they could crash the process. This may cause funny behaviour though. */
112 int putenv (char *str)
113 {
114     if (override)
115     {
116         LOG("Blocked", "\"%s\"", str);
117         return 0;
118     }
119     return CALL(putenv, str);
120 }
121
122 int setenv (const char *name, const char *value, int overwrite)
123 {
124     if (override)
125     {
126         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
127         return 0;
128     }
129     return CALL(setenv, name, value, overwrite);
130 }
131
132 int unsetenv (const char *name)
133 {
134     if (override)
135     {
136         LOG("Blocked", "\"%s\"", name);
137         return 0;
138     }
139     return CALL(unsetenv, name);
140 }
141
142
143 /*** Pseudo random numbers ***
144  *
145  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
146  * is much better as a reproducible non-secure PRNG). To work around this, we
147  * force evil callers to serialize. This makes the call safe, but fails to
148  * preserve reproducibility of the number sequence (which usually does not
149  * matter).
150  **/
151 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
152
153 void srand (unsigned int seed)
154 {
155     pthread_mutex_lock (&prng_lock);
156     LOG("Warning", "%d", seed);
157     CALL(srand, seed);
158     pthread_mutex_unlock (&prng_lock);
159 }
160
161 int rand (void)
162 {
163     int ret;
164
165     pthread_mutex_lock (&prng_lock);
166     LOG("Warning", "");
167     ret = CALL(rand);
168     pthread_mutex_unlock (&prng_lock);
169     return ret;
170 }
171
172
173 /** Signals **/
174 #include <signal.h>
175
176 static bool blocked_signal (int num)
177 {
178     switch (num)
179     {
180         case SIGINT:
181         case SIGHUP:
182         case SIGQUIT:
183         case SIGTERM:
184         case SIGPIPE:
185         case SIGCHLD:
186             return true;
187     }
188     return false;
189 }
190
191 void (*signal (int signum, void (*handler) (int))) (int)
192 {
193     if (override)
194     {
195         if (handler != SIG_IGN && handler != SIG_DFL)
196             goto error;
197         if (!blocked_signal (signum))
198             goto error;
199         /* For our blocked signals, the handler won't matter much... */
200         LOG("Warning", "%d, %p", signum, handler);
201     }
202     return CALL(signal, signum, handler);
203 error:
204     LOG("Blocked", "%d, %p", signum, handler);
205     return SIG_DFL;
206 }
207
208 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
209 {
210     if (override && act != NULL)
211     {
212         if ((act->sa_flags & SA_SIGINFO)
213          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
214             goto error;
215         LOG("Warning", "%d, %p, %p", signum, act, old);
216     }
217     return CALL(sigaction, signum, act, old);
218 error:
219     LOG("Blocked", "%d, %p, %p", signum, act, old);
220     return -1;
221 }
222
223
224 /*** Locales ***
225  * setlocale() is not thread-safe and has a tendency to crash other threads as
226  * quite many libc and libintl calls depend on the locale.
227  * Use uselocale() instead for thread-safety.
228  */
229 #include <locale.h>
230
231 char *setlocale (int cat, const char *locale)
232 {
233     if (override && locale != NULL)
234     {
235         LOG("Blocked", "%d, \"%s\"", cat, locale);
236         return NULL;
237     }
238     return CALL(setlocale, cat, locale);
239 }
240
241
242 /*** Xlib ****/
243 #ifdef HAVE_X11_XLIB_H
244 # include <X11/Xlib.h>
245
246 static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
247
248 int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
249      (Display *, XErrorEvent *)
250 {
251     if (override)
252     {
253         int (*ret) (Display *, XErrorEvent *);
254
255         pthread_mutex_lock (&xlib_lock);
256         LOG("Error", "%p", handler);
257         ret = CALL(XSetErrorHandler, handler);
258         pthread_mutex_unlock (&xlib_lock);
259         return ret;
260     }
261     return CALL(XSetErrorHandler, handler);
262 }
263
264 int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
265 {
266     if (override)
267     {
268         int (*ret) (Display *);
269
270         pthread_mutex_lock (&xlib_lock);
271         LOG("Error", "%p", handler);
272         ret = CALL(XSetIOErrorHandler, handler);
273         pthread_mutex_unlock (&xlib_lock);
274         return ret;
275     }
276     return CALL(XSetIOErrorHandler, handler);
277 }
278 #endif
279 #else
280 void vlc_enable_override (void)
281 {
282 }
283 #endif