Commit Graph

377 Commits

Author SHA1 Message Date
Simon McVittie
c8bebf470b Merge branch 'wip/smcv/autofd' into 'master'
backports, local-alloc: Provide a backport of g_autofd

See merge request GNOME/libglnx!68
2026-04-16 11:11:29 +00:00
Simon McVittie
346eaf6204 Merge branch 'bbhtt/glib-wrap' into 'master'
backports: Wrap g_clear_fd to silence deprecations for newer Glib

Closes #7

See merge request GNOME/libglnx!72
2026-04-16 11:09:12 +00:00
bbhtt
dbc0d4f553 backports: Wrap g_clear_fd to silence deprecations for newer Glib
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
2026-04-08 19:08:57 +05:30
Simon McVittie
b478f3e737 tests: Assert that glnx_close_fd preserves errno
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>
2026-04-07 16:03:31 +01:00
Simon McVittie
606dbdbc20 testlib: Add an assertion that a fd has really been closed
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 <smcv@collabora.com>
2026-04-07 15:59:31 +01:00
Simon McVittie
1c391d734c backports, local-alloc: Provide a backport of g_autofd
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 <smcv@collabora.com>
2026-04-07 15:59:31 +01:00
Simon McVittie
ccea836b79 Merge branch 'wip/smcv/autotools' into 'master'
build: Include glnx-chase in the Autotools build system, too

See merge request GNOME/libglnx!71
2026-04-07 14:54:55 +00:00
Simon McVittie
e3006ead94 build: Include glnx-chase in the Autotools build system, too
Fixes: a973baad "chase: Add glnx_chaseat which functions similar to openat2"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-04-06 21:29:01 +01:00
Simon McVittie
aae4505722 Merge branch 'wip/smcv/chase' into 'master'
chase: Fix a memory leak and some undefined behaviour

