mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-06 05:17:59 +00:00
create a new lxc_check_lock function
added in src/lock.c to replace call to lxc_get_lock and lxc_put_lock, when only need to check if container is active or not. Signed-off-by: Michel Normand <normand@fr.ibm.com> Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
parent
d737c07495
commit
305dbab7e6
@ -46,30 +46,25 @@ lxc_log_define(lxc_checkpoint, lxc);
|
||||
|
||||
#define MAXPIDLEN 20
|
||||
|
||||
int lxc_checkpoint(const char *name, const char *statefile,
|
||||
int lxc_checkpoint(const char *name, const char *statefile,
|
||||
unsigned long flags)
|
||||
{
|
||||
char init[MAXPATHLEN];
|
||||
char val[MAXPIDLEN];
|
||||
int fd, lock, ret = -1;
|
||||
int fd, ret = -1;
|
||||
size_t pid;
|
||||
|
||||
lock = lxc_get_lock(name);
|
||||
if (lock >= 0) {
|
||||
lxc_put_lock(lock);
|
||||
return -LXC_ERROR_ESRCH;
|
||||
if (lxc_check_lock(name) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (lock < 0 && lock != -LXC_ERROR_EBUSY)
|
||||
return lock;
|
||||
|
||||
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
|
||||
fd = open(init, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
SYSERROR("failed to open init file for %s", name);
|
||||
goto out_close;
|
||||
}
|
||||
|
||||
|
||||
if (read(fd, val, sizeof(val)) < 0) {
|
||||
SYSERROR("failed to read %s", init);
|
||||
goto out_close;
|
||||
|
@ -33,7 +33,9 @@
|
||||
#include "error.h"
|
||||
#include <lxc/lxc.h>
|
||||
|
||||
int lxc_get_lock(const char *name)
|
||||
lxc_log_define(lxc_lock, lxc);
|
||||
|
||||
static int __lxc_get_lock(const char *name)
|
||||
{
|
||||
char lock[MAXPATHLEN];
|
||||
int fd, ret;
|
||||
@ -41,31 +43,37 @@ int lxc_get_lock(const char *name)
|
||||
snprintf(lock, MAXPATHLEN, LXCPATH "/%s", name);
|
||||
|
||||
/* need to check access because of cap_dac_override */
|
||||
if (access(lock, R_OK |W_OK | X_OK)) {
|
||||
ret = errno;
|
||||
goto out_err;
|
||||
}
|
||||
if (access(lock, R_OK |W_OK | X_OK))
|
||||
return -errno;
|
||||
|
||||
fd = open(lock, O_RDONLY|O_DIRECTORY, S_IRUSR|S_IWUSR);
|
||||
if (fd < 0) {
|
||||
ret = errno;
|
||||
goto out_err;
|
||||
}
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
if (flock(fd, LOCK_EX|LOCK_NB)) {
|
||||
ret = errno;
|
||||
ret = -errno;
|
||||
close(fd);
|
||||
goto out_err;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = fd;
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int lxc_get_lock(const char *name)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = __lxc_get_lock(name);
|
||||
if (ret < 0)
|
||||
goto out_err;
|
||||
out:
|
||||
return ret;
|
||||
out_err:
|
||||
switch (ret) {
|
||||
switch (-ret) {
|
||||
case EWOULDBLOCK:
|
||||
ret = -LXC_ERROR_EBUSY;
|
||||
goto out;
|
||||
@ -81,6 +89,23 @@ out_err:
|
||||
}
|
||||
}
|
||||
|
||||
int lxc_check_lock(const char *name)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = __lxc_get_lock(name);
|
||||
if (ret >= 0) {
|
||||
ERROR("container '%s' is not active", name);
|
||||
lxc_put_lock(ret);
|
||||
return -1;
|
||||
}
|
||||
if (ret != -EWOULDBLOCK) {
|
||||
ERROR("container '%s' : %s", name, strerror(-ret));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lxc_put_lock(int lock)
|
||||
{
|
||||
flock(lock, LOCK_UN);
|
||||
|
@ -24,7 +24,7 @@
|
||||
#define _lock_h
|
||||
|
||||
extern int lxc_get_lock(const char *name);
|
||||
|
||||
extern int lxc_check_lock(const char *name);
|
||||
extern void lxc_put_lock(int lock);
|
||||
|
||||
#endif
|
||||
|
@ -65,10 +65,8 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
|
||||
err = lxc_stop(name);
|
||||
if (err) {
|
||||
fprintf(stderr, "%s\n", lxc_strerror(err));
|
||||
if (err)
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -42,17 +42,11 @@ int lxc_stop(const char *name)
|
||||
{
|
||||
char init[MAXPATHLEN];
|
||||
char val[MAXPIDLEN];
|
||||
int fd, lock, ret = -LXC_ERROR_INTERNAL;
|
||||
int fd, ret = -LXC_ERROR_INTERNAL;
|
||||
size_t pid;
|
||||
|
||||
lock = lxc_get_lock(name);
|
||||
if (lock >= 0) {
|
||||
lxc_put_lock(lock);
|
||||
return -LXC_ERROR_ESRCH;
|
||||
}
|
||||
|
||||
if (lock < 0 && lock != -LXC_ERROR_EBUSY)
|
||||
return lock;
|
||||
if (lxc_check_lock(name) < 0)
|
||||
return ret;
|
||||
|
||||
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
|
||||
fd = open(init, O_RDONLY);
|
||||
@ -60,7 +54,7 @@ int lxc_stop(const char *name)
|
||||
SYSERROR("failed to open init file for %s", name);
|
||||
goto out_close;
|
||||
}
|
||||
|
||||
|
||||
if (read(fd, val, sizeof(val)) < 0) {
|
||||
SYSERROR("failed to read %s", init);
|
||||
goto out_close;
|
||||
|
Loading…
Reference in New Issue
Block a user