From 5649bd03bdb39c513ec38545dffe5b00a6448160 Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Tue, 28 Jul 2015 13:12:10 +0200 Subject: [PATCH] lxcpp: Added logger, tests and changed exceptions [Feature] Using libLogger in lxcpp in an example clone function. Set up unit-tests Changed exception names [Cause] N/A [Solution] N/A [Verification] N/A Change-Id: I039bf3e17d791ccf1a18f17a9b2a180ee2081722 --- libs/lxcpp/CMakeLists.txt | 1 + libs/lxcpp/container-impl.cpp | 30 +++++++++---------- libs/lxcpp/container-impl.hpp | 2 +- libs/lxcpp/container.hpp | 2 +- libs/lxcpp/exception.hpp | 21 ++++++++----- libs/lxcpp/lxcpp.cpp | 2 +- libs/lxcpp/lxcpp.hpp | 4 +-- libs/lxcpp/process.cpp | 52 ++++++++++++++++++++++++++++++++ libs/lxcpp/process.hpp | 35 ++++++++++++++++++++++ tests/unit_tests/CMakeLists.txt | 2 +- tests/unit_tests/lxcpp/ut-container.cpp | 53 +++++++++++++++++++++++++++++++++ 11 files changed, 175 insertions(+), 29 deletions(-) create mode 100644 libs/lxcpp/process.cpp create mode 100644 libs/lxcpp/process.hpp create mode 100644 tests/unit_tests/lxcpp/ut-container.cpp diff --git a/libs/lxcpp/CMakeLists.txt b/libs/lxcpp/CMakeLists.txt index 74522a3..0d49df9 100644 --- a/libs/lxcpp/CMakeLists.txt +++ b/libs/lxcpp/CMakeLists.txt @@ -37,6 +37,7 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES ## Link libraries ############################################################## INCLUDE_DIRECTORIES(${LIBS_FOLDER}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} Logger) ## Generate the pc file ######################################################## CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) diff --git a/libs/lxcpp/container-impl.cpp b/libs/lxcpp/container-impl.cpp index a7f404e..e4f290d 100644 --- a/libs/lxcpp/container-impl.cpp +++ b/libs/lxcpp/container-impl.cpp @@ -21,8 +21,8 @@ * @brief ContainerImpl definition */ -#include -#include +#include "lxcpp/container-impl.hpp" +#include "lxcpp/exception.hpp" namespace lxcpp { @@ -36,62 +36,62 @@ ContainerImpl::~ContainerImpl() std::string ContainerImpl::getName() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::setName(const std::string& /* name */) { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::start() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::stop() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::freeze() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::unfreeze() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::reboot() { - throw NotImplemented(); + throw NotImplementedException(); } int ContainerImpl::getInitPid() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::create() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::destroy() { - throw NotImplemented(); + throw NotImplementedException(); } void ContainerImpl::setRootPath(const std::string& /* path */) { - throw NotImplemented(); + throw NotImplementedException(); } -std::string getRootPath() +std::string ContainerImpl::getRootPath() { - throw NotImplemented(); + throw NotImplementedException(); } } // namespace lxcpp diff --git a/libs/lxcpp/container-impl.hpp b/libs/lxcpp/container-impl.hpp index 9b9b1ea..f5d9547 100644 --- a/libs/lxcpp/container-impl.hpp +++ b/libs/lxcpp/container-impl.hpp @@ -24,7 +24,7 @@ #ifndef LXCPP_CONTAINER_IMPL_HPP #define LXCPP_CONTAINER_IMPL_HPP -#include +#include "lxcpp/container.hpp" namespace lxcpp { diff --git a/libs/lxcpp/container.hpp b/libs/lxcpp/container.hpp index 36e877e..74caa51 100644 --- a/libs/lxcpp/container.hpp +++ b/libs/lxcpp/container.hpp @@ -30,7 +30,7 @@ namespace lxcpp { class Container { public: - virtual ~Container(); + virtual ~Container() {}; virtual std::string getName() = 0; virtual void setName(const std::string& name) = 0; diff --git a/libs/lxcpp/exception.hpp b/libs/lxcpp/exception.hpp index 1e3ce4c..9d59101 100644 --- a/libs/lxcpp/exception.hpp +++ b/libs/lxcpp/exception.hpp @@ -28,18 +28,23 @@ namespace lxcpp { -class ContainerException : std::runtime_error -{ -public: - ContainerException(const std::string& what) : std::runtime_error(what) {} +/** + * Base class for exceptions in lxcpp + */ +struct Exception: public std::runtime_error { + Exception(const std::string& message) + : std::runtime_error(message) {} }; -class NotImplemented : ContainerException -{ -public: - NotImplemented() : ContainerException(std::string()) {} +struct NotImplementedException: public Exception { + NotImplementedException(const std::string& message = "Functionality not yet implemented") + : Exception(message) {} }; +struct ProcessSetupException: public Exception { + ProcessSetupException(const std::string& message = "Error during setting up a process") + : Exception(message) {} +}; } // namespace lxcpp #endif // LXCPP_EXCEPTION_HPP diff --git a/libs/lxcpp/lxcpp.cpp b/libs/lxcpp/lxcpp.cpp index 3cb906c..97c3866 100644 --- a/libs/lxcpp/lxcpp.cpp +++ b/libs/lxcpp/lxcpp.cpp @@ -21,7 +21,7 @@ * @brief lxcpp container factory definition */ -#include +#include "lxcpp/container-impl.hpp" namespace lxcpp { diff --git a/libs/lxcpp/lxcpp.hpp b/libs/lxcpp/lxcpp.hpp index 8037d3c..5eb3f57 100644 --- a/libs/lxcpp/lxcpp.hpp +++ b/libs/lxcpp/lxcpp.hpp @@ -18,13 +18,13 @@ /** * @file * @author Mateusz Malicki (m.malicki2@samsung.com) - * @brief LXCpp factory declaration + * @brief lxcpp container factory */ #ifndef LXCPP_LXCPP_HPP #define LXCPP_LXCPP_HPP -#include +#include "lxcpp/container.hpp" namespace lxcpp { diff --git a/libs/lxcpp/process.cpp b/libs/lxcpp/process.cpp new file mode 100644 index 0000000..200d12e --- /dev/null +++ b/libs/lxcpp/process.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief process handling routines + */ + +#include "lxcpp/process.hpp" +#include "lxcpp/exception.hpp" +#include "logger/logger.hpp" + +#include +#include +#include +#include + +namespace lxcpp { + +pid_t clone(int (*function)(void *), int flags, void *args) { + // Won't fail, well known resource name + size_t stackSize = ::sysconf(_SC_PAGESIZE); + + // PAGESIZE is enough, it'll exec after this + char *stack = static_cast(::alloca(stackSize)); + + pid_t ret; + ret = ::clone(function, stack + stackSize, flags | SIGCHLD, args); + if (ret < 0) { + LOGE("clone() failed"); + throw ProcessSetupException("clone() failed"); + } + + return ret; +} + +} // namespace lxcpp \ No newline at end of file diff --git a/libs/lxcpp/process.hpp b/libs/lxcpp/process.hpp new file mode 100644 index 0000000..8ca8aaf --- /dev/null +++ b/libs/lxcpp/process.hpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief process handling routines + */ + +#ifndef LXCPP_PROCESS_HPP +#define LXCPP_PROCESS_HPP + +#include + +namespace lxcpp { + +pid_t clone(int (*function)(void *), int flags, void *args); + +} // namespace lxcpp + +#endif // LXCPP_PROCESS_HPP \ No newline at end of file diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index aa652c7..956082d 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -61,7 +61,7 @@ SET_TARGET_PROPERTIES(${UT_SERVER_CODENAME} PROPERTIES ) TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} ${UT_SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES} - Logger Config Ipc) + Logger Config Ipc lxcpp) IF(NOT WITHOUT_DBUS) TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} SimpleDbus) ENDIF(NOT WITHOUT_DBUS) diff --git a/tests/unit_tests/lxcpp/ut-container.cpp b/tests/unit_tests/lxcpp/ut-container.cpp new file mode 100644 index 0000000..74ebbfe --- /dev/null +++ b/tests/unit_tests/lxcpp/ut-container.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Piotr Bartosiewicz + * + * 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 lxcpp Container class + */ + +#include "config.hpp" +#include "ut.hpp" + +#include "lxcpp/lxcpp.hpp" +#include "lxcpp/exception.hpp" + +#include + +namespace { + +struct Fixture { + Fixture() {} + ~Fixture() {} +}; + +} // namespace + +BOOST_FIXTURE_TEST_SUITE(LxcppContainerSuite, Fixture) + +using namespace lxcpp; + +BOOST_AUTO_TEST_CASE(ConstructorDestructor) +{ + auto c = createContainer(); + delete c; +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.7.4