swaybar: Fix json related crash.

This should fix the random json related crashes in swaybar.

The crashes occured because the same socket was used for listening on
workspace events and requesting workspace info, resulting in a
unreliable message queue on the socket.

The solution is to use one socket for the events and one socket for
reliably requesting workspace/output info.
This commit is contained in:
Mikkel Oscar Lyderik 2015-12-25 15:29:36 +01:00
parent 2179ac353c
commit e370187394

View file

@ -59,7 +59,7 @@ struct status_block {
list_t *status_line = NULL; list_t *status_line = NULL;
list_t *workspaces = NULL; list_t *workspaces = NULL;
int socketfd; int ipc_socketfd, ipc_listen_socketfd;
pid_t pid; pid_t pid;
int status_read_fd; int status_read_fd;
char line[1024]; char line[1024];
@ -142,6 +142,14 @@ void swaybar_teardown() {
if (status_read_fd) { if (status_read_fd) {
close(status_read_fd); close(status_read_fd);
} }
if (ipc_socketfd) {
close(ipc_socketfd);
}
if (ipc_listen_socketfd) {
close(ipc_listen_socketfd);
}
} }
void sway_terminate(void) { void sway_terminate(void) {
@ -181,7 +189,7 @@ void ipc_update_workspaces() {
workspaces = create_list(); workspaces = create_list();
uint32_t len = 0; uint32_t len = 0;
char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len); char *res = ipc_single_command(ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
json_object *results = json_tokener_parse(res); json_object *results = json_tokener_parse(res);
if (!results) { if (!results) {
free(res); free(res);
@ -189,12 +197,12 @@ void ipc_update_workspaces() {
} }
int i; int i;
for (i = 0; i < json_object_array_length(results); ++i) { int length = json_object_array_length(results);
json_object *ws_json = json_object_array_get_idx(results, i); json_object *ws_json;
json_object *num, *name, *visible, *focused, *out, *urgent; json_object *num, *name, *visible, *focused, *out, *urgent;
if (!ws_json) { for (i = 0; i < length; ++i) {
return; ws_json = json_object_array_get_idx(results, i);
}
json_object_object_get_ex(ws_json, "num", &num); json_object_object_get_ex(ws_json, "num", &num);
json_object_object_get_ex(ws_json, "name", &name); json_object_object_get_ex(ws_json, "name", &name);
json_object_object_get_ex(ws_json, "visible", &visible); json_object_object_get_ex(ws_json, "visible", &visible);
@ -259,7 +267,7 @@ static const int ws_spacing = 1;
void bar_ipc_init(int outputi, const char *bar_id) { void bar_ipc_init(int outputi, const char *bar_id) {
uint32_t len = 0; uint32_t len = 0;
char *res = ipc_single_command(socketfd, IPC_GET_OUTPUTS, NULL, &len); char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len);
json_object *outputs = json_tokener_parse(res); json_object *outputs = json_tokener_parse(res);
json_object *info = json_object_array_get_idx(outputs, outputi); json_object *info = json_object_array_get_idx(outputs, outputi);
json_object *name; json_object *name;
@ -269,7 +277,7 @@ void bar_ipc_init(int outputi, const char *bar_id) {
json_object_put(outputs); json_object_put(outputs);
len = strlen(bar_id); len = strlen(bar_id);
res = ipc_single_command(socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); res = ipc_single_command(ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len);
json_object *bar_config = json_tokener_parse(res); json_object *bar_config = json_tokener_parse(res);
json_object *tray_output, *mode, *hidden_state, *position, *_status_command; json_object *tray_output, *mode, *hidden_state, *position, *_status_command;
@ -384,7 +392,7 @@ void bar_ipc_init(int outputi, const char *bar_id) {
const char *subscribe_json = "[ \"workspace\" ]"; const char *subscribe_json = "[ \"workspace\" ]";
len = strlen(subscribe_json); len = strlen(subscribe_json);
res = ipc_single_command(socketfd, IPC_SUBSCRIBE, subscribe_json, &len); res = ipc_single_command(ipc_listen_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
ipc_update_workspaces(); ipc_update_workspaces();
} }
@ -822,7 +830,7 @@ void poll_for_update() {
dirty = false; dirty = false;
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(socketfd, &readfds); FD_SET(ipc_listen_socketfd, &readfds);
FD_SET(status_read_fd, &readfds); FD_SET(status_read_fd, &readfds);
activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
@ -830,10 +838,10 @@ void poll_for_update() {
sway_log(L_ERROR, "polling failed: %d", errno); sway_log(L_ERROR, "polling failed: %d", errno);
} }
if (FD_ISSET(socketfd, &readfds)) { if (FD_ISSET(ipc_listen_socketfd, &readfds)) {
sway_log(L_DEBUG, "Got workspace update."); sway_log(L_DEBUG, "Got workspace update.");
uint32_t len; uint32_t len;
char *buf = ipc_recv_response(socketfd, &len); char *buf = ipc_recv_response(ipc_listen_socketfd, &len);
free(buf); free(buf);
ipc_update_workspaces(); ipc_update_workspaces();
dirty = true; dirty = true;
@ -937,7 +945,9 @@ int main(int argc, char **argv) {
sway_abort("Unable to retrieve socket path"); sway_abort("Unable to retrieve socket path");
} }
} }
socketfd = ipc_open_socket(socket_path); ipc_socketfd = ipc_open_socket(socket_path);
ipc_listen_socketfd = ipc_open_socket(socket_path);
if (argc == optind) { if (argc == optind) {
sway_abort("No output index provided"); sway_abort("No output index provided");