From: Serge Hallyn Date: Wed, 5 Jun 2013 16:56:30 +0000 (-0500) Subject: lxclock and lxccontainer: switch from flock to fcntl X-Git-Tag: lxc-1.0.0.alpha1~1^2~163 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93dc5327aa0c2b13d619b8bedf893eea983d4d68;p=thirdparty%2Flxc.git lxclock and lxccontainer: switch from flock to fcntl flock is not supported on nfs. fcntl is at least supported on newer (v3 and above) nfs. Signed-off-by: Serge Hallyn Tested-by: zoolook --- diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 2edf74918..cf5252b08 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "config.h" #include "lxc.h" @@ -39,7 +40,6 @@ #include #include #include -#include #include #include @@ -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 4bbe873be..d004cc55f 100644 --- a/src/lxc/lxclock.c +++ b/src/lxc/lxclock.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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;