Commit Graph

42 Commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Lennart Poettering 2b33ab0957 tree-wide: port various places over to use new rearrange_stdio() 2018-03-02 11:42:10 +01:00
Lennart Poettering b4a343112e tree-wide: use EXIT_SUCCESS when comparing child process exit statuses
When we check the exit status of a subprocess, let's compare it with
EXIT_SUCCESS rather than 0 when looking for success.

This clarifies in code what kind of variable we are looking at and what
we are doing.
2018-01-04 13:27:27 +01:00
Lennart Poettering 7d4904fe7a process-util: rework wait_for_terminate_and_warn() to take a flags parameter
This renames wait_for_terminate_and_warn() to
wait_for_terminate_and_check(), and adds a flags parameter, that
controls how much to log: there's one flag that means we log about
abnormal stuff, and another one that controls whether we log about
non-zero exit codes. Finally, there's a shortcut flag value for logging
in both cases, as that's what we usually use.

All callers are accordingly updated. At three occasions duplicate logging
is removed, i.e. where the old function was called but logged in the
caller, too.
2018-01-04 13:27:27 +01:00
Lennart Poettering b6e1fff13d process-util: add another fork_safe() flag for enabling LOG_ERR/LOG_WARN logging 2018-01-04 13:27:26 +01:00
Lennart Poettering 4c253ed1ca tree-wide: introduce new safe_fork() helper and port everything over
This adds a new safe_fork() wrapper around fork() and makes use of it
everywhere. The new wrapper does a couple of things we previously did
manually and separately in a safer, more correct and automatic way:

1. Optionally resets signal handlers/mask in the child

