Skip to content

[1.18] Update subtree: libglnx 2026-06-18 - #6752

Merged
swick merged 14 commits into
flatpak:flatpak-1.18.xfrom
swick:backport/1.18/libglnx-update
Aug 3, 2026
Merged

swick merged 14 commits into
flatpak:flatpak-1.18.xfrom
swick:backport/1.18/libglnx-update

Conversation

@swick

@swick swick commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

This will become useful for other backports soo.

smcv and others added 14 commits April 7, 2026 15:59
The functionality that was prototyped in libglnx as glnx_fd_close
and then glnx_autofd was later added to GLib as g_autofd.

glnx_close_fd() doesn't have a direct equivalent in GLib, so keep
it intact, but using the backported _glnx_clear_fd_ignore_error as
its implementation. g_clear_fd() is the closest thing in GLib, but
g_clear_fd() guarantees to set errno on failure (making it useful for
error-checking), whereas glnx_close_fd() guarantees to leave errno
untouched, making it more useful for cleanup code paths that recover
from a syscall or similar function that sets errno:

    int fd = ...;

    success = (fsync (fd) == 0);
    glnx_close_fd (&fd);
    return success;    /* if false, errno indicates why fsync failed */

(Of course in many cases, including this simple example, it would have
been easier to use g_autofd.)

glnx_autofd and glnx_fd_close are now equivalent to the backport of
g_autofd, so document them as deprecated. There's essentially no cost to
retaining them, so don't apply deprecation attributes.

Signed-off-by: Simon McVittie <[email protected]>
We can't easily assert this without triggering warnings from tools like
valgrind by doing an invalid operation on a closed fd, so we only check
this when under `-m undefined`.

Originally contributed to GLib 2.76 in GNOME/glib@b3934133
"gstdio: Add g_clear_fd() and g_autofd". The implementation in GLib used
g_fsync() as a portable thing that we can do with a fd, but that
function is newer than our minimum GLib version, and libglnx isn't
portable to non-Unix anyway, so use fnctl() instead.

Signed-off-by: Simon McVittie <[email protected]>
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 <[email protected]>
When building with newer GLib with GLIB_VERSION_MAX_ALLOWED set
to < 2.76 in downstream, g_clear_fd is marked deprecated in
libglnx and triggers deprecation warnings at each call site.

This forwards to g_clear_fd while silencing deprecation warnings for
Glib >= 2.76

Fixes: https://gitlab.gnome.org/GNOME/libglnx/-/issues/7
backports: Wrap g_clear_fd to silence deprecations for newer Glib

Closes flatpak#7

See merge request GNOME/libglnx!72
backports, local-alloc: Provide a backport of g_autofd

See merge request GNOME/libglnx!68
This makes it possible to use a standard dependency('libglnx') call in a
parent project when libglnx is used as a subproject.
fdio: Add support for name_to_handle_at

See merge request GNOME/libglnx!70
build: Add meson.override_dependency('libglnx', libglnx_dep)

See merge request GNOME/libglnx!77
It takes a callback which gets called every time we try to open the next
segment of the path. This allows implementing more specific and advanced
use cases to be implemented without adding more complexity to the chase
algorithm itself.
We found that there is a common use case where we need to get a
subdirectory (potentially multiple levels) which might not exist yet.
Adding another flag for this to GlnxChaseFlags is what systemd has done,
but creating a directory takes a mode, so the flag creates directories
with a fixed mode. This approach instead takes the mode as argument.
chase: Add glnx_chase_and_mkdirat

See merge request GNOME/libglnx!76
* backports, local-alloc: Provide a backport of g_autofd
* build: Add meson.override_dependency('libglnx', libglnx_dep)
* fdio: Add support for name_to_handle_at
* chase: Add glnx_chase_and_mkdirat

Signed-off-by: Sebastian Wick <[email protected]>
@swick

swick commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

git subtree not letting me merge this again...

@swick
swick merged commit 47e74f2 into flatpak:flatpak-1.18.x Aug 3, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants