summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/context.c
diff options
context:
space:
mode:
authorKarel Zak2017-06-30 11:49:12 +0200
committerKarel Zak2017-07-14 11:34:55 +0200
commitb9da1532546630cda5d9dd9863ba2e6477a60bba (patch)
tree793c4643d2b690b086ef9cb40ed3b3a63a5294f7 /libfdisk/src/context.c
parentlibfdisk: remove unnecessary fstat() call (diff)
downloadkernel-qcow2-util-linux-b9da1532546630cda5d9dd9863ba2e6477a60bba.tar.gz
kernel-qcow2-util-linux-b9da1532546630cda5d9dd9863ba2e6477a60bba.tar.xz
kernel-qcow2-util-linux-b9da1532546630cda5d9dd9863ba2e6477a60bba.zip
libfdisk: add fdisk_device_is_used()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/context.c')
-rw-r--r--libfdisk/src/context.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index e8dfa0e69..1ebc1b981 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -2,6 +2,8 @@
# include <blkid.h>
#endif
+#include "blkdev.h"
+#include "loopdev.h"
#include "fdiskP.h"
@@ -734,6 +736,38 @@ int fdisk_reread_partition_table(struct fdisk_context *cxt)
/**
+ * fdisk_device_is_used:
+ * @cxt: context
+ *
+ * On systems where is no BLKRRPART ioctl the function returns zero and
+ * sets errno to ENOSYS.
+ *
+ * Returns: 1 if the device assigned to the context is used by system, or 0.
+ */
+int fdisk_device_is_used(struct fdisk_context *cxt)
+{
+ int rc = 0;
+
+ assert(cxt);
+ assert(cxt->dev_fd >= 0);
+
+ errno = 0;
+
+#ifdef BLKRRPART
+ /* it seems kernel always return EINVAL for BLKRRPART on loopdevices */
+ if (S_ISBLK(cxt->dev_st.st_mode)
+ && major(cxt->dev_st.st_rdev) != LOOPDEV_MAJOR) {
+ DBG(CXT, ul_debugobj(cxt, "calling re-read ioctl"));
+ rc = ioctl(cxt->dev_fd, BLKRRPART) != 0;
+ }
+#else
+ errno = ENOSYS;
+#endif
+ DBG(CXT, ul_debugobj(cxt, "device used: %s [errno=%d]", rc ? "TRUE" : "FALSE", errno));
+ return rc;
+}
+
+/**
* fdisk_is_readonly:
* @cxt: context
*