lxclock: cleanup lxclock()

parent 82c1fd64
...@@ -170,50 +170,36 @@ struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name) ...@@ -170,50 +170,36 @@ struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name)
int lxclock(struct lxc_lock *l, int timeout) int lxclock(struct lxc_lock *l, int timeout)
{ {
int ret = -1;
struct flock lk; struct flock lk;
int ret = -1, saved_errno = errno;
switch(l->type) { switch (l->type) {
case LXC_LOCK_ANON_SEM: case LXC_LOCK_ANON_SEM:
if (!timeout) { if (!timeout) {
ret = sem_wait(l->u.sem); ret = sem_wait(l->u.sem);
if (ret < 0)
saved_errno = errno;
} else { } else {
struct timespec ts; struct timespec ts;
ret = clock_gettime(CLOCK_REALTIME, &ts); ret = clock_gettime(CLOCK_REALTIME, &ts);
if (ret < 0) { if (ret < 0)
ret = -2; return -2;
goto on_error;
}
ts.tv_sec += timeout; ts.tv_sec += timeout;
ret = sem_timedwait(l->u.sem, &ts); ret = sem_timedwait(l->u.sem, &ts);
if (ret < 0)
saved_errno = errno;
} }
break; break;
case LXC_LOCK_FLOCK: case LXC_LOCK_FLOCK:
ret = -2; if (timeout)
if (timeout) { return log_error(-2, "Timeouts are not supported with file locks");
ERROR("Timeouts are not supported with file locks");
goto on_error;
}
if (!l->u.f.fname) { if (!l->u.f.fname)
ERROR("No filename set for file lock"); return log_error(-2, "No filename set for file lock");
goto on_error;
}
if (l->u.f.fd == -1) { if (l->u.f.fd < 0) {
l->u.f.fd = open(l->u.f.fname, O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, S_IWUSR | S_IRUSR); l->u.f.fd = open(l->u.f.fname, O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, S_IWUSR | S_IRUSR);
if (l->u.f.fd == -1) { if (l->u.f.fd < 0)
SYSERROR("Failed to open \"%s\"", l->u.f.fname); return log_error_errno(-2, errno, "Failed to open \"%s\"", l->u.f.fname);
saved_errno = errno;
goto on_error;
}
} }
memset(&lk, 0, sizeof(struct flock)); memset(&lk, 0, sizeof(struct flock));
...@@ -222,17 +208,13 @@ int lxclock(struct lxc_lock *l, int timeout) ...@@ -222,17 +208,13 @@ int lxclock(struct lxc_lock *l, int timeout)
lk.l_whence = SEEK_SET; lk.l_whence = SEEK_SET;
ret = fcntl(l->u.f.fd, F_OFD_SETLKW, &lk); ret = fcntl(l->u.f.fd, F_OFD_SETLKW, &lk);
if (ret < 0) { if (ret < 0 && errno == EINVAL)
if (errno == EINVAL) ret = flock(l->u.f.fd, LOCK_EX);
ret = flock(l->u.f.fd, LOCK_EX);
saved_errno = errno;
}
break; break;
default:
return ret_set_errno(-1, EINVAL);
} }
on_error:
errno = saved_errno;
return ret; return ret;
} }
...@@ -252,7 +234,7 @@ int lxcunlock(struct lxc_lock *l) ...@@ -252,7 +234,7 @@ int lxcunlock(struct lxc_lock *l)
break; break;
case LXC_LOCK_FLOCK: case LXC_LOCK_FLOCK:
if (l->u.f.fd != -1) { if (l->u.f.fd >= 0) {
memset(&lk, 0, sizeof(struct flock)); memset(&lk, 0, sizeof(struct flock));
lk.l_type = F_UNLCK; lk.l_type = F_UNLCK;
...@@ -300,7 +282,7 @@ void lxc_putlock(struct lxc_lock *l) ...@@ -300,7 +282,7 @@ void lxc_putlock(struct lxc_lock *l)
break; break;
case LXC_LOCK_FLOCK: case LXC_LOCK_FLOCK:
if (l->u.f.fd != -1) { if (l->u.f.fd >= 0) {
close(l->u.f.fd); close(l->u.f.fd);
l->u.f.fd = -1; 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