[lxc-devel] [lxc/master] lxccontainer: improve file locking

brauner on Github lxc-bot at linuxcontainers.org
Tue Aug 14 22:38:08 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180814/55015dc2/attachment.bin>
-------------- next part --------------
From d1bc8d484caa7b3157aa76da346badabf2a64d12 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 15 Aug 2018 00:37:05 +0200
Subject: [PATCH] lxccontainer: improve file locking

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/lxccontainer.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 04c2f672b..5f1e7330c 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -155,12 +155,13 @@ static int ongoing_create(struct lxc_container *c)
 	if (ret < 0 || (size_t)ret >= len)
 		return -1;
 
-	if (!file_exists(path))
-		return 0;
+	fd = open(path, O_RDWR | O_CLOEXEC);
+	if (fd < 0) {
+		if (errno != ENOENT)
+			return -1;
 
-	fd = open(path, O_RDWR);
-	if (fd < 0)
 		return 0;
+	}
 
 	lk.l_type = F_WRLCK;
 	lk.l_whence = SEEK_SET;
@@ -170,8 +171,11 @@ static int ongoing_create(struct lxc_container *c)
 	lk.l_pid = 0;
 
 	ret = fcntl(fd, F_OFD_GETLK, &lk);
-	if (ret < 0 && errno == EINVAL)
+	if (ret < 0 && errno == EINVAL) {
 		ret = flock(fd, LOCK_EX | LOCK_NB);
+		if (ret < 0 && errno == EWOULDBLOCK)
+			ret = 0;
+	}
 
 	close(fd);
 
@@ -198,7 +202,7 @@ static int create_partial(struct lxc_container *c)
 	if (ret < 0 || (size_t)ret >= len)
 		return -1;
 
-	fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0755);
+	fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0000);
 	if (fd < 0)
 		return -1;
 


More information about the lxc-devel mailing list