Add kernel version to DM debug output.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
index 9100a3e..ad6e908 100644 (file)
@@ -28,6 +28,7 @@
 #include <fcntl.h>
 #include <linux/fs.h>
 #include <uuid/uuid.h>
+#include <sys/utsname.h>
 
 #include "internal.h"
 
@@ -143,6 +144,16 @@ static void _dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
                verity_maj, verity_min, verity_patch);
 }
 
+static void _dm_kernel_info(void)
+{
+       struct utsname uts;
+
+       if (!uname(&uts))
+               log_dbg("Detected kernel %s %s %s.",
+                       uts.sysname, uts.release, uts.machine);
+
+}
+
 static int _dm_check_versions(void)
 {
        struct dm_task *dmt;
@@ -153,6 +164,8 @@ static int _dm_check_versions(void)
        if (_dm_crypt_checked)
                return 1;
 
+       _dm_kernel_info();
+
        /* Shut up DM while checking */
        _quiet_log = 1;
 
@@ -493,14 +506,19 @@ int dm_remove_device(struct crypt_device *cd, const char *name,
  * CRYPT-LUKS1-00000000000000000000000000000000-name
  * CRYPT-TEMP-name
  */
-static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
+static int dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
 {
        char *ptr, uuid2[UUID_LEN] = {0};
        uuid_t uu;
        unsigned i = 0;
 
        /* Remove '-' chars */
-       if (uuid && !uuid_parse(uuid, uu)) {
+       if (uuid) {
+               if (uuid_parse(uuid, uu) < 0) {
+                       log_dbg("Requested UUID %s has invalid format.", uuid);
+                       return -EINVAL;
+               }
+
                for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
                        if (uuid[i] != '-') {
                                *ptr = uuid[i];
@@ -516,6 +534,8 @@ static void dm_prepare_uuid(const char *name, const char *type, const char *uuid
        log_dbg("DM-UUID is %s", buf);
        if (i >= buflen)
                log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
+
+       return 0;
 }
 
 static int _dm_create_device(const char *name, const char *type,
@@ -542,7 +562,9 @@ static int _dm_create_device(const char *name, const char *type,
                if (!dm_task_set_name(dmt, name))
                        goto out_no_removal;
        } else {
-               dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
+               r = dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
+               if (r < 0)
+                       return r;
 
                if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
                        goto out_no_removal;
@@ -696,6 +718,14 @@ int dm_status_device(struct crypt_device *cd, const char *name)
 {
        int r;
        struct dm_info dmi;
+       struct stat st;
+
+       /* libdevmapper is too clever and handles
+        * path argument differenly with error.
+        * Fail early here if parameter is non-existent path.
+        */
+       if (strchr(name, '/') && stat(name, &st) < 0)
+               return -ENODEV;
 
        if (dm_init_context(cd))
                return -ENOTSUP;