mirror of
https://github.com/swaywm/sway.git
synced 2026-04-28 06:46:26 -04:00
Pass errors up to caller in ipc-client.c
This removes the dependency on log.c (and transitively, on wlroots).
This commit is contained in:
parent
62260ab56e
commit
320fe4ad30
6 changed files with 97 additions and 30 deletions
|
|
@ -7,7 +7,6 @@
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "ipc-client.h"
|
#include "ipc-client.h"
|
||||||
#include "log.h"
|
|
||||||
|
|
||||||
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
||||||
|
|
||||||
|
|
@ -53,23 +52,26 @@ char *get_socketpath(void) {
|
||||||
return NULL;
|
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;
|
struct sockaddr_un addr;
|
||||||
int socketfd;
|
int socketfd;
|
||||||
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
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;
|
addr.sun_family = AF_UNIX;
|
||||||
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
||||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||||
int l = sizeof(struct sockaddr_un);
|
int l = sizeof(struct sockaddr_un);
|
||||||
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
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;
|
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];
|
char data[IPC_HEADER_SIZE];
|
||||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
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) {
|
while (total < IPC_HEADER_SIZE) {
|
||||||
ssize_t received = recv(socketfd, data + total, IPC_HEADER_SIZE - total, 0);
|
ssize_t received = recv(socketfd, data + total, IPC_HEADER_SIZE - total, 0);
|
||||||
if (received <= 0) {
|
if (received <= 0) {
|
||||||
sway_abort("Unable to receive IPC response");
|
*error = "Unable to receive IPC response";
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
total += received;
|
total += received;
|
||||||
}
|
}
|
||||||
|
|
@ -99,7 +102,10 @@ struct ipc_response *ipc_recv_response(int socketfd) {
|
||||||
while (total < response->size) {
|
while (total < response->size) {
|
||||||
ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
|
ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
|
||||||
if (received < 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;
|
total += received;
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +117,7 @@ error_2:
|
||||||
free(response);
|
free(response);
|
||||||
free(payload);
|
free(payload);
|
||||||
error_1:
|
error_1:
|
||||||
wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response");
|
*error = "Unable to allocate memory for IPC response";
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,7 +126,8 @@ void free_ipc_response(struct ipc_response *response) {
|
||||||
free(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];
|
char data[IPC_HEADER_SIZE];
|
||||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||||
memcpy(data, ipc_magic, 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));
|
memcpy(&data32[1], &type, sizeof(type));
|
||||||
|
|
||||||
if (write(socketfd, data, IPC_HEADER_SIZE) == -1) {
|
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) {
|
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;
|
char *response = resp->payload;
|
||||||
*len = resp->size;
|
*len = resp->size;
|
||||||
free(resp);
|
free(resp);
|
||||||
|
|
|
||||||
|
|
@ -21,17 +21,24 @@ struct ipc_response {
|
||||||
char *get_socketpath(void);
|
char *get_socketpath(void);
|
||||||
/**
|
/**
|
||||||
* Opens the sway socket.
|
* 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
|
* Issues a single IPC command and returns the buffer. len will be updated with
|
||||||
* the length of the buffer returned from sway.
|
* 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.
|
* 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
|
* Free ipc_response struct
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
11
sway/main.c
11
sway/main.c
|
|
@ -123,9 +123,16 @@ void detect_proprietary(int allow_unsupported_gpu) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_as_ipc_client(char *command, char *socket_path) {
|
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);
|
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);
|
printf("%s\n", resp);
|
||||||
close(socketfd);
|
close(socketfd);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -358,8 +358,15 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) {
|
||||||
wl_list_init(&bar->outputs);
|
wl_list_init(&bar->outputs);
|
||||||
bar->eventloop = loop_create();
|
bar->eventloop = loop_create();
|
||||||
|
|
||||||
bar->ipc_socketfd = ipc_open_socket(socket_path);
|
const char *error = NULL;
|
||||||
bar->ipc_event_socketfd = ipc_open_socket(socket_path);
|
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)) {
|
if (!ipc_initialize(bar)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,12 @@ void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
|
||||||
uint32_t size = snprintf(NULL, 0, fmt, ws);
|
uint32_t size = snprintf(NULL, 0, fmt, ws);
|
||||||
char *command = malloc(sizeof(char) * (size + 1));
|
char *command = malloc(sizeof(char) * (size + 1));
|
||||||
snprintf(command, size, fmt, ws);
|
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);
|
free(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -340,8 +345,13 @@ bool ipc_get_workspaces(struct swaybar *bar) {
|
||||||
output->focused = false;
|
output->focused = false;
|
||||||
}
|
}
|
||||||
uint32_t len = 0;
|
uint32_t len = 0;
|
||||||
|
const char *error = NULL;
|
||||||
char *res = ipc_single_command(bar->ipc_socketfd,
|
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);
|
json_object *results = json_tokener_parse(res);
|
||||||
if (!results) {
|
if (!results) {
|
||||||
free(res);
|
free(res);
|
||||||
|
|
@ -407,8 +417,13 @@ bool ipc_get_workspaces(struct swaybar *bar) {
|
||||||
|
|
||||||
static void ipc_get_outputs(struct swaybar *bar) {
|
static void ipc_get_outputs(struct swaybar *bar) {
|
||||||
uint32_t len = 0;
|
uint32_t len = 0;
|
||||||
|
const char *error = NULL;
|
||||||
char *res = ipc_single_command(bar->ipc_socketfd,
|
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);
|
json_object *outputs = json_tokener_parse(res);
|
||||||
for (size_t i = 0; i < json_object_array_length(outputs); ++i) {
|
for (size_t i = 0; i < json_object_array_length(outputs); ++i) {
|
||||||
json_object *output = json_object_array_get_idx(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`",
|
wlr_log(WLR_DEBUG, "Executing binding for button %u (release=%d): `%s`",
|
||||||
bind->button, bind->release, bind->command);
|
bind->button, bind->release, bind->command);
|
||||||
uint32_t len = strlen(bind->command);
|
uint32_t len = strlen(bind->command);
|
||||||
|
const char *error = NULL;
|
||||||
free(ipc_single_command(bar->ipc_socketfd,
|
free(ipc_single_command(bar->ipc_socketfd,
|
||||||
IPC_COMMAND, bind->command, &len));
|
IPC_COMMAND, bind->command, &len, &error));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ipc_initialize(struct swaybar *bar) {
|
bool ipc_initialize(struct swaybar *bar) {
|
||||||
uint32_t len = strlen(bar->id);
|
uint32_t len = strlen(bar->id);
|
||||||
|
const char *error = NULL;
|
||||||
char *res = ipc_single_command(bar->ipc_socketfd,
|
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)) {
|
if (!ipc_parse_config(bar->config, res)) {
|
||||||
free(res);
|
free(res);
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -466,7 +487,7 @@ bool ipc_initialize(struct swaybar *bar) {
|
||||||
config->binding_mode_indicator ? ", \"mode\"" : "",
|
config->binding_mode_indicator ? ", \"mode\"" : "",
|
||||||
config->workspace_buttons ? ", \"workspace\"" : "");
|
config->workspace_buttons ? ", \"workspace\"" : "");
|
||||||
free(ipc_single_command(bar->ipc_event_socketfd,
|
free(ipc_single_command(bar->ipc_event_socketfd,
|
||||||
IPC_SUBSCRIBE, subscribe, &len));
|
IPC_SUBSCRIBE, subscribe, &len, &error));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -537,8 +558,10 @@ static bool handle_barconfig_update(struct swaybar *bar,
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handle_ipc_readable(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) {
|
if (!resp) {
|
||||||
|
wlr_log(WLR_ERROR, "failed to receive response: %s", error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include "stringop.h"
|
|
||||||
#include "ipc-client.h"
|
#include "ipc-client.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "stringop.h"
|
||||||
|
|
||||||
void sway_terminate(int exit_code) {
|
void sway_terminate(int exit_code) {
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
|
|
@ -434,9 +434,16 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = 0;
|
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);
|
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) {
|
if (!quiet) {
|
||||||
// pretty print the json
|
// pretty print the json
|
||||||
json_object *obj = json_tokener_parse(resp);
|
json_object *obj = json_tokener_parse(resp);
|
||||||
|
|
@ -466,7 +473,10 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (type == IPC_SUBSCRIBE && ret == 0) {
|
if (type == IPC_SUBSCRIBE && ret == 0) {
|
||||||
do {
|
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) {
|
if (!reply) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue