mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-11 04:58:56 +02:00
g_unix_set_fd_nonblocking: New API to control file descriptor blocking state
And use it in relevant places in GLib. https://bugzilla.gnome.org/show_bug.cgi?id=649225
This commit is contained in:
@ -135,6 +135,56 @@ g_unix_pipe_flags (int *fds,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_unix_set_fd_nonblocking:
|
||||
* @fd: A file descriptor
|
||||
* @nonblock: If %TRUE, set the descriptor to be non-blocking
|
||||
* @error: a #GError
|
||||
*
|
||||
* Control the non-blocking state of the given file descriptor,
|
||||
* according to @nonblock. On most systems this uses %O_NONBLOCK, but
|
||||
* on some older ones may use %O_NDELAY.
|
||||
*
|
||||
* Returns: %TRUE if successful
|
||||
*/
|
||||
gboolean
|
||||
g_unix_set_fd_nonblocking (gint fd,
|
||||
gboolean nonblock,
|
||||
GError **error)
|
||||
{
|
||||
#ifdef F_GETFL
|
||||
glong fcntl_flags;
|
||||
fcntl_flags = fcntl (fd, F_GETFL);
|
||||
|
||||
if (fcntl_flags == -1)
|
||||
return g_unix_set_error_from_errno (error);
|
||||
|
||||
if (nonblock)
|
||||
{
|
||||
#ifdef O_NONBLOCK
|
||||
fcntl_flags |= O_NONBLOCK;
|
||||
#else
|
||||
fcntl_flags |= O_NDELAY;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef O_NONBLOCK
|
||||
fcntl_flags &= ~O_NONBLOCK;
|
||||
#else
|
||||
fcntl_flags &= ~O_NDELAY;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
|
||||
return g_unix_set_error_from_errno (error);
|
||||
return TRUE;
|
||||
#else
|
||||
return g_unix_set_error_from_errno_saved (error, EINVAL);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* g_unix_signal_source_new:
|
||||
* @signum: A signal number
|
||||
|
Reference in New Issue
Block a user