From fc783ba069f9e46d08aa7f254c322a313d9dc22d Mon Sep 17 00:00:00 2001 From: mazunki Date: Fri, 18 Mar 2022 04:56:57 +0100 Subject: [PATCH] moved socket query to ipc-client.c since it's a common resource, required for both sway and swaymsg --- common/ipc-client.c | 37 +++++++++++++++++++++++++++++++++++++ sway/main.c | 39 ++++----------------------------------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/common/ipc-client.c b/common/ipc-client.c index d30212d25..9aa38a850 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -13,6 +13,25 @@ static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; #define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8) +int query_for_swaypid(char *xdg_runtime_dir) { + if (xdg_runtime_dir != NULL) { + char pidline[1024]; + char *pid; + FILE *fp = popen("pidof sway", "r"); + fgets(pidline, 1024, fp); + pid = strtok(pidline, " "); + int sway_pid = atoi(pid); + if (strtok(NULL, " ") != NULL) { + return -1; + } else { + return sway_pid; + } + } else { + exit(EXIT_FAILURE); + }; +} + + char *get_socketpath(void) { const char *swaysock = getenv("SWAYSOCK"); if (swaysock) { @@ -46,10 +65,28 @@ char *get_socketpath(void) { if (line[nret - 1] == '\n') { line[nret - 1] = '\0'; } + free(line); return line; } } free(line); + + if (getenv("XDG_RUNTIME_DIR") && getuid()) { + int swaypid = query_for_swaypid(getenv("XDG_RUNTIME_DIR")); + if (swaypid > 0) { + size_t path_sz = snprintf(NULL, 0, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid); + char *swaysock_runtime = malloc(path_sz+1); + snprintf(swaysock_runtime, path_sz+1, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid); + if (access(swaysock_runtime, F_OK) != -1) { + return strdup(swaysock_runtime); + } else { + fprintf(stderr, "sway socket not detected.\n"); // found a pid, but no socket file + } + } else { + fprintf(stderr, "Found more than one sway instance running.\n"); + exit(EXIT_FAILURE); + } + } return NULL; } diff --git a/sway/main.c b/sway/main.c index 4aacd6f07..91bdcc45a 100644 --- a/sway/main.c +++ b/sway/main.c @@ -87,24 +87,6 @@ void detect_proprietary(int allow_unsupported_gpu) { fclose(f); } -int query_for_swaypid(char *xdg_runtime_dir) { - if (xdg_runtime_dir != NULL) { - char pidline[1024]; - char *pid; - FILE *fp = popen("pidof sway", "r"); - fgets(pidline, 1024, fp); - pid = strtok(pidline, " "); - int sway_pid = atoi(pid); - if (strtok(NULL, " ") != NULL) { - return -1; - } else { - return sway_pid; - } - } else { - exit(EXIT_FAILURE); - }; -} - void run_as_ipc_client(char *command, char *socket_path) { int socketfd = ipc_open_socket(socket_path); uint32_t len = strlen(command); @@ -324,27 +306,14 @@ int main(int argc, char **argv) { verbose = true; break; case 'p': ; // --get-socketpath - if (getenv("SWAYSOCK")) { - printf("%s\n", getenv("SWAYSOCK")); - } else if (getenv("XDG_RUNTIME_DIR") && getuid()) { - int swaypid = query_for_swaypid(getenv("XDG_RUNTIME_DIR")); - char *swaysock = {0}; - if (swaypid > 0) { - sprintf(swaysock, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid); - if (access(swaysock, F_OK) != -1) { - printf("%s\n", swaysock); - } else { - fprintf(stderr, "sway socket not detected.\n"); // found a pid, but no socket file - } - } else { - fprintf(stderr, "Found more than one sway instance running.\n"); - exit(EXIT_FAILURE); - } + char *swaysock = get_socketpath(); + if (swaysock != NULL) { + printf("%s\n", swaysock); + exit(EXIT_SUCCESS); } else { fprintf(stderr, "sway socket not detected.\n"); exit(EXIT_FAILURE); } - break; default: fprintf(stderr, "%s", usage); exit(EXIT_FAILURE);