aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/status_line.c
diff options
context:
space:
mode:
authorLibravatar sghctoma <sghctoma@gmail.com>2018-09-26 20:38:16 +0200
committerLibravatar sghctoma <sghctoma@gmail.com>2018-09-26 20:38:16 +0200
commit315b2bf100acc5a8095807763e5d838eed0c3e4f (patch)
tree64e0da2c9c189910a3f665e09b1c5ddcb18fe478 /swaybar/status_line.c
parentMake libpam optional (diff)
downloadsway-315b2bf100acc5a8095807763e5d838eed0c3e4f.tar.gz
sway-315b2bf100acc5a8095807763e5d838eed0c3e4f.tar.zst
sway-315b2bf100acc5a8095807763e5d838eed0c3e4f.zip
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.
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r--swaybar/status_line.c40
1 files changed, 27 insertions, 13 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index 48b43248..1442e1a0 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -1,5 +1,6 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <fcntl.h> 2#include <fcntl.h>
3#include <sys/ioctl.h>
3#include <json-c/json.h> 4#include <json-c/json.h>
4#include <stdlib.h> 5#include <stdlib.h>
5#include <string.h> 6#include <string.h>
@@ -34,18 +35,35 @@ bool status_handle_readable(struct status_line *status) {
34 switch (status->protocol) { 35 switch (status->protocol) {
35 case PROTOCOL_UNDEF: 36 case PROTOCOL_UNDEF:
36 errno = 0; 37 errno = 0;
37 read_bytes = getline(&status->buffer, 38 int available_bytes;
38 &status->buffer_size, status->read); 39 if (ioctl(status->read_fd, FIONREAD, &available_bytes) == -1) {
39 if (errno == EAGAIN) { 40 wlr_log(WLR_ERROR, "Unable to read status command output size");
40 clearerr(status->read);
41 } else if (errno) {
42 status_error(status, "[error reading from status command]"); 41 status_error(status, "[error reading from status command]");
43 return true; 42 return true;
44 } 43 }
45 44
45 if ((size_t)available_bytes + 1 > status->buffer_size) {
46 // need room for leading '\0' too
47 status->buffer_size = available_bytes + 1;
48 status->buffer = realloc(status->buffer, status->buffer_size);
49 }
50 if (status->buffer == NULL) {
51 wlr_log_errno(WLR_ERROR, "Unable to read status line");
52 status_error(status, "[error reading from status command]");
53 return true;
54 }
55
56 read_bytes = read(status->read_fd, status->buffer, available_bytes);
57 if (read_bytes != available_bytes) {
58 status_error(status, "[error reading from status command]");
59 return true;
60 }
61 status->buffer[available_bytes] = 0;
62
46 // the header must be sent completely the first time round 63 // the header must be sent completely the first time round
64 char *newline = strchr(status->buffer, '\n');
47 json_object *header, *version; 65 json_object *header, *version;
48 if (status->buffer[read_bytes - 1] == '\n' 66 if (newline != NULL
49 && (header = json_tokener_parse(status->buffer)) 67 && (header = json_tokener_parse(status->buffer))
50 && json_object_object_get_ex(header, "version", &version) 68 && json_object_object_get_ex(header, "version", &version)
51 && json_object_get_int(version) == 1) { 69 && json_object_get_int(version) == 1) {
@@ -67,13 +85,9 @@ bool status_handle_readable(struct status_line *status) {
67 85
68 wl_list_init(&status->blocks); 86 wl_list_init(&status->blocks);
69 status->tokener = json_tokener_new(); 87 status->tokener = json_tokener_new();
70 read_bytes = getdelim(&status->buffer, &status->buffer_size, EOF, status->read); 88 status->buffer_index = strlen(newline + 1);
71 if (read_bytes > 0) { 89 memmove(status->buffer, newline + 1, status->buffer_index + 1);
72 status->buffer_index = read_bytes; 90 return i3bar_handle_readable(status);
73 return i3bar_handle_readable(status);
74 } else {
75 return false;
76 }
77 } 91 }
78 92
79 wlr_log(WLR_DEBUG, "Using text protocol."); 93 wlr_log(WLR_DEBUG, "Using text protocol.");