gstdio: make g_close() async-signal-safe under certain conditions

g_close() does something useful. It is not trivial to get EINTR handling of
close() right, in a portable manner. g_close() abstracts this.

We should allow glib users to use the function even in async-signal-safe
contexts, at least if the user heeds the caveat about GError and take care
not to fail assertions.
This commit is contained in:
Thomas Haller 2022-10-18 09:07:35 +02:00
parent 6418db829b
commit 3ed7f4d1ed

View File

@ -1749,8 +1749,9 @@ g_utime (const gchar *filename,
* @fd: A file descriptor * @fd: A file descriptor
* @error: a #GError * @error: a #GError
* *
* This wraps the close() call; in case of error, %errno will be * This wraps the close() call. In case of error, %errno will be
* preserved, but the error will also be stored as a #GError in @error. * preserved, but the error will also be stored as a #GError in @error.
* In case of success, %errno is undefined.
* *
* Besides using #GError, there is another major reason to prefer this * Besides using #GError, there is another major reason to prefer this
* function over the call provided by the system; on Unix, it will * function over the call provided by the system; on Unix, it will
@ -1759,6 +1760,9 @@ g_utime (const gchar *filename,
* *
* It is a bug to call this function with an invalid file descriptor. * It is a bug to call this function with an invalid file descriptor.
* *
* Since 2.76, this function is guaranteed to be async-signal-safe if (and only
* if) @error is %NULL and @fd is a valid open file descriptor.
*
* Returns: %TRUE on success, %FALSE if there was an error. * Returns: %TRUE on success, %FALSE if there was an error.
* *
* Since: 2.36 * Since: 2.36
@ -1769,6 +1773,9 @@ g_close (gint fd,
{ {
int res; int res;
/* Important: if @error is NULL, we must not do anything that is
* not async-signal-safe.
*/
res = close (fd); res = close (fd);
if (res == -1) if (res == -1)
@ -1790,12 +1797,17 @@ g_close (gint fd,
return TRUE; return TRUE;
} }
g_set_error_literal (error, G_FILE_ERROR, if (error)
g_file_error_from_errno (errsv), {
g_strerror (errsv)); g_set_error_literal (error, G_FILE_ERROR,
g_file_error_from_errno (errsv),
g_strerror (errsv));
}
if (errsv == EBADF) if (errsv == EBADF)
{ {
/* There is a bug. Fail an assertion. Note that this function is supposed to be
* async-signal-safe, but in case an assertion fails, all bets are already off. */
if (fd >= 0) if (fd >= 0)
{ {
/* Closing an non-negative, invalid file descriptor is a bug. The bug is /* Closing an non-negative, invalid file descriptor is a bug. The bug is