diff --git a/src/library/socketwrappers.cpp b/src/library/socketwrappers.cpp index ceef7e2b..f5716be1 100644 --- a/src/library/socketwrappers.cpp +++ b/src/library/socketwrappers.cpp @@ -20,6 +20,7 @@ #include "socketwrappers.h" #include "logging.h" #include "global.h" +#include "GlobalState.h" #include #include @@ -31,16 +32,33 @@ DEFINE_ORIG_POINTER(socket) /* Override */ int socket (int domain, int type, int protocol) __THROW { DEBUGLOGCALL(LCF_SOCKET); + LINK_NAMESPACE_GLOBAL(socket); + + /* Passthrough socket call if this is a native call (e.g. ALSA init) or our own code (e.g. X connections) */ + if (GlobalState::isNative() || GlobalState::isOwnCode()) { + return orig::socket(domain, type, protocol); + } /* Deny internet access */ if (domain == AF_INET || domain == AF_INET6) { + /* HACK: ALSA might use PulseAudio for host audio playback, for e.g. WSL. + * PulseAudio might then proceed to call socket with AF_INET on a new thread + * We need to allow this connection, otherwise ALSA init will fail. + * We also can't mark PulseAudio's thread with pthread_setname_np, + * as PulseAudio will bypass that with prctl (a variadic function!) */ + char thread_name[16]; + if (pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)) == 0) { + if (strcmp(thread_name, "threaded-ml") == 0) { + return orig::socket(domain, type, protocol); + } + } + if (!(Global::shared_config.debug_state & SharedConfig::DEBUG_NATIVE_INET)) { errno = EACCES; return -1; } } - LINK_NAMESPACE_GLOBAL(socket); return orig::socket(domain, type, protocol); } diff --git a/src/library/xcb/xcbconnection.cpp b/src/library/xcb/xcbconnection.cpp index 48beccab..253686ac 100644 --- a/src/library/xcb/xcbconnection.cpp +++ b/src/library/xcb/xcbconnection.cpp @@ -21,6 +21,7 @@ #include "../hook.h" #include "../logging.h" #include "XcbEventQueueList.h" +#include "../GlobalState.h" namespace libtas { @@ -34,7 +35,13 @@ xcb_connection_t *xcb_connect(const char *displayname, int *screenp) DEBUGLOGCALL(LCF_WINDOW); LINK_NAMESPACE_GLOBAL(xcb_connect); - xcb_connection_t* c = orig::xcb_connect(displayname, screenp); + xcb_connection_t* c; + OWNCALL(c = orig::xcb_connect(displayname, screenp)); + + if (!c) { + debuglogstdio(LCF_WINDOW | LCF_ERROR, "Could not open xcb connection to %s (%d)", displayname ? displayname : "$DISPLAY", screenp ? *screenp : 0); + return nullptr; + } int i; for (i=0; i