mirror of
https://github.com/swaywm/sway.git
synced 2024-11-29 03:11:27 +00:00
Replace getdelim to make swaybar work on FreeBSD
This commit fixes a segfault in swaybar on FreeBSD that was caused by using getdelim with EOF as delimiter on an infinite stream. The FreeBSD implementation handles the "no more data, delimiter not found, and EOF not reached" scenario as an error, so it can't be used to read the output of status command. This commit replaces the getline/getdelim calls with reading all available data from the stream in one go.
This commit is contained in:
parent
a8a0014e0f
commit
315b2bf100
|
@ -1,5 +1,6 @@
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -34,18 +35,35 @@ bool status_handle_readable(struct status_line *status) {
|
||||||
switch (status->protocol) {
|
switch (status->protocol) {
|
||||||
case PROTOCOL_UNDEF:
|
case PROTOCOL_UNDEF:
|
||||||
errno = 0;
|
errno = 0;
|
||||||
read_bytes = getline(&status->buffer,
|
int available_bytes;
|
||||||
&status->buffer_size, status->read);
|
if (ioctl(status->read_fd, FIONREAD, &available_bytes) == -1) {
|
||||||
if (errno == EAGAIN) {
|
wlr_log(WLR_ERROR, "Unable to read status command output size");
|
||||||
clearerr(status->read);
|
|
||||||
} else if (errno) {
|
|
||||||
status_error(status, "[error reading from status command]");
|
status_error(status, "[error reading from status command]");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((size_t)available_bytes + 1 > status->buffer_size) {
|
||||||
|
// need room for leading '\0' too
|
||||||
|
status->buffer_size = available_bytes + 1;
|
||||||
|
status->buffer = realloc(status->buffer, status->buffer_size);
|
||||||
|
}
|
||||||
|
if (status->buffer == NULL) {
|
||||||
|
wlr_log_errno(WLR_ERROR, "Unable to read status line");
|
||||||
|
status_error(status, "[error reading from status command]");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
read_bytes = read(status->read_fd, status->buffer, available_bytes);
|
||||||
|
if (read_bytes != available_bytes) {
|
||||||
|
status_error(status, "[error reading from status command]");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
status->buffer[available_bytes] = 0;
|
||||||
|
|
||||||
// the header must be sent completely the first time round
|
// the header must be sent completely the first time round
|
||||||
|
char *newline = strchr(status->buffer, '\n');
|
||||||
json_object *header, *version;
|
json_object *header, *version;
|
||||||
if (status->buffer[read_bytes - 1] == '\n'
|
if (newline != NULL
|
||||||
&& (header = json_tokener_parse(status->buffer))
|
&& (header = json_tokener_parse(status->buffer))
|
||||||
&& json_object_object_get_ex(header, "version", &version)
|
&& json_object_object_get_ex(header, "version", &version)
|
||||||
&& json_object_get_int(version) == 1) {
|
&& json_object_get_int(version) == 1) {
|
||||||
|
@ -67,13 +85,9 @@ bool status_handle_readable(struct status_line *status) {
|
||||||
|
|
||||||
wl_list_init(&status->blocks);
|
wl_list_init(&status->blocks);
|
||||||
status->tokener = json_tokener_new();
|
status->tokener = json_tokener_new();
|
||||||
read_bytes = getdelim(&status->buffer, &status->buffer_size, EOF, status->read);
|
status->buffer_index = strlen(newline + 1);
|
||||||
if (read_bytes > 0) {
|
memmove(status->buffer, newline + 1, status->buffer_index + 1);
|
||||||
status->buffer_index = read_bytes;
|
return i3bar_handle_readable(status);
|
||||||
return i3bar_handle_readable(status);
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wlr_log(WLR_DEBUG, "Using text protocol.");
|
wlr_log(WLR_DEBUG, "Using text protocol.");
|
||||||
|
|
Loading…
Reference in a new issue