diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-12-23 19:19:51 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-07-04 19:19:24 +0200 |
commit | a709a3154d52abe2fc34cae4a0720f20bcec4cef (patch) | |
tree | 44ea71a9e4a22a6f659f5bc62e9f4cc86c2ef151 /src/shared | |
parent | man: Add some notes about variable $prefix for StateDirectory= (diff) | |
download | systemd-a709a3154d52abe2fc34cae4a0720f20bcec4cef.tar.xz systemd-a709a3154d52abe2fc34cae4a0720f20bcec4cef.zip |
dissect: split out DM deferred remove into src/shared/dm-util.c
The function is generally useful, let's split it out so that we can make
use of it later on in systemd-homed.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/dissect-image.c | 37 | ||||
-rw-r--r-- | src/shared/dm-util.c | 41 | ||||
-rw-r--r-- | src/shared/dm-util.h | 4 | ||||
-rw-r--r-- | src/shared/meson.build | 2 |
4 files changed, 49 insertions, 35 deletions
diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index f930c02673..030e12e4ee 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -19,6 +19,7 @@ #include "device-nodes.h" #include "device-util.h" #include "dissect-image.h" +#include "dm-util.h" #include "env-file.h" #include "fd-util.h" #include "fileio.h" @@ -1225,40 +1226,6 @@ int dissected_image_decrypt_interactively( } } -#if HAVE_LIBCRYPTSETUP -static int deferred_remove(DecryptedPartition *p) { - struct dm_ioctl dm = { - .version = { - DM_VERSION_MAJOR, - DM_VERSION_MINOR, - DM_VERSION_PATCHLEVEL - }, - .data_size = sizeof(dm), - .flags = DM_DEFERRED_REMOVE, - }; - - _cleanup_close_ int fd = -1; - - assert(p); - - /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */ - - fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); - if (fd < 0) - return -errno; - - if (strlen(p->name) > sizeof(dm.name)) - return -ENAMETOOLONG; - - strncpy(dm.name, p->name, sizeof(dm.name)); - - if (ioctl(fd, DM_DEV_REMOVE, &dm)) - return -errno; - - return 0; -} -#endif - int decrypted_image_relinquish(DecryptedImage *d) { #if HAVE_LIBCRYPTSETUP @@ -1278,7 +1245,7 @@ int decrypted_image_relinquish(DecryptedImage *d) { if (p->relinquished) continue; - r = deferred_remove(p); + r = dm_deferred_remove(p->name); if (r < 0) return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); diff --git a/src/shared/dm-util.c b/src/shared/dm-util.c new file mode 100644 index 0000000000..a17b85b64a --- /dev/null +++ b/src/shared/dm-util.c @@ -0,0 +1,41 @@ +#include <fcntl.h> +#include <linux/dm-ioctl.h> +#include <string.h> +#include <sys/ioctl.h> + +#include "dm-util.h" +#include "fd-util.h" + +int dm_deferred_remove(const char *name) { + + struct dm_ioctl dm = { + .version = { + DM_VERSION_MAJOR, + DM_VERSION_MINOR, + DM_VERSION_PATCHLEVEL + }, + .data_size = sizeof(dm), + .flags = DM_DEFERRED_REMOVE, + }; + + _cleanup_close_ int fd = -1; + + assert(name); + + /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() + * directly. */ + + if (strlen(name) > sizeof(dm.name)-1) + return -ENODEV; /* A device with a name longer than this cannot possibly exist */ + + fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); + if (fd < 0) + return -errno; + + strncpy(dm.name, name, sizeof(dm.name)); + + if (ioctl(fd, DM_DEV_REMOVE, &dm)) + return -errno; + + return 0; +} diff --git a/src/shared/dm-util.h b/src/shared/dm-util.h new file mode 100644 index 0000000000..6c78bfe75b --- /dev/null +++ b/src/shared/dm-util.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +int dm_deferred_remove(const char *name); diff --git a/src/shared/meson.build b/src/shared/meson.build index 59b50a754e..6202cd471e 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -50,6 +50,8 @@ shared_sources = files(''' dev-setup.h dissect-image.c dissect-image.h + dm-util.c + dm-util.h dns-domain.c dns-domain.h dropin.c |