From 320fe4ad30167f6201c41f5a6dedbd1f0a9bdbf5 Mon Sep 17 00:00:00 2001 From: M Stoeckl Date: Sat, 19 Jan 2019 13:40:01 -0500 Subject: [PATCH] Pass errors up to caller in ipc-client.c This removes the dependency on log.c (and transitively, on wlroots). --- common/ipc-client.c | 37 +++++++++++++++++++++++++------------ include/ipc-client.h | 13 ++++++++++--- sway/main.c | 11 +++++++++-- swaybar/bar.c | 11 +++++++++-- swaybar/ipc.c | 37 ++++++++++++++++++++++++++++++------- swaymsg/main.c | 18 ++++++++++++++---- 6 files changed, 97 insertions(+), 30 deletions(-) diff --git a/common/ipc-client.c b/common/ipc-client.c index 79ed3555c..8fcaf3328 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -7,7 +7,6 @@ #include #include #include "ipc-client.h" -#include "log.h" static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; @@ -53,23 +52,26 @@ char *get_socketpath(void) { return NULL; } -int ipc_open_socket(const char *socket_path) { +int ipc_open_socket(const char *socket_path, const char **error) { struct sockaddr_un addr; int socketfd; if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - sway_abort("Unable to open Unix socket"); + *error = "Unable to open Unix socket"; + return -1; } addr.sun_family = AF_UNIX; strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); addr.sun_path[sizeof(addr.sun_path) - 1] = 0; int l = sizeof(struct sockaddr_un); if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { - sway_abort("Unable to connect to %s", socket_path); + *error = "Unable to connect to socket path"; + close(socketfd); + return -1; } return socketfd; } -struct ipc_response *ipc_recv_response(int socketfd) { +struct ipc_response *ipc_recv_response(int socketfd, const char **error) { char data[IPC_HEADER_SIZE]; uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); @@ -77,7 +79,8 @@ struct ipc_response *ipc_recv_response(int socketfd) { while (total < IPC_HEADER_SIZE) { ssize_t received = recv(socketfd, data + total, IPC_HEADER_SIZE - total, 0); if (received <= 0) { - sway_abort("Unable to receive IPC response"); + *error = "Unable to receive IPC response"; + return NULL; } total += received; } @@ -99,7 +102,10 @@ struct ipc_response *ipc_recv_response(int socketfd) { while (total < response->size) { ssize_t received = recv(socketfd, payload + total, response->size - total, 0); if (received < 0) { - sway_abort("Unable to receive IPC response"); + *error = "Unable to receive IPC response"; + free(response); + free(payload); + return NULL; } total += received; } @@ -111,7 +117,7 @@ error_2: free(response); free(payload); error_1: - wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response"); + *error = "Unable to allocate memory for IPC response"; return NULL; } @@ -120,7 +126,8 @@ void free_ipc_response(struct ipc_response *response) { free(response); } -char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { +char *ipc_single_command(int socketfd, uint32_t type, const char *payload, + uint32_t *len, const char **error) { char data[IPC_HEADER_SIZE]; uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); memcpy(data, ipc_magic, sizeof(ipc_magic)); @@ -128,14 +135,20 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint3 memcpy(&data32[1], &type, sizeof(type)); if (write(socketfd, data, IPC_HEADER_SIZE) == -1) { - sway_abort("Unable to send IPC header"); + *error = "Unable to send IPC header"; + return NULL; } if (write(socketfd, payload, *len) == -1) { - sway_abort("Unable to send IPC payload"); + *error = "Unable to send IPC payload"; + return NULL; } - struct ipc_response *resp = ipc_recv_response(socketfd); + struct ipc_response *resp = ipc_recv_response(socketfd, error); + if (!resp) { + // error already set by `ipc_recv_response` + return NULL; + } char *response = resp->payload; *len = resp->size; free(resp); diff --git a/include/ipc-client.h b/include/ipc-client.h index c9f5b3441..f6f11d5a1 100644 --- a/include/ipc-client.h +++ b/include/ipc-client.h @@ -21,17 +21,24 @@ struct ipc_response { char *get_socketpath(void); /** * Opens the sway socket. + * + * On failure, sets `*error` to a string constant, and returns -1; */ -int ipc_open_socket(const char *socket_path); +int ipc_open_socket(const char *socket_path, const char **error); /** * Issues a single IPC command and returns the buffer. len will be updated with * the length of the buffer returned from sway. + * + * On failure, sets `*error` to a string constant, and returns NULL. */ -char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len); +char *ipc_single_command(int socketfd, uint32_t type, const char *payload, + uint32_t *len, const char **error); /** * Receives a single IPC response and returns an ipc_response. + * + * On failure, sets `*error` to a string constant, and returns NULL. */ -struct ipc_response *ipc_recv_response(int socketfd); +struct ipc_response *ipc_recv_response(int socketfd, const char **error); /** * Free ipc_response struct */ diff --git a/sway/main.c b/sway/main.c index 6e2f16db1..b9d4300d8 100644 --- a/sway/main.c +++ b/sway/main.c @@ -123,9 +123,16 @@ void detect_proprietary(int allow_unsupported_gpu) { } void run_as_ipc_client(char *command, char *socket_path) { - int socketfd = ipc_open_socket(socket_path); + const char *error = NULL; + int socketfd = ipc_open_socket(socket_path, &error); + if (socketfd == -1) { + sway_abort("Error opening socket '%s': %s", socket_path, error); + } uint32_t len = strlen(command); - char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len); + char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len, &error); + if (!resp) { + sway_abort("Error invoking command over IPC: %s", error); + } printf("%s\n", resp); close(socketfd); } diff --git a/swaybar/bar.c b/swaybar/bar.c index d36367fc4..8ee6e32b4 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -358,8 +358,15 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) { wl_list_init(&bar->outputs); bar->eventloop = loop_create(); - bar->ipc_socketfd = ipc_open_socket(socket_path); - bar->ipc_event_socketfd = ipc_open_socket(socket_path); + const char *error = NULL; + bar->ipc_socketfd = ipc_open_socket(socket_path, &error); + if (bar->ipc_socketfd == -1) { + sway_abort("Error opening socket '%s': %s", socket_path, error); + } + bar->ipc_event_socketfd = ipc_open_socket(socket_path, &error); + if (bar->ipc_event_socketfd == -1) { + sway_abort("Error opening socket '%s' again: %s", socket_path, error); + } if (!ipc_initialize(bar)) { return false; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 5565dc76a..1737605e7 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -15,7 +15,12 @@ void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { uint32_t size = snprintf(NULL, 0, fmt, ws); char *command = malloc(sizeof(char) * (size + 1)); snprintf(command, size, fmt, ws); - ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size); + const char *error = NULL; + char *resp = ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size, &error); + if (!resp) { + wlr_log(WLR_ERROR, "failed to send command: %s", error); + } + free(resp); free(command); } @@ -340,8 +345,13 @@ bool ipc_get_workspaces(struct swaybar *bar) { output->focused = false; } uint32_t len = 0; + const char *error = NULL; char *res = ipc_single_command(bar->ipc_socketfd, - IPC_GET_WORKSPACES, NULL, &len); + IPC_GET_WORKSPACES, NULL, &len, &error); + if (!res) { + wlr_log(WLR_ERROR, "failed to send command: %s", error); + return false; + } json_object *results = json_tokener_parse(res); if (!results) { free(res); @@ -407,8 +417,13 @@ bool ipc_get_workspaces(struct swaybar *bar) { static void ipc_get_outputs(struct swaybar *bar) { uint32_t len = 0; + const char *error = NULL; char *res = ipc_single_command(bar->ipc_socketfd, - IPC_GET_OUTPUTS, NULL, &len); + IPC_GET_OUTPUTS, NULL, &len, &error); + if (!res) { + wlr_log(WLR_ERROR, "failed to send command: %s", error); + return; + } json_object *outputs = json_tokener_parse(res); for (size_t i = 0; i < json_object_array_length(outputs); ++i) { json_object *output = json_object_array_get_idx(outputs, i); @@ -444,14 +459,20 @@ void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind) { wlr_log(WLR_DEBUG, "Executing binding for button %u (release=%d): `%s`", bind->button, bind->release, bind->command); uint32_t len = strlen(bind->command); + const char *error = NULL; free(ipc_single_command(bar->ipc_socketfd, - IPC_COMMAND, bind->command, &len)); + IPC_COMMAND, bind->command, &len, &error)); } bool ipc_initialize(struct swaybar *bar) { uint32_t len = strlen(bar->id); + const char *error = NULL; char *res = ipc_single_command(bar->ipc_socketfd, - IPC_GET_BAR_CONFIG, bar->id, &len); + IPC_GET_BAR_CONFIG, bar->id, &len, &error); + if (!res) { + wlr_log(WLR_ERROR, "failed to send command: %s", error); + return false; + } if (!ipc_parse_config(bar->config, res)) { free(res); return false; @@ -466,7 +487,7 @@ bool ipc_initialize(struct swaybar *bar) { config->binding_mode_indicator ? ", \"mode\"" : "", config->workspace_buttons ? ", \"workspace\"" : ""); free(ipc_single_command(bar->ipc_event_socketfd, - IPC_SUBSCRIBE, subscribe, &len)); + IPC_SUBSCRIBE, subscribe, &len, &error)); return true; } @@ -537,8 +558,10 @@ static bool handle_barconfig_update(struct swaybar *bar, } bool handle_ipc_readable(struct swaybar *bar) { - struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd); + const char *error = NULL; + struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd, &error); if (!resp) { + wlr_log(WLR_ERROR, "failed to receive response: %s", error); return false; } diff --git a/swaymsg/main.c b/swaymsg/main.c index e5eee6318..034bafa3f 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -10,9 +10,9 @@ #include #include #include -#include "stringop.h" #include "ipc-client.h" #include "log.h" +#include "stringop.h" void sway_terminate(int exit_code) { exit(exit_code); @@ -434,9 +434,16 @@ int main(int argc, char **argv) { } int ret = 0; - int socketfd = ipc_open_socket(socket_path); + const char *error = NULL; + int socketfd = ipc_open_socket(socket_path, &error); + if (socketfd == -1) { + sway_abort("Error opening socket '%s': %s", socket_path, error); + } uint32_t len = strlen(command); - char *resp = ipc_single_command(socketfd, type, command, &len); + char *resp = ipc_single_command(socketfd, type, command, &len, &error); + if (!resp) { + sway_abort("Error invoking command '%s': %s", command, error); + } if (!quiet) { // pretty print the json json_object *obj = json_tokener_parse(resp); @@ -466,7 +473,10 @@ int main(int argc, char **argv) { if (type == IPC_SUBSCRIBE && ret == 0) { do { - struct ipc_response *reply = ipc_recv_response(socketfd); + struct ipc_response *reply = ipc_recv_response(socketfd, &error); + if (!resp) { + sway_abort("Error receiving response: %s", error); + } if (!reply) { break; }