glib-unix: Add O_NONBLOCK support to g_unix_open_pipe()

Add support for it, but don’t use it anywhere yet — this is an API
addition, but currently doesn’t cause any functional changes. It’ll be
used in the next commit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall
2023-03-23 00:55:00 +00:00
parent 56c013cb6e
commit 5c65437d73
4 changed files with 60 additions and 24 deletions

View File

@@ -41,15 +41,22 @@ G_BEGIN_DECLS
static inline gboolean
g_unix_open_pipe_internal (int *fds,
gboolean close_on_exec)
gboolean close_on_exec,
gboolean nonblock)
{
#ifdef HAVE_PIPE2
do
{
int ecode;
int flags = 0;
if (close_on_exec)
flags |= O_CLOEXEC;
if (nonblock)
flags |= O_NONBLOCK;
/* Atomic */
ecode = pipe2 (fds, close_on_exec ? O_CLOEXEC : 0);
ecode = pipe2 (fds, flags);
if (ecode == -1 && errno != ENOSYS)
return FALSE;
else if (ecode == 0)
@@ -62,21 +69,44 @@ g_unix_open_pipe_internal (int *fds,
if (pipe (fds) == -1)
return FALSE;
if (!close_on_exec)
return TRUE;
if (fcntl (fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
fcntl (fds[1], F_SETFD, FD_CLOEXEC) == -1)
if (close_on_exec)
{
int saved_errno = errno;
if (fcntl (fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
fcntl (fds[1], F_SETFD, FD_CLOEXEC) == -1)
{
int saved_errno = errno;
close (fds[0]);
close (fds[1]);
fds[0] = -1;
fds[1] = -1;
close (fds[0]);
close (fds[1]);
fds[0] = -1;
fds[1] = -1;
errno = saved_errno;
return FALSE;
errno = saved_errno;
return FALSE;
}
}
if (nonblock)
{
#ifdef O_NONBLOCK
int flags = O_NONBLOCK;
#else
int flags = O_NDELAY;
#endif
if (fcntl (fds[0], F_SETFL, flags) == -1 ||
fcntl (fds[1], F_SETFL, flags) == -1)
{
int saved_errno = errno;
close (fds[0]);
close (fds[1]);
fds[0] = -1;
fds[1] = -1;
errno = saved_errno;
return FALSE;
}
}
return TRUE;