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
+32 -30
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",
gerr->message);
g_error_free(gerr);
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