Commit Graph

172 Commits

Author SHA1 Message Date
Colin Walters
dea16cd8be fdio: Squash compiler warning from previous commit
Oops.
2017-10-05 14:45:40 -04:00
Colin Walters
b72906dbe0 fdio: Generate tmpname for RENAME_EXCHANGE fallback
I was using this in rpm-ostree and glanced at the code. This was clearly the
intent, but isn't a full fix. See code comments for more details.
2017-10-02 10:09:02 -04:00
Colin Walters
dd5fd9c1e5 missing: Sync from latest systemd, add memfd_create()
Planning to use memfd_create() in flatpak and rpm-ostree, which both use
bubblewrap, and want to pass read-only data via file descriptor to the
container. Passing via `O_TMPFILE` requires `O_RDWR` (read and write),
and passing via a pipe would require buffering.

The systemd `missing.h` has grown enormously; I only cherry-picked the bits for
memfd.
2017-10-01 09:51:14 -07: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
32a4293101 lockfile: Use an initialized member rather than explicit init
This makes us more friendly to being embedded in a GObject or
the like that's fully zero-initialized, rather than relying on the special
`-1` value for the fd.

Calls to `glnx_release_lock_file()` become idempotent, so it's easy to call it
unconditionally in an object finalizer.
2017-09-25 15:36:23 -04:00
Jonathan Lebon
292cfc807e macros: use size_t for glnx_strjoina len
This was in my workspace for a while.
`strlen` returns a `size_t` and `alloca` expects a `size_t`.
2017-09-25 11:55:43 -04: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
Philip Withnall
56e7e728ab dirfd: Fix typo in documentation for glnx_ensure_dir()
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-09-21 20:20:47 +01:00
Colin Walters
e5856ca293 build-sys: Fix make dist
The libostree Travis builds use make dist.
2017-09-17 09:04:57 -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
667d8aa721 tree-wide: Use our own syscall wrappers or error prefixing
Followup to similar commits in the ostree stack recently.
2017-09-13 10:34:21 -04:00
Colin Walters
0428fd87ff dirfd: Extend tmpdir API to support optional cleaning
We have a use case in libostree's staging dirs where we try to reuse
them across multiple ostree txns, but we want the fd-relative bits
here.

Extend the tmpdir API to make deletion optional. While here, also extend the API
to support checking for errors when deleting for projects like libostree that
want to do so consistently.

Also while here, add a change to set the fd to `-1` after clearing to be extra
defensive.
2017-09-13 10:28:25 -04:00
Colin Walters
b59bb2be7c fdio: Add gtk-doc for stbuf parameter of glnx_file_copy_at()
Spotted in ba5e1cf9f5
2017-09-12 11:09:29 -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
806bb46e05 fdio: Use O_EXCL for anonymous tmpfiles
I noticed while reading the manpage for `linkat()` that `O_TMPFILE`
supports `O_EXCL` to mean exactly what we're doing with the anonymous
tmpfile API.

Change the code to start using it; this required refactoring the internals since
we had a check to be sure the caller wasn't passing `O_EXCL` for the
non-anonymous path which we want to keep.

Presumably the storage system could do smarter things if it knows a file will
always be anonymous, e.g. it doesn't need to journal its data.
2017-09-12 09:42:47 -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
Jonathan Lebon
e226ccf691 console: trim useless check
The `percentage` var is a guint and so is always >= 0.

Coverity CID: 163703
2017-08-15 16:28:27 -04:00
Matthew Leeds
6bd24baed2 dirfd: Fix typo in comment 2017-08-15 13:17:03 -07:00
Philip Withnall
1893c1ff74 glnx-console: Add missing NULL check before writing out text
It’s possible that text is NULL on this path.

Coverity CID: 1376570

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-08-15 17:37:47 +01:00
Colin Walters
d18f026ee7 fdio: Merge systemd code to use copy_file_range(), use FICLONE
FICLONE is the new alias for the formerly btrfs-specific ioctl; XFS
has experimental patches to support it.

Further, we should use copy_file_range() for the case where we're only doing a
limited copy. Both NFS and XFS (with reflink enabled) understand it.

Part of the reason I'm doing this is so that ostree's `/etc` merge will start
using XFS reflinks. But another major reason is to take the next step after and
copy this code into GLib as well, so that all of the general GLib users will
benefit; e.g. Nautilus will transparently do server copy offloads with NFS home
directories.

See also this coreutils thread about `copy_file_range()`:
<https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24399>. I don't care about file
holes for our use cases, so it's fine.

Other changes while I'm here:
 - Tweak the sendfile() case to match the newly inlined logic for cfr
 - Add a TEMP_FAILURE_RETRY() around the read()
2017-08-08 21:16:34 -04:00
Colin Walters
29ad99c9b6 console: Fix Coverity NULL deref warning
We need to handle our "empty to NULL canonicalization" before
doing the length.

