Hello Hiltjo,

> Where is the patch?
Sorry, see the appendix of this mail.

First let me say that i never worked with the codebase of surf before.
I realised that my previous understanding of this bug seems to be
partially wrong, so this patch works, but i don't know why exactly.
My idea to avoid the readpipe function is probably bullshit, so you
could as well just ignore this patch.

The underlying problem continues to be the fact, that the web extension
can be initialized multiple times, but i didn't figure out yet, why this
should lead to some broken pipe.
I will get back to you if i do.

Regards,
Jona
>From edf2be7f3390d9714c51771aab25c89d14cea86b Mon Sep 17 00:00:00 2001
From: Jona Ackerschott <jona....@gmail.com>
Date: Sat, 17 Oct 2020 11:46:11 +0200
Subject: [PATCH] Fix communication through closed pipe

---
 libsurf-webext.c | 23 ++---------------------
 surf.c           | 45 +++++----------------------------------------
 2 files changed, 7 insertions(+), 61 deletions(-)

diff --git a/libsurf-webext.c b/libsurf-webext.c
index ec9a235..6fa7364 100644
--- a/libsurf-webext.c
+++ b/libsurf-webext.c
@@ -18,7 +18,7 @@ typedef struct Page {
        struct Page *next;
 } Page;
 
-static int pipein, pipeout;
+static int pipein;
 static Page *pages;
 
 Page *
@@ -38,24 +38,6 @@ newpage(WebKitWebPage *page)
        return p;
 }
 
-static void
-msgsurf(Page *p, const char *s)
-{
-       static char msg[MSGBUFSZ];
-       size_t sln = strlen(s);
-       int ret;
-
-       if ((ret = snprintf(msg, sizeof(msg), "%c%c%s",
-                           2 + sln, p ? p->id : 0, s))
-           >= sizeof(msg)) {
-               fprintf(stderr, "webext: message too long: %d\n", ret);
-               return;
-       }
-
-       if (pipeout && write(pipeout, msg, sizeof(msg)) < 0)
-               fprintf(stderr, "webext: error sending: %.*s\n", ret-2, msg+2);
-}
-
 static gboolean
 readpipe(GIOChannel *s, GIOCondition c, gpointer unused)
 {
@@ -118,8 +100,7 @@ 
webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e, GVariant *
 
        g_signal_connect(e, "page-created", G_CALLBACK(webpagecreated), NULL);
 
-       g_variant_get(gv, "(ii)", &pipein, &pipeout);
-       msgsurf(NULL, "i");
+       g_variant_get(gv, "(i)", &pipein);
 
        gchanpipe = g_io_channel_unix_new(pipein);
        g_io_channel_set_encoding(gchanpipe, NULL, NULL);
diff --git a/surf.c b/surf.c
index 2b54e3c..2a8edbd 100644
--- a/surf.c
+++ b/surf.c
@@ -185,7 +185,6 @@ static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, 
Client *c);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
                                 gpointer d);
 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
-static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
 static void showview(WebKitWebView *v, Client *c);
 static GtkWidget *createwindow(Client *c);
 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
@@ -251,7 +250,7 @@ static char *stylefile;
 static const char *useragent;
 static Parameter *curconfig;
 static int modparams[ParameterLast];
-static int pipein[2], pipeout[2];
+static int pipeout[2];
 char *argv0;
 
 static ParamName loadtransient[] = {
@@ -343,13 +342,8 @@ setup(void)
 
        gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
 
-       if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
+       if (pipe(pipeout) < 0) {
                fputs("Unable to create pipes\n", stderr);
-       } else {
-               gchanin = g_io_channel_unix_new(pipein[0]);
-               g_io_channel_set_encoding(gchanin, NULL, NULL);
-               g_io_channel_set_close_on_unref(gchanin, TRUE);
-               g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
        }
 
 
@@ -1038,7 +1032,6 @@ spawn(Client *c, const Arg *a)
        if (fork() == 0) {
                if (dpy)
                        close(ConnectionNumber(dpy));
-               close(pipein[0]);
                close(pipeout[1]);
                setsid();
                execvp(((char **)a->v)[0], (char **)a->v);
@@ -1073,7 +1066,7 @@ cleanup(void)
        while (clients)
                destroyclient(clients);
 
-       close(pipein[0]);
+       close(pipeout[0]);
        close(pipeout[1]);
        g_free(cookiefile);
        g_free(scriptfile);
@@ -1206,43 +1199,15 @@ newview(Client *c, WebKitWebView *rv)
        return v;
 }
 
-static gboolean
-readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
-{
-       static char msg[MSGBUFSZ], msgsz;
-       GError *gerr = NULL;
-
-       if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
-           G_IO_STATUS_NORMAL) {
-               fprintf(stderr, "surf: error reading pipe: %s\n",
-                       gerr->message);
-               g_error_free(gerr);
-               return TRUE;
-       }
-       if ((msgsz = msg[0]) < 3) {
-               fprintf(stderr, "surf: message too short: %d\n", msgsz);
-               return TRUE;
-       }
-
-       switch (msg[2]) {
-       case 'i':
-               close(pipein[1]);
-               close(pipeout[0]);
-               break;
-       }
-
-       return TRUE;
-}
-
 void
 initwebextensions(WebKitWebContext *wc, Client *c)
 {
        GVariant *gv;
 
-       if (!pipeout[0] || !pipein[1])
+       if (!pipeout[0])
                return;
 
-       gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
+       gv = g_variant_new("(i)", pipeout[0]);
 
        webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
        webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
-- 
2.28.0

Reply via email to