Use libblkid directly instead of the binary command in blkid.
Signed-off-by: Wanlong Gao <gaowanlong(a)cn.fujitsu.com>
---
appliance/packagelist.in | 1 +
configure.ac | 10 +++++++
daemon/Makefile.am | 1 +
daemon/blkid.c | 59 +++++++++++++++++++++++++---------------------
4 files changed, 44 insertions(+), 27 deletions(-)
diff --git a/appliance/packagelist.in b/appliance/packagelist.in
index 2ab6b80..575b8dd 100644
--- a/appliance/packagelist.in
+++ b/appliance/packagelist.in
@@ -45,6 +45,7 @@
vim-minimal
xz
zfs-fuse
+ libblkid
#endif /* REDHAT */
#ifdef DEBIAN
diff --git a/configure.ac b/configure.ac
index 31a2cc5..517149d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -393,6 +393,16 @@ if test "x$have_libselinux" = "xyes"; then
AC_DEFINE([HAVE_LIBSELINUX],[1],[Define to 1 if you have libselinux])
fi
AC_SUBST([SELINUX_LIB])
+AC_CHECK_HEADERS([blkid/blkid.h])
+AC_CHECK_LIB([blkid],[blkid_new_probe],[
+ have_libblkid="$ac_cv_header_blkid_blkid_h"
+ LIBBLKID="-lblkid"
+ LIBS="$LIBS $LIBBLKID"
+ ],[have_libblkid=no])
+if test "x$have_libblkid" = "xyes"; then
+ AC_DEFINE([HAVE_LIBBLKID],[1],[Define to 1 if you have libblkid])
+fi
+AC_SUBST([LIBBLKID])
dnl Check for systemtap/DTrace userspace probes (optional).
dnl
http://sourceware.org/systemtap/wiki/AddingUserSpaceProbingToApps
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 3a698cc..5e4db57 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -167,6 +167,7 @@ guestfsd_LDADD = \
liberrnostring.a \
libprotocol.a \
$(SELINUX_LIB) \
+ $(LIBBLKID) \
$(AUGEAS_LIBS) \
$(top_builddir)/gnulib/lib/.libs/libgnu.a \
$(GETADDRINFO_LIB) \
diff --git a/daemon/blkid.c b/daemon/blkid.c
index 8728c29..0472707 100644
--- a/daemon/blkid.c
+++ b/daemon/blkid.c
@@ -23,6 +23,8 @@
#include <string.h>
#include <unistd.h>
#include <limits.h>
+#include <fcntl.h>
+#include <blkid/blkid.h>
#include "daemon.h"
#include "actions.h"
@@ -30,40 +32,43 @@
static char *
get_blkid_tag (const char *device, const char *tag)
{
- char *out, *err;
- int r;
+ int fd, rc;
+ const char *data = NULL;
+ blkid_probe blkprobe;
- r = commandr (&out, &err,
- "blkid",
- /* Adding -c option kills all caching, even on RHEL 5. */
- "-c", "/dev/null",
- "-o", "value", "-s", tag, device, NULL);
- if (r != 0 && r != 2) {
- if (r >= 0)
- reply_with_error ("%s: %s (blkid returned %d)", device, err, r);
- else
- reply_with_error ("%s: %s", device, err);
- free (out);
- free (err);
+ fd = open (device, O_RDONLY);
+ if (fd < 0) {
+ reply_with_perror ("open %s", device);
return NULL;
}
- free (err);
+ blkprobe = blkid_new_probe ();
+ if (!blkprobe)
+ goto done;
+ if (blkid_probe_set_device (blkprobe, fd, 0, 0))
+ goto done;
- if (r == 2) { /* means UUID etc not found */
- free (out);
- out = strdup ("");
- if (out == NULL)
- reply_with_perror ("strdup");
- return out;
- }
+ blkid_probe_enable_superblocks (blkprobe, 1);
+ blkid_probe_enable_partitions (blkprobe, 1);
- /* Trim trailing \n if present. */
- size_t len = strlen (out);
- if (len > 0 && out[len-1] == '\n')
- out[len-1] = '\0';
+ blkid_probe_set_superblocks_flags (blkprobe, BLKID_SUBLKS_LABEL |
+ BLKID_SUBLKS_UUID | BLKID_SUBLKS_TYPE);
+
+ rc = blkid_do_safeprobe (blkprobe);
+ if (!rc)
+ blkid_probe_lookup_value (blkprobe, tag, &data, NULL);
+
+done:
+ close (fd);
+ if (data)
+ data = strdup ((char *) data);
+ else
+ data = strdup ("");
+ blkid_free_probe (blkprobe);
- return out; /* caller frees */
+ if (!data)
+ reply_with_perror ("strdup");
+ return (char *) data;
}
char *
--
1.7.9