Coverity CID: 1376570
2017-08-02 13:14:34 -04:00
Colin Walters
a46752edcd local-alloc: Remove almost all macros like glnx_free, glnx_unref_variant
We should be able to rely upstream on everything *except* `glnx_unref_object`
which requires the library itself to depend on a newer glib, which isn't true
for e.g. RHEL7 libsoup.

libostree was almost ready for this; just a few patches to push
it to completion in
https://github.com/ostreedev/ostree/pull/1042
2017-07-31 13:29:27 -04:00
Colin Walters
50a0feaba0 localalloc: Abort on EBADF from close() by default
systemd does this by default. I think we should treat this as a fatal error
since it can cause really painful-to-debug problems if we don't just get
EBADF but actually close something else's fd due to a race.
2017-07-26 16:13:06 -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
Jonathan Lebon
c820571bc4 errors: check for an error before prefixing
Minor tweak to the new `GLNX_AUTO_PREFIX_ERROR`. Since the common case
is that there's no errors, let's bring down the same check that
`g_prefix_error` does to avoid a function call most of the time.
2017-07-21 14:09:24 -07:00
Colin Walters
7d6a31fb93 errors: Mark GLNX_AUTO_PREFIX_ERROR() as used
Since it's intentional we never use it, and `clang` barfs on this (rightly).
2017-07-20 15:12:08 -04:00
Colin Walters
1468b70dbf dirfd: Add missing includes for errno
Thought the previous patch would have been obvious enough not
to compile test but...
2017-07-20 15:12:08 -04:00
Colin Walters
1c0bfd24b1 dirfd: Add glnx_ensure_dir()
Another one where we have a lot of inlines in ostree at least. Not the same as
`glnx_shutil_mkdir_p_at()` since in these cases we don't want automatic
intermediate dirs, and it's cheaper to just call `mkdirat()` and handle `EEXIST`
rather than do a `stat()` first.
2017-07-20 09:59:08 -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
23f7df1500 dirfd: Add filename to glnx_opendirat()
This showed up in https://github.com/projectatomic/rpm-ostree/issues/883

We'll have to audit callers to be sure to avoid double-prefixing.
2017-07-19 09:30:13 -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
61ef326ad8 fdio: Add string prefix for glnx_fstat()
For consistency.
2017-07-17 12:12:14 -04:00
Colin Walters
547bcea280 fdio: Add a fchmod wrapper
There are a number of versions of this in ostree at least, might as well wrap
it.
2017-07-17 12:12:14 -04:00
Colin Walters
8b75c8e341 Remove glnx_stream_fstat()
There are only two users of this in ostree, and one of them is
fairly bogus; we can just use `fstat()`.
2017-07-17 12:12:14 -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
Matthew Leeds
210bcfcb65 README.md: Change xdg-app to flatpak 2017-07-13 15:43:48 -07: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
a37e672739 macros: Add a size check for hashtable iters
If the user provides a less than pointer-sized type, we'll clobber other things
on the stack.

See https://github.com/ostreedev/ostree/pull/990/
2017-06-30 12:17:49 -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
Colin Walters
6c2967c1ad fdio: Remove extra ';' in header
This was confusing `g-ir-scanner`.
2017-06-28 11:23:01 -04:00
Colin Walters
71d875543c macros: Avoid scanning macros
`g-ir-scanner` is confused by some of the syntax extensions in `G_IN_SET()`;
none of this is applicable to bindings, so just skip it.
2017-06-28 11:23:01 -04:00
Colin Walters
e55fd8ee31 fdio: Introduce glnx_open_anonymous_tmpfile()
There was a user of this in the libostree static delta code.
2017-06-28 11:23:01 -04:00
Colin Walters
d4c5c02327 fdio: Be sure to unset tmpfile's initialized state on cleanup
I'm not aware of a problem in practice here, but we should do this on general
principle. Writing this patch now because I hit a fd leak in the ostree static
delta processing that was introduced in the tmpfile prep code, but fixed in the
final port.
2017-06-28 11:23:01 -04:00
Jonathan Lebon
5ab15ac175 macros: add GLNX_HASH_TABLE_FOREACH_V
Looking at converting the ostree codebase, iterating over only the
values of a hash table (while ignoring the key) is actually a more
common pattern than I thought. So let's give it its own macro as well so
users don't have to resort to the _KV variant.
2017-06-28 08:04:11 -07:00
Colin Walters
4d34066a2f fdio: Add wrappers for renameat(), unlinkat()
Besides doing `TEMP_FAILURE_RETRY` and `GError` conversion,
these also prefix the error with arguments.
2017-06-26 13:37:05 -04:00
Jonathan Lebon
caa51ac24f glnx-macros.h: add GLNX_HASH_TABLE_FOREACH macros
These macros make it much easier to iterate over a GHashTable. It takes
care of initializing an iterator and casting keys and values to their
proper types.

See the example usage in the docstring for more info.
2017-06-17 16:26:05 -04:00
Jonathan Lebon
e8b7d8f60c test-libglnx-macros.c: fix missing semicolon 2017-06-17 14:52:41 -04:00