]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: add udev support
authorIlias Mamedov <arknir@yandex.ru>
Mon, 3 Oct 2011 12:22:42 +0000 (14:22 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 3 Oct 2011 12:25:43 +0000 (14:25 +0200)
[kzak@redhat.com: - enable udev support by default
                  - don't check for libudev.h
                  - minor udev code refactoring in lsblk.c]

Signed-off-by: Ilias Mamedov <arknir@yandex.ru>
Signed-off-by: Karel Zak <kzak@redhat.com>
configure.ac
misc-utils/Makefile.am
misc-utils/lsblk.c

index fb0b0eb37e088e28b6ad0fc78ad5fecfa0313963..d12a4a0371f0b427a2c26f84e086bcbc592dc42f 100644 (file)
@@ -1111,11 +1111,28 @@ else
   UTIL_CHECK_LIB(audit, audit_log_user_message)
   case "$with_audit:$have_audit" in
   yes:no)
-   AC_MSG_ERROR([Audit selected but libaudit not found (or doesn't support audit_log_user_message())])
+   AC_MSG_ERROR([Audit selected but libaudit not found (or does not support audit_log_user_message())])
    ;;
   esac
 fi
 
+
+AC_ARG_WITH([udev], AS_HELP_STRING([--without-udev], [compile without udev support]),
+  [], with_udev=auto
+)
+
+if test "x$with_udev" = xno; then
+  AM_CONDITIONAL(HAVE_UDEV, false)
+else
+  UTIL_CHECK_LIB(udev, udev_new)
+  case "$with_udev:$have_udev" in
+  yes:no)
+   AC_MSG_ERROR([udev selected but libudev not found])
+   ;;
+  esac
+fi
+
+
 AC_ARG_ENABLE([schedutils],
   AS_HELP_STRING([--disable-schedutils], [do not build chrt, ionice, teskset]),
   [], enable_schedutils=yes
index 300dab46e46fdb1a28fd4bfb552a530cf33e566e..9b941f518115c7ccc5761aed54560c23a15621fd 100644 (file)
@@ -66,6 +66,9 @@ lsblk_SOURCES = lsblk.c \
                $(top_srcdir)/lib/at.c
 lsblk_LDADD = $(ul_libblkid_la)
 lsblk_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
+if HAVE_UDEV
+lsblk_LDADD += -ludev
+endif
 endif
 
 if HAVE_STATIC_BLKID
index 3c4555d871f71249481807fd7c414992c60efcbb..f9c06fe075b851e078cca8332c5a4bb3558993b5 100644 (file)
 
 #include <blkid.h>
 
+#ifdef HAVE_LIBUDEV
+#include <libudev.h>
+#endif
+
 #include <assert.h>
 
 #include "pathnames.h"
@@ -284,20 +288,58 @@ static char *get_device_mountpoint(struct blkdev_cxt *cxt)
        return strlen(mnt) ? xstrdup(mnt) : NULL;
 }
 
-/* TODO: read info from udev db (if possible) for non-root users
- */
+#ifndef HAVE_LIBUDEV
+static int probe_device_by_udev(struct blkdev_cxt *cxt
+                               __attribute__((__unused__)))
+{
+       return -1;
+}
+#else
+static int probe_device_by_udev(struct blkdev_cxt *cxt)
+{
+       struct udev *udev;
+       struct udev_device *dev;
+
+       udev = udev_new();
+       if (!udev)
+               return -1;
+
+       dev = udev_device_new_from_subsystem_sysname(udev, "block", cxt->name);
+       if (dev) {
+               const char *data;
+
+               if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL")))
+                       cxt->label = xstrdup(data);
+               if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE")))
+                       cxt->fstype = xstrdup(data);
+               if ((data = udev_device_get_property_value(dev, "ID_FS_UUID")))
+                       cxt->uuid = xstrdup(data);
+
+               udev_device_unref(dev);
+       }
+
+       udev_unref(udev);
+        return 0;
+}
+#endif /* HAVE_LIBUDEV */
+
 static void probe_device(struct blkdev_cxt *cxt)
 {
-       char *path = NULL;
        blkid_probe pr = NULL;
 
        if (cxt->probed)
                return;
+
        cxt->probed = 1;
 
        if (!cxt->size)
                return;
 
+       /* try udev DB */
+       if (probe_device_by_udev(cxt) == 0)
+               return;                         /* success */
+
+       /* try libblkid */
        pr = blkid_new_probe_from_filename(cxt->filename);
        if (!pr)
                return;
@@ -320,7 +362,6 @@ static void probe_device(struct blkdev_cxt *cxt)
                        cxt->label = xstrdup(data);
        }
 
-       free(path);
        blkid_free_probe(pr);
        return;
 }