[Lxc-users] [PATCH] lxclock and lxccontainer: switch from flock to fcntl

2013-06-05 Thread Serge Hallyn
flock is not supported on nfs.  fcntl is at least supported on newer
(v3 and above) nfs.

Signed-off-by: Serge Hallyn serge.hal...@ubuntu.com
---
 src/lxc/lxccontainer.c |   22 +-
 src/lxc/lxclock.c  |   16 ++--
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 2edf749..cf5252b 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -23,6 +23,7 @@
 #include sys/types.h
 #include sys/wait.h
 #include errno.h
+#include fcntl.h
 #include sched.h
 #include config.h
 #include lxc.h
@@ -39,7 +40,6 @@
 #include lxc/utils.h
 #include lxc/monitor.h
 #include sched.h
-#include fcntl.h
 #include arpa/inet.h
 #include ifaddrs.h
 
@@ -66,6 +66,8 @@ int ongoing_create(struct lxc_container *c)
int len = strlen(c-config_path) + strlen(c-name) + 10;
char *path = alloca(len);
int fd, ret;
+   struct flock lk;
+
ret = snprintf(path, len, %s/%s/partial, c-config_path, c-name);
if (ret  0 || ret = len) {
ERROR(Error writing partial pathname);
@@ -82,8 +84,12 @@ int ongoing_create(struct lxc_container *c)
process_unlock();
return 0;
}
-   if ((ret = flock(fd, LOCK_EX | LOCK_NB)) == -1 
-   errno == EWOULDBLOCK) {
+   lk.l_type = F_WRLCK;
+   lk.l_whence = SEEK_SET;
+   lk.l_start = 0;
+   lk.l_len = 0;
+   lk.l_pid = -1;
+   if (fcntl(fd, F_GETLK, lk) == 0  lk.l_pid != -1) {
// create is still ongoing
close(fd);
process_unlock();
@@ -101,6 +107,8 @@ int create_partial(struct lxc_container *c)
int len = strlen(c-config_path) + strlen(c-name) + 10;
char *path = alloca(len);
int fd, ret;
+   struct flock lk;
+
ret = snprintf(path, len, %s/%s/partial, c-config_path, c-name);
if (ret  0 || ret = len) {
ERROR(Error writing partial pathname);
@@ -108,12 +116,16 @@ int create_partial(struct lxc_container *c)
}
if (process_lock())
return -1;
-   if ((fd=open(path, O_CREAT | O_EXCL, 0755))  0) {
+   if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755))  0) {
SYSERROR(Erorr creating partial file);
process_unlock();
return -1;
}
-   if (flock(fd, LOCK_EX)  0) {
+   lk.l_type = F_WRLCK;
+   lk.l_whence = SEEK_SET;
+   lk.l_start = 0;
+   lk.l_len = 0;
+   if (fcntl(fd, F_SETLKW, lk)  0) {
SYSERROR(Error locking partial file %s, path);
close(fd);
process_unlock();
diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
index 4bbe873..d004cc5 100644
--- a/src/lxc/lxclock.c
+++ b/src/lxc/lxclock.c
@@ -23,6 +23,7 @@
 #include stdio.h
 #include errno.h
 #include unistd.h
+#include fcntl.h
 #include lxc/utils.h
 #include lxc/log.h
 #include lxc/lxccontainer.h
@@ -111,6 +112,7 @@ out:
 int lxclock(struct lxc_lock *l, int timeout)
 {
int ret = -1, saved_errno = errno;
+   struct flock lk;
 
switch(l-type) {
case LXC_LOCK_ANON_SEM:
@@ -152,7 +154,11 @@ int lxclock(struct lxc_lock *l, int timeout)
goto out;
}
}
-   ret = flock(l-u.f.fd, LOCK_EX);
+   lk.l_type = F_WRLCK;
+   lk.l_whence = SEEK_SET;
+   lk.l_start = 0;
+   lk.l_len = 0;
+   ret = fcntl(l-u.f.fd, F_SETLKW, lk);
process_unlock();
if (ret == -1)
saved_errno = errno;
@@ -167,6 +173,7 @@ out:
 int lxcunlock(struct lxc_lock *l)
 {
int ret = 0, saved_errno = errno;
+   struct flock lk;
 
switch(l-type) {
case LXC_LOCK_ANON_SEM:
@@ -179,7 +186,12 @@ int lxcunlock(struct lxc_lock *l)
case LXC_LOCK_FLOCK:
process_lock();
if (l-u.f.fd != -1) {
-   if ((ret = flock(l-u.f.fd, LOCK_UN))  0)
+   lk.l_type = F_UNLCK;
+   lk.l_whence = SEEK_SET;
+   lk.l_start = 0;
+   lk.l_len = 0;
+   ret = fcntl(l-u.f.fd, F_SETLK, lk);
+   if (ret  0)
saved_errno = errno;
close(l-u.f.fd);
l-u.f.fd = -1;
-- 
1.7.9.5


--
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
___
Lxc-users mailing list
Lxc-users@lists.sourceforge.net

Re: [Lxc-users] [PATCH] lxclock and lxccontainer: switch from flock to fcntl

2013-06-05 Thread zoolook
Hi Serge,

On Wed, Jun 5, 2013 at 1:56 PM, Serge Hallyn serge.hal...@ubuntu.com wrote:
 flock is not supported on nfs.  fcntl is at least supported on newer
 (v3 and above) nfs.



Applied on top of lxc-0.9.0.0~staging~20130605-1508 and it is now
working ok here.

Thank you Serge!

Best Regards,
Norberto



 Signed-off-by: Serge Hallyn serge.hal...@ubuntu.com
 ---
  src/lxc/lxccontainer.c |   22 +-
  src/lxc/lxclock.c  |   16 ++--
  2 files changed, 31 insertions(+), 7 deletions(-)

 diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
 index 2edf749..cf5252b 100644
 --- a/src/lxc/lxccontainer.c
 +++ b/src/lxc/lxccontainer.c
 @@ -23,6 +23,7 @@
  #include sys/types.h
  #include sys/wait.h
  #include errno.h
 +#include fcntl.h
  #include sched.h
  #include config.h
  #include lxc.h
 @@ -39,7 +40,6 @@
  #include lxc/utils.h
  #include lxc/monitor.h
  #include sched.h
 -#include fcntl.h
  #include arpa/inet.h
  #include ifaddrs.h

 @@ -66,6 +66,8 @@ int ongoing_create(struct lxc_container *c)
 int len = strlen(c-config_path) + strlen(c-name) + 10;
 char *path = alloca(len);
 int fd, ret;
 +   struct flock lk;
 +
 ret = snprintf(path, len, %s/%s/partial, c-config_path, c-name);
 if (ret  0 || ret = len) {
 ERROR(Error writing partial pathname);
 @@ -82,8 +84,12 @@ int ongoing_create(struct lxc_container *c)
 process_unlock();
 return 0;
 }
 -   if ((ret = flock(fd, LOCK_EX | LOCK_NB)) == -1 
 -   errno == EWOULDBLOCK) {
 +   lk.l_type = F_WRLCK;
 +   lk.l_whence = SEEK_SET;
 +   lk.l_start = 0;
 +   lk.l_len = 0;
 +   lk.l_pid = -1;
 +   if (fcntl(fd, F_GETLK, lk) == 0  lk.l_pid != -1) {
 // create is still ongoing
 close(fd);
 process_unlock();
 @@ -101,6 +107,8 @@ int create_partial(struct lxc_container *c)
 int len = strlen(c-config_path) + strlen(c-name) + 10;
 char *path = alloca(len);
 int fd, ret;
 +   struct flock lk;
 +
 ret = snprintf(path, len, %s/%s/partial, c-config_path, c-name);
 if (ret  0 || ret = len) {
 ERROR(Error writing partial pathname);
 @@ -108,12 +116,16 @@ int create_partial(struct lxc_container *c)
 }
 if (process_lock())
 return -1;
 -   if ((fd=open(path, O_CREAT | O_EXCL, 0755))  0) {
 +   if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755))  0) {
 SYSERROR(Erorr creating partial file);
 process_unlock();
 return -1;
 }
 -   if (flock(fd, LOCK_EX)  0) {
 +   lk.l_type = F_WRLCK;
 +   lk.l_whence = SEEK_SET;
 +   lk.l_start = 0;
 +   lk.l_len = 0;
 +   if (fcntl(fd, F_SETLKW, lk)  0) {
 SYSERROR(Error locking partial file %s, path);
 close(fd);
 process_unlock();
 diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
 index 4bbe873..d004cc5 100644
 --- a/src/lxc/lxclock.c
 +++ b/src/lxc/lxclock.c
 @@ -23,6 +23,7 @@
  #include stdio.h
  #include errno.h
  #include unistd.h
 +#include fcntl.h
  #include lxc/utils.h
  #include lxc/log.h
  #include lxc/lxccontainer.h
 @@ -111,6 +112,7 @@ out:
  int lxclock(struct lxc_lock *l, int timeout)
  {
 int ret = -1, saved_errno = errno;
 +   struct flock lk;

 switch(l-type) {
 case LXC_LOCK_ANON_SEM:
 @@ -152,7 +154,11 @@ int lxclock(struct lxc_lock *l, int timeout)
 goto out;
 }
 }
 -   ret = flock(l-u.f.fd, LOCK_EX);
 +   lk.l_type = F_WRLCK;
 +   lk.l_whence = SEEK_SET;
 +   lk.l_start = 0;
 +   lk.l_len = 0;
 +   ret = fcntl(l-u.f.fd, F_SETLKW, lk);
 process_unlock();
 if (ret == -1)
 saved_errno = errno;
 @@ -167,6 +173,7 @@ out:
  int lxcunlock(struct lxc_lock *l)
  {
 int ret = 0, saved_errno = errno;
 +   struct flock lk;

 switch(l-type) {
 case LXC_LOCK_ANON_SEM:
 @@ -179,7 +186,12 @@ int lxcunlock(struct lxc_lock *l)
 case LXC_LOCK_FLOCK:
 process_lock();
 if (l-u.f.fd != -1) {
 -   if ((ret = flock(l-u.f.fd, LOCK_UN))  0)
 +   lk.l_type = F_UNLCK;
 +   lk.l_whence = SEEK_SET;
 +   lk.l_start = 0;
 +   lk.l_len = 0;
 +   ret = fcntl(l-u.f.fd, F_SETLK, lk);
 +   if (ret  0)
 saved_errno = errno;
 close(l-u.f.fd);
 l-u.f.fd = -1;
 --
 1.7.9.5


--
How ServiceNow helps IT people transform IT