From: Wei Liu Subject: [PATCH 2/2] libxl_internal: comment on domain userdata unlock function Date: Wed, 7 Jan 2015 15:23:00 +0000 Message-ID: <1420644180-7733-3-git-send-email-wei.liu2@citrix.com> References: <1420644180-7733-1-git-send-email-wei.liu2@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1420644180-7733-1-git-send-email-wei.liu2@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Ian Jackson , Wei Liu , Ian Campbell List-Id: xen-devel@lists.xenproject.org Discuss why we need to unlink file path before closes fd. Signed-off-by: Wei Liu Cc: Ian Campbell Cc: Ian Jackson --- tools/libxl/libxl_internal.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 9d8025d..a70214b 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -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); -- 1.7.10.4