]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/dissect-image.c
resolved: rework parsing of /etc/hosts
[thirdparty/systemd.git] / src / shared / dissect-image.c
index 5f19ffc4fb679e9673e04f6c38b869611f7d010d..4e572ac861c5a18c24b633f593ee46888b9b16b9 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/prctl.h>
 #include <sys/wait.h>
 
+#include "sd-device.h"
 #include "sd-id128.h"
 
 #include "architecture.h"
@@ -14,7 +15,9 @@
 #include "crypt-util.h"
 #include "def.h"
 #include "device-nodes.h"
+#include "device-util.h"
 #include "dissect-image.h"
+#include "env-file.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
@@ -25,6 +28,7 @@
 #include "linux-3.13/dm-ioctl.h"
 #include "missing.h"
 #include "mount-util.h"
+#include "mountpoint-util.h"
 #include "os-util.h"
 #include "path-util.h"
 #include "process-util.h"
@@ -35,7 +39,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
-#include "udev-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "xattr-util.h"
 
@@ -94,19 +98,20 @@ not_found:
 #if HAVE_BLKID
 /* Detect RPMB and Boot partitions, which are not listed by blkid.
  * See https://github.com/systemd/systemd/issues/5806. */
-static bool device_is_mmc_special_partition(struct udev_device *d) {
+static bool device_is_mmc_special_partition(sd_device *d) {
         const char *sysname;
 
-        sysname = udev_device_get_sysname(d);
-        return sysname && startswith(sysname, "mmcblk") &&
+        if (sd_device_get_sysname(d, &sysname) < 0)
+                return false;
+
+        return startswith(sysname, "mmcblk") &&
                 (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1"));
 }
 
-static bool device_is_block(struct udev_device *d) {
+static bool device_is_block(sd_device *d) {
         const char *ss;
 
-        ss = udev_device_get_subsystem(d);
-        if (!ss)
+        if (sd_device_get_subsystem(d, &ss) < 0)
                 return false;
 
         return streq(ss, "block");
@@ -122,19 +127,18 @@ int dissect_image(
 
 #if HAVE_BLKID
         sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
-        _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
+        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
-        _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *d = NULL;
         _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
         _cleanup_(blkid_free_probep) blkid_probe b = NULL;
-        _cleanup_(udev_unrefp) struct udev *udev = NULL;
         _cleanup_free_ char *generic_node = NULL;
         sd_id128_t generic_uuid = SD_ID128_NULL;
         const char *pttype = NULL;
-        struct udev_list_entry *first, *item;
         blkid_partlist pl;
         int r, generic_nr;
         struct stat st;
+        sd_device *q;
         unsigned i;
 
         assert(fd >= 0);
@@ -218,8 +222,9 @@ int dissect_image(
                                         return -ENOMEM;
                         }
 
-                        if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
-                                return -ENOMEM;
+                        r = device_path_make_major_minor(st.st_mode, st.st_rdev, &n);
+                        if (r < 0)
+                                return r;
 
                         m->partitions[PARTITION_ROOT] = (DissectedPartition) {
                                 .found = true,
@@ -230,7 +235,7 @@ int dissect_image(
                                 .node = TAKE_PTR(n),
                         };
 
-                        m->encrypted = streq(fstype, "crypto_LUKS");
+                        m->encrypted = streq_ptr(fstype, "crypto_LUKS");
 
                         *ret = TAKE_PTR(m);
 
@@ -253,13 +258,9 @@ int dissect_image(
         if (!pl)
                 return -errno ?: -ENOMEM;
 
-        udev = udev_new();
-        if (!udev)
-                return -errno;
-
-        d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
-        if (!d)
-                return -ENOMEM;
+        r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
+        if (r < 0)
+                return r;
 
         for (i = 0;; i++) {
                 int n, z;
@@ -269,31 +270,22 @@ int dissect_image(
                         return -ENXIO;
                 }
 
-                e = udev_enumerate_new(udev);
-                if (!e)
-                        return -errno;
+                r = sd_device_enumerator_new(&e);
+                if (r < 0)
+                        return r;
 
-                r = udev_enumerate_add_match_parent(e, d);
+                r = sd_device_enumerator_allow_uninitialized(e);
                 if (r < 0)
                         return r;
 
-                r = udev_enumerate_scan_devices(e);
+                r = sd_device_enumerator_add_match_parent(e, d);
                 if (r < 0)
                         return r;
 
                 /* Count the partitions enumerated by the kernel */
                 n = 0;
-                first = udev_enumerate_get_list_entry(e);
-                udev_list_entry_foreach(item, first) {
-                        _cleanup_(udev_device_unrefp) struct udev_device *q;
-                        dev_t qn;
-
-                        q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
-                        if (!q)
-                                return -errno;
-
-                        qn = udev_device_get_devnum(q);
-                        if (major(qn) == 0)
+                FOREACH_DEVICE(e, q) {
+                        if (sd_device_get_devnum(q, NULL) < 0)
                                 continue;
 
                         if (!device_is_block(q))
@@ -353,24 +345,18 @@ int dissect_image(
                         }
                 }
 
-                e = udev_enumerate_unref(e);
+                e = sd_device_enumerator_unref(e);
         }
 
-        first = udev_enumerate_get_list_entry(e);
-        udev_list_entry_foreach(item, first) {
-                _cleanup_(udev_device_unrefp) struct udev_device *q;
+        FOREACH_DEVICE(e, q) {
                 unsigned long long pflags;
                 blkid_partition pp;
                 const char *node;
                 dev_t qn;
                 int nr;
 
-                q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
-                if (!q)
-                        return -errno;
-
-                qn = udev_device_get_devnum(q);
-                if (major(qn) == 0)
+                r = sd_device_get_devnum(q, &qn);
+                if (r < 0)
                         continue;
 
                 if (st.st_rdev == qn)
@@ -382,8 +368,8 @@ int dissect_image(
                 if (device_is_mmc_special_partition(q))
                         continue;
 
-                node = udev_device_get_devnode(q);
-                if (!node)
+                r = sd_device_get_devname(q, &node);
+                if (r < 0)
                         continue;
 
                 pp = blkid_partlist_devno_to_partition(pl, qn);
@@ -787,7 +773,7 @@ int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift,
                 }
         }
 
-        if ((flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY))
+        if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY)
                 return 0;
 
         r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags);
@@ -1104,14 +1090,13 @@ int dissected_image_decrypt_interactively(
                 else if (r != -ENOKEY)
                         return log_error_errno(r, "Failed to decrypt image: %m");
 
-                if (--n < 0) {
-                        log_error("Too many retries.");
-                        return -EKEYREJECTED;
-                }
+                if (--n < 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED),
+                                               "Too many retries.");
 
                 z = strv_free(z);
 
-                r = ask_password_auto("Please enter image passphrase!", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
+                r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query for passphrase: %m");
 
@@ -1351,14 +1336,14 @@ int dissected_image_acquire_metadata(DissectedImage *m) {
                 }
 
                 case META_MACHINE_INFO:
-                        r = load_env_file_pairs(f, "machine-info", NULL, &machine_info);
+                        r = load_env_file_pairs(f, "machine-info", &machine_info);
                         if (r < 0)
                                 log_debug_errno(r, "Failed to read /etc/machine-info: %m");
 
                         break;
 
                 case META_OS_RELEASE:
-                        r = load_env_file_pairs(f, "os-release", NULL, &os_release);
+                        r = load_env_file_pairs(f, "os-release", &os_release);
                         if (r < 0)
                                 log_debug_errno(r, "Failed to read OS release file: %m");