Skip to contentSkip to conversation

Two libxl misc patches

PATCH In review Wei Liu 3 participants 7 messages Jan 7, 2015 – Jan 8, 2015

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover Two libxl misc patches

These changes were requested by Ian Campbell when relevant patches were posted but they didn't warrant a resend at that time.

Wei Liu (2):
libxl_internal: lock_carefd -> carefd
libxl_internal: comment on domain userdata unlock function

 tools/libxl/libxl_internal.c |   20 +++++++++++++++++---
 tools/libxl/libxl_internal.h |    2 +-
 2 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 1/2 libxl_internal: lock_carefd -> carefd

lock_ prefix is redundant.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl_internal.c |    6 +++---
 tools/libxl/libxl_internal.h |    2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/libxl/libxl_internal.c +3 -3
index 00c3b1e..9d8025d 100644
@@ -405,7 +405,7 @@ libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc,
         fd = open(lockfile, O_RDWR|O_CREAT, 0666);
         if (fd < 0)
             LOGE(ERROR, "cannot open lockfile %s, errno=%d", lockfile, errno);
-        lock->lock_carefd = libxl__carefd_opened(CTX, fd);
+        lock->carefd = libxl__carefd_opened(CTX, fd);
         if (fd < 0) goto out;
 
         /* Lock the file in exclusive mode, wait indefinitely to
@@ -440,7 +440,7 @@ libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc,
                 break;
         }
 
-        libxl__carefd_close(lock->lock_carefd);
+        libxl__carefd_close(lock->carefd);
     }
 
     /* Check the domain is still there, if not we should release the
@@ -459,7 +459,7 @@ out:
 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock)
 {
     if (lock->path) unlink(lock->path);
-    if (lock->lock_carefd) libxl__carefd_close(lock->lock_carefd);
+    if (lock->carefd) libxl__carefd_close(lock->carefd);
     free(lock->path);
     free(lock);
 }
tools/libxl/libxl_internal.h +1 -1
index 9695f18..3a4e222 100644
@@ -3522,7 +3522,7 @@ int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl);
 
 /* Portability note: a proper flock(2) implementation is required */
 typedef struct {
-    libxl__carefd *lock_carefd;
+    libxl__carefd *carefd;
     char *path; /* path of the lock file itself */
 } libxl__domain_userdata_lock;
 libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc,
Signature · 1 line
1.7.10.4
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] libxl_internal: lock_carefd -> carefd

Wei Liu writes ("[PATCH 1/2] libxl_internal: lock_carefd -> carefd"):

Quoted 1 line

lock_ prefix is redundant.

(for 4.6)

author
#3 lore raw

PATCH 2/2 libxl_internal: comment on domain userdata unlock function

In reply to Wei Liu

Discuss why we need to unlink file path before closes fd.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl_internal.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/libxl/libxl_internal.c +14 -0
index 9d8025d..a70214b 100644
@@ -458,6 +458,20 @@ out:
 
 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock)
 {
+    /* It's important to unlink the file before closing fd to avoid
+     * such race (if close before unlink):
+     *
+     *   P1 LOCK                         P2 UNLOCK
+     *   fd1 = open(lockfile)
+     *                                   close(fd2)
+     *   flock(fd1)
+     *   fstat and stat check success
+     *                                   unlink(lockfile)
+     *   return lock
+     *
+     * In above case P1 thinks it has got hold of the lock but
+     * actually lock is released by P2 (lockfile unlinked).
+     */
     if (lock->path) unlink(lock->path);
     if (lock->carefd) libxl__carefd_close(lock->carefd);
     free(lock->path);
Signature · 1 line
1.7.10.4
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] libxl_internal: comment on domain userdata unlock function

Wei Liu writes ("[PATCH 2/2] libxl_internal: comment on domain userdata unlock function"):

Quoted 1 line

Discuss why we need to unlink file path before closes fd.

subject to minor grammar complaint:

Potential backport candidate for 4.5.1 ?

tools/libxl/libxl_internal.c out: · line 462

Quoted 10 lines
tools/libxl/libxl_internal.c +2 -0
index 9d8025d..a70214b 100644
@@ -458,6 +458,20 @@ out:
 
 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock)
 {
+    /* It's important to unlink the file before closing fd to avoid
+     * such race (if close before unlink):
          ^^^^

"to avoid the following race". "Such" must refer to a thing which precedes.

Ian.

author
#5 lore raw

Re: [PATCH 2/2] libxl_internal: comment on domain userdata unlock function

Quoted 8 lines · Ian Jackson

Wei Liu writes ("[PATCH 2/2] libxl_internal: comment on domain userdata unlock function"):

Quoted 1 line

Discuss why we need to unlink file path before closes fd.

subject to minor grammar complaint:

Potential backport candidate for 4.5.1 ?

Sure, if you feel this is important.

Quoted 6 lines
Quoted 6 lines
tools/libxl/libxl_internal.c +0 -0
index 9d8025d..a70214b 100644
@@ -458,6 +458,20 @@ out:
 
Quoted 9 lines

tools/libxl/libxl_internal.c out: · line 462

Quoted 4 lines
tools/libxl/libxl_internal.c +2 -0
 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock)
 {
+    /* It's important to unlink the file before closing fd to avoid
+     * such race (if close before unlink):
          ^^^^

"to avoid the following race". "Such" must refer to a thing which precedes.

I suppose you (or Ian C) can fix this when committing? Do I need to resend?

Wei.

Quoted 1 line

Ian.

#6 lore raw

Re: [PATCH 2/2] libxl_internal: comment on domain userdata unlock function

Quoted 12 lines · Wei Liu
Quoted 12 lines

tools/libxl/libxl_internal.c out: · line 462

Quoted 10 lines
tools/libxl/libxl_internal.c +2 -0
index 9d8025d..a70214b 100644
@@ -458,6 +458,20 @@ out:
 
 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock)
 {
+    /* It's important to unlink the file before closing fd to avoid
+     * such race (if close before unlink):
          ^^^^
Quoted 6 lines
Quoted 3 lines

"to avoid the following race". "Such" must refer to a thing which precedes.

I suppose you (or Ian C) can fix this when committing? Do I need to resend?

I've applied both patches and fixed this up.