]> git.sesse.net Git - vlc/blobdiff - bin/override.c
Improve signal/sigaction wrapper
[vlc] / bin / override.c
index c6084d88e4b0644016b84b8ce1a955b4f5bd3f22..a8b0754887f4758ea774d4fde466c01b3e3a4343 100644 (file)
@@ -38,6 +38,9 @@ void vlc_enable_override (void);
 #ifdef HAVE_EXECINFO_H
 # include <execinfo.h>
 #endif
+#ifdef NDEBUG
+# undef HAVE_BACKTRACE
+#endif
 
 static bool override = false;
 
@@ -56,7 +59,7 @@ 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);
@@ -86,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;
@@ -169,31 +173,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;
 }
 
 
+/*** 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
-static void vlc_enable_override (void)
+void vlc_enable_override (void)
 {
 }
 #endif