+({ \
+ static typeof (func) *sym = NULL; \
+ static pthread_once_t once = PTHREAD_ONCE_INIT; \
+ auto void getsym_once (void); \
+ void getsym_once (void) \
+ { \
+ sym = getsym ( # func); \
+ } \
+ pthread_once (&once, getsym_once); \
+ sym (__VA_ARGS__); \
+})
+
+
+/*** Environment ***
+ *
+ * "Conforming multi-threaded applications shall not use the environ variable
+ * to access or modify any environment variable while any other thread is
+ * concurrently modifying any environment variable." -- POSIX.
+ *
+ * Some evil libraries modify the environment. We currently ignore the calls as
+ * they could crash the process. This may cause funny behaviour though. */
+int putenv (char *str)
+{
+ if (override)
+ {
+ LOG("Blocked", "\"%s\"", str);
+ return 0;
+ }
+ return CALL(putenv, str);
+}
+
+int setenv (const char *name, const char *value, int overwrite)
+{
+ if (override)
+ {
+ LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
+ return 0;
+ }
+ return CALL(setenv, name, value, overwrite);
+}
+
+int unsetenv (const char *name)
+{
+ if (override)
+ {
+ LOG("Blocked", "\"%s\"", name);
+ return 0;
+ }
+ return CALL(unsetenv, name);
+}
+
+
+/*** 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;
+}