lxccontainer.c revision 4ba0d9af63fbf7e9acfa068a1fe36b3d287b9c6b
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* liblxcapi
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright © 2012 Canonical Ltd.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This program is free software; you can redistribute it and/or modify
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it under the terms of the GNU General Public License version 2, as
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * published by the Free Software Foundation.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This program is distributed in the hope that it will be useful,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * but WITHOUT ANY WARRANTY; without even the implied warranty of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * GNU General Public License for more details.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You should have received a copy of the GNU General Public License along
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * with this program; if not, write to the Free Software Foundation, Inc.,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define _GNU_SOURCE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdarg.h>
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana#include <pthread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/wait.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sched.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "config.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "lxc.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "state.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "lxccontainer.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "conf.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "confile.h"
ad9a7bd3532cf0ef931ded51a5ffe5d0496aad88Richard Lowe#include "console.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "cgroup.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "commands.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "version.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "log.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "bdev.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "utils.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "attach.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <lxc/utils.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <lxc/monitor.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sched.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <arpa/inet.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if HAVE_IFADDRS_H
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <ifaddrs.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <../include/ifaddrs.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelxc_log_define(lxc_container, lxc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool file_exists(char *f)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat statbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return stat(f, &statbuf) == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A few functions to help detect when a container creation failed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If a container creation was killed partway through, then trying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to actually start that container could harm the host. We detect
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * this by creating a 'partial' file under the container directory,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and keeping an advisory lock. When container creation completes,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we remove that file. When we load or try to start a container, if
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we find that file, without a flock, we remove the container.
ad9a7bd3532cf0ef931ded51a5ffe5d0496aad88Richard Lowe */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint ongoing_create(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *path = alloca(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct flock lk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error writing partial pathname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!file_exists(path))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (process_lock())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open(path, O_RDWR)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // give benefit of the doubt
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Error opening partial file");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_type = F_WRLCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_whence = SEEK_SET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_start = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_pid = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // create is still ongoing
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // create completed but partial is still there.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint create_partial(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // $lxcpath + '/' + $name + '/partial' + \0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *path = alloca(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct flock lk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error writing partial pathname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (process_lock())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Erorr creating partial file");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_type = F_WRLCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_whence = SEEK_SET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_start = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lk.l_len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fcntl(fd, F_SETLKW, &lk) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Error locking partial file %s", path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return fd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid remove_partial(struct lxc_container *c, int fd)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // $lxcpath + '/' + $name + '/partial' + \0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *path = alloca(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error writing partial pathname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (process_lock())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (unlink(path) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Error unlink partial file %s", path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* LOCKING
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 2. container_disk_lock(c) protects the on-disk container data - in particular the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * container configuration file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The container_disk_lock also takes the container_mem_lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * NOTHING mutexes two independent programs with their own struct
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lxc_container for the same c->name, between API calls. For instance,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->config_read(); c->start(); Between those calls, data on disk
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * could change (which shouldn't bother the caller unless for instance
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the rootfs get moved). c->config_read(); update; c->config_write();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Two such updaters could race. The callers should therefore check their
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * results. Trying to prevent that would necessarily expose us to deadlocks
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * due to hung callers. So I prefer to keep the locks only within our own
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * functions, not across functions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If you're going to clone while holding a lxccontainer, increment
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->numthreads (under privlock) before forking. When deleting,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * decrement numthreads under privlock, then if it hits 0 you can delete.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Do not ever use a lxccontainer whose numthreads you did not bump.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void lxc_container_free(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->configfile) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->configfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->configfile = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->error_string) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->error_string);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->error_string = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->slock) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_putlock(c->slock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->slock = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->privlock) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_putlock(c->privlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->privlock = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->name) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->name = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_conf_free(c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->config_path) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Consider the following case:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefreer | racing get()er
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte==================================================================
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelxc_container_put() | lxc_container_get()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxclock(c->privlock) | c->numthreads < 1? (no)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxcunlock() | \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxc_container_free() | \ lxclock() returns
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte | \ c->numthreads < 1 -> return 0
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana\ \ (free stuff) |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ \ sem_destroy(privlock) |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When the get()er checks numthreads the first time, one of the following
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is true:
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * 1. freer has set numthreads = 0. get() returns 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 2. freer is between lxclock and setting numthreads to 0. get()er will
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * sem_wait on privlock, get lxclock after freer() drops it, then see
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * numthreads is 0 and exit without touching lxclock again..
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * 3. freer has not yet locked privlock. If get()er runs first, then put()er
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will see --numthreads = 1 and not call lxc_container_free().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint lxc_container_get(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // if someone else has already started freeing the container, don't
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // try to take the lock, which may be invalid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->numthreads < 1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->numthreads < 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // bail without trying to unlock, bc the privlock is now probably
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // in freed memory
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->numthreads++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint lxc_container_put(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (--c->numthreads < 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_container_free(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_is_defined(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat statbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool ret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int statret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->configfile)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte statret = stat(c->configfile, &statbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (statret != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_state(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_state_t s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = lxc_getstate(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_state2str(s);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool is_stopped(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_state_t s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = lxc_getstate(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (s == STOPPED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_is_running(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = lxcapi_state(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!s || strcmp(s, "STOPPED") == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_freeze(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_freeze(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_unfreeze(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_unfreeze(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ttyfd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ttyfd = lxc_console_getfd(c, ttynum, masterfd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ttyfd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int stdoutfd, int stderrfd, int escape)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic pid_t lxcapi_init_pid(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_cmd_get_init_pid(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool load_config_locked(struct lxc_container *c, const char *fname)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = lxc_conf_init();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf && !lxc_config_read(fname, c->lxc_conf))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool ret = false, need_disklock = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int lret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *fname;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fname = c->configfile;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fname = alt_file;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!fname)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we're reading something other than the container's config,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * we only need to lock the in-memory container. If loading the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * container's config file, take the disk lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(fname, c->configfile) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte need_disklock = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (need_disklock)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lret = container_disk_lock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lret = container_mem_lock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lret)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = load_config_locked(c, fname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (need_disklock)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_disk_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void lxcapi_want_daemonize(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error getting mem lock");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->daemonize = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_wait(c->name, state, timeout, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool wait_on_daemonized_start(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we'll probably want to make this timeout configurable? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int timeout = 5, ret, status;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * our child is going to fork again, then exit. reap the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * child
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana ret = wait(&status);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana DEBUG("failed waiting for first dual-fork child");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxcapi_wait(c, "RUNNING", timeout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * I can't decide if it'd be more convenient for callers if we accept '...',
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or a null-terminated array (i.e. execl vs execv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_conf *conf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int daemonize = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *default_args[] = {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/sbin/init",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte '\0',
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte };
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container exists */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container has been setup */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((ret = ongoing_create(c)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error checking for incomplete creation");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret == 2) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: %s creation was not completed", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->destroy(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (ret == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: creation of %s is ongoing", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* is this app meant to be run through lxcinit, as in lxc-execute? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (useinit && !argv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte conf = c->lxc_conf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonize = c->daemonize;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (useinit) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0 ? true : false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!argv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte argv = default_args;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * say, I'm not sure - what locks do we want here? Any?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Is liblxc's locking enough here to protect the on disk
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * container? We don't want to exclude things like lxc_info
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * while container is running...
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (daemonize) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!lxc_container_get(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_monitord_spawn(c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (process_lock())
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_container_put(c);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = wait_on_daemonized_start(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana process_unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* second fork to be reparented by init */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Error doing dual-fork");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (chdir("/")) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Error chdir()ing to /.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana close(0);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana close(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/zero", O_RDONLY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/null", O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/null", O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte setsid();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortereboot:
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana conf->reboot = 0;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana ret = lxc_start(c->name, argv, conf, c->config_path);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (conf->reboot) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte INFO("container requested reboot");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte conf->reboot = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto reboot;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (daemonize) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_container_put(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit (ret == 0 ? true : false);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ret == 0 ? true : false);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * note there MUST be an ending NULL
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **inargs = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container exists */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, useinit);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte inargs = lxc_va_arg_list_to_argv(ap, 0, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!inargs) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Memory allocation error.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* pass NULL if no arguments were supplied */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (inargs) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *arg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (arg = *inargs; arg; arg++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(arg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(inargs);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return bret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_stop(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_cmd_stop(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create the standard expected container dir
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool create_container_dir(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(c->config_path) + strlen(c->name) + 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s = malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(s);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = mkdir(s, 0755);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (errno == EEXIST)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to create container path for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(s);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_get_config_path(struct lxc_container *c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it returns a mounted bdev on success, NULL on error.
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev_specs *specs)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *dest;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *lxcpath = lxcapi_get_config_path(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev *bdev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* lxcpath/lxcname/rootfs */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana len = strlen(c->name) + strlen(lxcpath) + 9;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dest = alloca(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_create(dest, type, c->name, specs);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!bdev) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Failed to create backing store type %s\n", type);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return bdev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Given the '-t' template option to lxc-create, figure out what to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do. If the template is a full executable path, use that. If it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is something like 'sshd', then return $templatepath/lxc-sshd. If
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * no template was passed in, return NULL (this is ok).
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * On error return (char *) -1.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *get_template_path(const char *t)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret, len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *tpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!t)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (t[0] == '/' && access(t, X_OK) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tpath = strdup(t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!tpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return tpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tpath = malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!tpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (access(tpath, X_OK) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("bad template: %s\n", t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return tpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *lxcbasename(char *path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *p = path + strlen(path) - 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*p != '/' && p > path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!tpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to fork task for container creation template\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid == 0) { // child
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *patharg, *namearg, *rootfsarg, *src;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev *bdev = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana int ret, len, nargs = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **newargv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (quiet) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/zero", O_RDONLY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/null", O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte open("/dev/null", O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (unshare(CLONE_NEWNS) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error unsharing mounts");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte src = c->lxc_conf->rootfs.path;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * for an overlayfs create, what the user wants is the template to fill
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in what will become the readonly lower layer. So don't mount for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the template
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(src, "overlayfs:", 10) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte src = overlayfs_getlower(src+10);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_init(src, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!bdev) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error opening rootfs");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (bdev->ops->mount(bdev) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error mounting rootfs");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create our new array, pre-pend the template name and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * base args
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (argv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (nargs = 0; argv[nargs]; nargs++) ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nargs += 4; // template, path, rootfs and name args
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv = malloc(nargs * sizeof(*newargv));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!newargv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[0] = lxcbasename(tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte patharg = malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!patharg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[1] = patharg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen("--name=") + strlen(c->name) + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte namearg = malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!namearg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(namearg, len, "--name=%s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[2] = namearg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rootfsarg = malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!rootfsarg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[3] = rootfsarg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add passed-in args */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (argv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 4; i < nargs; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[i] = argv[i-4];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add trailing NULL */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nargs++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv = realloc(newargv, nargs * sizeof(*newargv));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!newargv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv[nargs - 1] = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* execute */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte execv(tpath, newargv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to execute template %s", tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wait_for_pid(pid) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("container creation template for %s failed\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortebool prepend_lxc_header(char *path, const char *t, char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t flen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *contents, *tpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *f;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if HAVE_LIBGNUTLS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned char md_value[SHA_DIGEST_LENGTH];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool have_tpath = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((f = fopen(path, "r")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Opening old config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fseek(f, 0, SEEK_END) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Seeking to end of old config file");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((flen = ftell(f)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("telling size of old config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fseek(f, 0, SEEK_SET) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("rewinding old config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((contents = malloc(flen + 1)) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("out of memory");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fread(contents, 1, flen, f) != flen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Reading old config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte contents[flen] = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fclose(f) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("closing old config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tpath = get_template_path(t)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("bad template: %s\n", t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if HAVE_LIBGNUTLS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tpath) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte have_tpath = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = sha1sum_file(tpath, md_value);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (ret < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error getting sha1sum of %s", tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((f = fopen(path, "w")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("reopening config for writing");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Template used to create this container: %s\n", t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (argv) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Parameters passed to the template:");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*argv) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, " %s", *argv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte argv++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if HAVE_LIBGNUTLS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (have_tpath) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Template script checksum (SHA-1): ");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<SHA_DIGEST_LENGTH; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "%02x", md_value[i]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fwrite(contents, 1, flen, f) != flen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Writing original contents");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(contents);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fclose(f) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("Closing config file after write");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_destroy(struct lxc_container *c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lxcapi_create:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create a container with the given parameters.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @c: container to be created. It has the lxcpath, name, and a starting
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * configuration already set
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @t: the template to execute to instantiate the root filesystem and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * adjust the configuration.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @bdevtype: backing store type to use. If NULL, dir will be used.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @specs: additional parameters for the backing store, i.e. LVM vg to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * use.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @argv: the arguments to pass to the template, terminated by NULL. If no
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * arguments, you can just pass NULL.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_create(struct lxc_container *c, const char *t,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, struct bdev_specs *specs, int flags,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *tpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int partial_fd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tpath = get_template_path(t)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("bad template: %s\n", t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->save_config(c, NULL)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("failed to save starting configuration for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container is already created if we have a config and rootfs.path is accessible */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Mark that this container is being created */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((partial_fd = create_partial(c)) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana /* no need to get disk lock bc we have the partial locked */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Create the backing store
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Note we can't do this in the same task as we use to execute the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * template because of the way zfs works.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * After you 'zfs create', zfs mounts the fs only in the initial
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * namespace.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to fork task for container creation template\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out_unlock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid == 0) { // child
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev *bdev = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error creating backing store type %s for %s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdevtype ? bdevtype : "(none)", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* save config file again to store the new rootfs location */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->save_config(c, NULL)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("failed to save starting configuration for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // parent task won't see bdev in config so we delete it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev->ops->umount(bdev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev->ops->destroy(bdev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wait_for_pid(pid) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* reload config to get the rootfs */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_conf_free(c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!load_config_locked(c, c->configfile))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out_unlock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // now clear out the lxc_conf we have, reload from the created
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // container
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_conf_free(c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!prepend_lxc_header(c->configfile, tpath, argv)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error prepending header to configuration file");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out_unlock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bret = load_config_locked(c, c->configfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout_unlock:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (partial_fd >= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remove_partial(c, partial_fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!bret && c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxcapi_destroy(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return bret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_reboot(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->is_running(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = c->init_pid(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid <= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (kill(pid, SIGINT) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_shutdown(struct lxc_container *c, int timeout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool retv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!timeout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte timeout = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->is_running(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = c->init_pid(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid <= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kill(pid, SIGPWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retv = c->wait(c, "STOPPED", timeout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!retv && timeout > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->stop(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retv = c->wait(c, "STOPPED", 0); // 0 means don't wait
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return retv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_createl(struct lxc_container *c, const char *t,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, struct bdev_specs *specs, int flags, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **args = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * since we're going to wait for create to finish, I don't think we
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * need to get a copy of the arguments.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, flags);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte args = lxc_va_arg_list_to_argv(ap, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!args) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Memory allocation error.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bret = c->create(c, t, bdevtype, specs, flags, args);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(args);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return bret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_clear_config_item(c->lxc_conf, key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int count = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char addressOutputBuffer[INET6_ADDRSTRLEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *tempAddrPtr = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **addresses = NULL, **temp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *address = NULL;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana char new_netns_path[MAXPATHLEN];
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana int old_netns = -1, new_netns = -1, ret = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->is_running(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Save reference to old netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte old_netns = open("/proc/self/ns/net", O_RDONLY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (old_netns < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to open /proc/self/ns/net");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Switch to new netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= MAXPATHLEN)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_netns = open(new_netns_path, O_RDONLY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_netns < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to open %s", new_netns_path);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setns(new_netns, CLONE_NEWNET)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to setns");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Grab the list of interfaces */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (getifaddrs(&interfaceArray)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to get interfaces list");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Iterate through the interfaces */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if(tempIfAddr->ifa_addr->sa_family == AF_INET) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (family && strcmp(family, "inet"))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (family && strcmp(family, "inet6"))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (interface && strcmp(interface, tempIfAddr->ifa_name))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tempAddrPtr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addressOutputBuffer,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof(addressOutputBuffer));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!address)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte count += 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte temp = realloc(addresses, count * sizeof(*addresses));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!temp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte count--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addresses = temp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addresses[count - 1] = strdup(address);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if(interfaceArray)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte freeifaddrs(interfaceArray);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Switch back to original netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (old_netns >= 0 && setns(old_netns, CLONE_NEWNET))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to setns");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_netns >= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(new_netns);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (old_netns >= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(old_netns);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Append NULL to the array */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (count) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte count++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte temp = realloc(addresses, count * sizeof(*addresses));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!temp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < count-1; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(addresses[i]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(addresses);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addresses = temp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addresses[count - 1] = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return addresses;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_listconfigs(retv, inlen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This is an intelligent result to show which keys are valid given
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the type of nic it is
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(key, "lxc.network.", 12) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *fout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool ret = false, need_disklock = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int lret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte alt_file = c->configfile;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false; // should we write to stdout if no file is specified?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // If we haven't yet loaded a config, load the stock config
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->load_config(c, LXC_DEFAULT_CONFIG)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!create_container_dir(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we're writing to the container's config file, take the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * disk lock. Otherwise just take the memlock to protect the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * struct lxc_container while we're traversing it.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(c->configfile, alt_file) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte need_disklock = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (need_disklock)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lret = container_disk_lock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lret = container_mem_lock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lret)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fout = fopen(alt_file, "w");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!fout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte write_config(fout, c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(fout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (need_disklock)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_disk_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte// do we want the api to support --force, or leave that to the caller?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_destroy(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana struct bdev *r = NULL;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana bool ret = false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (!c || !lxcapi_is_defined(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_disk_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!is_stopped(c)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we should queue some sort of error - in c->error_string?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("container %s is not stopped", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana r = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (r) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (r->ops->destroy(r) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error destroying rootfs for %s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *p1 = lxcapi_get_config_path(c);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana char *path = alloca(strlen(p1) + strlen(c->name) + 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sprintf(path, "%s/%s", p1, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lxc_rmdir_onedev(path) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error destroying container directory for %s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_disk_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_config_t *config;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = lxc_conf_init();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte config = lxc_getconfig(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!config)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0 == config->cb(key, v, c->lxc_conf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool b = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte b = set_config_item_locked(c, key, v);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return b;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *lxcapi_config_file_name(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->configfile)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return strdup(c->configfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_get_config_path(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->config_path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (const char *)(c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * not for export
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Just recalculate the c->configfile based on the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->config_path, which must be set.
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * The lxc_container must be locked or not yet public.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool set_config_filename(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *newpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->config_path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana newpath = malloc(len);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (!newpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error printing out config file name\n");
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana free(newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->configfile)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->configfile);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->configfile = newpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool b = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *oldpath = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return b;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return b;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = strdup(path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!p) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Out of memory setting new lxc path");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte b = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->config_path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte oldpath = c->config_path;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path = p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Since we've changed the config path, we have to change the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * config file name too */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!set_config_filename(c)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Out of memory setting new config filename");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte b = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path = oldpath;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte oldpath = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteerr:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (oldpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(oldpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return b;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (is_stopped(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_disk_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_cgroup_set(c->name, subsys, value, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_disk_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (is_stopped(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_disk_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_cgroup_get(c->name, subsys, retv, inlen, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_disk_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_default_config_path(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return default_lxc_path();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_default_lvm_vg(void)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return default_lvm_vg();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_default_zfs_root(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return default_zfs_root();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_version(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_version();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copy_file(char *old, char *new)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int in, out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ssize_t len, ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char buf[8096];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat sbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (file_exists(new)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("copy destination %s exists", new);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = stat(old, &sbuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("stat'ing %s", old);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte in = open(old, O_RDONLY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (in < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("opening original file %s", old);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (out < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("opening new file %s", new);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(in);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = read(in, buf, 8096);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (len < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("reading old file %s", old);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (len == 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = write(out, buf, len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < len) { // should we retry?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("write to new file %s was interrupted", new);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(in);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(out);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we set mode, but not owner/group
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = chmod(new, sbuf.st_mode);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret) {
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana SYSERROR("setting mode on %s", new);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteerr:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(in);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(out);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_list *it;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<NUM_LXC_HOOKS; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *hookname = it->elem;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *fname = rindex(hookname, '/');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char tmppath[MAXPATHLEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!fname) // relative path - we don't support, but maybe we should
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana // copy the script, and change the entry in confile
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path, c->name, fname+1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= MAXPATHLEN)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = copy_file(it->elem, tmppath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(it->elem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte it->elem = strdup(tmppath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!it->elem) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("out of memory copying hook path");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->save_config(c, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void new_hwaddr(char *hwaddr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *f = fopen("/dev/urandom", "r");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (f) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned int seed;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret = fread(&seed, sizeof(seed), 1, f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret != 1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte seed = time(NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte srand(seed);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte srand(time(NULL));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rand() % 255, rand() % 255, rand() % 255);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void network_new_hwaddrs(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_list *it;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_list_for_each(it, &c->lxc_conf->network) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_netdev *n = it->elem;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n->hwaddr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_hwaddr(n->hwaddr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char newpath[MAXPATHLEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *oldpath = oldc->lxc_conf->fstab;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!oldpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *p = rindex(oldpath, '/');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!p)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path, c->name, p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= MAXPATHLEN) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error printing new path for %s", oldpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (file_exists(newpath)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: fstab file %s exists", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (copy_file(oldpath, newpath) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: copying %s to %s", oldpath, newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->lxc_conf->fstab);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf->fstab = strdup(newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf->fstab) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: allocating pathname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copy_storage(struct lxc_container *c0, struct lxc_container *c,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *newtype, int flags, const char *bdevdata, unsigned long newsize)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev *bdev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_copy(c0->lxc_conf->rootfs.path, c0->name, c->name,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c0->config_path, c->config_path, newtype, !!(flags & LXC_CLONE_SNAPSHOT),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdevdata, newsize);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (!bdev) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error copying storage");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(c->lxc_conf->rootfs.path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf->rootfs.path = strdup(bdev->src);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev_put(bdev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->lxc_conf->rootfs.path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // here we could also update all lxc.mount.entries or even
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // items in the lxc.mount fstab list. As discussed on m-l,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we could do either any source paths starting with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // lxcpath/oldname, or simply anythign which is not a virtual
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // fs or a bind mount.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int clone_update_rootfs(struct lxc_container *c0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_container *c, int flags,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **hookargs)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret = -1;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana char path[MAXPATHLEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct bdev *bdev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *fout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_conf *conf = c->lxc_conf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update hostname in rootfs */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we're going to mount, so run in a clean namespace to simplify cleanup */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid < 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid > 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return wait_for_pid(pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (unshare(CLONE_NEWNS) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error unsharing mounts");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!bdev)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (bdev->ops->mount(bdev) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Start of environment variable setup for hooks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_SRC_NAME", c0->name, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for source container name");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_NAME", c->name, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for container name");
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for config path");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for rootfs mount");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for rootfs mount");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error executing clone hook for %s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(flags & LXC_CLONE_KEEPNAME)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana if (ret < 0 || ret >= MAXPATHLEN)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!file_exists(path))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(fout = fopen(path, "w"))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("unable to open %s: ignoring\n", path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fprintf(fout, "%s", c->name) < 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fclose(fout) < 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * We want to support:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte-s [ implies overlayfs]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte-s -B overlayfs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte-s -B aufs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteonly rootfs gets converted (copied/snapshotted) on clone.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int create_file_dirname(char *path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *p = rindex(path, '/');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!p)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = mkdir(path, 0755);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret && errno != EEXIST)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("creating container path %s\n", path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = '/';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *lxcpath, int flags,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, const char *bdevdata, unsigned long newsize,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **hookargs)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_container *c2 = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char newpath[MAXPATHLEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret, storage_copied = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *n, *l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *fout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->is_defined(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (container_mem_lock(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!is_stopped(c)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: Original container (%s) is running", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Make sure the container doesn't yet exist.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = newname ? newname : c->name;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l = lxcpath ? lxcpath : c->get_config_path(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", l, n);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 || ret >= MAXPATHLEN) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("clone: failed making config pathname");
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana goto out;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (file_exists(newpath)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: clone: %s exists", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = create_file_dirname(newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0 && errno != EEXIST) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error creating container dir for %s", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // copy the configuration, tweak it as needed,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana fout = fopen(newpath, "w");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!fout) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("open %s", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte write_config(fout, c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(fout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sprintf(newpath, "%s/%s/rootfs", l, n);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (mkdir(newpath, 0755) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("error creating %s", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c2 = lxc_container_new(n, l);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c2) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("clone: failed to create new container (%s %s)", n, l);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // update utsname
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!set_config_item_locked(c2, "lxc.utsname", newname)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error setting new hostname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // copy hooks if requested
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (flags & LXC_CLONE_COPYHOOKS) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = copyhooks(c, c2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error copying hooks");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (copy_fstab(c, c2) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error copying fstab");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // update macaddrs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(flags & LXC_CLONE_KEEPMACADDR))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte network_new_hwaddrs(c2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // copy/snapshot rootfs's
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // We've now successfully created c2's storage, so clear it out if we
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // fail after this
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte storage_copied = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c2->save_config(c2, NULL))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (clone_update_rootfs(c, c2, flags, hookargs) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto out;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // TODO: update c's lxc.snapshot = count
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return c2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteout:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte container_mem_unlock(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c2) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!storage_copied)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c2->lxc_conf->rootfs.path = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c2->destroy(c2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_container_put(c2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_attach_command_t command;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int r;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte command.program = (char*)program;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte command.argv = (char**)argv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (r < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("ups");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return r;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_wait_for_pid_status(pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **argv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, arg);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana argv = lxc_va_arg_list_to_argv_const(ap, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!argv) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Memory allocation error.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte argv[0] = arg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free((void*)argv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lxc_container *lxc_container_new(const char *name, const char *configpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lxc_container *c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = malloc(sizeof(*c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "failed to malloc lxc_container\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memset(c, 0, sizeof(*c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (configpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path = strdup(configpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_path = strdup(default_lxc_path());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->config_path) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Out of memory");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->name = malloc(strlen(name)+1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c->name) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error allocating lxc_container name\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strcpy(c->name, name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->numthreads = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(c->slock = lxc_newlock(c->config_path, name))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "failed to create lock\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(c->privlock = lxc_newlock(NULL, NULL))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "failed to alloc privlock\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!set_config_filename(c)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error allocating config file pathname\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (file_exists(c->configfile))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxcapi_load_config(c, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ongoing_create(c) == 2) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: %s creation was not completed", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxcapi_destroy(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_conf_free(c->lxc_conf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->lxc_conf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // assign the member functions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->is_defined = lxcapi_is_defined;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->state = lxcapi_state;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->is_running = lxcapi_is_running;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->freeze = lxcapi_freeze;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->unfreeze = lxcapi_unfreeze;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->console = lxcapi_console;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->console_getfd = lxcapi_console_getfd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->init_pid = lxcapi_init_pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->load_config = lxcapi_load_config;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->want_daemonize = lxcapi_want_daemonize;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->start = lxcapi_start;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->startl = lxcapi_startl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->stop = lxcapi_stop;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->config_file_name = lxcapi_config_file_name;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->wait = lxcapi_wait;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->set_config_item = lxcapi_set_config_item;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->destroy = lxcapi_destroy;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->save_config = lxcapi_save_config;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->get_keys = lxcapi_get_keys;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->create = lxcapi_create;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->createl = lxcapi_createl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->shutdown = lxcapi_shutdown;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->reboot = lxcapi_reboot;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->clear_config_item = lxcapi_clear_config_item;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->get_config_item = lxcapi_get_config_item;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->get_cgroup_item = lxcapi_get_cgroup_item;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->set_cgroup_item = lxcapi_set_cgroup_item;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->get_config_path = lxcapi_get_config_path;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->set_config_path = lxcapi_set_config_path;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->clone = lxcapi_clone;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->get_ips = lxcapi_get_ips;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->attach = lxcapi_attach;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->attach_run_wait = lxcapi_attach_run_wait;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c->attach_run_waitl = lxcapi_attach_run_waitl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we'll allow the caller to update these later */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "failed to open log\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteerr:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxc_container_free(c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint lxc_get_wait_states(const char **states)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (states)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<MAX_STATE; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte states[i] = lxc_state2str(i);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return MAX_STATE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte