Commit 93dc5327 by Serge Hallyn

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: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Tested-by: 's avatarzoolook <nbensa+lxcusers@gmail.com>
parent 1af60b51
......@@ -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();
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment