mirror of
https://github.com/flatpak/flatpak.git
synced 2026-06-26 17:26:50 -04:00
We document glnx_close_fd as preserving errno, so let's assert that it really does. There are three code paths we need to exercise: 1. fd < 0: glnx_close_fd does nothing, successfully 2. fd >= 0 and close() succeeds 3. fd >= 0 and close() fails The first two are easy, but it's difficult to make close() fail on-demand with only valid code. close(2) documents EIO, but it's difficult to cause an I/O error on-demand. Similarly, close(2) documents ENOSPC and EDQUOT on NFS, but we are unlikely to have a full NFS filesystem available during testing. Instead, we can trigger a failure via the programming error of passing a fd to glnx_close_fd that was already closed, which makes close(2) fail with EBADF. In older libglnx, we wouldn't have been able to test this because it caused an assertion failure, but in GLib and new libglnx it only causes a critical warning, which we can catch and ignore. See also GLib commit GNOME/glib@f1f711dc "tests: Test EBADF and errno handling when closing fds". GLib doesn't have a 1:1 equivalent of glnx_close_fd as public API, but an internal version is used to implement g_autofd. Signed-off-by: Simon McVittie <smcv@collabora.com>