From 78cbcbabff2f685e11b3b74df825aae953e04898 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Thu, 11 Jul 2019 16:54:29 +0200 Subject: [PATCH] #108 cleanup --- src/osax/sa.h | 1 + src/osax/sa.m | 35 ++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/osax/sa.h b/src/osax/sa.h index 758316ca..ce338b2b 100644 --- a/src/osax/sa.h +++ b/src/osax/sa.h @@ -6,6 +6,7 @@ #define PAYLOAD_STATUS_SUCCESS 0 #define PAYLOAD_STATUS_OUTDATED 1 #define PAYLOAD_STATUS_NO_ATTRIB 2 +#define PAYLOAD_STATUS_CON_ERROR 3 int scripting_addition_load(void); bool scripting_addition_is_installed(void); diff --git a/src/osax/sa.m b/src/osax/sa.m index 8832fae3..4c4e2439 100644 --- a/src/osax/sa.m +++ b/src/osax/sa.m @@ -175,9 +175,9 @@ static void scripting_addition_prepare_binaries(void) system(cmd); } -static char *scripting_addition_request_handshake(uint32_t *flags) +static bool scripting_addition_request_handshake(char *version, uint32_t *attrib) { - char *result = NULL; + bool result = false; int sockfd; char message[MAXLEN]; @@ -185,13 +185,20 @@ static void scripting_addition_prepare_binaries(void) if (socket_connect_un(&sockfd, g_sa_socket_file)) { snprintf(message, sizeof(message), "handshake"); if (socket_write(sockfd, message)) { + result = true; + int length; - result = socket_read(sockfd, &length); - if (!result) goto out; + char *rsp = socket_read(sockfd, &length); + if (!rsp) goto out; + + char *zero = rsp; + while (*zero != '\0') ++zero; + + assert(*zero == '\0'); + memcpy(version, rsp, zero - rsp + 1); + memcpy(attrib, zero+1, sizeof(uint32_t)); - char *attrib = result + 6; - assert(attrib[-1] == '\0'); - memcpy(flags, attrib, sizeof(uint32_t)); + free(rsp); } } @@ -202,11 +209,17 @@ static void scripting_addition_prepare_binaries(void) static int scripting_addition_perform_validation(void) { - uint32_t attrib; - char *version = scripting_addition_request_handshake(&attrib); - if (version) debug("yabai: osax version = %s, osax attrib = 0x%X\n", version, attrib); + uint32_t attrib = 0; + char version[MAXLEN] = {}; + + if (!scripting_addition_request_handshake(version, &attrib)) { + notify("connection failed!", "scripting-addition"); + return PAYLOAD_STATUS_CON_ERROR; + } + + debug("yabai: osax version = %s, osax attrib = 0x%X\n", version, attrib); - if (!version || !string_equals(version, OSAX_VERSION)) { + if (!string_equals(version, OSAX_VERSION)) { notify("payload is outdated, please reinstall!", "scripting-addition"); return PAYLOAD_STATUS_OUTDATED; } else if ((attrib & OSAX_ATTRIB_ALL) != OSAX_ATTRIB_ALL) {