From 1100561cc74a4173266ac1443179ddf3d8e74a00 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 02:59:30 +0900 Subject: [PATCH 1/7] growfs,repart: fix misuse of sd_device_get_devpath() Follow-up for 0f79b3469f167583033d17f9a6fa0e4341a68003. To obtain device node, sd_device_get_devname() must be used. --- src/partition/growfs.c | 8 ++++---- src/partition/repart.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/partition/growfs.c b/src/partition/growfs.c index 88e503c86d..e5f570f0d3 100644 --- a/src/partition/growfs.c +++ b/src/partition/growfs.c @@ -50,7 +50,7 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_ return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(main_devno)); - r = sd_device_get_devpath(main_dev, &main_devpath); + r = sd_device_get_devname(main_dev, &main_devpath); if (r < 0) return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m"); @@ -69,7 +69,7 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_ return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devpath(dev, &devpath); + r = sd_device_get_devname(dev, &devpath); if (r < 0) return log_device_error_errno(dev, r, "Failed to get devpath: %m"); @@ -137,7 +137,7 @@ static int maybe_resize_underlying_device( return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devpath(dev, &devpath); + r = sd_device_get_devname(dev, &devpath); if (r < 0) return log_device_error_errno(dev, r, "Failed to get devpath: %m"); @@ -265,7 +265,7 @@ static int run(int argc, char *argv[]) { return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devpath(dev, &devpath); + r = sd_device_get_devname(dev, &devpath); if (r < 0) return log_device_error_errno(dev, r, "Failed to get devpath: %m"); diff --git a/src/partition/repart.c b/src/partition/repart.c index 8824c105ff..588aaa6132 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -4049,7 +4049,7 @@ static int context_open_copy_block_paths( if (r < 0) return log_error_errno(r, "Failed to create sd-device object for block device backing '%s': %m", opened); - r = sd_device_get_devpath(dev, &bdev); + r = sd_device_get_devname(dev, &bdev); if (r < 0) return log_error_errno(r, "Failed to get device name for block device backing '%s': %m", opened); From a2ded81f73a90fa98a9bf6abb91b2455f79e2ec1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 03:22:02 +0900 Subject: [PATCH 2/7] sd-device: rename device-util.c -> device-filter.c The functions provided by the file are only used in sd-device. --- src/libsystemd/meson.build | 3 ++- src/libsystemd/sd-device/device-enumerator.c | 1 + .../sd-device/{device-util.c => device-filter.c} | 2 +- src/libsystemd/sd-device/device-filter.h | 13 +++++++++++++ src/libsystemd/sd-device/device-monitor.c | 1 + src/libsystemd/sd-device/device-util.h | 6 ------ 6 files changed, 18 insertions(+), 8 deletions(-) rename src/libsystemd/sd-device/{device-util.c => device-filter.c} (99%) create mode 100644 src/libsystemd/sd-device/device-filter.h diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index 2b288ccb28..7f10e87d6c 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -121,12 +121,13 @@ libsystemd_sources = files( 'sd-bus/sd-bus.c', 'sd-device/device-enumerator-private.h', 'sd-device/device-enumerator.c', + 'sd-device/device-filter.c', + 'sd-device/device-filter.h', 'sd-device/device-internal.h', 'sd-device/device-monitor-private.h', 'sd-device/device-monitor.c', 'sd-device/device-private.c', 'sd-device/device-private.h', - 'sd-device/device-util.c', 'sd-device/device-util.h', 'sd-device/sd-device.c', 'sd-hwdb/hwdb-internal.h', diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index 30f2520b72..ffed6fef9b 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -7,6 +7,7 @@ #include "alloc-util.h" #include "device-enumerator-private.h" +#include "device-filter.h" #include "device-util.h" #include "dirent-util.h" #include "fd-util.h" diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-filter.c similarity index 99% rename from src/libsystemd/sd-device/device-util.c rename to src/libsystemd/sd-device/device-filter.c index 3b8689e0d6..4101e7d2ba 100644 --- a/src/libsystemd/sd-device/device-util.c +++ b/src/libsystemd/sd-device/device-filter.c @@ -2,7 +2,7 @@ #include -#include "device-util.h" +#include "device-filter.h" #include "path-util.h" int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null) { diff --git a/src/libsystemd/sd-device/device-filter.h b/src/libsystemd/sd-device/device-filter.h new file mode 100644 index 0000000000..0c5f34e242 --- /dev/null +++ b/src/libsystemd/sd-device/device-filter.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +#include "sd-device.h" + +#include "hashmap.h" +#include "set.h" + +int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null); +bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr); +bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent); diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index 566280d66f..ba997b7f2c 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -12,6 +12,7 @@ #include "MurmurHash2.h" #include "alloc-util.h" +#include "device-filter.h" #include "device-monitor-private.h" #include "device-private.h" #include "device-util.h" diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index d9e9f1e8c4..d92cbd6cc7 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -5,10 +5,8 @@ #include "sd-device.h" -#include "hashmap.h" #include "log.h" #include "macro.h" -#include "set.h" #define device_unref_and_replace(a, b) \ unref_and_replace_full(a, b, sd_device_ref, sd_device_unref) @@ -81,7 +79,3 @@ #define log_device_notice_errno(device, error, ...) log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__) #define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__) #define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__) - -int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null); -bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr); -bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent); From 4492b5d80489482802a3708e2aeaa7c5b5d20e09 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 04:15:35 +0900 Subject: [PATCH 3/7] devnum-util: split-out device_path_make_inaccessible() --- src/basic/devnum-util.c | 37 ++++++++++++++++++++----------------- src/basic/devnum-util.h | 1 + 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/basic/devnum-util.c b/src/basic/devnum-util.c index 70c07315c5..bd1b4d6c39 100644 --- a/src/basic/devnum-util.c +++ b/src/basic/devnum-util.c @@ -57,6 +57,24 @@ int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) { return 0; } +int device_path_make_inaccessible(mode_t mode, char **ret) { + char *s; + + assert(ret); + + if (S_ISCHR(mode)) + s = strdup("/run/systemd/inaccessible/chr"); + else if (S_ISBLK(mode)) + s = strdup("/run/systemd/inaccessible/blk"); + else + return -ENODEV; + if (!s) + return -ENOMEM; + + *ret = s; + return 0; +} + int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) { _cleanup_free_ char *p = NULL; int r; @@ -65,25 +83,10 @@ int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) { assert(ret); - if (major(devnum) == 0 && minor(devnum) == 0) { - char *s; - + if (major(devnum) == 0 && minor(devnum) == 0) /* A special hack to make sure our 'inaccessible' device nodes work. They won't have symlinks in * /dev/block/ and /dev/char/, hence we handle them specially here. */ - - if (S_ISCHR(mode)) - s = strdup("/run/systemd/inaccessible/chr"); - else if (S_ISBLK(mode)) - s = strdup("/run/systemd/inaccessible/blk"); - else - return -ENODEV; - - if (!s) - return -ENOMEM; - - *ret = s; - return 0; - } + return device_path_make_inaccessible(mode, ret); r = device_path_make_major_minor(mode, devnum, &p); if (r < 0) diff --git a/src/basic/devnum-util.h b/src/basic/devnum-util.h index 3f1894b2fd..38aa4efa87 100644 --- a/src/basic/devnum-util.h +++ b/src/basic/devnum-util.h @@ -29,6 +29,7 @@ int parse_devnum(const char *s, dev_t *ret); }) int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret); +int device_path_make_inaccessible(mode_t mode, char **ret); int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret); int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum); From f79fdea6fda42e0de4857a31545558e7471da422 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 02:45:56 +0900 Subject: [PATCH 4/7] sd-device: introduce devpath_from_devnum() --- src/libsystemd/meson.build | 1 + src/libsystemd/sd-device/device-private.h | 1 + src/libsystemd/sd-device/device-util.c | 33 +++++++++++++++++++++++ src/libsystemd/sd-device/device-util.h | 3 +++ src/libsystemd/sd-device/sd-device.c | 2 +- src/libsystemd/sd-device/test-sd-device.c | 31 +++++++++++++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/libsystemd/sd-device/device-util.c diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index 7f10e87d6c..9238e32980 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -128,6 +128,7 @@ libsystemd_sources = files( 'sd-device/device-monitor.c', 'sd-device/device-private.c', 'sd-device/device-private.h', + 'sd-device/device-util.c', 'sd-device/device-util.h', 'sd-device/sd-device.c', 'sd-hwdb/hwdb-internal.h', diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h index d993b77b49..d33cddcf80 100644 --- a/src/libsystemd/sd-device/device-private.h +++ b/src/libsystemd/sd-device/device-private.h @@ -10,6 +10,7 @@ #include "macro.h" +int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum); int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len); int device_new_from_strv(sd_device **ret, char **strv); int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd); diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c new file mode 100644 index 0000000000..f1cd67aa28 --- /dev/null +++ b/src/libsystemd/sd-device/device-util.c @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "device-private.h" +#include "device-util.h" +#include "devnum-util.h" +#include "string-util.h" + +int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + _cleanup_free_ char *s = NULL; + const char *devname; + int r; + + assert(ret); + + if (major(devnum) == 0 && minor(devnum) == 0) + return device_path_make_inaccessible(mode, ret); + + r = device_new_from_mode_and_devnum(&dev, mode, devnum); + if (r < 0) + return r; + + r = sd_device_get_devname(dev, &devname); + if (r < 0) + return r; + + s = strdup(devname); + if (!s) + return -ENOMEM; + + *ret = TAKE_PTR(s); + return 0; +} diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index d92cbd6cc7..6150c45082 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include "sd-device.h" @@ -79,3 +80,5 @@ #define log_device_notice_errno(device, error, ...) log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__) #define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__) #define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__) + +int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret); diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 51bee24d51..26b9e51c93 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -279,7 +279,7 @@ _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) { return device_new_from_syspath(ret, syspath, /* strict = */ true); } -static int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) { +int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) { _cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_free_ char *syspath = NULL; const char *t, *subsystem; diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index 66c8d70faa..74fdd7b600 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -14,6 +14,7 @@ #include "nulstr-util.h" #include "path-util.h" #include "rm-rf.h" +#include "stat-util.h" #include "string-util.h" #include "tests.h" #include "time-util.h" @@ -499,4 +500,34 @@ TEST(sd_device_new_from_path) { } } +static void test_devpath_from_devnum_one(const char *path) { + _cleanup_free_ char *resolved = NULL; + struct stat st; + + log_debug("> %s", path); + + if (stat(path, &st) < 0) { + assert_se(errno == ENOENT); + log_notice("Path %s not found, skipping test", path); + return; + } + + assert_se(devpath_from_devnum(st.st_mode, st.st_rdev, &resolved) >= 0); + assert_se(path_equal(path, resolved)); +} + +TEST(devpath_from_devnum) { + test_devpath_from_devnum_one("/dev/null"); + test_devpath_from_devnum_one("/dev/zero"); + test_devpath_from_devnum_one("/dev/full"); + test_devpath_from_devnum_one("/dev/random"); + test_devpath_from_devnum_one("/dev/urandom"); + test_devpath_from_devnum_one("/dev/tty"); + + if (is_device_node("/run/systemd/inaccessible/blk") > 0) { + test_devpath_from_devnum_one("/run/systemd/inaccessible/chr"); + test_devpath_from_devnum_one("/run/systemd/inaccessible/blk"); + } +} + DEFINE_TEST_MAIN(LOG_INFO); From ff65036cfe95c3c16af3077b7067de33b88c5ba5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 04:30:23 +0900 Subject: [PATCH 5/7] sd-device: introduce device_open_from_devnum() --- src/libsystemd/sd-device/device-util.c | 32 ++++++++++++++++++++++++++ src/libsystemd/sd-device/device-util.h | 1 + 2 files changed, 33 insertions(+) diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c index f1cd67aa28..f000278c75 100644 --- a/src/libsystemd/sd-device/device-util.c +++ b/src/libsystemd/sd-device/device-util.c @@ -3,6 +3,7 @@ #include "device-private.h" #include "device-util.h" #include "devnum-util.h" +#include "fd-util.h" #include "string-util.h" int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) { @@ -31,3 +32,34 @@ int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) { *ret = TAKE_PTR(s); return 0; } + +int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + _cleanup_close_ int fd = -1; + int r; + + r = device_new_from_mode_and_devnum(&dev, mode, devnum); + if (r < 0) + return r; + + fd = sd_device_open(dev, flags); + if (fd < 0) + return fd; + + if (ret) { + const char *devname; + char *s; + + r = sd_device_get_devname(dev, &devname); + if (r < 0) + return r; + + s = strdup(devname); + if (!s) + return -ENOMEM; + + *ret = s; + } + + return TAKE_FD(fd); +} diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 6150c45082..53943b8382 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -82,3 +82,4 @@ #define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__) int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret); +int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret); From 42899bcee5a7ed08640e0f958fd9b48e5b21eb3d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 06:43:30 +0900 Subject: [PATCH 6/7] sd-device: skip to check diskseq if device is not initialized --- src/libsystemd/sd-device/sd-device.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 26b9e51c93..a7ba7dd6e9 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -2422,13 +2422,20 @@ _public_ int sd_device_open(sd_device *device, int flags) { if (FLAGS_SET(flags, O_PATH)) return TAKE_FD(fd); - r = device_get_property_bool(device, "ID_IGNORE_DISKSEQ"); - if (r < 0 && r != -ENOENT) + /* If the device is not initialized, then we cannot determine if we should check diskseq through + * ID_IGNORE_DISKSEQ property. Let's skip to check diskseq in that case. */ + r = sd_device_get_is_initialized(device); + if (r < 0) return r; - if (r <= 0) { - r = sd_device_get_diskseq(device, &diskseq); + if (r > 0) { + r = device_get_property_bool(device, "ID_IGNORE_DISKSEQ"); if (r < 0 && r != -ENOENT) return r; + if (r <= 0) { + r = sd_device_get_diskseq(device, &diskseq); + if (r < 0 && r != -ENOENT) + return r; + } } fd2 = open(FORMAT_PROC_FD_PATH(fd), flags); From ca8228295ec20e561119729a2cf117e4531d7f56 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Aug 2022 05:56:25 +0900 Subject: [PATCH 7/7] tree-wide: use devpath_from_devnum() and device_open_from_devnum() Fixes #24465. --- src/core/swap.c | 10 ++--- src/dissect/dissect.c | 19 ++++----- src/home/homework-luks.c | 11 ++--- src/nspawn/nspawn-oci.c | 3 +- src/partition/growfs.c | 58 ++++++--------------------- src/partition/repart.c | 64 ++++++++---------------------- src/shared/blockdev-util.c | 7 +--- src/shared/find-esp.c | 35 ++++++++-------- src/shared/loop-util.c | 8 +--- src/shared/quota-util.c | 4 +- src/sysupdate/sysupdate-resource.c | 4 +- src/udev/udevadm-lock.c | 4 +- 12 files changed, 68 insertions(+), 159 deletions(-) diff --git a/src/core/swap.c b/src/core/swap.c index 2d1190d018..f5c65dc312 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -295,9 +295,8 @@ static int swap_verify(Swap *s) { } static int swap_load_devnode(Swap *s) { - _cleanup_(sd_device_unrefp) sd_device *d = NULL; + _cleanup_free_ char *p = NULL; struct stat st; - const char *p; int r; assert(s); @@ -305,16 +304,13 @@ static int swap_load_devnode(Swap *s) { if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode)) return 0; - r = sd_device_new_from_stat_rdev(&d, &st); + r = devpath_from_devnum(S_IFBLK, st.st_rdev, &p); if (r < 0) { log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, - "Failed to allocate device for swap %s: %m", s->what); + "Failed to get device node for swap %s: %m", s->what); return 0; } - if (sd_device_get_devname(d, &p) < 0) - return 0; - return swap_set_devnode(s, p); } diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 4ae24e7153..17291cf22f 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -14,6 +14,8 @@ #include "blockdev-util.h" #include "chase-symlinks.h" #include "copy.h" +#include "device-util.h" +#include "devnum-util.h" #include "dissect-image.h" #include "env-util.h" #include "fd-util.h" @@ -859,11 +861,9 @@ static int action_copy(DissectedImage *m, LoopDevice *d) { static int action_umount(const char *path) { _cleanup_close_ int fd = -1; - _cleanup_free_ char *canonical = NULL; - dev_t devno; - const char *devname; + _cleanup_free_ char *canonical = NULL, *devname = NULL; _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; - _cleanup_(sd_device_unrefp) sd_device *device = NULL; + dev_t devno; int r, k; fd = chase_symlinks_and_open(path, NULL, 0, O_DIRECTORY, &canonical); @@ -882,15 +882,10 @@ static int action_umount(const char *path) { if (r < 0) return log_error_errno(r, "Failed to find backing block device for '%s': %m", canonical); - r = sd_device_new_from_devnum(&device, 'b', devno); + r = devpath_from_devnum(S_IFBLK, devno, &devname); if (r < 0) - return log_error_errno(r, "Failed to create sd-device object for block device %u:%u: %m", - major(devno), minor(devno)); - - r = sd_device_get_devname(device, &devname); - if (r < 0) - return log_error_errno(r, "Failed to get devname of block device %u:%u: %m", - major(devno), minor(devno)); + return log_error_errno(r, "Failed to get devname of block device " DEVNUM_FORMAT_STR ": %m", + DEVNUM_FORMAT_VAL(devno)); r = loop_device_open(devname, 0, &d); if (r < 0) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index c83292df7d..edf310f16d 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -19,6 +19,7 @@ #include "blockdev-util.h" #include "btrfs-util.h" #include "chattr-util.h" +#include "device-util.h" #include "devnum-util.h" #include "dm-util.h" #include "env-util.h" @@ -3121,20 +3122,14 @@ int home_resize_luks( log_info("Operating on partition device %s, using parent device.", ip); - r = device_path_make_major_minor(st.st_mode, parent, &whole_disk); + opened_image_fd = r = device_open_from_devnum(st.st_mode, parent, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK, &whole_disk); if (r < 0) - return log_error_errno(r, "Failed to derive whole disk path for %s: %m", ip); - - opened_image_fd = open(whole_disk, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); - if (opened_image_fd < 0) - return log_error_errno(errno, "Failed to open whole block device %s: %m", whole_disk); + return log_error_errno(r, "Failed to open whole block device for %s: %m", ip); image_fd = opened_image_fd; if (fstat(image_fd, &st) < 0) return log_error_errno(errno, "Failed to stat whole block device %s: %m", whole_disk); - if (!S_ISBLK(st.st_mode)) - return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Whole block device %s is not actually a block device, refusing.", whole_disk); } else log_info("Operating on whole block device %s.", ip); diff --git a/src/nspawn/nspawn-oci.c b/src/nspawn/nspawn-oci.c index 86c014d25e..40d3b31700 100644 --- a/src/nspawn/nspawn-oci.c +++ b/src/nspawn/nspawn-oci.c @@ -8,6 +8,7 @@ #include "bus-util.h" #include "cap-list.h" #include "cpu-set-util.h" +#include "device-util.h" #include "devnum-util.h" #include "env-util.h" #include "format-util.h" @@ -899,7 +900,7 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags } /* Suppress a couple of implicit device nodes */ - r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path); + r = devpath_from_devnum(node->mode, makedev(node->major, node->minor), &path); if (r < 0) json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor); else { diff --git a/src/partition/growfs.c b/src/partition/growfs.c index e5f570f0d3..a2c95453d4 100644 --- a/src/partition/growfs.c +++ b/src/partition/growfs.c @@ -34,8 +34,7 @@ static bool arg_dry_run = false; #if HAVE_LIBCRYPTSETUP static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) { - _cleanup_(sd_device_unrefp) sd_device *main_dev = NULL, *dev = NULL; - const char *devpath, *main_devpath; + _cleanup_free_ char *devpath = NULL, *main_devpath = NULL; _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; _cleanup_close_ int main_devfd = -1; uint64_t size; @@ -45,33 +44,21 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_ if (r < 0) return log_error_errno(r, "Cannot resize LUKS device: %m"); - r = sd_device_new_from_devnum(&main_dev, 'b', main_devno); + main_devfd = r = device_open_from_devnum(S_IFBLK, main_devno, O_RDONLY|O_CLOEXEC, &main_devpath); if (r < 0) - return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m", + return log_error_errno(r, "Failed to open main block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(main_devno)); - r = sd_device_get_devname(main_dev, &main_devpath); - if (r < 0) - return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m"); - - main_devfd = sd_device_open(main_dev, O_RDONLY|O_CLOEXEC); - if (main_devfd < 0) - return log_device_error_errno(main_dev, main_devfd, "Failed to open block device \"%s\": %m", - main_devpath); - if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0) return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m", main_devpath); log_debug("%s is %"PRIu64" bytes", main_devpath, size); - r = sd_device_new_from_devnum(&dev, 'b', devno); - if (r < 0) - return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", - DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devname(dev, &devpath); + r = devpath_from_devnum(S_IFBLK, devno, &devpath); if (r < 0) - return log_device_error_errno(dev, r, "Failed to get devpath: %m"); + return log_error_errno(r, "Failed to get devpath of " DEVNUM_FORMAT_STR ": %m", + DEVNUM_FORMAT_VAL(devno)); r = sym_crypt_init(&cd, devpath); if (r < 0) @@ -105,9 +92,7 @@ static int maybe_resize_underlying_device( const char *mountpath, dev_t main_devno) { - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; - _cleanup_free_ char *fstype = NULL; - const char *devpath; + _cleanup_free_ char *devpath = NULL, *fstype = NULL; dev_t devno; int r; @@ -132,15 +117,11 @@ static int maybe_resize_underlying_device( if (devno == main_devno) return 0; - r = sd_device_new_from_devnum(&dev, 'b', devno); + r = devpath_from_devnum(S_IFBLK, devno, &devpath); if (r < 0) - return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", + return log_error_errno(r, "Failed to get devpath for block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devname(dev, &devpath); - if (r < 0) - return log_device_error_errno(dev, r, "Failed to get devpath: %m"); - r = probe_filesystem(devpath, &fstype); if (r == -EUCLEAN) return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath); @@ -224,11 +205,9 @@ static int parse_argv(int argc, char *argv[]) { } static int run(int argc, char *argv[]) { - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_close_ int mountfd = -1, devfd = -1; - const char *devpath; + _cleanup_free_ char *devpath = NULL; uint64_t size, newsize; - struct stat st; dev_t devno; int r; @@ -260,24 +239,11 @@ static int run(int argc, char *argv[]) { if (r < 0) log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target); - r = sd_device_new_from_devnum(&dev, 'b', devno); + devfd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC, &devpath); if (r < 0) - return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m", + return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - r = sd_device_get_devname(dev, &devpath); - if (r < 0) - return log_device_error_errno(dev, r, "Failed to get devpath: %m"); - - devfd = sd_device_open(dev, O_RDONLY|O_CLOEXEC); - if (devfd < 0) - return log_device_error_errno(dev, devfd, "Failed to open block device \"%s\": %m", devpath); - - if (fstat(devfd, &st) < 0) - return log_error_errno(r, "Failed to stat() device %s: %m", devpath); - if (!S_ISBLK(st.st_mode)) - return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Backing device of file system is not a block device, refusing."); - if (ioctl(devfd, BLKGETSIZE64, &size) != 0) return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath); diff --git a/src/partition/repart.c b/src/partition/repart.c index 588aaa6132..815e81052b 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -24,6 +24,7 @@ #include "conf-parser.h" #include "cryptsetup-util.h" #include "def.h" +#include "device-util.h" #include "devnum-util.h" #include "dirent-util.h" #include "efivars.h" @@ -3673,9 +3674,9 @@ static int resolve_copy_blocks_auto_candidate( sd_id128_t *ret_uuid) { _cleanup_(blkid_free_probep) blkid_probe b = NULL; - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_close_ int fd = -1; - const char *pttype, *t, *p; + _cleanup_free_ char *p = NULL; + const char *pttype, *t; sd_id128_t pt_parsed, u; blkid_partition pp; dev_t whole_devno; @@ -3702,19 +3703,10 @@ static int resolve_copy_blocks_auto_candidate( major(partition_devno), minor(partition_devno), major(restrict_devno), minor(restrict_devno)); - r = sd_device_new_from_devnum(&dev, 'b', whole_devno); + fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p); if (r < 0) - return log_error_errno(r, "Failed to create sd-device for block device %u:%u: %m", - major(whole_devno), minor(whole_devno)); - - r = sd_device_get_devname(dev, &p); - if (r < 0) - return log_error_errno(r, "Failed to get name of block device %u:%u: %m", - major(whole_devno), minor(whole_devno)); - - fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (fd < 0) - return log_error_errno(fd, "Failed to open block device %s: %m", p); + return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m", + DEVNUM_FORMAT_VAL(whole_devno)); b = blkid_new_probe(); if (!b) @@ -3996,29 +3988,16 @@ static int context_open_copy_block_paths( "Copying from block device node is not permitted in --image=/--root= mode, refusing."); } else if (p->copy_blocks_auto) { - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; - const char *devname; dev_t devno; r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid); if (r < 0) return r; - r = sd_device_new_from_devnum(&dev, 'b', devno); + source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened); if (r < 0) - return log_error_errno(r, "Failed to create sd-device object for device %u:%u: %m", major(devno), minor(devno)); - - r = sd_device_get_devname(dev, &devname); - if (r < 0) - return log_error_errno(r, "Failed to get device name of %u:%u: %m", major(devno), minor(devno)); - - opened = strdup(devname); - if (!opened) - return log_oom(); - - source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (source_fd < 0) - return log_error_errno(source_fd, "Failed to open automatically determined source block copy device '%s': %m", opened); + return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m", + DEVNUM_FORMAT_VAL(devno)); if (fstat(source_fd, &st) < 0) return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened); @@ -4026,38 +4005,27 @@ static int context_open_copy_block_paths( continue; if (S_ISDIR(st.st_mode)) { - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; - const char *bdev; + _cleanup_free_ char *bdev = NULL; + dev_t devt; /* If the file is a directory, automatically find the backing block device */ if (major(st.st_dev) != 0) - r = sd_device_new_from_devnum(&dev, 'b', st.st_dev); + devt = st.st_dev; else { - dev_t devt; - /* Special support for btrfs */ - r = btrfs_get_block_device_fd(source_fd, &devt); if (r == -EUCLEAN) return btrfs_log_dev_root(LOG_ERR, r, opened); if (r < 0) return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened); - - r = sd_device_new_from_devnum(&dev, 'b', devt); } - if (r < 0) - return log_error_errno(r, "Failed to create sd-device object for block device backing '%s': %m", opened); - - r = sd_device_get_devname(dev, &bdev); - if (r < 0) - return log_error_errno(r, "Failed to get device name for block device backing '%s': %m", opened); safe_close(source_fd); - source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (source_fd < 0) - return log_error_errno(source_fd, "Failed to open block device '%s': %m", bdev); + source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev); + if (r < 0) + return log_error_errno(r, "Failed to open block device backing '%s': %m", opened); if (fstat(source_fd, &st) < 0) return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev); @@ -4548,7 +4516,7 @@ static int acquire_root_devno( if (r < 0) log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p); - r = device_path_make_canonical(S_IFBLK, devno, ret); + r = devpath_from_devnum(S_IFBLK, devno, ret); if (r < 0) return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p); diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index 0d921cc045..6038591d83 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -221,7 +221,6 @@ int get_block_device_harder(const char *path, dev_t *ret) { } int lock_whole_block_device(dev_t devt, int operation) { - _cleanup_free_ char *whole_node = NULL; _cleanup_close_ int lock_fd = -1; dev_t whole_devt; int r; @@ -232,14 +231,10 @@ int lock_whole_block_device(dev_t devt, int operation) { if (r < 0) return r; - r = device_path_make_major_minor(S_IFBLK, whole_devt, &whole_node); + lock_fd = r = device_open_from_devnum(S_IFBLK, whole_devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, NULL); if (r < 0) return r; - lock_fd = open(whole_node, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (lock_fd < 0) - return -errno; - if (flock(lock_fd, operation) < 0) return -errno; diff --git a/src/shared/find-esp.c b/src/shared/find-esp.c index 8a20fa7e47..d21b6742b9 100644 --- a/src/shared/find-esp.c +++ b/src/shared/find-esp.c @@ -45,9 +45,9 @@ static int verify_esp_blkid( const char *v; int r; - r = device_path_make_major_minor(S_IFBLK, devid, &node); + r = devpath_from_devnum(S_IFBLK, devid, &node); if (r < 0) - return log_error_errno(r, "Failed to format major/minor device path: %m"); + return log_error_errno(r, "Failed to get device path for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid)); errno = 0; b = blkid_new_probe_from_filename(node); @@ -151,21 +151,20 @@ static int verify_esp_udev( sd_id128_t *ret_uuid) { _cleanup_(sd_device_unrefp) sd_device *d = NULL; - _cleanup_free_ char *node = NULL; sd_id128_t uuid = SD_ID128_NULL; uint64_t pstart = 0, psize = 0; uint32_t part = 0; - const char *v; + const char *node, *v; int r; - r = device_path_make_major_minor(S_IFBLK, devid, &node); - if (r < 0) - return log_error_errno(r, "Failed to format major/minor device path: %m"); - r = sd_device_new_from_devnum(&d, 'b', devid); if (r < 0) return log_error_errno(r, "Failed to get device from device number: %m"); + r = sd_device_get_devname(d, &node); + if (r < 0) + return log_error_errno(r, "Failed to get device node: %m"); + r = sd_device_get_property_value(d, "ID_FS_TYPE", &v); if (r < 0) return log_error_errno(r, "Failed to get device property: %m"); @@ -509,10 +508,10 @@ static int verify_xbootldr_blkid( const char *type, *v; int r; - r = device_path_make_major_minor(S_IFBLK, devid, &node); + r = devpath_from_devnum(S_IFBLK, devid, &node); if (r < 0) - return log_error_errno(r, "Failed to format block device path for %u:%u: %m", - major(devid), minor(devid)); + return log_error_errno(r, "Failed to get block device path for " DEVNUM_FORMAT_STR ": %m", + DEVNUM_FORMAT_VAL(devid)); errno = 0; b = blkid_new_probe_from_filename(node); @@ -583,19 +582,17 @@ static int verify_xbootldr_udev( sd_id128_t *ret_uuid) { _cleanup_(sd_device_unrefp) sd_device *d = NULL; - _cleanup_free_ char *node = NULL; sd_id128_t uuid = SD_ID128_NULL; - const char *type, *v; + const char *node, *type, *v; int r; - r = device_path_make_major_minor(S_IFBLK, devid, &node); - if (r < 0) - return log_error_errno(r, "Failed to format block device path for %u:%u: %m", - major(devid), minor(devid)); - r = sd_device_new_from_devnum(&d, 'b', devid); if (r < 0) - return log_error_errno(r, "%s: Failed to get block device: %m", node); + return log_error_errno(r, "Failed to get block device for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid)); + + r = sd_device_get_devname(d, &node); + if (r < 0) + return log_error_errno(r, "Failed to get device node: %m"); r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &type); if (r < 0) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index fa71086741..b9c63e2e4c 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -806,7 +806,7 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) { static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) { char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1]; - _cleanup_free_ char *whole = NULL, *buffer = NULL; + _cleanup_free_ char *buffer = NULL; uint64_t current_offset, current_size, partno; _cleanup_close_ int whole_fd = -1; struct stat st; @@ -865,14 +865,10 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) { if (r < 0) return r; - r = device_path_make_major_minor(S_IFBLK, devno, &whole); + whole_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL); if (r < 0) return r; - whole_fd = open(whole, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (whole_fd < 0) - return -errno; - struct blkpg_partition bp = { .pno = partno, .start = offset == UINT64_MAX ? current_offset : offset, diff --git a/src/shared/quota-util.c b/src/shared/quota-util.c index 7aacad1204..5bf533ddff 100644 --- a/src/shared/quota-util.c +++ b/src/shared/quota-util.c @@ -5,7 +5,7 @@ #include "alloc-util.h" #include "blockdev-util.h" -#include "devnum-util.h" +#include "device-util.h" #include "quota-util.h" int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) { @@ -15,7 +15,7 @@ int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) { /* Like quotactl() but takes a dev_t instead of a path to a device node, and fixes caddr_t → void*, * like we should, today */ - r = device_path_make_major_minor(S_IFBLK, devnum, &devnode); + r = devpath_from_devnum(S_IFBLK, devnum, &devnode); if (r < 0) return r; diff --git a/src/sysupdate/sysupdate-resource.c b/src/sysupdate/sysupdate-resource.c index 359b80ac1c..2a2fb29c20 100644 --- a/src/sysupdate/sysupdate-resource.c +++ b/src/sysupdate/sysupdate-resource.c @@ -7,7 +7,7 @@ #include "alloc-util.h" #include "blockdev-util.h" #include "chase-symlinks.h" -#include "devnum-util.h" +#include "device-util.h" #include "dirent-util.h" #include "env-util.h" #include "fd-util.h" @@ -608,7 +608,7 @@ int resource_resolve_path( return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File system is not placed on a partition block device, cannot determine whole block device backing root file system."); - r = device_path_make_canonical(S_IFBLK, d, &p); + r = devpath_from_devnum(S_IFBLK, d, &p); if (r < 0) return r; diff --git a/src/udev/udevadm-lock.c b/src/udev/udevadm-lock.c index a1e04f6516..07641c24c3 100644 --- a/src/udev/udevadm-lock.c +++ b/src/udev/udevadm-lock.c @@ -7,7 +7,7 @@ #include "blockdev-util.h" #include "btrfs-util.h" -#include "devnum-util.h" +#include "device-util.h" #include "fd-util.h" #include "fdset.h" #include "main-func.h" @@ -321,7 +321,7 @@ int lock_main(int argc, char *argv[], void *userdata) { for (size_t i = 0; i < n_devnos; i++) { _cleanup_free_ char *node = NULL; - r = device_path_make_canonical(S_IFBLK, devnos[i], &node); + r = devpath_from_devnum(S_IFBLK, devnos[i], &node); if (r < 0) return log_error_errno(r, "Failed to format block device path: %m");