Commit Graph

64 Commits

Author SHA1 Message Date
Simon McVittie
54ad67db3f Merge branch 'wip/smcv/disable-crash-reporting' into 'master'
testutils: Add a backport of g_test_disable_crash_reporting()

See merge request GNOME/libglnx!50
2023-08-29 18:47:47 +00:00
Simon McVittie
30b89b830d testutils: Add a backport of g_test_disable_crash_reporting()
When testing something that is expected to fail or crash, it's useful
to disable core dump reporting.

This is a slightly simplified version of GNOME/glib!3510: because
libglnx isn't portable to non-Linux, we can assume that <sys/prctl.h>,
prctl() and <sys/resource.h> are always available, so we don't need
to check for them in the build system.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-28 14:50:13 +01:00
Ludovico de Nittis
52f66d482b backports: Add g_strv_equal from GLib >= 2.60
This is a very useful utility function that allows you to compare two
strings arrays.

Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
2023-03-01 09:42:00 +01:00
Simon McVittie
ea18312ed0 backports: Add g_steal_fd, from GLib >= 2.70
This is essentially the same as glnx_steal_fd, so make glnx_steal_fd an
alias for it.

The unit test is taken from GLib, slightly modified to avoid g_close()
and also test the old name glnx_steal_fd().

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-08 11:17:41 +00:00
Simon McVittie
5275410e6c tests: Add a simple test for g_memdup2, from GLib
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-10-28 12:07:45 +01:00
Simon McVittie
e701578c6f Merge branch 'wip/smcv/assert-true-false' into 'master'
backport-testutils: Add g_assert_true(), g_assert_false()

See merge request GNOME/libglnx!44
2022-10-10 18:29:50 +00:00
Simon McVittie
416744a265 tests: Ensure saveptr is NULL before first call to strtok_r()
The standards-conformant behaviour is that the contents of saveptr
are ignored when the first argument is non-NULL, but the man page notes
that some older implementations required saveptr to be NULL in that
situation, and the gcc 4.8 provided by the Steam Runtime warns about this.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-10-10 15:35:11 +01:00
Simon McVittie
a91959e0d4 backport-testutils: Add g_assert_true(), g_assert_false()
These were only added in 2.38, and some projects with particularly
ancient dependencies (like the Steam Runtime, based on a 2012 version
of Ubuntu) still avoid depending on that version.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-10-10 15:27:56 +01:00
Will Thompson
8f2e676d9b Merge branch 'wip/smcv/warnings' into 'master'
build: Explicitly disable warnings for non-ISO C features

See merge request GNOME/libglnx!42
2022-10-10 13:43:00 +00:00
Simon McVittie
2e46d5b145 Merge branch 'wip/smcv/testing-helpers' into 'master'
Backport g_test_skip_printf(), etc.

See merge request GNOME/libglnx!38
2022-10-10 13:36:44 +00:00
Simon McVittie
45108f1852 build: Explicitly disable warnings for non-ISO C features
libglnx is intentionally not portable to non-Unix platforms or to
compilers that do not implement gcc/clang extensions, so it's
counterproductive to warn about these extensions, even if libglnx is used
by a parent project that generally (for the parts that don't use
libglnx) wants to be portable to any ISO C compiler.

Suggested by Will Thompson on !36.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-10-10 14:09:29 +01:00
Simon McVittie
c52b73d4ae Backport most of the test convenience helpers from GLib's GTest
This includes a test (who tests the tests themselves?). Run
as `${build}/tests/testing --tap` with semi-modern GLib, or
`${build}/tests/testing --verbose` with GLib < 2.38, to check that the
output is as reasonable as possible given the limitations of the GLib
version in use.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-08-19 10:59:24 +01:00
Simon McVittie
9a0e6fd004 testlib: Be compatible with ancient GLib by using glnx_close_fd
This is an enabler for testing a backport of GTest helpers
(g_test_skip(), etc.) in the oldest environment that I have conveniently
available (Steam Runtime 1 'scout' with GLib 2.32).

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-08-19 10:56:30 +01:00
Simon McVittie
c5e1b295bc backports: Backport g_info()
The rest of the g_log() wrappers have been there since time immemorial,
but g_info() was initially omitted. It's useful for projects like Flatpak
that want to have two levels of off-by-default logging, which map nicely
to info and debug.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-27 16:37:57 +01:00
Phaedrus Leeds
c59eb271b5 Merge branch 'wip/smcv/warnings' into 'master'
Fix and enable more compiler warnings

