#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
+#include <fcntl.h>
#include <sched.h>
#include "config.h"
#include "lxc.h"
#include <lxc/utils.h>
#include <lxc/monitor.h>
#include <sched.h>
-#include <fcntl.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
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");
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();
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");
}
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();
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
+#include <fcntl.h>
#include <lxc/utils.h>
#include <lxc/log.h>
#include <lxc/lxccontainer.h>
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:
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;
int lxcunlock(struct lxc_lock *l)
{
int ret = 0, saved_errno = errno;
+ struct flock lk;
switch(l->type) {
case LXC_LOCK_ANON_SEM:
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;