+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Implementation of the class wrapping connection to libvirtd
- */
-
-#include "config.hpp"
-#include "logger/logger.hpp"
-#include "libvirt/helpers.hpp"
-#include "libvirt/connection.hpp"
-#include "libvirt/exception.hpp"
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-LibvirtConnection::LibvirtConnection(const std::string& uri)
- : mCon(nullptr)
-{
- libvirtInitialize();
-
- mCon = virConnectOpen(uri.c_str());
-
- if (mCon == nullptr) {
- LOGE("Failed to open a connection to the libvirtd:\n"
- << libvirtFormatError());
- throw LibvirtOperationException();
- }
-}
-
-LibvirtConnection::~LibvirtConnection()
-{
- if (virConnectClose(mCon) < 0) {
- LOGE("Error while disconnecting from the libvirtd:\n"
- << libvirtFormatError());
- };
-}
-
-virConnectPtr LibvirtConnection::get()
-{
- return mCon;
-}
-
-LibvirtConnection::operator bool() const
-{
- return mCon != nullptr;
-}
-
-
-} // namespace libvirt
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Declaration of the class wrapping connection to libvirtd
- */
-
-#ifndef COMMON_LIBVIRT_CONNECTION_HPP
-#define COMMON_LIBVIRT_CONNECTION_HPP
-
-#include <libvirt/libvirt.h>
-#include <string>
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-const std::string LIBVIRT_LXC_ADDRESS = "lxc://";
-
-/**
- * A class wrapping connection to libvirtd
- */
-class LibvirtConnection {
-
-public:
- LibvirtConnection(const std::string& uri);
- ~LibvirtConnection();
-
- /**
- * @return The libvirt connection pointer
- */
- virConnectPtr get();
-
- /**
- * @return connection pointer is not NULL
- */
- operator bool() const;
-
-private:
- virConnectPtr mCon;
-};
-
-
-} // namespace libvirt
-} // namespace security_containers
-
-
-#endif // COMMON_LIBVIRT_CONNECTION_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Implementation of the class wrapping libvirt domain
- */
-
-#include "config.hpp"
-#include "logger/logger.hpp"
-#include "libvirt/domain.hpp"
-#include "libvirt/helpers.hpp"
-#include "libvirt/exception.hpp"
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-LibvirtDomain::LibvirtDomain(const std::string& configXML)
- : mCon(LIBVIRT_LXC_ADDRESS), mDom(nullptr)
-{
- mDom = virDomainDefineXML(mCon.get(), configXML.c_str());
-
- if (mDom == nullptr) {
- LOGE("Error while defining a domain:\n"
- << libvirtFormatError());
- throw LibvirtOperationException();
- }
-}
-
-LibvirtDomain::~LibvirtDomain()
-{
- if (virDomainUndefine(mDom) < 0) {
- LOGE("Error while undefining the domain:\n"
- << libvirtFormatError());
- }
-
- if (virDomainFree(mDom) < 0) {
- LOGE("Error while destroying the domain object:\n"
- << libvirtFormatError());
- }
-}
-
-virDomainPtr LibvirtDomain::get()
-{
- return mDom;
-}
-
-LibvirtDomain::operator bool() const
-{
- return mDom != nullptr;
-}
-
-} // namespace libvirt
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Declaration of the class wrapping libvirt domain
- */
-
-#ifndef COMMON_LIBVIRT_DOMAIN_HPP
-#define COMMON_LIBVIRT_DOMAIN_HPP
-
-#include "libvirt/connection.hpp"
-
-#include <libvirt/libvirt.h>
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-/**
- * A class wrapping libvirtd domain
- */
-class LibvirtDomain {
-
-public:
- LibvirtDomain(const std::string& configXML);
- ~LibvirtDomain();
-
- /**
- * @return The libvirt domain pointer
- */
- virDomainPtr get();
-
- /**
- * @return libvirt domain pointer is not NULL
- */
- operator bool() const;
-
-private:
- LibvirtConnection mCon;
- virDomainPtr mDom;
-};
-
-
-} // namespace libvirt
-} // namespace security_containers
-
-
-#endif // COMMON_LIBVIRT_DOMAIN_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Exceptions for the server
- */
-
-
-#ifndef COMMON_LIBVIRT_EXCEPTION_HPP
-#define COMMON_LIBVIRT_EXCEPTION_HPP
-
-#include "base-exception.hpp"
-
-
-namespace security_containers {
-
-
-/**
- * Base class for exceptions in libvirt
- */
-struct LibvirtOperationException: public SecurityContainersException {
-
- LibvirtOperationException(const std::string& error = "") : SecurityContainersException(error) {}
-};
-
-
-}
-
-
-#endif // COMMON_LIBVIRT_EXCEPTION_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief A function helpers for the libvirt library
- */
-
-#include "config.hpp"
-#include "libvirt/helpers.hpp"
-#include "logger/logger.hpp"
-
-#include <mutex>
-#include <libvirt/virterror.h>
-#include <libvirt-glib/libvirt-glib-event.h>
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-namespace {
-
-std::once_flag gInitFlag;
-
-/**
- * This function intentionally is not displaying any errors,
- * we log them ourselves elsewhere.
- * It is however displaying warnings for the time being so we can
- * learn whether such situations occur.
- */
-void libvirtErrorFunction(void* /*userData*/, virErrorPtr error)
-{
- if (error->level == VIR_ERR_WARNING) {
- LOGW("LIBVIRT reported a warning: \n" << error->message);
- }
-}
-
-} // namespace
-
-void libvirtInitialize(void)
-{
- std::call_once(gInitFlag, []() {
- virInitialize();
- virSetErrorFunc(NULL, &libvirtErrorFunction);
- gvir_event_register();
- });
-}
-
-
-std::string libvirtFormatError(void)
-{
- virErrorPtr error = virGetLastError();
-
- if (error == NULL) {
- return std::string();
- }
-
- return "Libvirt error: " + std::string(error->message);
-}
-
-std::string libvirtEventToString(const int eventId)
-{
- switch(eventId) {
- case VIR_DOMAIN_EVENT_DEFINED:
- return "Defined";
- case VIR_DOMAIN_EVENT_UNDEFINED:
- return "Undefined";
- case VIR_DOMAIN_EVENT_STARTED:
- return "Started";
- case VIR_DOMAIN_EVENT_SUSPENDED:
- return "Suspended";
- case VIR_DOMAIN_EVENT_RESUMED:
- return "Resumed";
- case VIR_DOMAIN_EVENT_STOPPED:
- return "Stopped";
- case VIR_DOMAIN_EVENT_SHUTDOWN:
- return "Shutdown";
- case VIR_DOMAIN_EVENT_PMSUSPENDED:
- return "PM Suspended";
- case VIR_DOMAIN_EVENT_CRASHED:
- return "Crashed";
- default:
- return "Unknown EventId";
- }
-}
-
-std::string libvirtEventDetailToString(const int eventId, const int detailId)
-{
- switch (eventId) {
- case VIR_DOMAIN_EVENT_DEFINED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_DEFINED_ADDED:
- return "Added";
- case VIR_DOMAIN_EVENT_DEFINED_UPDATED:
- return "Updated";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_UNDEFINED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_UNDEFINED_REMOVED:
- return "Removed";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_STARTED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_STARTED_BOOTED:
- return "Booted";
- case VIR_DOMAIN_EVENT_STARTED_MIGRATED:
- return "Migrated";
- case VIR_DOMAIN_EVENT_STARTED_RESTORED:
- return "Restored";
- case VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT:
- return "From Snapshot";
- case VIR_DOMAIN_EVENT_STARTED_WAKEUP:
- return "Wakeup";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_SUSPENDED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_SUSPENDED_PAUSED:
- return "Paused";
- case VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED:
- return "Migrated";
- case VIR_DOMAIN_EVENT_SUSPENDED_IOERROR:
- return "IO Error";
- case VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG:
- return "Watchdog";
- case VIR_DOMAIN_EVENT_SUSPENDED_RESTORED:
- return "Restored";
- case VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT:
- return "From Snapshot";
- case VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR:
- return "API Error";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_RESUMED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_RESUMED_UNPAUSED:
- return "Unpaused";
- case VIR_DOMAIN_EVENT_RESUMED_MIGRATED:
- return "Migrated";
- case VIR_DOMAIN_EVENT_RESUMED_FROM_SNAPSHOT:
- return "From Snapshot";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_STOPPED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN:
- return "Shutdown";
- case VIR_DOMAIN_EVENT_STOPPED_DESTROYED:
- return "Destroyed";
- case VIR_DOMAIN_EVENT_STOPPED_CRASHED:
- return "Crashed";
- case VIR_DOMAIN_EVENT_STOPPED_MIGRATED:
- return "Migrated";
- case VIR_DOMAIN_EVENT_STOPPED_SAVED:
- return "Failed";
- case VIR_DOMAIN_EVENT_STOPPED_FAILED:
- return "Failed";
- case VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT:
- return "From Snapshot";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_SHUTDOWN:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED:
- return "Finished";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_PMSUSPENDED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_PMSUSPENDED_MEMORY:
- return "Memory";
- case VIR_DOMAIN_EVENT_PMSUSPENDED_DISK:
- return "Disk";
- default:
- return "Unknown detail";
- }
- case VIR_DOMAIN_EVENT_CRASHED:
- switch (detailId) {
- case VIR_DOMAIN_EVENT_CRASHED_PANICKED:
- return "Panicked";
- default:
- return "Unknown detail";
- }
- default:
- return "Unknown event";
- }
-}
-
-
-} // namespace libvirt
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief A function helpers for the libvirt library
- */
-
-#ifndef COMMON_LIBVIRT_HELPERS_HPP
-#define COMMON_LIBVIRT_HELPERS_HPP
-
-#include <string>
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-/**
- * Initialize libvirt library in a thread safety manner
- */
-void libvirtInitialize(void);
-
-/**
- * Formats libvirt's last error.
- */
-std::string libvirtFormatError(void);
-
-/**
- * Converts an event ID to an event name.
- */
-std::string libvirtEventToString(const int event);
-
-/**
- * Converts an event's detail ID to an event's detail name.
- */
-std::string libvirtEventDetailToString(const int event, const int detail);
-
-
-} // namespace libvirt
-} // namespace security_containers
-
-
-#endif // COMMON_LIBVIRT_HELPERS_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Jan Olszak <j.olszak@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Jan Olszak (j.olszak@samsung.com)
- * @brief Implementation of the class wrapping libvirt network
- */
-
-#include "config.hpp"
-
-#include "logger/logger.hpp"
-#include "libvirt/network-filter.hpp"
-#include "libvirt/helpers.hpp"
-#include "libvirt/exception.hpp"
-
-
-namespace security_containers {
-namespace libvirt {
-
-LibvirtNWFilter::LibvirtNWFilter(const std::string& configXML)
- : mCon(LIBVIRT_LXC_ADDRESS), mNetFilter(nullptr),
- mDetachOnExit(false)
-{
- mNetFilter = virNWFilterDefineXML(mCon.get(), configXML.c_str());
-
- if (mNetFilter == nullptr) {
- LOGE("Error while definig a network filter:\n"
- << libvirtFormatError());
- throw LibvirtOperationException();
- }
-}
-
-LibvirtNWFilter::~LibvirtNWFilter()
-{
- if (!mDetachOnExit)
- {
- if (virNWFilterUndefine(mNetFilter) < 0) {
- LOGE("Error while undefining the network filter:\n"
- << libvirtFormatError());
- }
- }
-
- if (virNWFilterFree(mNetFilter) < 0) {
- LOGE("Error while destroying the network filter object:\n"
- << libvirtFormatError());
- }
-}
-
-void LibvirtNWFilter::setDetachOnExit()
-{
- mDetachOnExit = true;
-}
-
-virNWFilterPtr LibvirtNWFilter::get()
-{
- return mNetFilter;
-}
-
-LibvirtNWFilter::operator bool() const
-{
- return mNetFilter != nullptr;
-}
-
-} // namespace libvirt
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Jan Olszak <j.olszak@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Jan Olszak (j.olszak@samsung.com)
- * @brief Declaration of the class wrapping libvirt network
- */
-
-#ifndef COMMON_LIBVIRT_NETWORK_FILTER_HPP
-#define COMMON_LIBVIRT_NETWORK_FILTER_HPP
-
-#include "libvirt/connection.hpp"
-
-#include <libvirt/libvirt.h>
-
-
-namespace security_containers {
-namespace libvirt {
-
-class LibvirtNWFilter {
-
-public:
- LibvirtNWFilter(const std::string& configXML);
- ~LibvirtNWFilter();
-
- /**
- * @return The libvirt network pointer
- */
- virNWFilterPtr get();
-
- /**
- * @return libvirt network pointer is not NULL
- */
- operator bool() const;
-
- /**
- * Set whether container should be detached on exit.
- */
- void setDetachOnExit();
-
-private:
- LibvirtConnection mCon;
- virNWFilterPtr mNetFilter;
- bool mDetachOnExit;
-};
-
-} // namespace libvirt
-} // namespace security_containers
-
-
-#endif // COMMON_LIBVIRT_NETWORK_FILTER_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Jan Olszak <j.olszak@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Jan Olszak (j.olszak@samsung.com)
- * @brief Implementation of the class wrapping libvirt network
- */
-
-#include "config.hpp"
-
-#include "logger/logger.hpp"
-#include "libvirt/network.hpp"
-#include "libvirt/helpers.hpp"
-#include "libvirt/exception.hpp"
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-LibvirtNetwork::LibvirtNetwork(const std::string& configXML)
- : mCon(LIBVIRT_LXC_ADDRESS), mNet(nullptr)
-{
- mNet = virNetworkDefineXML(mCon.get(), configXML.c_str());
-
- if (mNet == nullptr) {
- LOGE("Error while defining a network:\n"
- << libvirtFormatError());
- throw LibvirtOperationException();
- }
-}
-
-LibvirtNetwork::~LibvirtNetwork()
-{
- if (virNetworkUndefine(mNet) < 0) {
- LOGE("Error while undefining the network:\n"
- << libvirtFormatError());
- }
-
- if (virNetworkFree(mNet) < 0) {
- LOGE("Error while destroying the network object:\n"
- << libvirtFormatError());
- }
-}
-
-virNetworkPtr LibvirtNetwork::get()
-{
- return mNet;
-}
-
-LibvirtNetwork::operator bool() const
-{
- return mNet != nullptr;
-}
-
-} // namespace libvirt
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Jan Olszak <j.olszak@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-/**
- * @file
- * @author Jan Olszak (j.olszak@samsung.com)
- * @brief Declaration of the class wrapping libvirt network
- */
-
-#ifndef COMMON_LIBVIRT_NETWORK_HPP
-#define COMMON_LIBVIRT_NETWORK_HPP
-
-#include "libvirt/connection.hpp"
-
-#include <libvirt/libvirt.h>
-
-
-namespace security_containers {
-namespace libvirt {
-
-
-class LibvirtNetwork {
-
-public:
- LibvirtNetwork(const std::string& configXML);
- ~LibvirtNetwork();
-
- /**
- * @return The libvirt network pointer
- */
- virNetworkPtr get();
-
- /**
- * @return libvirt network pointer is not NULL
- */
- operator bool() const;
-
-private:
- LibvirtConnection mCon;
- virNetworkPtr mNet;
-};
-
-} // namespace libvirt
-} // namespace security_containers
-
-
-#endif // COMMON_LIBVIRT_NETWORK_HPP
## Link libraries ##############################################################
FIND_PACKAGE(Boost COMPONENTS program_options system filesystem regex)
-PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt libvirt-glib-1.0 json gio-2.0 libsystemd-journal
+PKG_CHECK_MODULES(SERVER_DEPS REQUIRED json gio-2.0 libsystemd-journal
libcap-ng libLogger libSimpleDbus libConfig)
INCLUDE_DIRECTORIES(${COMMON_FOLDER})
#include "container-admin.hpp"
#include "exception.hpp"
-#include "libvirt/helpers.hpp"
+//#include "libvirt/helpers.hpp"
#include "logger/logger.hpp"
#include "utils/fs.hpp"
#include "utils/latch.hpp"
// TODO: this should be in container's configuration file
const int SHUTDOWN_WAIT = 10 * 1000;
-std::string getDomainName(virDomainPtr dom)
-{
- assert(dom);
-
- const char* name = virDomainGetName(dom);
- if (name == nullptr) {
- LOGE("Failed to get the domain's id:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- return name;
-}
+//std::string getDomainName(virDomainPtr dom)
+//{
+// assert(dom);
+//
+// const char* name = virDomainGetName(dom);
+// if (name == nullptr) {
+// LOGE("Failed to get the domain's id:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// return name;
+//}
} // namespace
ContainerAdmin::ContainerAdmin(const ContainerConfig& config)
: mConfig(config),
- mDom(utils::readFileContent(mConfig.config)),
- mId(getDomainName(mDom.get())),
+ //mDom(utils::readFileContent(mConfig.config)),
+ mId("TODO"),//mId(getDomainName(mDom.get())),
mDetachOnExit(false),
mLifecycleCallbackId(-1),
mRebootCallbackId(-1),
mNextIdForListener(1)
{
- LOGD(mId << ": Instantiating ContainerAdmin object");
-
- // ContainerAdmin owns those callbacks
- mLifecycleCallbackId = virConnectDomainEventRegisterAny(virDomainGetConnect(mDom.get()),
- mDom.get(),
- VIR_DOMAIN_EVENT_ID_LIFECYCLE,
- VIR_DOMAIN_EVENT_CALLBACK(&ContainerAdmin::libvirtLifecycleCallback),
- utils::createCallbackWrapper(this, mLibvirtGuard.spawn()),
- &utils::deleteCallbackWrapper<ContainerAdmin*>);
-
- if (mLifecycleCallbackId < 0) {
- LOGE(mId << ": Failed to register a libvirt lifecycle callback");
- throw ContainerOperationException(mId + ": Failed to register a libvirt lifecycle callback");
- }
-
- LOGT(mId << ": registered lifecycle callback");
-
- mRebootCallbackId = virConnectDomainEventRegisterAny(virDomainGetConnect(mDom.get()),
- mDom.get(),
- VIR_DOMAIN_EVENT_ID_REBOOT,
- VIR_DOMAIN_EVENT_CALLBACK(&ContainerAdmin::libvirtRebootCallback),
- utils::createCallbackWrapper(this, mLibvirtGuard.spawn()),
- &utils::deleteCallbackWrapper<ContainerAdmin*>);
-
- if (mRebootCallbackId < 0) {
- LOGE(mId << ": Failed to register a libvirt reboot callback");
- virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
- mLifecycleCallbackId);
- throw ContainerOperationException(mId + ": Failed to register a libvirt reboot callback");
- }
-
- LOGT(mId << ": registered reboot callback");
+// LOGD(mId << ": Instantiating ContainerAdmin object");
+//
+// // ContainerAdmin owns those callbacks
+// mLifecycleCallbackId = virConnectDomainEventRegisterAny(virDomainGetConnect(mDom.get()),
+// mDom.get(),
+// VIR_DOMAIN_EVENT_ID_LIFECYCLE,
+// VIR_DOMAIN_EVENT_CALLBACK(&ContainerAdmin::libvirtLifecycleCallback),
+// utils::createCallbackWrapper(this, mLibvirtGuard.spawn()),
+// &utils::deleteCallbackWrapper<ContainerAdmin*>);
+//
+// if (mLifecycleCallbackId < 0) {
+// LOGE(mId << ": Failed to register a libvirt lifecycle callback");
+// throw ContainerOperationException(mId + ": Failed to register a libvirt lifecycle callback");
+// }
+//
+// LOGT(mId << ": registered lifecycle callback");
+//
+// mRebootCallbackId = virConnectDomainEventRegisterAny(virDomainGetConnect(mDom.get()),
+// mDom.get(),
+// VIR_DOMAIN_EVENT_ID_REBOOT,
+// VIR_DOMAIN_EVENT_CALLBACK(&ContainerAdmin::libvirtRebootCallback),
+// utils::createCallbackWrapper(this, mLibvirtGuard.spawn()),
+// &utils::deleteCallbackWrapper<ContainerAdmin*>);
+//
+// if (mRebootCallbackId < 0) {
+// LOGE(mId << ": Failed to register a libvirt reboot callback");
+// virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
+// mLifecycleCallbackId);
+// throw ContainerOperationException(mId + ": Failed to register a libvirt reboot callback");
+// }
+//
+// LOGT(mId << ": registered reboot callback");
}
ContainerAdmin::~ContainerAdmin()
{
- LOGD(mId << ": Destroying ContainerAdmin object...");
-
- // Deregister callbacks
- if (mLifecycleCallbackId >= 0) {
- virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
- mLifecycleCallbackId);
- }
- if (mRebootCallbackId >= 0) {
- virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
- mRebootCallbackId);
- }
-
- // Try to forcefully stop
- if (!mDetachOnExit) {
- try {
- destroy();
- } catch (ServerException&) {
- LOGE(mId << ": Failed to destroy the container");
- }
- }
-
- LOGD(mId << ": ContainerAdmin object destroyed");
+// LOGD(mId << ": Destroying ContainerAdmin object...");
+//
+// // Deregister callbacks
+// if (mLifecycleCallbackId >= 0) {
+// virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
+// mLifecycleCallbackId);
+// }
+// if (mRebootCallbackId >= 0) {
+// virConnectDomainEventDeregisterAny(virDomainGetConnect(mDom.get()),
+// mRebootCallbackId);
+// }
+//
+// // Try to forcefully stop
+// if (!mDetachOnExit) {
+// try {
+// destroy();
+// } catch (ServerException&) {
+// LOGE(mId << ": Failed to destroy the container");
+// }
+// }
+//
+// LOGD(mId << ": ContainerAdmin object destroyed");
}
void ContainerAdmin::start()
{
- assert(mDom);
-
- LOGD(mId << ": Starting...");
- if (isRunning()) {
- LOGD(mId << ": Already running - nothing to do...");
- return;
- }
-
- // In order to update daemon without shutting down the containers
- // autodestroy option must NOT be set. It's best to create domain
- // without any flags.
- u_int flags = VIR_DOMAIN_NONE;
-
- if (virDomainCreateWithFlags(mDom.get(), flags) < 0) {
- LOGE(mId << ": Failed to start the container\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Started");
+// assert(mDom);
+//
+// LOGD(mId << ": Starting...");
+// if (isRunning()) {
+// LOGD(mId << ": Already running - nothing to do...");
+// return;
+// }
+//
+// // In order to update daemon without shutting down the containers
+// // autodestroy option must NOT be set. It's best to create domain
+// // without any flags.
+// u_int flags = VIR_DOMAIN_NONE;
+//
+// if (virDomainCreateWithFlags(mDom.get(), flags) < 0) {
+// LOGE(mId << ": Failed to start the container\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Started");
}
void ContainerAdmin::stop()
{
- assert(mDom);
-
- LOGD(mId << ": Stopping procedure started...");
- if (isStopped()) {
- LOGD(mId << ": Already crashed/down/off - nothing to do");
- return;
- }
-
- utils::Latch stoppedOccured;
-
- LifecycleListener setStopped = [&](const int eventId, const int detailId) {
- if (eventId == VIR_DOMAIN_EVENT_STOPPED) {
- if (detailId != VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
- LOGW(mId << ": shutdown requested, but the container stopped with a different status: "
- << libvirt::libvirtEventDetailToString(eventId, detailId));
- }
- stoppedOccured.set();
- }
- };
-
- ListenerId id = registerLifecycleListener(setStopped, nullptr);
- shutdown();
- bool stopped = stoppedOccured.wait(SHUTDOWN_WAIT);
- removeListener(id);
-
- if (!stopped) {
- LOGW(mId << ": Gracefull shutdown timed out, the container is still running, destroying");
- destroy();
- }
-
- LOGD(mId << ": Stopping procedure ended");
+// assert(mDom);
+//
+// LOGD(mId << ": Stopping procedure started...");
+// if (isStopped()) {
+// LOGD(mId << ": Already crashed/down/off - nothing to do");
+// return;
+// }
+//
+// utils::Latch stoppedOccured;
+//
+// LifecycleListener setStopped = [&](const int eventId, const int detailId) {
+// if (eventId == VIR_DOMAIN_EVENT_STOPPED) {
+// if (detailId != VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
+// LOGW(mId << ": shutdown requested, but the container stopped with a different status: "
+// << libvirt::libvirtEventDetailToString(eventId, detailId));
+// }
+// stoppedOccured.set();
+// }
+// };
+//
+// ListenerId id = registerLifecycleListener(setStopped, nullptr);
+// shutdown();
+// bool stopped = stoppedOccured.wait(SHUTDOWN_WAIT);
+// removeListener(id);
+//
+// if (!stopped) {
+// LOGW(mId << ": Gracefull shutdown timed out, the container is still running, destroying");
+// destroy();
+// }
+//
+// LOGD(mId << ": Stopping procedure ended");
}
void ContainerAdmin::destroy()
{
- assert(mDom);
-
- LOGD(mId << ": Destroying...");
- if (isStopped()) {
- LOGD(mId << ": Already crashed/down/off - nothing to do");
- return;
- }
-
- setSchedulerLevel(SchedulerLevel::FOREGROUND);
-
- // Forceful termination of the guest
- u_int flags = VIR_DOMAIN_DESTROY_DEFAULT;
-
- if (virDomainDestroyFlags(mDom.get(), flags) < 0) {
- LOGE(mId << ": Error while destroying the container:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Destroyed");
+// assert(mDom);
+//
+// LOGD(mId << ": Destroying...");
+// if (isStopped()) {
+// LOGD(mId << ": Already crashed/down/off - nothing to do");
+// return;
+// }
+//
+// setSchedulerLevel(SchedulerLevel::FOREGROUND);
+//
+// // Forceful termination of the guest
+// u_int flags = VIR_DOMAIN_DESTROY_DEFAULT;
+//
+// if (virDomainDestroyFlags(mDom.get(), flags) < 0) {
+// LOGE(mId << ": Error while destroying the container:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Destroyed");
}
void ContainerAdmin::shutdown()
{
- assert(mDom);
-
- LOGD(mId << ": Shutting down...");
- if (isStopped()) {
- LOGD(mId << ": Already crashed/down/off - nothing to do");
- return;
- }
-
- setSchedulerLevel(SchedulerLevel::FOREGROUND);
-
- if (virDomainShutdownFlags(mDom.get(), VIR_DOMAIN_SHUTDOWN_SIGNAL) < 0) {
- LOGE(mId << ": Error while shutting down the container:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Shut down initiated (async)");
+// assert(mDom);
+//
+// LOGD(mId << ": Shutting down...");
+// if (isStopped()) {
+// LOGD(mId << ": Already crashed/down/off - nothing to do");
+// return;
+// }
+//
+// setSchedulerLevel(SchedulerLevel::FOREGROUND);
+//
+// if (virDomainShutdownFlags(mDom.get(), VIR_DOMAIN_SHUTDOWN_SIGNAL) < 0) {
+// LOGE(mId << ": Error while shutting down the container:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Shut down initiated (async)");
}
bool ContainerAdmin::isRunning()
{
- return getState() == VIR_DOMAIN_RUNNING;
+// return getState() == VIR_DOMAIN_RUNNING;
+ return false;
}
bool ContainerAdmin::isStopped()
{
- int state = getState();
- return state == VIR_DOMAIN_SHUTDOWN ||
- state == VIR_DOMAIN_SHUTOFF ||
- state == VIR_DOMAIN_CRASHED;
+// int state = getState();
+// return state == VIR_DOMAIN_SHUTDOWN ||
+// state == VIR_DOMAIN_SHUTOFF ||
+// state == VIR_DOMAIN_CRASHED;
+ return false;
}
void ContainerAdmin::suspend()
{
- assert(mDom);
-
- LOGD(mId << ": Pausing...");
- if (isPaused()) {
- LOGD(mId << ": Already paused - nothing to do...");
- return;
- }
-
- if (virDomainSuspend(mDom.get()) < 0) {
- LOGE(mId << ": Error while suspending the container:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Paused");
+// assert(mDom);
+//
+// LOGD(mId << ": Pausing...");
+// if (isPaused()) {
+// LOGD(mId << ": Already paused - nothing to do...");
+// return;
+// }
+//
+// if (virDomainSuspend(mDom.get()) < 0) {
+// LOGE(mId << ": Error while suspending the container:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Paused");
}
void ContainerAdmin::resume()
{
- assert(mDom);
-
- LOGD(mId << ": Resuming...");
- if (!isPaused()) {
- LOGD(mId << ": Is not paused - nothing to do...");
- return;
- }
-
- if (virDomainResume(mDom.get()) < 0) {
- LOGE(mId << ": Error while resuming the container:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Resumed");
+// assert(mDom);
+//
+// LOGD(mId << ": Resuming...");
+// if (!isPaused()) {
+// LOGD(mId << ": Is not paused - nothing to do...");
+// return;
+// }
+//
+// if (virDomainResume(mDom.get()) < 0) {
+// LOGE(mId << ": Error while resuming the container:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Resumed");
}
bool ContainerAdmin::isPaused()
{
- return getState() == VIR_DOMAIN_PAUSED;
+// return getState() == VIR_DOMAIN_PAUSED;
+ return false;
}
int ContainerAdmin::getState()
{
- assert(mDom);
-
- int state;
-
- if (virDomainGetState(mDom.get(), &state, NULL, 0)) {
- LOGE(mId << ": Error while getting the container's state:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- return state;
+// assert(mDom);
+//
+// int state;
+//
+// if (virDomainGetState(mDom.get(), &state, NULL, 0)) {
+// LOGE(mId << ": Error while getting the container's state:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// return state;
+ return 0;
}
}
-void ContainerAdmin::setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota)
+void ContainerAdmin::setSchedulerParams(std::uint64_t, std::uint64_t, std::int64_t)
+//void ContainerAdmin::setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota)
{
- assert(mDom);
-
- int maxParams = 3;
- int numParamsBuff = 0;
-
- std::unique_ptr<virTypedParameter[]> params(new virTypedParameter[maxParams]);
-
- virTypedParameterPtr paramsTmp = params.get();
-
- virTypedParamsAddULLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_CPU_SHARES, cpuShares);
- virTypedParamsAddULLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_VCPU_PERIOD, vcpuPeriod);
- virTypedParamsAddLLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_VCPU_QUOTA, vcpuQuota);
-
- if (virDomainSetSchedulerParameters(mDom.get(), params.get(), numParamsBuff) < 0) {
- LOGE(mId << ": Error while setting the container's scheduler params:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
+// assert(mDom);
+//
+// int maxParams = 3;
+// int numParamsBuff = 0;
+//
+// std::unique_ptr<virTypedParameter[]> params(new virTypedParameter[maxParams]);
+//
+// virTypedParameterPtr paramsTmp = params.get();
+//
+// virTypedParamsAddULLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_CPU_SHARES, cpuShares);
+// virTypedParamsAddULLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_VCPU_PERIOD, vcpuPeriod);
+// virTypedParamsAddLLong(¶msTmp, &numParamsBuff, &maxParams, VIR_DOMAIN_SCHEDULER_VCPU_QUOTA, vcpuQuota);
+//
+// if (virDomainSetSchedulerParameters(mDom.get(), params.get(), numParamsBuff) < 0) {
+// LOGE(mId << ": Error while setting the container's scheduler params:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
}
void ContainerAdmin::setDetachOnExit()
std::int64_t ContainerAdmin::getSchedulerQuota()
{
- assert(mDom);
-
- int numParamsBuff;
- std::unique_ptr<char, void(*)(void*)> type(virDomainGetSchedulerType(mDom.get(), &numParamsBuff), free);
-
- if (type == NULL || numParamsBuff <= 0 || strcmp(type.get(), "posix") != 0) {
- LOGE(mId << ": Error while getting the container's scheduler type:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- std::unique_ptr<virTypedParameter[]> params(new virTypedParameter[numParamsBuff]);
-
- if (virDomainGetSchedulerParameters(mDom.get(), params.get(), &numParamsBuff) < 0) {
- LOGE(mId << ": Error while getting the container's scheduler params:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- long long quota;
- if (virTypedParamsGetLLong(params.get(),
- numParamsBuff,
- VIR_DOMAIN_SCHEDULER_VCPU_QUOTA,
- "a) <= 0) {
- LOGE(mId << ": Error while getting the container's scheduler quota param:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- return quota;
+// assert(mDom);
+//
+// int numParamsBuff;
+// std::unique_ptr<char, void(*)(void*)> type(virDomainGetSchedulerType(mDom.get(), &numParamsBuff), free);
+//
+// if (type == NULL || numParamsBuff <= 0 || strcmp(type.get(), "posix") != 0) {
+// LOGE(mId << ": Error while getting the container's scheduler type:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// std::unique_ptr<virTypedParameter[]> params(new virTypedParameter[numParamsBuff]);
+//
+// if (virDomainGetSchedulerParameters(mDom.get(), params.get(), &numParamsBuff) < 0) {
+// LOGE(mId << ": Error while getting the container's scheduler params:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// long long quota;
+// if (virTypedParamsGetLLong(params.get(),
+// numParamsBuff,
+// VIR_DOMAIN_SCHEDULER_VCPU_QUOTA,
+// "a) <= 0) {
+// LOGE(mId << ": Error while getting the container's scheduler quota param:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// return quota;
+ return 0;
}
ContainerAdmin::ListenerId ContainerAdmin::registerLifecycleListener(const ContainerAdmin::LifecycleListener& listener,
mRebootListeners.erase(id);
}
-int ContainerAdmin::libvirtLifecycleCallback(virConnectPtr /*con*/,
- virDomainPtr /*dom*/,
- int event,
- int detail,
- void* opaque)
-{
- ContainerAdmin* thisPtr = utils::getCallbackFromPointer<ContainerAdmin*>(opaque);
-
- LOGI(thisPtr->getId()
- << ": Lifecycle event: "
- << libvirt::libvirtEventToString(event)
- << ": "
- << libvirt::libvirtEventDetailToString(event, detail));
-
- std::unique_lock<std::mutex> lock(thisPtr->mListenerMutex);
- for (auto& it : thisPtr->mLifecycleListeners) {
- LifecycleListener f = it.second.get();
- f(event, detail);
- }
-
- // ignored, libvirt's legacy
- return 0;
-}
-
-void ContainerAdmin::libvirtRebootCallback(virConnectPtr /*con*/,
- virDomainPtr /*dom*/,
- void* opaque)
-{
- ContainerAdmin* thisPtr = utils::getCallbackFromPointer<ContainerAdmin*>(opaque);
-
- LOGI(thisPtr->getId() << ": Reboot event");
-
- std::unique_lock<std::mutex> lock(thisPtr->mListenerMutex);
- for (auto& it : thisPtr->mRebootListeners) {
- RebootListener f = it.second.get();
- f();
- }
-}
+//int ContainerAdmin::libvirtLifecycleCallback(virConnectPtr /*con*/,
+// virDomainPtr /*dom*/,
+// int event,
+// int detail,
+// void* opaque)
+//{
+// ContainerAdmin* thisPtr = utils::getCallbackFromPointer<ContainerAdmin*>(opaque);
+//
+// LOGI(thisPtr->getId()
+// << ": Lifecycle event: "
+// << libvirt::libvirtEventToString(event)
+// << ": "
+// << libvirt::libvirtEventDetailToString(event, detail));
+//
+// std::unique_lock<std::mutex> lock(thisPtr->mListenerMutex);
+// for (auto& it : thisPtr->mLifecycleListeners) {
+// LifecycleListener f = it.second.get();
+// f(event, detail);
+// }
+//
+// // ignored, libvirt's legacy
+// return 0;
+//}
+//
+//void ContainerAdmin::libvirtRebootCallback(virConnectPtr /*con*/,
+// virDomainPtr /*dom*/,
+// void* opaque)
+//{
+// ContainerAdmin* thisPtr = utils::getCallbackFromPointer<ContainerAdmin*>(opaque);
+//
+// LOGI(thisPtr->getId() << ": Reboot event");
+//
+// std::unique_lock<std::mutex> lock(thisPtr->mListenerMutex);
+// for (auto& it : thisPtr->mRebootListeners) {
+// RebootListener f = it.second.get();
+// f();
+// }
+//}
} // namespace security_containers
#include "utils/callback-guard.hpp"
#include "utils/callback-wrapper.hpp"
-#include "libvirt/connection.hpp"
-#include "libvirt/domain.hpp"
+//#include "libvirt/connection.hpp"
+//#include "libvirt/domain.hpp"
#include <map>
#include <mutex>
#include <string>
#include <cstdint>
-#include <libvirt/libvirt.h>
namespace security_containers {
private:
const ContainerConfig& mConfig;
- libvirt::LibvirtDomain mDom;
+ //libvirt::LibvirtDomain mDom;
const std::string mId;
bool mDetachOnExit;
int mLifecycleCallbackId;
int mRebootCallbackId;
- // virConnectDomainEventCallback
- static int libvirtLifecycleCallback(virConnectPtr con,
- virDomainPtr dom,
- int event,
- int detail,
- void* opaque);
-
- // virConnectDomainEventGenericCallback
- static void libvirtRebootCallback(virConnectPtr con,
- virDomainPtr dom,
- void* opaque);
+// // virConnectDomainEventCallback
+// static int libvirtLifecycleCallback(virConnectPtr con,
+// virDomainPtr dom,
+// int event,
+// int detail,
+// void* opaque);
+//
+// // virConnectDomainEventGenericCallback
+// static void libvirtRebootCallback(virConnectPtr con,
+// virDomainPtr dom,
+// void* opaque);
// for handling external listeners triggered from libvirt callbacks
// TODO, the Listener type might not be unique, reimplement using proper listeners
#include <string>
#include <unordered_map>
-#include <libvirt/libvirt.h>
#include <memory>
#include "network-admin.hpp"
#include "exception.hpp"
-#include "libvirt/helpers.hpp"
+//#include "libvirt/helpers.hpp"
#include "logger/logger.hpp"
#include "utils/fs.hpp"
namespace {
-std::string getNetworkName(virNetworkPtr net)
-{
- assert(net);
-
- const char* name = virNetworkGetName(net);
- if (name == nullptr) {
- LOGE("Failed to get the network's id:\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- return name;
-}
+//std::string getNetworkName(virNetworkPtr net)
+//{
+// assert(net);
+//
+// const char* name = virNetworkGetName(net);
+// if (name == nullptr) {
+// LOGE("Failed to get the network's id:\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// return name;
+//}
} // namespace
NetworkAdmin::NetworkAdmin(const ContainerConfig& config)
: mConfig(config),
- mNWFilter(utils::readFileContent(mConfig.networkFilterConfig)),
- mNetwork(utils::readFileContent(mConfig.networkConfig)),
- mId(getNetworkName(mNetwork.get())),
+ //mNWFilter(utils::readFileContent(mConfig.networkFilterConfig)),
+ //mNetwork(utils::readFileContent(mConfig.networkConfig)),
+ mId("TODO"),//mId(getNetworkName(mNetwork.get())),
mDetachOnExit(false)
{
LOGD(mId << ": Instantiating NetworkAdmin object");
void NetworkAdmin::start()
{
- assert(mNetwork);
-
- LOGD(mId << ": Starting...");
- if (isActive()) {
- LOGD(mId << ": Already running - nothing to do...");
- return;
- }
-
- if (virNetworkCreate(mNetwork.get()) < 0) {
- LOGE(mId << ": Failed to start the network\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Started");
+// assert(mNetwork);
+//
+// LOGD(mId << ": Starting...");
+// if (isActive()) {
+// LOGD(mId << ": Already running - nothing to do...");
+// return;
+// }
+//
+// if (virNetworkCreate(mNetwork.get()) < 0) {
+// LOGE(mId << ": Failed to start the network\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Started");
}
void NetworkAdmin::stop()
{
- assert(mNetwork);
-
- LOGD(mId << ": Stopping procedure started...");
- if (!isActive()) {
- LOGD(mId << ": Already crashed/down/off - nothing to do");
- return;
- }
-
- if (virNetworkDestroy(mNetwork.get()) < 0) {
- LOGE(mId << ": Failed to destroy the network\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
-
- LOGD(mId << ": Stopping procedure ended");
+// assert(mNetwork);
+//
+// LOGD(mId << ": Stopping procedure started...");
+// if (!isActive()) {
+// LOGD(mId << ": Already crashed/down/off - nothing to do");
+// return;
+// }
+//
+// if (virNetworkDestroy(mNetwork.get()) < 0) {
+// LOGE(mId << ": Failed to destroy the network\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+//
+// LOGD(mId << ": Stopping procedure ended");
}
bool NetworkAdmin::isActive()
{
- assert(mNetwork);
- int ret = virNetworkIsActive(mNetwork.get());
- if (ret < 0) {
- LOGE(mId << ": Failed to get network state\n"
- << libvirt::libvirtFormatError());
- throw ContainerOperationException();
- }
- return ret > 0;
+// assert(mNetwork);
+// int ret = virNetworkIsActive(mNetwork.get());
+// if (ret < 0) {
+// LOGE(mId << ": Failed to get network state\n"
+// << libvirt::libvirtFormatError());
+// throw ContainerOperationException();
+// }
+// return ret > 0;
+ return false;
}
void NetworkAdmin::setDetachOnExit()
{
- mDetachOnExit = true;
- mNWFilter.setDetachOnExit();
+// mDetachOnExit = true;
+// mNWFilter.setDetachOnExit();
}
#include "container-config.hpp"
-#include "libvirt/network-filter.hpp"
-#include "libvirt/network.hpp"
+//#include "libvirt/network-filter.hpp"
+//#include "libvirt/network.hpp"
namespace security_containers {
private:
const ContainerConfig& mConfig;
- libvirt::LibvirtNWFilter mNWFilter;
- libvirt::LibvirtNetwork mNetwork;
+ //libvirt::LibvirtNWFilter mNWFilter;
+ //libvirt::LibvirtNetwork mNetwork;
const std::string mId;
bool mDetachOnExit;
};
## Link libraries ##############################################################
FIND_PACKAGE (Boost COMPONENTS unit_test_framework system filesystem regex)
-PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED libvirt libvirt-glib-1.0 json gio-2.0
+PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED json gio-2.0
libsystemd-journal libcap-ng libLogger libSimpleDbus libConfig)
INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_FOLDER} ${UNIT_TESTS_FOLDER} ${CLIENT_FOLDER})
INCLUDE_DIRECTORIES(SYSTEM ${UT_SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Unit tests of the LibvirtConnection class
- */
-
-#include "config.hpp"
-#include "ut.hpp"
-
-#include "libvirt/connection.hpp"
-#include "libvirt/exception.hpp"
-
-#include <memory>
-
-BOOST_AUTO_TEST_SUITE(LibvirtConnectionSuite)
-
-
-using namespace security_containers;
-using namespace security_containers::libvirt;
-
-
-const std::string CORRECT_URI_STRING = LIBVIRT_LXC_ADDRESS;
-const std::string BUGGY_URI_STRING = "some_random_string";
-
-
-BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
-{
- std::unique_ptr<LibvirtConnection> conPtr;
- BOOST_REQUIRE_NO_THROW(conPtr.reset(new LibvirtConnection(CORRECT_URI_STRING)));
- BOOST_REQUIRE_NO_THROW(conPtr.reset());
-}
-
-BOOST_AUTO_TEST_CASE(BuggyConfigTest)
-{
- BOOST_REQUIRE_THROW(LibvirtConnection con(BUGGY_URI_STRING), LibvirtOperationException);
-}
-
-BOOST_AUTO_TEST_CASE(ConnectionTest)
-{
- LibvirtConnection con(CORRECT_URI_STRING);
- BOOST_CHECK(con.get() != NULL);
-}
-
-BOOST_AUTO_TEST_CASE(BoolTest)
-{
- LibvirtConnection con(CORRECT_URI_STRING);
- BOOST_CHECK(con);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file
- * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief Unit tests of the LibvirtDomain class
- */
-
-#include "config.hpp"
-#include "ut.hpp"
-
-#include "libvirt/domain.hpp"
-#include "libvirt/exception.hpp"
-
-#include <memory>
-
-BOOST_AUTO_TEST_SUITE(LibvirtDomainSuite)
-
-
-using namespace security_containers;
-using namespace security_containers::libvirt;
-
-
-namespace {
-
-const std::string CORRECT_CONFIG_XML = "<domain type=\"lxc\">"
- " <name>test-domain</name>"
- " <uuid>444d6e30-efdf-41b0-aafa-6684e6376831</uuid>"
- " <memory>102400</memory>"
- " <os>"
- " <type>exe</type>"
- " <init>/bin/sh</init>"
- " </os>"
- " <devices>"
- " <console type=\"pty\"/>"
- " </devices>"
- "</domain>";
-const std::string BUGGY_CONFIG_XML = "<><TRASH>";
-
-} // namespace
-
-BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
-{
- std::unique_ptr<LibvirtDomain> domPtr;
- BOOST_REQUIRE_NO_THROW(domPtr.reset(new LibvirtDomain(CORRECT_CONFIG_XML)));
- BOOST_REQUIRE_NO_THROW(domPtr.reset());
-}
-
-BOOST_AUTO_TEST_CASE(BuggyConfigTest)
-{
- BOOST_REQUIRE_THROW(LibvirtDomain dom(BUGGY_CONFIG_XML), LibvirtOperationException);
-}
-
-BOOST_AUTO_TEST_CASE(DefinitionTest)
-{
- LibvirtDomain dom(CORRECT_CONFIG_XML);
- BOOST_CHECK(dom.get() != NULL);
-}
-
-BOOST_AUTO_TEST_CASE(BoolTest)
-{
- LibvirtDomain dom(CORRECT_CONFIG_XML);
- BOOST_CHECK(dom);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Jan Olszak <j.olszak@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file
- * @author Jan Olszak (j.olszak@samsung.com)
- * @brief Unit tests of the LibvirtNetwork class
- */
-
-#include "config.hpp"
-#include "ut.hpp"
-
-#include "libvirt/network-filter.hpp"
-#include "libvirt/network.hpp"
-#include "libvirt/exception.hpp"
-
-#include <memory>
-
-BOOST_AUTO_TEST_SUITE(LibvirtNetworkSuite)
-
-
-using namespace security_containers;
-using namespace security_containers::libvirt;
-
-
-namespace {
-
-const std::string CORRECT_CONFIG_XML = "<network>"
- " <name>test-network</name>"
- " <uuid>44089687-5004-4def-87f0-01c9565f74fd</uuid>"
- " <forward mode='nat'>"
- " <nat>"
- " <port start='1024' end='65535'/>"
- " </nat>"
- " </forward>"
- " <bridge name='test-virbr0' stp='on' delay='0'/>"
- " <ip address='192.168.122.1' netmask='255.255.255.0'>"
- " <dhcp>"
- " <range start='192.168.122.2' end='192.168.122.254'/>"
- " </dhcp>"
- " </ip>"
- "</network>";
-
-const std::string CORRECT_CONFIG_FILTER_XML = "<filter name='test-nwfilter' chain='root'>"
- " <rule action='reject' direction='in' priority='100'>"
- " <ip srcipaddr='192.168.121.0' srcipmask='255.255.255.0'/>"
- " </rule>"
- " <rule action='reject' direction='out' priority='100'>"
- " <ip dstipaddr='192.168.121.0' srcipmask='255.255.255.0'/>"
- " </rule>"
- "</filter>";
-
-const std::string BUGGY_CONFIG_XML = "<><TRASH>";
-
-const std::string BUGGY_CONFIG_FILTER_XML = "<><TRASH";
-
-} // namespace
-
-BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
-{
- std::unique_ptr<LibvirtNWFilter> nwFilterPtr;
- BOOST_REQUIRE_NO_THROW(nwFilterPtr.reset(new LibvirtNWFilter(CORRECT_CONFIG_FILTER_XML)));
- BOOST_REQUIRE_NO_THROW(nwFilterPtr.reset());
-
- std::unique_ptr<LibvirtNetwork> netPtr;
- BOOST_REQUIRE_NO_THROW(netPtr.reset(new LibvirtNetwork(CORRECT_CONFIG_XML)));
- BOOST_REQUIRE_NO_THROW(netPtr.reset());
-}
-
-BOOST_AUTO_TEST_CASE(BuggyConfigTest)
-{
- BOOST_REQUIRE_THROW(LibvirtNWFilter filter(BUGGY_CONFIG_FILTER_XML), LibvirtOperationException);
- BOOST_REQUIRE_THROW(LibvirtNetwork net(BUGGY_CONFIG_XML), LibvirtOperationException);
-}
-
-BOOST_AUTO_TEST_CASE(DefinitionTest)
-{
- LibvirtNWFilter filter(CORRECT_CONFIG_FILTER_XML);
- BOOST_CHECK(filter.get() != NULL);
-
- LibvirtNetwork net(CORRECT_CONFIG_XML);
- BOOST_CHECK(net.get() != NULL);
-}
-
-BOOST_AUTO_TEST_CASE(BoolTest)
-{
- LibvirtNWFilter filter(CORRECT_CONFIG_FILTER_XML);
- BOOST_CHECK(filter);
-
- LibvirtNetwork net(CORRECT_CONFIG_XML);
- BOOST_CHECK(net);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
* @brief Unit tests of the ContainerAdmin class
*/
-#include "config.hpp"
-#include "ut.hpp"
-
-#include "container-admin.hpp"
-#include "exception.hpp"
-
-#include "utils/latch.hpp"
-#include "utils/glib-loop.hpp"
-#include "utils/exception.hpp"
-#include "utils/callback-guard.hpp"
-#include "libvirt/exception.hpp"
-#include "config/manager.hpp"
-
-#include <memory>
-#include <string>
-#include <thread>
-#include <chrono>
-
-
-using namespace security_containers;
-
-namespace {
-
-const std::string TEST_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/test.conf";
-const std::string TEST_NO_SHUTDOWN_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/test-no-shutdown.conf";
-const std::string BUGGY_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/buggy.conf";
-const std::string MISSING_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/missing.conf";
-const unsigned int WAIT_TIMEOUT = 5 * 1000;
-const unsigned int WAIT_STOP_TIMEOUT = 15 * 1000;
-
-void ensureStarted()
-{
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
-}
-
-struct Fixture {
- utils::ScopedGlibLoop mLoop;
- utils::CallbackGuard mGuard;
-};
-
-} // namespace
-
-
-BOOST_FIXTURE_TEST_SUITE(ContainerAdminSuite, Fixture)
-
-BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
-{
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- std::unique_ptr<ContainerAdmin> admin;
- BOOST_REQUIRE_NO_THROW(admin.reset(new ContainerAdmin(config)));
- BOOST_REQUIRE_NO_THROW(admin.reset());
-}
-
-BOOST_AUTO_TEST_CASE(BuggyConfigTest)
-{
- ContainerConfig config;
- config::loadFromFile(BUGGY_CONFIG_PATH, config);
- BOOST_REQUIRE_THROW(ContainerAdmin ca(config), LibvirtOperationException);
-}
-
-BOOST_AUTO_TEST_CASE(MissingConfigTest)
-{
- ContainerConfig config;
- config::loadFromFile(MISSING_CONFIG_PATH, config);
- BOOST_REQUIRE_THROW(ContainerAdmin ca(config), UtilsException);
-}
-
-BOOST_AUTO_TEST_CASE(StartTest)
-{
- utils::Latch booted;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener bootedListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_STARTED && detail == VIR_DOMAIN_EVENT_STARTED_BOOTED) {
- booted.set();
- }
- };
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(bootedListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
-
- BOOST_CHECK(booted.wait(WAIT_TIMEOUT));
- BOOST_CHECK(ca.isRunning());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(ShutdownTest)
-{
- utils::Latch shutdown;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener shutdownListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
- shutdown.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(shutdownListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.shutdown());
- BOOST_CHECK(shutdown.wait(WAIT_TIMEOUT));
- BOOST_CHECK(!ca.isRunning());
- BOOST_CHECK(ca.isStopped());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(DestroyTest)
-{
- utils::Latch destroyed;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener destroyedListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_DESTROYED) {
- destroyed.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(destroyedListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.destroy());
- BOOST_CHECK(destroyed.wait(WAIT_TIMEOUT));
- BOOST_CHECK(!ca.isRunning());
- BOOST_CHECK(ca.isStopped());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(StopShutdownTest)
-{
- utils::Latch shutdown;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener shutdownListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
- shutdown.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(shutdownListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.stop());
- BOOST_CHECK(shutdown.wait(WAIT_TIMEOUT));
- BOOST_CHECK(!ca.isRunning());
- BOOST_CHECK(ca.isStopped());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-// This test needs to wait for a shutdown timer in stop() method. This takes 10s+.
-BOOST_AUTO_TEST_CASE(StopDestroyTest)
-{
- utils::Latch destroyed;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_NO_SHUTDOWN_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener destroyedListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_DESTROYED) {
- destroyed.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(destroyedListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.stop());
- BOOST_CHECK(destroyed.wait(WAIT_STOP_TIMEOUT));
- BOOST_CHECK(!ca.isRunning());
- BOOST_CHECK(ca.isStopped());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(SuspendTest)
-{
- utils::Latch paused;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener pausedListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_SUSPENDED && detail == VIR_DOMAIN_EVENT_SUSPENDED_PAUSED) {
- paused.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start())
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(pausedListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.suspend());
- BOOST_CHECK(paused.wait(WAIT_TIMEOUT));
- BOOST_CHECK(!ca.isRunning());
- BOOST_CHECK(ca.isPaused());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(ResumeTest)
-{
- utils::Latch unpaused;
- ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
-
- ContainerAdmin::LifecycleListener unpausedListener = [&](const int event, const int detail) {
- if (event == VIR_DOMAIN_EVENT_RESUMED && detail == VIR_DOMAIN_EVENT_RESUMED_UNPAUSED) {
- unpaused.set();
- }
- };
-
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE(ca.isRunning());
- BOOST_REQUIRE_NO_THROW(ca.suspend())
- BOOST_REQUIRE(ca.isPaused());
- BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(unpausedListener, mGuard.spawn()));
-
- BOOST_REQUIRE_NO_THROW(ca.resume());
- BOOST_CHECK(unpaused.wait(WAIT_TIMEOUT));
- BOOST_CHECK(!ca.isPaused());
- BOOST_CHECK(ca.isRunning());
-
- BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
-}
-
-BOOST_AUTO_TEST_CASE(SchedulerLevelTest)
-{
- ContainerConfig config;
- config::loadFromFile(TEST_CONFIG_PATH, config);
- ContainerAdmin ca(config);
- BOOST_REQUIRE_NO_THROW(ca.start());
- ensureStarted();
- BOOST_REQUIRE_NO_THROW(ca.setSchedulerLevel(SchedulerLevel::FOREGROUND));
- BOOST_REQUIRE(ca.getSchedulerQuota() == config.cpuQuotaForeground);
- BOOST_REQUIRE_NO_THROW(ca.setSchedulerLevel(SchedulerLevel::BACKGROUND));
- BOOST_REQUIRE(ca.getSchedulerQuota() == config.cpuQuotaBackground);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
+//#include "config.hpp"
+//#include "ut.hpp"
+//
+//#include "container-admin.hpp"
+//#include "exception.hpp"
+//
+//#include "utils/latch.hpp"
+//#include "utils/glib-loop.hpp"
+//#include "utils/exception.hpp"
+//#include "utils/callback-guard.hpp"
+//#include "libvirt/exception.hpp"
+//#include "config/manager.hpp"
+//
+//#include <memory>
+//#include <string>
+//#include <thread>
+//#include <chrono>
+//
+//
+//using namespace security_containers;
+//
+//namespace {
+//
+//const std::string TEST_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/test.conf";
+//const std::string TEST_NO_SHUTDOWN_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/test-no-shutdown.conf";
+//const std::string BUGGY_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/buggy.conf";
+//const std::string MISSING_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-container-admin/containers/missing.conf";
+//const unsigned int WAIT_TIMEOUT = 5 * 1000;
+//const unsigned int WAIT_STOP_TIMEOUT = 15 * 1000;
+//
+//void ensureStarted()
+//{
+// std::this_thread::sleep_for(std::chrono::milliseconds(200));
+//}
+//
+//struct Fixture {
+// utils::ScopedGlibLoop mLoop;
+// utils::CallbackGuard mGuard;
+//};
+//
+//} // namespace
+//
+//
+//BOOST_FIXTURE_TEST_SUITE(ContainerAdminSuite, Fixture)
+//
+//BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
+//{
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// std::unique_ptr<ContainerAdmin> admin;
+// BOOST_REQUIRE_NO_THROW(admin.reset(new ContainerAdmin(config)));
+// BOOST_REQUIRE_NO_THROW(admin.reset());
+//}
+//
+//BOOST_AUTO_TEST_CASE(BuggyConfigTest)
+//{
+// ContainerConfig config;
+// config::loadFromFile(BUGGY_CONFIG_PATH, config);
+// BOOST_REQUIRE_THROW(ContainerAdmin ca(config), LibvirtOperationException);
+//}
+//
+//BOOST_AUTO_TEST_CASE(MissingConfigTest)
+//{
+// ContainerConfig config;
+// config::loadFromFile(MISSING_CONFIG_PATH, config);
+// BOOST_REQUIRE_THROW(ContainerAdmin ca(config), UtilsException);
+//}
+//
+//BOOST_AUTO_TEST_CASE(StartTest)
+//{
+// utils::Latch booted;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener bootedListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_STARTED && detail == VIR_DOMAIN_EVENT_STARTED_BOOTED) {
+// booted.set();
+// }
+// };
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(bootedListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+//
+// BOOST_CHECK(booted.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(ca.isRunning());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(ShutdownTest)
+//{
+// utils::Latch shutdown;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener shutdownListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
+// shutdown.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(shutdownListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.shutdown());
+// BOOST_CHECK(shutdown.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(!ca.isRunning());
+// BOOST_CHECK(ca.isStopped());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(DestroyTest)
+//{
+// utils::Latch destroyed;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener destroyedListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_DESTROYED) {
+// destroyed.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(destroyedListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.destroy());
+// BOOST_CHECK(destroyed.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(!ca.isRunning());
+// BOOST_CHECK(ca.isStopped());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(StopShutdownTest)
+//{
+// utils::Latch shutdown;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener shutdownListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN) {
+// shutdown.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(shutdownListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.stop());
+// BOOST_CHECK(shutdown.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(!ca.isRunning());
+// BOOST_CHECK(ca.isStopped());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//// This test needs to wait for a shutdown timer in stop() method. This takes 10s+.
+//BOOST_AUTO_TEST_CASE(StopDestroyTest)
+//{
+// utils::Latch destroyed;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_NO_SHUTDOWN_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener destroyedListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_STOPPED && detail == VIR_DOMAIN_EVENT_STOPPED_DESTROYED) {
+// destroyed.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(destroyedListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.stop());
+// BOOST_CHECK(destroyed.wait(WAIT_STOP_TIMEOUT));
+// BOOST_CHECK(!ca.isRunning());
+// BOOST_CHECK(ca.isStopped());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(SuspendTest)
+//{
+// utils::Latch paused;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener pausedListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_SUSPENDED && detail == VIR_DOMAIN_EVENT_SUSPENDED_PAUSED) {
+// paused.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start())
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(pausedListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.suspend());
+// BOOST_CHECK(paused.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(!ca.isRunning());
+// BOOST_CHECK(ca.isPaused());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(ResumeTest)
+//{
+// utils::Latch unpaused;
+// ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+//
+// ContainerAdmin::LifecycleListener unpausedListener = [&](const int event, const int detail) {
+// if (event == VIR_DOMAIN_EVENT_RESUMED && detail == VIR_DOMAIN_EVENT_RESUMED_UNPAUSED) {
+// unpaused.set();
+// }
+// };
+//
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE(ca.isRunning());
+// BOOST_REQUIRE_NO_THROW(ca.suspend())
+// BOOST_REQUIRE(ca.isPaused());
+// BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(unpausedListener, mGuard.spawn()));
+//
+// BOOST_REQUIRE_NO_THROW(ca.resume());
+// BOOST_CHECK(unpaused.wait(WAIT_TIMEOUT));
+// BOOST_CHECK(!ca.isPaused());
+// BOOST_CHECK(ca.isRunning());
+//
+// BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
+//}
+//
+//BOOST_AUTO_TEST_CASE(SchedulerLevelTest)
+//{
+// ContainerConfig config;
+// config::loadFromFile(TEST_CONFIG_PATH, config);
+// ContainerAdmin ca(config);
+// BOOST_REQUIRE_NO_THROW(ca.start());
+// ensureStarted();
+// BOOST_REQUIRE_NO_THROW(ca.setSchedulerLevel(SchedulerLevel::FOREGROUND));
+// BOOST_REQUIRE(ca.getSchedulerQuota() == config.cpuQuotaForeground);
+// BOOST_REQUIRE_NO_THROW(ca.setSchedulerLevel(SchedulerLevel::BACKGROUND));
+// BOOST_REQUIRE(ca.getSchedulerQuota() == config.cpuQuotaBackground);
+//}
+//
+//BOOST_AUTO_TEST_SUITE_END()
#include "network-admin.hpp"
#include "utils/exception.hpp"
-#include "libvirt/exception.hpp"
+//#include "libvirt/exception.hpp"
#include "config/manager.hpp"
BOOST_REQUIRE_NO_THROW(admin.reset());
}
-BOOST_AUTO_TEST_CASE(BuggyConfigTest)
-{
- ContainerConfig config;
- config::loadFromFile(BUGGY_CONFIG_PATH, config);
- BOOST_REQUIRE_THROW(NetworkAdmin na(config), LibvirtOperationException);
-}
+//BOOST_AUTO_TEST_CASE(BuggyConfigTest)
+//{
+// ContainerConfig config;
+// config::loadFromFile(BUGGY_CONFIG_PATH, config);
+// BOOST_REQUIRE_THROW(NetworkAdmin na(config), LibvirtOperationException);
+//}
BOOST_AUTO_TEST_CASE(MissingConfigTest)
{