+/*** Pseudo random numbers ***
+ *
+ * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
+ * is much better as a reproducible non-secure PRNG). To work around this, we
+ * force evil callers to serialize. This makes the call safe, but fails to
+ * preserve reproducibility of the number sequence (which usually does not
+ * matter).
+ **/
+static struct
+{
+ pthread_mutex_t lock;
+ unsigned int seed;
+} prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
+
+void srand (unsigned int seed)
+{
+ pthread_mutex_lock (&prng.lock);
+ LOG("Warning", "%d", seed);
+ prng.seed = seed;
+ pthread_mutex_unlock (&prng.lock);
+}
+
+int rand (void)
+{
+ int ret;
+
+ pthread_mutex_lock (&prng.lock);
+ LOG("Warning", "");
+ ret = rand_r (&prng.seed);
+ pthread_mutex_unlock (&prng.lock);
+ return ret;
+}
+
+
+#if 0
+/** 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)
+ {
+ 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... */
+ if (handler == SIG_DFL)
+ LOG("Warning", "%d, SIG_DFL", 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 (override && act != NULL)
+ {
+ if ((act->sa_flags & SA_SIGINFO)
+ || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
+ goto error;
+ if (act->sa_handler == SIG_DFL)
+ LOG("Warning", "%d, %p, SIG_DFL", signum, act);
+ }
+ return CALL(sigaction, signum, act, old);
+error:
+ LOG("Blocked", "%d, %p, %p", signum, act, old);
+ return -1;
+}
+#endif
+
+
+/*** 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);
+}
+
+
+/* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
+ * This caused quite nasty crashes in the history of VLC/Linux. */
+char *strerror (int val)
+{
+ if (override)
+ {
+ static const char msg[] =
+ "Error message unavailable (use strerror_r instead of strerror)!";
+ LOG("Blocked", "%d", val);
+ return (char *)msg;
+ }
+ return CALL(strerror, val);
+}
+
+/*** 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