Communicate through a Unix socket instead of a pipe pair

This commit is contained in:
Quentin Rameau 2019-09-07 13:20:09 +02:00
parent f61cfc720c
commit e92fd1aa5f
2 changed files with 65 additions and 57 deletions

58
surf.c
View file

@ -3,6 +3,7 @@
* To understand surf, start reading main().
*/
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <glib.h>
@ -187,7 +188,7 @@ 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 gboolean readsock(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,
@ -253,7 +254,7 @@ static char *stylefile;
static const char *useragent;
static Parameter *curconfig;
static int modparams[ParameterLast];
static int pipein[2], pipeout[2];
static int spair[2];
char *argv0;
static ParamName loadtransient[] = {
@ -359,13 +360,15 @@ setup(void)
gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
fputs("Unable to create pipes\n", stderr);
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, spair) < 0) {
fputs("Unable to create sockets\n", stderr);
spair[0] = spair[1] = -1;
} else {
gchanin = g_io_channel_unix_new(pipein[0]);
gchanin = g_io_channel_unix_new(spair[0]);
g_io_channel_set_encoding(gchanin, NULL, NULL);
g_io_channel_set_flags(gchanin, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_close_on_unref(gchanin, TRUE);
g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
g_io_add_watch(gchanin, G_IO_IN, readsock, NULL);
}
@ -1054,8 +1057,8 @@ spawn(Client *c, const Arg *a)
if (fork() == 0) {
if (dpy)
close(ConnectionNumber(dpy));
close(pipein[0]);
close(pipeout[1]);
close(spair[0]);
close(spair[1]);
setsid();
execvp(((char **)a->v)[0], (char **)a->v);
fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
@ -1089,8 +1092,8 @@ cleanup(void)
while (clients)
destroyclient(clients);
close(pipein[0]);
close(pipeout[1]);
close(spair[0]);
close(spair[1]);
g_free(cookiefile);
g_free(scriptfile);
g_free(stylefile);
@ -1233,30 +1236,26 @@ newview(Client *c, WebKitWebView *rv)
}
static gboolean
readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
readsock(GIOChannel *s, GIOCondition ioc, gpointer unused)
{
static char msg[MSGBUFSZ], msgsz;
static char msg[MSGBUFSZ];
GError *gerr = NULL;
gsize msgsz;
if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
G_IO_STATUS_NORMAL) {
fprintf(stderr, "surf: error reading pipe: %s\n",
if (gerr) {
fprintf(stderr, "surf: error reading socket: %s\n",
gerr->message);
g_error_free(gerr);
}
return TRUE;
}
if ((msgsz = msg[0]) < 3) {
if (msgsz < 2) {
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;
}
@ -1265,10 +1264,10 @@ initwebextensions(WebKitWebContext *wc, Client *c)
{
GVariant *gv;
if (!pipeout[0] || !pipein[1])
if (spair[1] < 0)
return;
gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
gv = g_variant_new("i", spair[1]);
webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
@ -1874,15 +1873,18 @@ msgext(Client *c, char type, const Arg *a)
static char msg[MSGBUFSZ];
int ret;
if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
4, c->pageid, type, a->i))
if (spair[0] < 0)
return;
if ((ret = snprintf(msg, sizeof(msg), "%c%c%c", c->pageid, type, a->i))
>= sizeof(msg)) {
fprintf(stderr, "surf: message too long: %d\n", ret);
return;
}
if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
if (send(spair[0], msg, ret, 0) != ret)
fprintf(stderr, "surf: error sending: %d%c%d (%dB)\n",
c->pageid, type, a->i, ret);
}
void

View file

@ -1,6 +1,8 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>
@ -18,7 +20,7 @@ typedef struct Page {
struct Page *next;
} Page;
static int pipein, pipeout;
static int sock;
static Page *pages;
Page *
@ -47,60 +49,64 @@ msgsurf(Page *p, const char *s)
size_t sln = strlen(s);
int ret;
if ((ret = snprintf(msg, sizeof(msg), "%c%c%s",
2 + sln, p ? p->id : 0, s))
if ((ret = snprintf(msg, sizeof(msg), "%c%s", p ? p->id : 0, s))
>= sizeof(msg)) {
fprintf(stderr, "webext: message too long: %d\n", ret);
fprintf(stderr, "webext: msg: 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);
if (send(sock, msg, ret, 0) < 0)
fprintf(stderr, "webext: error sending: %s\n", msg+1);
}
static gboolean
readpipe(GIOChannel *s, GIOCondition c, gpointer unused)
readsock(GIOChannel *s, GIOCondition c, gpointer unused)
{
static char msg[MSGBUFSZ], msgsz;
static char msg[MSGBUFSZ];
WebKitDOMDOMWindow *view;
GError *gerr = NULL;
gsize msgsz;
glong wh, ww;
Page *p;
if (g_io_channel_read_chars(s, msg, LENGTH(msg), NULL, &gerr) !=
if (g_io_channel_read_chars(s, msg, LENGTH(msg), &msgsz, &gerr) !=
G_IO_STATUS_NORMAL) {
fprintf(stderr, "webext: error reading pipe: %s\n",
if (gerr) {
fprintf(stderr, "webext: error reading socket: %s\n",
gerr->message);
g_error_free(gerr);
}
return TRUE;
}
if ((msgsz = msg[0]) < 3) {
fprintf(stderr, "webext: message too short: %d\n", msgsz);
if (msgsz < 2) {
fprintf(stderr, "webext: readsock: message too short: %d\n",
msgsz);
return TRUE;
}
for (p = pages; p; p = p->next) {
if (p->id == msg[1])
if (p->id == msg[0])
break;
}
if (!p || !(view = webkit_dom_document_get_default_view(
webkit_web_page_get_dom_document(p->webpage))))
return TRUE;
switch (msg[2]) {
switch (msg[1]) {
case 'h':
if (msgsz != 4)
if (msgsz != 3)
return TRUE;
ww = webkit_dom_dom_window_get_inner_width(view);
webkit_dom_dom_window_scroll_by(view,
(ww / 100) * msg[3], 0);
(ww / 100) * msg[2], 0);
break;
case 'v':
if (msgsz != 4)
if (msgsz != 3)
return TRUE;
wh = webkit_dom_dom_window_get_inner_height(view);
webkit_dom_dom_window_scroll_by(view,
0, (wh / 100) * msg[3]);
0, (wh / 100) * msg[2]);
break;
}
@ -116,15 +122,15 @@ webpagecreated(WebKitWebExtension *e, WebKitWebPage *wp, gpointer unused)
G_MODULE_EXPORT void
webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e, GVariant *gv)
{
GIOChannel *gchanpipe;
GIOChannel *gchansock;
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", &sock);
gchanpipe = g_io_channel_unix_new(pipein);
g_io_channel_set_encoding(gchanpipe, NULL, NULL);
g_io_channel_set_close_on_unref(gchanpipe, TRUE);
g_io_add_watch(gchanpipe, G_IO_IN, readpipe, NULL);
gchansock = g_io_channel_unix_new(sock);
g_io_channel_set_encoding(gchansock, NULL, NULL);
g_io_channel_set_flags(gchansock, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_close_on_unref(gchansock, TRUE);
g_io_add_watch(gchansock, G_IO_IN, readsock, NULL);
}