2. Sets a name on all processes we fork off right after forking off (and
   the patch assigns useful names for all processes we fork off now,
   following a systematic naming scheme: always enclosed in () – in order
   to indicate that these are not proper, exec()ed processes, but only
   forked off children, and if the process is long-running with only our
   own code, without execve()'ing something else, it gets am "sd-" prefix.)

3. Optionally closes all file descriptors in the child

4. Optionally sets a PR_SET_DEATHSIG to SIGTERM in the child, in a safe
   way so that the parent dying before this happens being handled
   safely.

5. Optionally reopens the logs

6. Optionally connects stdin/stdout/stderr to /dev/null

7. Debug logs about the forked off processes.
2017-12-25 11:48:21 +01:00
Daniel Lockyer 4600a396d5 Remove NULL as last parameter to strjoin 2017-11-24 10:37:08 +00:00
Lennart Poettering 0133d5553a
Merge pull request #7198 from poettering/stdin-stdout
Add StandardInput=data, StandardInput=file:... and more
2017-11-19 19:49:11 +01:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Lennart Poettering 046a82c1b2 fd-util: add new helper move_fd() and make use of it
We are using the same pattern at various places: call dup2() on an fd,
and close the old fd, usually in combination with some O_CLOEXEC
fiddling. Let's add a little helper for this, and port a few obvious
cases over.
2017-11-17 11:13:44 +01:00
Thomas Hindoe Paaboel Andersen 6eeec374c1 tree-wide: remove unused variables 2017-04-28 23:56:44 +02:00
tblume 697be0be15 importd: support SUSE style checksums (#5206)
In order to verify a pulled container or disk image, importd only supports
SHA256SUMS files with the detached signature in SHA256SUMS.gpg.
SUSE is using an inline signed file with the name of the image itself and the
suffix .sha256 instead.
This commit adds support for this type of signature files.

It is first attempted to pull the .sha256 file.
If this fails with error 404, the SHA256SUMS and SHA256SUMS.gpg files are
pulled and used for verification.
2017-04-24 20:37:11 +02:00
Lennart Poettering 91359193c3 importd: automatically download .roothash in addition to .nspawn for raw downloads
Now that nspawn looks for these files, and mkosi generates them, we
should also make sure importd downloads them if it can.
2016-12-21 19:09:31 +01:00
Lennart Poettering 17cbb288fa nspawn: add fallback top normal copy/reflink when we cannot btrfs snapshot
Given that other file systems (notably: xfs) support reflinks these days, let's
extend the file system snapshotting logic to fall back to plan copies or
reflinks when full btrfs subvolume snapshots are not available.

This essentially makes "systemd-nspawn --ephemeral" and "systemd-nspawn
--template=" available on non-btrfs subvolumes. Of course, both operations will
still be slower on non-btrfs than on btrfs (simply because reflinking each file
individually in a directory tree is still slower than doing this in one step
for a whole subvolume), but it's probably good enough for many cases, and we
should provide the users with the tools, they have to figure out what's good
for them.

Note that "machinectl clone" already had a fallback like this in place, this
patch generalizes this, and adds similar support to our other cases.
2016-11-22 13:35:09 +01:00
Alexander Kuleshov 913f38e440 treewide: use stdio_unset_cloexec() function 2016-07-02 23:42:01 +06:00
Lennart Poettering 89c9030d31 util: rework sigkill_wait() to not require pid_t pointer
Let's make sigkill_wait() take a normal pid_t, and add sigkill_waitp() that
takes a pointer (which is useful for usage in _cleanup_), following the usual
logic we have for this.
2016-05-02 11:15:30 +02:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Daniel Mack 933f9caeeb siphash24: let siphash24_finalize() and siphash24() return the result directly
Rather than passing a pointer to return the result, return it directly
from the function calls.

Also, return the result in native endianess, and let the callers care
about the conversion. For hash tables and bloom filters, we don't care,
but in order to keep MAC addresses and DHCP client IDs stable, we
explicitly convert to LE.
2015-11-16 23:17:52 +01:00
Martin Pitt dbe81cbd2a siphash24: change result argument to uint64_t
Change the "out" parameter from uint8_t[8] to uint64_t. On architectures which
enforce pointer alignment this fixes crashes when we previously cast an
unaligned array to uint64_t*, and on others this should at least improve
performance as the compiler now aligns these properly.

This also simplifies the code in most cases by getting rid of typecasts. The
only place which we can't change is struct duid's en.id, as that is _packed_
and public API, so we can't enforce alignment of the "id" field and have to
use memcpy instead.
2015-11-16 15:20:29 +01:00
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 49cf4170d0 util-lib: move web-related calls into web-util.[ch] 2015-10-27 13:25:57 +01:00
Lennart Poettering 430f0182b7 src/basic: rename audit.[ch] → audit-util.[ch] and capability.[ch] → capability-util.[ch]
The files are named too generically, so that they might conflict with
the upstream project headers. Hence, let's add a "-util" suffix, to
clarify that this are just our utility headers and not any official
upstream headers.
2015-10-27 13:25:57 +01:00
Lennart Poettering a09561746f util-lib: introduce dirent-util.[ch] for directory entry calls
Also, move a couple of more path-related functions to path-util.c.
2015-10-27 13:25:56 +01:00
Lennart Poettering bb15fafe9c util: move filename_is_valid() and path_is_safe() to path-util.[ch] 2015-10-27 13:25:55 +01:00
Lennart Poettering c004493cde util-lib: split out IO related calls to io-util.[ch] 2015-10-26 01:24:38 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Lennart Poettering 4f5dd3943b util: split out escaping code into escape.[ch]
This really deserves its own file, given how much code this is now.
2015-10-24 23:04:42 +02:00
Lennart Poettering 1f9aa80a59 import: correct handling if .nspawn file could not be downloaded 2015-10-22 01:59:25 +02:00
Lennart Poettering 5bcd08db28 btrfs: beef-up btrfs support with a limited understanding of quota
With this change we understand more than just leaf quota groups for
btrfs file systems. Specifically:

- When we create a subvolume we can now optionally add the new subvolume
  to all qgroups its parent subvolume was member of too. Alternatively
  it is also possible to insert an intermediary quota group between the
  parent's qgroups and the subvolume's leaf qgroup, which is useful for
  a concept of "subtree" qgroups, that contain a subvolume and all its
  children.

- The remove logic for subvolumes has been updated to optionally remove
  any leaf qgroups or "subtree" qgroups, following the logic above.

- The snapshot logic for subvolumes has been updated to replicate the
  original qgroup setup of the source, if it follows the "subtree"
  design described above. It will not cover qgroup setups that introduce
  arbitrary qgroups, especially those orthogonal to the subvolume
  hierarchy.

This also tries to be more graceful when setting up /var/lib/machines as
btrfs. For example, if mkfs.btrfs is missing we don't even try to set it
up as loopback device.

Fixes #1559
Fixes #1129
2015-10-22 01:59:25 +02:00
Jan Synacek 9818005de6 import: hash URL in paths if they are too long
https://bugzilla.redhat.com/show_bug.cgi?id=1266775
2015-10-07 14:06:49 +02:00
Daniel Mack 9c00a6adfa importd: fix typos 2015-09-09 10:10:23 +02:00
Lennart Poettering 9854730b45 importd: for .raw and .tar images, try to download .nspawn settings file too 2015-09-08 18:26:29 +02:00
Lennart Poettering ce30c8dcb4 tree-wide: whenever we fork off a foreign child process reset signal mask/handlers
Also, when the child is potentially long-running make sure to set a
death signal.

Also, ignore the result of the reset operations explicitly by casting
them to (void).
2015-06-10 01:28:58 +02:00
Lennart Poettering 24882e06c1 util: split out signal-util.[ch] from util.[ch]
No functional changes.
2015-05-29 20:14:11 +02:00
Ronny Chevalier 0b452006de shared: add process-util.[ch] 2015-04-10 23:54:49 +02:00
Lennart Poettering 527b7a421f util: rework cunescape(), improve error handling
Change cunescape() to return a normal error code, so that we can
distuingish OOM errors from parse errors.

This also adds a flags parameter to control whether "relaxed" or normal
parsing shall be done. If set no parse failures are generated, and the
only reason why cunescape() can fail is OOM.
2015-04-07 15:42:25 +02:00
Lennart Poettering e9bc1871b9 btrfs: make btrfs_subvol_snapshot() parameters a flags field 2015-04-06 14:54:58 +02:00
Lennart Poettering d9e2daaf3d btrfs: support recursively removing btrfs snapshots 2015-04-06 11:28:16 +02:00
Lennart Poettering c687863750 util: rework rm_rf() logic
- Move to its own file rm-rf.c

- Change parameters into a single flags parameter

- Remove "honour sticky" logic, it's unused these days
2015-04-06 10:57:53 +02:00
Lennart Poettering b6e676ce41 importd: add new bus calls for importing local tar and raw images
This also adds "machinectl import-raw" and "machinectl import-tar" to
wrap these new bus calls.

THe commands basically do for local files that "machinectl pull-raw" and
friends do for remote files.
2015-03-05 00:59:38 +01:00
Lennart Poettering dc2c282b6a import: rename download code from "import" to "pull"
That way we can call the code for local container/VM imports "import"
without confusion.
2015-03-05 00:59:37 +01:00
Renamed from src/import/import-common.c (Browse further)