Commit 884866b3 by Daniel Lezcano Committed by Daniel Lezcano

Remove the usage of a lock file

The lock is no longer needed as the mutual exclusion and 'is running' check is done via the af_unix command socket. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 641c20a6
......@@ -120,7 +120,7 @@ static int copy_config_file(const char *name, const char *file)
int lxc_create(const char *name, const char *confile)
{
int lock, err = -1;
int err = -1;
if (create_lxc_directory(name))
return err;
......@@ -128,26 +128,21 @@ int lxc_create(const char *name, const char *confile)
if (!confile)
return 0;
lock = lxc_get_lock(name);
if (lock < 0)
goto err;
if (copy_config_file(name, confile)) {
ERROR("failed to copy the configuration file");
goto err_state;
goto err;
}
err = 0;
out:
lxc_put_lock(lock);
return err;
err_state:
err:
lxc_unconfigure(name);
if (lxc_rmstate(name))
ERROR("failed to remove state file for %s", name);
err:
if (remove_lxc_directory(name))
ERROR("failed to cleanup lxc directory for %s", name);
goto out;
......
......@@ -71,19 +71,15 @@ static int remove_config_file(const char *name)
int lxc_destroy(const char *name)
{
int lock, ret = -1;
int ret = -1;
char path[MAXPATHLEN];
lock = lxc_get_lock(name);
if (lock < 0)
return ret;
if (remove_config_file(name))
WARN("failed to remove the configuration file");
if (lxc_rmstate(name)) {
ERROR("failed to remove state file for %s", name);
goto out_lock;
return -1;
}
#warning keep access to LXCPATH/<name> to destroy old created container
......@@ -95,17 +91,13 @@ int lxc_destroy(const char *name)
if (lxc_unconfigure(name)) {
ERROR("failed to cleanup %s", name);
goto out_lock;
return -1;
}
if (remove_lxc_directory(name)) {
SYSERROR("failed to remove '%s'", name);
goto out_lock;
return -1;
}
ret = 0;
out_lock:
lxc_put_lock(lock);
return ret;
return 0;
}
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/param.h>
#include "error.h"
#include "config.h"
#include <lxc/lxc.h>
lxc_log_define(lxc_lock, lxc);
static int __lxc_get_lock(const char *name)
{
char lock[MAXPATHLEN];
int fd, ret;
snprintf(lock, MAXPATHLEN, LXCPATH "/%s", name);
/* need to check access because of cap_dac_override */
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)
return -errno;
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (flock(fd, LOCK_EX|LOCK_NB)) {
ret = -errno;
close(fd);
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;
return ret;
out_err:
switch (-ret) {
case EWOULDBLOCK:
ERROR("container '%s' is busy", name);
break;
case ENOENT:
ERROR("container '%s' is not found", name);
break;
case EACCES:
ERROR("not enough privilege to use container '%s'", name);
break;
default:
ERROR("container '%s' failed to lock : %s",
name, strerror(-ret));
}
return -1;
}
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);
close(lock);
}
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _lock_h
#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
......@@ -37,7 +37,6 @@ extern "C" {
#include <lxc/list.h>
#include <lxc/log.h>
#include <lxc/conf.h>
#include <lxc/lock.h>
#include <lxc/namespace.h>
#include <lxc/utils.h>
#include <lxc/error.h>
......
......@@ -246,14 +246,10 @@ struct lxc_handler *lxc_init(const char *name)
memset(handler, 0, sizeof(*handler));
handler->lock = lxc_get_lock(name);
if (handler->lock < 0)
goto out_free;
/* Begin the set the state to STARTING*/
if (set_state(name, handler, STARTING)) {
ERROR("failed to set state '%s'", lxc_state2str(STARTING));
goto out_put_lock;
goto out_free;
}
if (lxc_conf_init(&handler->conf)) {
......@@ -302,8 +298,6 @@ out_delete_tty:
lxc_delete_tty(&handler->conf.tty_info);
out_aborting:
set_state(name, handler, ABORTING);
out_put_lock:
lxc_put_lock(handler->lock);
out_free:
free(handler);
handler = NULL;
......@@ -322,7 +316,6 @@ void lxc_fini(const char *name, struct lxc_handler *handler)
if (handler) {
remove_init_pid(name, handler->pid);
lxc_delete_tty(&handler->conf.tty_info);
lxc_put_lock(handler->lock);
free(handler);
}
......
......@@ -27,7 +27,6 @@ struct lxc_handler {
lxc_state_t state;
int sigfd;
int lock;
char nsgroup[MAXPATHLEN];
sigset_t oldmask;
struct lxc_conf conf;
......
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