Commit Graph

370 Commits

Author SHA1 Message Date
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
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
Simon McVittie
debdea2907 missing: Add a wrapper for close_range()
This is not the same as systemd's, because systemd's disagrees
with glibc on the signedness of the arguments
(https://github.com/systemd/systemd/issues/31270).

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 16:54:14 +00:00
Simon McVittie
9d66aa7375 missing: Add syscall number for close_range()
Taken from systemd, but replacing assert_cc with G_STATIC_ASSERT.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 16:54:03 +00:00
Simon McVittie
f2ff19d7c8 build: Really check for close_range() as intended
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 16:50:04 +00:00
Colin Walters
c2c6c9502a Merge branch 'wip/smcv/closefrom' into 'master'
backports: Add a backport of g_closefrom(), g_fdwalk_set_cloexec()

See merge request GNOME/libglnx!53
2024-02-09 15:43:34 +00:00
Simon McVittie
6ada39c384 backports: Add a backport of g_closefrom(), g_fdwalk_set_cloexec()
These will be new API in GLib 2.79.2.

The only code change in the implementation, other than the _glnx
wrappers, was to use `close()` instead of `g_close (fd, NULL)` in a
context where it must be async-signal safe.

The test-case needed some more adjustments to be backwards-compatible
with GLib from the distant past.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-09 15:29:52 +00:00
Colin Walters
b415d04688 Merge branch 'pr/fix-lsetxattrat' into 'master'
glnx-xattrs: Fix invalid argument to lsetxattr()

See merge request GNOME/libglnx!52
2024-01-10 16:12:28 +00:00
Jonathan Lebon
79b809ae68 glnx-xattrs: Fix invalid argument to lsetxattr()
We weren't passing the right path argument here.

This bug would've been quickly noticed if the function were actually
used but I still did at least a global GitHub search which didn't return
any users.
2024-01-09 16:48:32 -05:00