xcb_open: Improve abstraction for launchd secure sockets
This changes away from hard-coding the /tmp/launch-* path to now supporting a generic <path to unix socket>[.<screen>] format for $DISPLAY. Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
This commit is contained in:
parent
29e419c584
commit
d978a4f69b
|
@ -60,6 +60,10 @@
|
||||||
# include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LAUNCHD
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
int xcb_popcount(uint32_t mask)
|
int xcb_popcount(uint32_t mask)
|
||||||
{
|
{
|
||||||
uint32_t y;
|
uint32_t y;
|
||||||
|
@ -78,6 +82,59 @@ int xcb_sumof(uint8_t *list, int len)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_LAUNCHD
|
||||||
|
/* Return true and parse if name matches <path to socket>[.<screen>]
|
||||||
|
* Upon success:
|
||||||
|
* host = <path to socket>
|
||||||
|
* protocol = "unix"
|
||||||
|
* display = 0
|
||||||
|
* screen = <screen>
|
||||||
|
*/
|
||||||
|
static int _xcb_parse_display_path_to_socket(const char *name, char **host, char **protocol,
|
||||||
|
int *displayp, int *screenp)
|
||||||
|
{
|
||||||
|
struct stat sbuf;
|
||||||
|
char path[PATH_MAX];
|
||||||
|
int _screen = 0;
|
||||||
|
|
||||||
|
strlcpy(path, name, sizeof(path));
|
||||||
|
if (0 != stat(path, &sbuf)) {
|
||||||
|
char *dot = strrchr(path, '.');
|
||||||
|
if (!dot)
|
||||||
|
return 0;
|
||||||
|
*dot = '\0';
|
||||||
|
|
||||||
|
if (0 != stat(path, &sbuf))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
_screen = atoi(dot + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (host) {
|
||||||
|
*host = strdup(path);
|
||||||
|
if (!*host)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (protocol) {
|
||||||
|
*protocol = strdup("unix");
|
||||||
|
if (!*protocol) {
|
||||||
|
if (host)
|
||||||
|
free(*host);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (displayp)
|
||||||
|
*displayp = 0;
|
||||||
|
|
||||||
|
if (screenp)
|
||||||
|
*screenp = _screen;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static int _xcb_parse_display(const char *name, char **host, char **protocol,
|
static int _xcb_parse_display(const char *name, char **host, char **protocol,
|
||||||
int *displayp, int *screenp)
|
int *displayp, int *screenp)
|
||||||
{
|
{
|
||||||
|
@ -90,10 +147,11 @@ static int _xcb_parse_display(const char *name, char **host, char **protocol,
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
#ifdef HAVE_LAUNCHD
|
#ifdef HAVE_LAUNCHD
|
||||||
if(strncmp(name, "/tmp/launch", 11) == 0)
|
/* First check for <path to socket>[.<screen>] */
|
||||||
slash = NULL;
|
if (_xcb_parse_display_path_to_socket(name, host, protocol, displayp, screenp))
|
||||||
else
|
return 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
slash = strrchr(name, '/');
|
slash = strrchr(name, '/');
|
||||||
|
|
||||||
if (slash) {
|
if (slash) {
|
||||||
|
@ -178,14 +236,6 @@ static int _xcb_open(const char *host, char *protocol, const int display)
|
||||||
char *file = NULL;
|
char *file = NULL;
|
||||||
int actual_filelen;
|
int actual_filelen;
|
||||||
|
|
||||||
#ifdef HAVE_LAUNCHD
|
|
||||||
if(strncmp(host, "/tmp/launch", 11) == 0) {
|
|
||||||
base = host;
|
|
||||||
host = "";
|
|
||||||
protocol = "unix";
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* If protocol or host is "unix", fall through to Unix socket code below */
|
/* If protocol or host is "unix", fall through to Unix socket code below */
|
||||||
if ((!protocol || (strcmp("unix",protocol) != 0)) &&
|
if ((!protocol || (strcmp("unix",protocol) != 0)) &&
|
||||||
(*host != '\0') && (strcmp("unix",host) != 0))
|
(*host != '\0') && (strcmp("unix",host) != 0))
|
||||||
|
@ -211,18 +261,23 @@ static int _xcb_open(const char *host, char *protocol, const int display)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
|
|
||||||
file = malloc(filelen);
|
|
||||||
if(file == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* display specifies Unix socket */
|
|
||||||
#ifdef HAVE_LAUNCHD
|
#ifdef HAVE_LAUNCHD
|
||||||
if(strncmp(base, "/tmp/launch", 11) == 0)
|
struct stat sbuf;
|
||||||
actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
|
if (0 == stat(host, &sbuf)) {
|
||||||
else
|
file = strdup(host);
|
||||||
|
filelen = actual_filelen = strlen(file);
|
||||||
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
|
||||||
|
file = malloc(filelen);
|
||||||
|
if(file == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/* display specifies Unix socket */
|
||||||
actual_filelen = snprintf(file, filelen, "%s%d", base, display);
|
actual_filelen = snprintf(file, filelen, "%s%d", base, display);
|
||||||
|
}
|
||||||
|
|
||||||
if(actual_filelen < 0)
|
if(actual_filelen < 0)
|
||||||
{
|
{
|
||||||
free(file);
|
free(file);
|
||||||
|
|
Loading…
Reference in New Issue