]> git.sesse.net Git - vlc/blobdiff - bin/override.c
Avoid problems with srand() and speed up a bit (avoid dlsym)
[vlc] / bin / override.c
index 1eabda0135b5efa9edec3a727d3f349f68993794..02402d50ec554d34e9b795237096d26b967a49a0 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. */
@@ -42,14 +35,43 @@ void vlc_enable_override (void)
 #include <stdlib.h>
 #include <dlfcn.h>
 #include <pthread.h>
+#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 = 4;
+    void *framev[framec];
+
+    backtrace (framev, framec);
+#endif
     flockfile (stderr);
     fprintf (stderr, "%s: call to %s(", level, func);
     vfprintf (stderr, fmt, ap);
     fputs (")\n", stderr);
+    fflush (stderr);
+#ifdef HAVE_BACKTRACE
+    backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
+#endif
     funlockfile (stderr);
 }
 
@@ -67,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;
@@ -125,24 +148,28 @@ int unsetenv (const char *name)
  * preserve reproducibility of the number sequence (which usually does not
  * matter).
  **/
-static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
+static struct
+{
+    pthread_mutex_t lock;
+    unsigned int seed;
+} prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
 
 void srand (unsigned int seed)
 {
-    pthread_mutex_lock (&prng_lock);
+    pthread_mutex_lock (&prng.lock);
     LOG("Warning", "%d", seed);
-    CALL(srand, seed);
-    pthread_mutex_unlock (&prng_lock);
+    prng.seed = seed;
+    pthread_mutex_unlock (&prng.lock);
 }
 
 int rand (void)
 {
     int ret;
 
-    pthread_mutex_lock (&prng_lock);
+    pthread_mutex_lock (&prng.lock);
     LOG("Warning", "");
-    ret = CALL(rand);
-    pthread_mutex_unlock (&prng_lock);
+    ret = rand_r (&prng.seed);
+    pthread_mutex_unlock (&prng.lock);
     return ret;
 }
 
@@ -150,27 +177,111 @@ int rand (void)
 /** Signals **/
 #include <signal.h>
 
+static bool blocked_signal (int num)
+{
+    switch (num)
+    {
+        case SIGINT:
+        case SIGHUP:
+        case SIGQUIT:
+        case SIGTERM:
+        case SIGPIPE:
+        case SIGCHLD:
+            return true;
+    }
+    return false;
+}
+
 void (*signal (int signum, void (*handler) (int))) (int)
 {
     if (override)
     {
-        const char *msg = "Error";
-
-        if ((signum == SIGPIPE && handler == SIG_IGN)
-         || (signum != SIGPIPE && handler == SIG_DFL))
-            /* Same settings we already use */
-            msg = "Warning";
-        LOG(msg, "%d, %p", signum, handler);
+        if (handler != SIG_IGN && handler != SIG_DFL)
+            goto error;
+        if (!blocked_signal (signum))
+            goto error;
+        /* For our blocked signals, the handler won't matter much... */
+        LOG("Warning", "%d, %p", signum, handler);
     }
     return CALL(signal, signum, handler);
+error:
+    LOG("Blocked", "%d, %p", signum, handler);
+    return SIG_DFL;
 }
 
 int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
 {
-    if (act != NULL)
-        LOG("Error", "%d, %p, %p", signum, act, old);
+    if (override && act != NULL)
+    {
+        if ((act->sa_flags & SA_SIGINFO)
+         || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
+            goto error;
+        LOG("Warning", "%d, %p, %p", signum, act, old);
+    }
     return CALL(sigaction, signum, act, old);
+error:
+    LOG("Blocked", "%d, %p, %p", signum, act, old);
+    return -1;
 }
 
 
-#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