[parted-devel] [PATCH] libparted: Use flock to lock access to the device

Brian C. Lane bcl at redhat.com
Tue Aug 11 20:49:07 UTC 2015


It is possible for other processes, espceially udev, to try and access
the device while parted is examining it or making changes. This tries to
get an exclusive lock on the device when it is opened for RW access. If
it fails it prints a warning and continues on.

Resolves: rhbz#1245144
---
 libparted/arch/linux.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 341bbbb..e2e2e83 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -35,6 +35,7 @@
 #include <unistd.h>
 #include <stdbool.h>
 #include <dirent.h>
+#include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -1595,6 +1596,15 @@ retry:
                 }
         } else {
                 dev->read_only = 0;
+
+                /* Exclusive access to the device until finished with it. */
+                if (flock(arch_specific->fd, LOCK_EX) < 0) {
+                    ped_exception_throw (
+                            PED_EXCEPTION_WARNING,
+                            PED_EXCEPTION_OK,
+                            _("Unable to lock %s for exclusive access (%s)."),
+                            dev->path, strerror (errno));
+                }
         }
 
         _flush_cache (dev);
-- 
2.4.3




More information about the parted-devel mailing list