qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v4 21/29] util: use fcntl() for qemu_write_pidfile()


From: Marc-André Lureau
Subject: [Qemu-devel] [PATCH v4 21/29] util: use fcntl() for qemu_write_pidfile() locking
Date: Fri, 13 Jul 2018 15:09:08 +0200

According to Daniel Berrange, fcntl() locks have better portable
semantics than lockf().

Use an exclusive lock on the first byte with fcntl().

Signed-off-by: Marc-André Lureau <address@hidden>
---
 util/oslib-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index da1d4a3201..26b11490b9 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -92,6 +92,11 @@ bool qemu_write_pidfile(const char *pidfile, Error **errp)
 {
     int pidfd;
     char pidstr[32];
+    struct flock lock = {
+        .l_type = F_WRLCK,
+        .l_whence = SEEK_SET,
+        .l_len = 1,
+    };
 
     pidfd = qemu_open(pidfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
     if (pidfd == -1) {
@@ -99,10 +104,11 @@ bool qemu_write_pidfile(const char *pidfile, Error **errp)
         return false;
     }
 
-    if (lockf(pidfd, F_TLOCK, 0)) {
+    if (fcntl(pidfd, F_SETLK, &lock)) {
         error_setg_errno(errp, errno, "Cannot lock pid file");
         goto fail;
     }
+
     if (ftruncate(pidfd, 0)) {
         error_setg_errno(errp, errno, "Failed to truncate pid file");
         goto fail;
-- 
2.18.0.129.ge3331758f1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]