Skip to content

Commit

Permalink
lxccontainer: use thread-safe *_OFD_* locks
Browse files Browse the repository at this point in the history
If they aren't available fallback to BSD flock()s.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Aug 23, 2018
1 parent 1615599 commit 82cdb21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
33 changes: 19 additions & 14 deletions src/lxc/lxccontainer.c
Expand Up @@ -19,6 +19,7 @@
*/

#define _GNU_SOURCE
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
Expand All @@ -29,22 +30,22 @@
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/sysmacros.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#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"
Expand All @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/lxc/lxclock.c
Expand Up @@ -19,13 +19,14 @@
*/

#define _GNU_SOURCE
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <unistd.h>

#include <lxc/lxccontainer.h>

Expand Down

0 comments on commit 82cdb21

Please sign in to comment.