From ae93c6e6fe5649473116aa081c1acedac10f2be4 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 10:55:54 +0200 Subject: [PATCH 1/6] sway/ipc: Dynamically assign ipc_sockaddr. --- sway/ipc.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/sway/ipc.c b/sway/ipc.c index 0d111778..7a1d8a30 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -21,10 +21,7 @@ static int ipc_socket = -1; static struct wlc_event_source *ipc_event_source = NULL; -static struct sockaddr_un ipc_sockaddr = { - .sun_family = AF_UNIX, - .sun_path = "/tmp/sway-ipc.sock" -}; +static struct sockaddr_un *ipc_sockaddr = NULL; static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; @@ -35,6 +32,7 @@ struct ipc_client { enum ipc_command_type current_command; }; +struct sockaddr_un *ipc_user_sockaddr(void); int ipc_handle_connection(int fd, uint32_t mask, void *data); int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); void ipc_client_disconnect(struct ipc_client *client); @@ -49,12 +47,14 @@ void ipc_init(void) { sway_abort("Unable to create IPC socket"); } + ipc_sockaddr = ipc_user_sockaddr(); + if (getenv("SWAYSOCK") != NULL) { - strncpy(ipc_sockaddr.sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr.sun_path)); + strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path)); } - unlink(ipc_sockaddr.sun_path); - if (bind(ipc_socket, (struct sockaddr *)&ipc_sockaddr, sizeof(ipc_sockaddr)) == -1) { + unlink(ipc_sockaddr->sun_path); + if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) { sway_abort("Unable to bind IPC socket"); } @@ -63,7 +63,7 @@ void ipc_init(void) { } // Set i3 IPC socket path so that i3-msg works out of the box - setenv("I3SOCK", ipc_sockaddr.sun_path, 1); + setenv("I3SOCK", ipc_sockaddr->sun_path, 1); ipc_event_source = wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL); } @@ -73,7 +73,17 @@ void ipc_terminate(void) { wlc_event_source_remove(ipc_event_source); } close(ipc_socket); - unlink(ipc_sockaddr.sun_path); + unlink(ipc_sockaddr->sun_path); +} + +struct sockaddr_un *ipc_user_sockaddr(void) { + struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); + assert(ipc_sockaddr != NULL); + + ipc_sockaddr->sun_family = AF_UNIX; + strcpy(ipc_sockaddr->sun_path, "/tmp/sway-ipc.sock"); + + return ipc_sockaddr; } int ipc_handle_connection(int fd, uint32_t mask, void *data) { From f94f8fde7ec5c96e794772d75bc59b45cea54141 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 11:42:20 +0200 Subject: [PATCH 2/6] sway/ipc: Support multiple users. --- sway/ipc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sway/ipc.c b/sway/ipc.c index 7a1d8a30..c1518c1f 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -81,7 +81,11 @@ struct sockaddr_un *ipc_user_sockaddr(void) { assert(ipc_sockaddr != NULL); ipc_sockaddr->sun_family = AF_UNIX; - strcpy(ipc_sockaddr->sun_path, "/tmp/sway-ipc.sock"); + + int path_size = sizeof(ipc_sockaddr->sun_path); + + // Without logind: + assert(snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()) < path_size); return ipc_sockaddr; } From 26beb66eb552569d4eb979916bee750184c4ec24 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 12:02:41 +0200 Subject: [PATCH 3/6] sway/ipc: Free ipc_sockaddr upon termination. --- sway/ipc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sway/ipc.c b/sway/ipc.c index c1518c1f..325d78ab 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -74,6 +74,10 @@ void ipc_terminate(void) { } close(ipc_socket); unlink(ipc_sockaddr->sun_path); + + if (ipc_sockaddr) { + free(ipc_sockaddr); + } } struct sockaddr_un *ipc_user_sockaddr(void) { From 382b4e425a18f3c64354cc57d98857ec9852705c Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 13:06:30 +0200 Subject: [PATCH 4/6] sway/ipc: Fix whitespace. --- sway/ipc.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sway/ipc.c b/sway/ipc.c index 325d78ab..5599cdf0 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -81,17 +81,17 @@ void ipc_terminate(void) { } struct sockaddr_un *ipc_user_sockaddr(void) { - struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); - assert(ipc_sockaddr != NULL); + struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); + assert(ipc_sockaddr != NULL); - ipc_sockaddr->sun_family = AF_UNIX; + ipc_sockaddr->sun_family = AF_UNIX; - int path_size = sizeof(ipc_sockaddr->sun_path); + int path_size = sizeof(ipc_sockaddr->sun_path); - // Without logind: - assert(snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()) < path_size); + // Without logind: + assert(snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()) < path_size); - return ipc_sockaddr; + return ipc_sockaddr; } int ipc_handle_connection(int fd, uint32_t mask, void *data) { From 7cb073203038983ac641770ea8cc0db222ca0df7 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 13:04:46 +0200 Subject: [PATCH 5/6] sway/ipc: ipc_user_sockaddr: Use sway_assert instead of assert. --- sway/ipc.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sway/ipc.c b/sway/ipc.c index 5599cdf0..dba2acb3 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -82,14 +82,20 @@ void ipc_terminate(void) { struct sockaddr_un *ipc_user_sockaddr(void) { struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); - assert(ipc_sockaddr != NULL); + if (!sway_assert(ipc_sockaddr != NULL, "can't malloc ipc_sockaddr")) { + return NULL; + } ipc_sockaddr->sun_family = AF_UNIX; int path_size = sizeof(ipc_sockaddr->sun_path); // Without logind: - assert(snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()) < path_size); + int allocating_path_size = snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()); + + if (!sway_assert(allocating_path_size < path_size, "socket path won't fit into ipc_sockaddr->sun_path")) { + return NULL; + } return ipc_sockaddr; } From 783290e81a5947865c00a5af30c1ba5e2ecd1052 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 16 Oct 2015 13:21:34 +0200 Subject: [PATCH 6/6] sway/ipc: Use sway_abort instead of returning NULL. --- sway/ipc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sway/ipc.c b/sway/ipc.c index dba2acb3..e4deb4d4 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -82,8 +82,8 @@ void ipc_terminate(void) { struct sockaddr_un *ipc_user_sockaddr(void) { struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); - if (!sway_assert(ipc_sockaddr != NULL, "can't malloc ipc_sockaddr")) { - return NULL; + if (ipc_sockaddr == NULL) { + sway_abort("can't malloc ipc_sockaddr"); } ipc_sockaddr->sun_family = AF_UNIX; @@ -93,8 +93,8 @@ struct sockaddr_un *ipc_user_sockaddr(void) { // Without logind: int allocating_path_size = snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid()); - if (!sway_assert(allocating_path_size < path_size, "socket path won't fit into ipc_sockaddr->sun_path")) { - return NULL; + if (allocating_path_size >= path_size) { + sway_abort("socket path won't fit into ipc_sockaddr->sun_path"); } return ipc_sockaddr;