gunix: Fix {Input,Output}Stream pollable detection

For devices such as PTYs, where not being able to cancel a pending read
operation is problematic for many applications.

Fixes: #1180
This commit is contained in:
Ole André Vadla Ravnås
2021-01-19 22:14:41 +01:00
parent 4dc7603b3e
commit d7ee70c013
8 changed files with 282 additions and 32 deletions

View File

@@ -20,12 +20,9 @@
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <glib.h>
@@ -38,6 +35,7 @@
#include "gfiledescriptorbased.h"
#include "glibintl.h"
#include "gioprivate.h"
#include "giounix-private.h"
/**
@@ -66,7 +64,7 @@ enum {
struct _GUnixOutputStreamPrivate {
int fd;
guint close_fd : 1;
guint is_pipe_or_socket : 1;
guint can_poll : 1;
};
static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
@@ -186,10 +184,7 @@ g_unix_output_stream_set_property (GObject *object,
{
case PROP_FD:
unix_stream->priv->fd = g_value_get_int (value);
if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
unix_stream->priv->is_pipe_or_socket = TRUE;
else
unix_stream->priv->is_pipe_or_socket = FALSE;
unix_stream->priv->can_poll = _g_fd_is_pollable (unix_stream->priv->fd);
break;
case PROP_CLOSE_FD:
unix_stream->priv->close_fd = g_value_get_boolean (value);
@@ -339,7 +334,7 @@ g_unix_output_stream_write (GOutputStream *stream,
poll_fds[0].events = G_IO_OUT;
nfds++;
if (unix_stream->priv->is_pipe_or_socket &&
if (unix_stream->priv->can_poll &&
g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
nfds++;
@@ -446,7 +441,7 @@ g_unix_output_stream_writev (GOutputStream *stream,
poll_fds[0].events = G_IO_OUT;
nfds++;
if (unix_stream->priv->is_pipe_or_socket &&
if (unix_stream->priv->can_poll &&
g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
nfds++;
@@ -532,7 +527,7 @@ g_unix_output_stream_close (GOutputStream *stream,
static gboolean
g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream)
{
return G_UNIX_OUTPUT_STREAM (stream)->priv->is_pipe_or_socket;
return G_UNIX_OUTPUT_STREAM (stream)->priv->can_poll;
}
static gboolean