See merge request GNOME/libglnx!69
2026-03-24 20:45:24 +00:00
Simon McVittie
6dbe18bcda chase: Factor out a function to append to the queue
Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-03-24 20:43:47 +00:00
Simon McVittie
46205a62d2 chase: Don't leak struct glnx_statx when we go up a level
Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-03-24 20:43:47 +00:00
Simon McVittie
916b70619c chase: Don't left-shift signed integer 1 by 31 places
This overflows, which is undefined behaviour (in practice it will usually
wrap around into unsigned space, but this can't be guaranteed).

Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-03-24 20:43:45 +00:00
Sebastian Wick
7c1e8bd756 Merge branch 'wip/chaseat' into 'master'
chase: Safely traversing the filesystem

See merge request GNOME/libglnx!64
2026-03-24 11:21:17 +00:00
Sebastian Wick
4bacb4332a tests: Add comprehensive tests for glnx_chase_and_statxat
Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-17 18:40:38 +01:00
Sebastian Wick
b4b9951901 chase: Add glnx_chase_and_statxat
This combines glnx_chaseat with a call to glnx_statx for convenience.
2026-03-17 18:40:38 +01:00
Sebastian Wick
5f92fc4a93 tests: Add comprehensive tests for glnx_chaseat 2026-03-17 18:40:38 +01:00
Sebastian Wick
a973baad08 chase: Add glnx_chaseat which functions similar to openat2
The selling features are:

* Support for RESOLVE_BENEATH, RESOLVE_IN_ROOT and RESOLVE_NO_SYMLINKS
* Fallback from openat2 to open_tree to openat for compatibility
* Triggering of automounts
2026-03-17 18:40:38 +01:00
Sebastian Wick
2a75ac86e9 missing: Add syscall number for openat2() and open_tree()
They are taken from an older revision of systemd with minimal changes.
2026-03-12 00:41:48 +01:00
Sebastian Wick
01508367b4 fdio: Add glnx_statx 2026-03-12 00:41:48 +01:00
Sebastian Wick
14578d856e missing: Add syscall and structs for statx()
They are taken from an older revision of systemd with minimal changes.
2026-03-12 00:41:48 +01:00
Simon McVittie
8eb974e539 Merge branch 'wip/smcv/dup-unref-object' into 'master'
[trivial] local-alloc: Remove duplicate definition of glnx_unref_object

See merge request GNOME/libglnx!67
2026-03-10 17:32:50 +00:00
Colin Walters
287353e5ae Merge branch 'wip/smcv/wpointer-sign' into 'master'
[trivial] tests: Avoid a -Wpointer-sign warning

See merge request GNOME/libglnx!66
2026-03-10 17:11:27 +00:00
Simon McVittie
27f2b28c4f local-alloc: Remove duplicate definition of glnx_unref_object
The same `#define` exists both before and after `glnx_local_obj_unref()`.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-03-05 13:29:31 +00:00
Simon McVittie
f4101e805c tests: Avoid a -Wpointer-sign warning
test_data is an array of (usually) signed char, but
glnx_file_replace_contents_at() takes an array of unsigned bytes.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2026-03-05 13:24:43 +00:00
Colin Walters
51a1c2a9b6 Merge branch 'wip/various-improvements-1' into 'master'
glnx_fd_reopen, glnx_fd_throw, gcc fanalyzer improvements

See merge request GNOME/libglnx!65
2026-03-03 14:45:27 +00:00
Sebastian Wick
0b0703c3f0 tests: Add comprehensive tests for glnx_fd_reopen
Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-21 01:29:05 +01:00
Sebastian Wick
1ec49dfc9b fdio: Add glnx_fd_reopen
Reopens the specified fd with new flags. This is useful for convert an
O_PATH fd into a regular one, or to turn O_RDWR fds into O_RDONLY fds.
2026-02-21 01:29:05 +01:00
Sebastian Wick
c33f0ed05e glnx-errors.h: add glnx_fd_throw[_*] variants
Similar to the glnx_null_throw variants, this one returns -1 instead of
FALSE or NULL. This is helpful when dealing with functions which return
a fd.
2026-02-18 00:35:43 +01:00
Sebastian Wick
cdce8957b2 backports: Add g_clear_fd 2026-02-18 00:35:43 +01:00
Sebastian Wick
6a556d05d1 lockfile: Assert non-null path in make_lock_file for analyzers
Asserting the value helps the gcc analyzer and avoids a false-positive.
2026-02-17 22:53:31 +01:00
Simon McVittie
de29c5f7d9 errors: Fix URL to an old libgsystem commit
Signed-off-by: Simon McVittie <smcv@collabora.com>
2025-05-23 18:09:03 +01:00
Colin Walters
fed928addd Merge branch 'wip/smcv/issue5' into 'master'
fdio: Avoid relying on VLAs or gcc-specific constant folding

Closes #5

See merge request GNOME/libglnx!63
2025-01-06 16:03:35 +00:00
Simon McVittie
cd4b7b53eb fdio: Avoid relying on VLAs or gcc-specific constant folding
Formally, `strlen("x")` is not a constant expression, although gcc can
and does optimize it to a constant expression in simple cases.
clang 19.1.6 warns about this usage.

Replace it with the size of a static array (1 more than the strlen of
its contents), which is unambiguously a constant expression.

Resolves: https://gitlab.gnome.org/GNOME/libglnx/-/issues/5
Signed-off-by: Simon McVittie <smcv@collabora.com>
2025-01-06 15:01:43 +00:00
Will Thompson
5544bfdd82 Merge branch 'wip/smcv/issue1' into 'master'
glnx-shutil: Cope with ENOENT even after recursing to create parents

Closes #1

See merge request GNOME/libglnx!62
2024-12-06 13:57:01 +00:00
Simon McVittie
e6151ffbc0 Add a test for glnx_shutil_mkdir_p_at with an unusable parent
This is a slight generalization of the reproducer contributed by Will
Thompson: as well as exercising the case where the parent is a dangling
symlink (reproducing GNOME/libglnx#1), this also exercises the case where
the parent is a non-directory non-symlink (in this case a regular file).

Reproduces: GNOME/libglnx#1
Co-authored-by: Will Thompson <wjt@endlessos.org>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-12-06 13:29:50 +00:00
Simon McVittie
2eb4bcc282 glnx-shutil: Cope with ENOENT even after recursing to create parents
If we try to create `link/content` where `link` is a dangling symlink,
recursing to create `link` will succeed (mkdirat fails with EEXIST,
which is explicitly ignored), but then mkdirat for `link/content` will
still fail. Fail gracefully instead of crashing out with an assertion
failure.

Resolves: GNOME/libglnx#1
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-12-06 13:29:50 +00:00
Simon McVittie
f44d944233 tests: Run each shutil test in a temporary directory
Otherwise it could potentially race with tests in other executables that
also want to create `./test`, or interfere with other test-cases in the
same executable that expect `./test` to be nonexistent or empty.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-12-06 13:29:50 +00:00
Will Thompson
b345955140 Merge branch 'wip/smcv/meson-bool' into 'master'
build: Use a boolean default for a boolean option, rather than a string

See merge request GNOME/libglnx!60
2024-12-05 11:39:21 +00:00
Simon McVittie
f92968a8d2 build: Use a boolean default for a boolean option, rather than a string
Meson 1.1.0 officially deprecates string defaults for boolean options,
but boolean defaults worked in many older Meson versions, going back to
at least 0.49.x.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-12-05 11:35:45 +00:00
Simon McVittie
87f2768fab Merge branch 'wip/smcv/glib-unix' into 'master'
glnx-backports: Include `<glib-unix.h>`

See merge request GNOME/libglnx!59
2024-08-23 13:53:06 +00:00
Simon McVittie
8ed45a1a22 glnx-backports: Include <glib-unix.h>
When compiling with GLib older than 2.80.x, we provide a compatibility
backport of g_closefrom() and g_fdwalk_set_cloexec(), but when compiling
with newer GLib, we rely on GLib to provide those.

The intended scope of libglnx (Linux only) is narrower than the scope
of `<glib-unix.h>` (any Unix platform), so we can safely include
`<glib-unix.h>` unconditionally. This provides least-astonishment by
having `#include <libglnx.h>` with newer GLib always provide a superset
of the functionality it provides with older GLib.

This would have avoided needing
https://github.com/flatpak/flatpak/pull/5737.

Fixes: 6ada39c3 "backports: Add a backport of g_closefrom(), g_fdwalk_set_cloexec()"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-05-13 14:55:37 +01:00
Simon McVittie
b38235ac2d Merge branch 'wip/smcv/issue4' into 'master'
build: Fix function detection when using -Werror=return-type

Closes #4

See merge request GNOME/libglnx!57
2024-04-20 14:59:03 +00:00
Simon McVittie
d56b85b61c Merge branch 'wip/smcv/pid-format' into 'master'
backports: Add a fallback definition for G_PID_FORMAT

See merge request GNOME/libglnx!55
2024-04-19 09:45:44 +00:00
Simon McVittie
ed4c1fbbc4 Merge branch 'wip/smcv/g-steal-fd-old-glib' into 'master'
backports: Avoid warnings for g_steal_fd when targeting older GLib

See merge request GNOME/libglnx!56
2024-04-19 09:45:29 +00:00
Simon McVittie
284b75d31b build: Fix function detection when using -Werror=return-type
Recent versions of openSUSE default to using -Werror=return-type for
package builds, which results in all of these function checks
incorrectly detecting the function as not being available.

Co-authored-by: Peng Yi
Resolves: https://gitlab.gnome.org/GNOME/libglnx/-/issues/4
Signed-off-by: Simon McVittie <smcv@debian.org>
2024-04-19 10:29:07 +01:00
Simon McVittie
e31547a6e0 backports: Avoid warnings for g_steal_fd when targeting older GLib
flatpak-builder does this.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-07 10:41:09 +00:00
Simon McVittie
f57d443926 backports: Add a fallback definition for G_PID_FORMAT
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-22 12:45:22 +00:00
Colin Walters
202b294e60 Merge branch 'wip/smcv/closefrom' into 'master'
Fixes for g_closefrom() backport

See merge request GNOME/libglnx!54
2024-02-09 21:00:45 +00:00
Simon McVittie
e4beedc0f6 backports: Use glnx-missing.h to provide missing syscalls
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 16:54:27 +00:00
Simon McVittie
1e6efc7846 missing: Add flags for close_range()
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 16:54:24 +00:00