From 455d862774b791f4ca93f9885e1e899208a5080c Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Sat, 10 Sep 2016 22:32:56 -0700 Subject: [PATCH 3/3] os/connection: Improve abstraction for launchd secure sockets This changes away from hard-coding the /tmp/launch-* path to now supporting a generic [.] format for $DISPLAY. cf-libxcb: d978a4f69b30b630f28d07f1003cf290284d24d8 Signed-off-by: Jeremy Huddleston Sequoia --- os/connection.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/os/connection.c b/os/connection.c index a901ebf..ac7d12b 100644 --- a/os/connection.c +++ b/os/connection.c @@ -79,6 +79,8 @@ SOFTWARE. #include #include +#include + #ifndef WIN32 #include @@ -1267,15 +1269,34 @@ void ListenOnOpenFD(int fd, int noxauth) { - char port[256]; + char port[PATH_MAX]; XtransConnInfo ciptr; const char *display_env = getenv("DISPLAY"); - if (display_env && (strncmp(display_env, "/tmp/launch", 11) == 0)) { - /* Make the path the launchd socket if our DISPLAY is set right */ - strcpy(port, display_env); + /* First check if display_env matches a [.] scheme (eg: launchd) */ + if (display_env) { + struct stat sbuf; + + strlcpy(port, display_env, sizeof(port)); + + /* If the path exists, we don't have do do anything else. + * If it doesn't, we need to check for a . to strip off and recheck. + */ + if (0 != stat(port, &sbuf)) { + char *dot = strrchr(port, '.'); + if (dot) { + *dot = '\0'; + + if (0 != stat(port, &sbuf)) { + display_env = NULL; + } + } else { + display_env = NULL; + } + } } - else { + + if (!display_env) { /* Just some default so things don't break and die. */ snprintf(port, sizeof(port), ":%d", atoi(display)); } -- 2.9.3