See merge request GNOME/libglnx!35
2022-05-10 18:08:28 +00:00
Simon McVittie
7b26e39a1f tests: Make signedness of timestamp agree
This silences a warning from gcc -Wsign-compare. g_get_monotonic_time()
returns a signed int64.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-24 11:07:38 +01:00
Simon McVittie
4c51a54c36 tests: Make iterator signedness agree with limit
g_variant_n_children() returns a size_t (or gsize, which is equivalent)
so in principle it could overflow an int, although in practice we are
not going to have that many extended attributes. This silences a
warning from gcc -Wsign-compare.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-24 11:06:51 +01:00
Simon McVittie
238f7bfc49 fdio: Skip glnx_try_fallocate() if _GNU_SOURCE is not defined
fallocate() is only visible in fcntl.h if _GNU_SOURCE is defined.
Most users of libglnx will want to do that anyway, but it seems nicer
to avoid "implicit declaration of function ‘fallocate’" warnings from
simply including <libglnx.h> into naive code.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 19:56:41 +01:00
Simon McVittie
4d5b1fcf02 tests: Verify libglnx can work as a subproject
What isn't tested usually doesn't work, and the only way to use libglnx
is as a subproject, so test it like that.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 19:51:53 +01:00
Simon McVittie
96ba9f4d8d tests: Optionally skip building and running the actual tests
If we're building libglnx as a subproject in a larger project, we won't
necessarily want to run these.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 19:44:13 +01:00
Simon McVittie
c500c362b8 tests: Build libglnx-testlib as a separate static library
One of Flatpak's unit tests uses this, for _GLNX_TEST_SCOPED_TEMP_DIR.
Its API is not really stable, but libglnx is a "copylib" anyway, so
none of it has (or needs) a strictly stable API.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 19:44:13 +01:00
Simon McVittie
b6ee0e2423 tests: Don't copy extended attributes when testing pseudo-files
It seems that on SELinux systems, files in /proc have extended
attributes that cannot be copied by an unprivileged user.

Signed-off-by: Simon McVittie <smcv@debian.org>
2022-02-22 10:26:39 +00:00
Simon McVittie
68191d8274 Update copyright holder for Endless' contributions
Will Thompson reports that all of Endless' IP was transferred to
Endless OS Foundation LLC.

Signed-off-by: Simon McVittie <smcv@debian.org>
2022-02-19 22:57:52 +00:00
Simon McVittie
81c6adb0f7 Declare copyright and licensing using REUSE
To fill in some gaps, I've had to make some assumptions:

* trivial changes (such as checking for an additional function or
  header file in libglnx.m4) are assumed to not be copyrightable
* Will Thompson and Matthew Leeds are assumed to be contributing on
  behalf of Endless Mobile Inc.
* files with no explicit licensing information are assumed to be
  under the license found in COPYING

Reference: https://reuse.software/
Signed-off-by: Simon McVittie <smcv@debian.org>
2022-02-19 22:57:48 +00:00
Colin Walters
a29f340fb6 Merge branch 'wip/smcv/copy-pseudo-files' into 'master'
fdio: Use a read/write loop until EOF if st_size is zero

See merge request GNOME/libglnx!31
2022-02-18 14:06:47 +00:00
Simon McVittie
586bdfe1c3 fdio: Use a read/write loop until EOF if st_size is zero
Some pseudo-files in /proc and /sys list a size of 0 bytes in stat()
results, but do in fact have content. For these pseudo-files, the only
way to find out the true file size is to try read() until EOF, so leave
the max_bytes set to -1.

