From: Christian Brauner Date: Sun, 29 Apr 2018 11:39:28 +0000 (+0200) Subject: lxccontainer: use thread-safe *_OFD_* locks X-Git-Tag: lxc-2.0.10~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82cdb21218ac9351965dcc17527efcd9efdd2346;p=thirdparty%2Flxc.git lxccontainer: use thread-safe *_OFD_* locks If they aren't available fallback to BSD flock()s. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index fcc8ca65e..c0fb95963 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -19,6 +19,7 @@ */ #define _GNU_SOURCE +#include #include #include #include @@ -29,22 +30,22 @@ #include #include #include -#include -#include -#include +#include #include #include #include +#include #include #include +#include #include "af_unix.h" #include "attach.h" #include "cgroup.h" -#include "conf.h" -#include "config.h" #include "commands.h" #include "commands_utils.h" +#include "conf.h" +#include "config.h" #include "confile.h" #include "console.h" #include "criu.h" @@ -59,9 +60,9 @@ #include "start.h" #include "state.h" #include "storage.h" -#include "storage_utils.h" #include "storage/btrfs.h" #include "storage/overlay.h" +#include "storage_utils.h" #include "sync.h" #include "utils.h" #include "version.h" @@ -140,7 +141,7 @@ static int ongoing_create(struct lxc_container *c) int fd, ret; size_t len; char *path; - struct flock lk; + struct flock lk = {0}; len = strlen(c->config_path) + strlen(c->name) + 10; path = alloca(len); @@ -157,11 +158,11 @@ static int ongoing_create(struct lxc_container *c) lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; - lk.l_start = 0; - lk.l_len = 0; lk.l_pid = -1; - ret = fcntl(fd, F_GETLK, &lk); + ret = fcntl(fd, F_OFD_GETLK, &lk); + if (ret < 0 && errno == EINVAL) + ret = flock(fd, LOCK_EX | LOCK_NB); close(fd); if (ret == 0 && lk.l_pid != -1) { /* create is still ongoing */ @@ -176,7 +177,7 @@ static int create_partial(struct lxc_container *c) int fd, ret; size_t len; char *path; - struct flock lk; + struct flock lk = {0}; /* $lxcpath + '/' + $name + '/partial' + \0 */ len = strlen(c->config_path) + strlen(c->name) + 10; @@ -191,11 +192,15 @@ static int create_partial(struct lxc_container *c) lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; - lk.l_start = 0; - lk.l_len = 0; - ret = fcntl(fd, F_SETLKW, &lk); + ret = fcntl(fd, F_OFD_SETLKW, &lk); if (ret < 0) { + if (errno == EINVAL) { + ret = flock(fd, LOCK_EX); + if (ret == 0) + return fd; + } + SYSERROR("Failed to lock partial file %s", path); close(fd); return -1; diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c index 31dbd1c13..08c96e0b1 100644 --- a/src/lxc/lxclock.c +++ b/src/lxc/lxclock.c @@ -19,13 +19,14 @@ */ #define _GNU_SOURCE -#include -#include #include -#include #include -#include +#include #include +#include +#include +#include +#include #include