]> git.sesse.net Git - vlc/blobdiff - bin/override.c
Avoid a call to unsetenv/putenv on Qt4.6
[vlc] / bin / override.c
index c98ff81deba45f8e97d1d6be5dda0d30dc4e768e..60bee0ce3eebfa21a527a5359bbc9a82da79f087 100644 (file)
 
 void vlc_enable_override (void);
 
-static bool override = false;
-
-void vlc_enable_override (void)
-{
-    override = true;
-}
-
 #if defined (__GNUC__) /* typeof and statement-expression */ \
  && (defined (__ELF__) && !defined (__sun__))
 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
@@ -45,12 +38,28 @@ void vlc_enable_override (void)
 #ifdef HAVE_EXECINFO_H
 # include <execinfo.h>
 #endif
+#ifdef NDEBUG
+# undef HAVE_BACKTRACE
+#endif
+
+static bool override = false;
+
+static void vlc_reset_override (void)
+{
+    override = false;
+}
+
+void vlc_enable_override (void)
+{
+    override = true;
+    pthread_atfork (NULL, NULL, vlc_reset_override);
+}
 
 static void vlogbug (const char *level, const char *func, const char *fmt,
                      va_list ap)
 {
 #ifdef HAVE_BACKTRACE
-    const size_t framec = 8;
+    const size_t framec = 4;
     void *framev[framec];
 
     backtrace (framev, framec);
@@ -80,7 +89,8 @@ static void *getsym (const char *name)
     void *sym = dlsym (RTLD_NEXT, name);
     if (sym == NULL)
     {
-        fprintf (stderr, "Cannot resolve symbol %s!\n", name);
+        fprintf (stderr, "Cannot resolve symbol %s: %s\n", name,
+                 dlerror ());
         abort ();
     }
     return sym;
@@ -186,4 +196,63 @@ int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
 }
 
 
-#endif /* __ELF__ */
+/*** Locales ***
+ * setlocale() is not thread-safe and has a tendency to crash other threads as
+ * quite many libc and libintl calls depend on the locale.
+ * Use uselocale() instead for thread-safety.
+ */
+#include <locale.h>
+
+char *setlocale (int cat, const char *locale)
+{
+    if (override && locale != NULL)
+    {
+        LOG("Blocked", "%d, \"%s\"", cat, locale);
+        return NULL;
+    }
+    return CALL(setlocale, cat, locale);
+}
+
+
+/*** Xlib ****/
+#ifdef HAVE_X11_XLIB_H
+# include <X11/Xlib.h>
+
+static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;
+
+int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
+     (Display *, XErrorEvent *)
+{
+    if (override)
+    {
+        int (*ret) (Display *, XErrorEvent *);
+
+        pthread_mutex_lock (&xlib_lock);
+        LOG("Error", "%p", handler);
+        ret = CALL(XSetErrorHandler, handler);
+        pthread_mutex_unlock (&xlib_lock);
+        return ret;
+    }
+    return CALL(XSetErrorHandler, handler);
+}
+
+int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
+{
+    if (override)
+    {
+        int (*ret) (Display *);
+
+        pthread_mutex_lock (&xlib_lock);
+        LOG("Error", "%p", handler);
+        ret = CALL(XSetIOErrorHandler, handler);
+        pthread_mutex_unlock (&xlib_lock);
+        return ret;
+    }
+    return CALL(XSetIOErrorHandler, handler);
+}
+#endif
+#else
+void vlc_enable_override (void)
+{
+}
+#endif