copy_file_range() and sendfile() don't work for such files in some
kernel versions (see <https://lwn.net/Articles/846403/>), so skip the
fast-paths and use a read() and write() loop. For pseudo-files, we
expect to be able to copy the whole content in one read() in any case.

Signed-off-by: Simon McVittie <smcv@debian.org>
2022-01-28 11:41:47 +00:00
Simon McVittie
c306703c6f Include libglnx-config.h instead of config.h
This avoids colliding with a config.h generated by a parent Meson
project.

In the Meson build system, we generate libglnx-config.h by doing our
own checks, so we want to avoid it colliding.

In the Autotools build system, we assume that the parent project will
generate its own config.h that contains the results of LIBGLNX_CONFIGURE,
and create a forwarding header libglnx-config.h in the $(top_builddir)
(so that it is next to config.h).

Note that after updating libglnx in an Autotools non-recursive-Make
project (libostree, flatpak, flatpak-builder) it will be necessary to
re-run autogen.sh.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 15:53:32 +01:00
Simon McVittie
bf851fbe08 Merge branch 'auto-temp-dir-leak' into 'master'
_GLNX_TEST_SCOPED_TEMP_DIR: Fix memory and fd leak

See merge request GNOME/libglnx!14
2020-03-15 18:23:36 +00:00
Simon McVittie
a652ede20b _GLNX_TEST_SCOPED_TEMP_DIR: Fix memory and fd leak
This doesn't really matter, since it only happens when our process is
about to exit anyway, but it makes it easier to use AddressSanitizer
and similar tools.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-11-11 19:29:28 +00:00
Simon McVittie
bc9c31f785 _GLNX_TEST_SCOPED_TEMP_DIR: Mark variable as G_GNUC_UNUSED
Otherwise, clang diagnoses it as unused. It is - deliberately - only
allocated and cleaned up, with no other use.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-11-11 18:37:38 +00:00
Simon McVittie
3063c69515 Make tests cope with older GLib
Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-11-11 18:32:06 +00:00
Simon McVittie
04c11c7390 Run the fdio test in its own temporary directory
The temporary directory will be deleted on success, but will remain
intact on failure.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2019-05-02 19:14:50 +01:00
Will Thompson
0f53dc35e4 Add meson.build files 2019-03-28 16:09:32 +00:00
Colin Walters
771d7a01d1 tree-wide: Use glnx_autofd and glnx_close_fd()
Port to `glnx_autofd` tree wide, and add one missed `glnx_close_fd()` use in the
tmpfile code.
2017-10-16 18:06:53 -04:00
Jonathan Lebon
b923a950af tests: drop unused variable 2017-10-11 20:03:12 +00:00
Jonathan Lebon
5362f6bc3f fdio: allow NULL for fstatat_allow_noent stbuf
Often, the caller doesn't actually care about the details of the stat
struct itself, but just whether the entry exists or not. It does work
to just pass `NULL` directly to glibc in a quick test, but given that
the argument is tagged as `__nonnull` and that the documentation does
not explicitly specify this is supported, let's do this safely.
2017-10-06 21:45:55 +00:00
Philip Withnall
e30154431d shutil: Fix assertion failure in glnx_shutil_mkdir_p_at()
If the directory for @dfd is deleted after being opened,
glnx_shutil_mkdir_p_at() would fail with an assertion failure. Fix that,
and make it return an ENOENT error instead.

Add a unit test.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Reviewed-by: Colin Walters <walters@verbum.org>
Reviewed-by: Jonathan Lebon <jlebon@redhat.com>

https://github.com/ostreedev/ostree/issues/1215
2017-09-26 15:08:04 +01:00
Colin Walters
5ee2f1be7a fdio: Open target dirname for glnx_file_copy_at()
Particularly if `AT_FDCWD` is used, we need to open
in the target dir, otherwise we can get `EXDEV` when trying
to do the final link.

(Theoretically we can cross a mountpoint even with fd-relative
 though this is a lot less likely)
2017-09-25 11:53:40 -04:00
Colin Walters
c2bcca04ba tests: Add macro for auto-error checking
Having our tests forced into a `goto out` style is seriously annoying
since we can't write tests like we write production code.  Add
a macro that checks for the error being NULL.

This doesn't fully solve the problem since the test functions are
still forced into `void` returns; at some point I may extend
GLib to have `g_test_add_err_func()`.
2017-09-13 10:57:30 -04:00
Colin Walters
673f48f6ca fdio: Use O_TMPFILE + rename-overwrite for regfile copies
I was working on rpm-ostree unified core, and hit the fact that
`glnx_file_copy_at()` had the same bug with `fsetxattr()` and files whose mode
is <= `0400` (e.g. `000` in the case of `/etc/shadow`) that libostree did a
while ago.  Basically, Linux currently allows `write()` on non-writable open files
but not `fsetxattr()`.  This situation is masked for privileged (i.e.
`CAP_DAC_OVERRIDE`) code.

Looking at this, I think it's cleaner to convert to `O_TMPFILE` here,
since that code already handles setting the tmpfile to mode `0600`.  Now,
this *is* a behavior change in the corner case of existing files which
are symbolic links.  Previously we'd do an `open(O_TRUNC)` which would follow
the link.

But in the big picture, I think the use cases for `open(O_TRUNC)` are really
rare - I audited all callers of this in ostree/rpm-ostree/flatpak, and all of
them will be fine with this behavior change. For example, the ostree `/etc`
merge code already explicitly unlinks the target beforehand. Other cases like
supporting `repo/pubring.gpg` in an ostree repo being a symlink...eh, just no.

Making this change allows us to convert to new style, and brings all of the
general benefits of using `O_TMPFILE` too.
2017-09-12 11:05:59 -04:00
Colin Walters
9d995a3620 fdio: Support taking ownership of tmpfile fd
While reading a strace I noticed a double close in the tests; this was because
we were missing an assignment to `-1` in the tests. However, let's make
supporting this clearer by explicitly supporting the fd being `-1` while still
setting the `initialized` variable to `FALSE`. We also add the `EBADF` assertion
checking.
2017-09-12 09:43:05 -04:00
Colin Walters
627d4e2f15 fdio: Add glnx_fstatat_allow_noent()
This is a very common pattern in both ostree/rpm-ostree. Make a better API for
this. I thought a lot about simply zeroing out `struct stat` but that feels
dangerous; none of the values have seem obviously `cannot be zero`.
2017-09-07 16:05:26 -04:00
Jonathan Lebon
47d8163293 test-libglnx-xattrs.c: appease -Wunused-variable 2017-08-25 11:02:37 -04:00
Colin Walters
7100ebbc68 dirfd: New tmpdir API
Basically all of the ostree/rpm-ostree callers want to both create and open, so
let's merge `glnx_mkdtempat()` and `glnx_mkdtempat_open()`.

Second, all of them want to do `glnx_shutil_rm_rf_at()` on cleanup, so we do the
same thing we did with `GLnxTmpfile` and create `GLnxTmpDir` that has a cleanup
attribute.

The cleanup this results in for rpm-ostree is pretty substantial.
2017-08-18 16:01:38 -04:00
Colin Walters
ea6df95f22 tests: Fix a -Wmaybe-uninitialized warning
It'd be really nice if gtest had a variant which had the funcs take `GError`.
May work on that.
2017-07-24 12:01:25 -04:00
Colin Walters
268ae48816 fdio: Introduce glnx_openat_read()
This is kind of long overdue. Reasons are the same as the other wrappers. I
debated adding `O_NOFOLLOW` support but the use cases for that are pretty
obscure, callers who want that can just use the syscall directly for now.
2017-07-19 11:35:09 -04:00
Colin Walters
607f1775bb errors: Add GLNX_AUTO_PREFIX_ERROR
In a lot of places in ostree, we end up prefixing errors in the *caller*.
Often we only have 1-2 callers, and doing the error prefixing isn't
too duplicative.  But there are definitely cases where it's cleaner
to do the prefixing in the callee.  We have functions that aren't
ported to new style for this reason (they still do the prefixing
in `out:`).

Introduce a cleanup-oriented version of error prefixing so we can port those
functions too.
2017-07-17 12:23:31 -04:00
Colin Walters
e30a773f2c fdio: Add cleanup+flush API for FILE*
Mostly in ostree/rpm-ostree, we work in either raw `int fd`, or
`G{Input,Output}Stream`.  One exception is the rpm-ostree `/etc/passwd`
handling, which uses `FILE*` since that's what glibc exposes.

And in general, there are use cases for `FILE*`; the raw `GUnixOutputStream` for
example isn't buffered, and doing so via e.g. `GBufferedOutputStream` means
allocating *two* GObjects and even worse going through multiple vfuncs for every
write.

`FILE*` is used heavily in systemd, and provides buffering. It is a bit cheaper
than gobjects, but has its own trap; by default every operation locks a mutex.
For more information on that, see `unlocked_stdio(3)`. However, callers can
avoid that by using e.g. `fwrite_unlocked`, which I plan to do for most users of
`FILE*` that aren't writing to one of the standard streams like `stdout` etc.
2017-07-17 12:06:26 -04:00
Colin Walters
452c371ff3 fdio: Ensure O_TMPFILE is mode 0600
Work around an older glibc bug.
2017-07-10 12:12:29 -04:00
Colin Walters
01e934c18e tests: Fix compilation of fdio test
Not sure how I missed this before.
2017-06-28 11:23:02 -04:00