aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/common.c4
-rw-r--r--fs/common.h16
-rw-r--r--fs/tabfs.c4
-rw-r--r--fs/ws.c8
4 files changed, 28 insertions, 4 deletions
diff --git a/fs/common.c b/fs/common.c
index 7281147..c2bf3dd 100644
--- a/fs/common.c
+++ b/fs/common.c
@@ -16,6 +16,10 @@ void common_init() {
if (pipe(ws_to_tabfs)) exit(1);
}
+// FIXME: we probably need memory fences here?? especially on
+// non-x86?? idk
+// see https://stackoverflow.com/questions/35837539/does-the-use-of-an-anonymous-pipe-introduce-a-memory-barrier-for-interthread-com
+
void common_send_tabfs_to_ws(char *request_data) {
write(tabfs_to_ws[1], &request_data, sizeof(request_data));
}
diff --git a/fs/common.h b/fs/common.h
index 79fd9f3..7004c7b 100644
--- a/fs/common.h
+++ b/fs/common.h
@@ -1,3 +1,6 @@
+// common provides an interface for tabfs.c (which talks to FUSE) to
+// talk to ws.c (which talks to the browser over WebSocket).
+
#ifndef COMMON_H
#define COMMON_H
@@ -9,7 +12,20 @@ void common_init();
typedef int (*fd_set_filler_fn_t)(fd_set*, fd_set*, fd_set*);
+// All send and receive calls are blocking!
+
void common_send_tabfs_to_ws(char *request_data);
+// This function is called by the ws thread; it blocks waiting for
+// tabfs thread to send a request _from FUSE_, which means that the ws
+// thread wouldn't be able to hear about events _from the browser_
+// while blocked here (including important ones, like 'the browser
+// wants to connect to us!').
+//
+// The hack solution is that ws passes a function `filler` to add the
+// WebSocket file descriptors to the set that
+// `common_receive_tabfs_to_ws` polls, so it _also_ waits on
+// _browser-side_ events from the WebSocket file descriptors, not just
+// FUSE-side events.
char *common_receive_tabfs_to_ws(fd_set_filler_fn_t filler);
void common_send_ws_to_tabfs(char *response_data);
diff --git a/fs/tabfs.c b/fs/tabfs.c
index 76dce41..1fa5476 100644
--- a/fs/tabfs.c
+++ b/fs/tabfs.c
@@ -12,7 +12,8 @@
#include "ws.h"
static cJSON *send_request_then_await_response(cJSON *req) {
- char *request_data = cJSON_Print(req); // Will be freed on ws side.
+ // Will be freed at receiver (ws.c, receive_tabfs_request_then_send_to_browser).
+ char *request_data = cJSON_Print(req);
common_send_tabfs_to_ws(request_data);
char *response_data = common_receive_ws_to_tabfs();
@@ -22,6 +23,7 @@ static cJSON *send_request_then_await_response(cJSON *req) {
}
cJSON *resp = cJSON_Parse((const char *) response_data);
+ // Was allocated by sender (ws.c, websocket_frame).
free(response_data);
return resp;
diff --git a/fs/ws.c b/fs/ws.c
index 6798b3f..a397eab 100644
--- a/fs/ws.c
+++ b/fs/ws.c
@@ -37,20 +37,22 @@ static int fill_fd_set_with_ws_sockets(fd_set *read_fds, fd_set *write_fds, fd_s
static void receive_tabfs_request_then_send_to_browser() {
char *request_data = common_receive_tabfs_to_ws(fill_fd_set_with_ws_sockets);
if (request_data == NULL) {
- return;
+ goto done;
}
if (con == NULL) {
common_send_ws_to_tabfs(NULL);
- return;
+ goto done;
}
wby_frame_begin(con, WBY_WSOP_TEXT_FRAME);
wby_write(con, request_data, strlen(request_data));
wby_frame_end(con);
+ done:
// Was allocated by sender (tabfs.c, send_request_then_await_response).
- free(request_data);
+ if (request_data != NULL) { free(request_data); }
+ return;
}
static int