Make the code compatible with more compilers 19/21619/5
authorLukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
Fri, 23 May 2014 12:20:11 +0000 (14:20 +0200)
committerLukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
Mon, 2 Jun 2014 15:09:46 +0000 (17:09 +0200)
[Bug/Feature]   Make the code compatible with more compilers.
[Cause]         N/A
[Solution]      Redefine some C++11 keywords.
                Remove some specific C++11 constructs not found in C++0x.
                Specific defines for various compilers and their versions.
[Verification]  Built with GCC 4.6, GCC 4.8 and CLANG 3.4 and run tests.

Change-Id: I5ce7c2c3ca4372ec79b41facb1793c7df5b1f6b0
Signed-off-by: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
66 files changed:
CMakeLists.txt
client/exception.hpp
client/main.cpp
common/base-exception.hpp
common/config.hpp [new file with mode: 0644]
common/config/configuration.cpp
common/config/exception.hpp
common/dbus/connection.cpp
common/dbus/exception.hpp
common/libvirt/connection.cpp
common/libvirt/connection.hpp
common/libvirt/domain.cpp
common/libvirt/domain.hpp
common/libvirt/exception.hpp
common/libvirt/helpers.cpp
common/libvirt/network.cpp
common/libvirt/network.hpp
common/log/backend-journal.cpp
common/log/backend-stderr.cpp
common/log/ccolor.cpp
common/log/ccolor.hpp
common/log/formatter.cpp
common/log/level.cpp
common/log/logger.cpp
common/utils/callback-guard.cpp
common/utils/exception.hpp
common/utils/file-wait.cpp
common/utils/fs.cpp
common/utils/glib-loop.cpp
common/utils/latch.cpp
common/utils/paths.hpp
common/utils/typeinfo.cpp
container-daemon/daemon-connection.cpp
container-daemon/daemon.cpp
container-daemon/exception.hpp
container-daemon/main.cpp
container-daemon/runner.cpp
server/container-admin.cpp
server/container-admin.hpp
server/container-connection-transport.cpp
server/container-connection.cpp
server/container.cpp
server/containers-manager.cpp
server/exception.hpp
server/main.cpp
server/server.cpp
tests/unit_tests/config/ut-configuration.cpp
tests/unit_tests/dbus/test-client.cpp
tests/unit_tests/dbus/test-server.cpp
tests/unit_tests/dbus/ut-connection.cpp
tests/unit_tests/libvirt/connection.cpp
tests/unit_tests/libvirt/domain.cpp
tests/unit_tests/libvirt/network.cpp
tests/unit_tests/log/ut-logger.cpp
tests/unit_tests/server/ut-container-admin.cpp
tests/unit_tests/server/ut-container-connection.cpp
tests/unit_tests/server/ut-container.cpp
tests/unit_tests/server/ut-containers-manager.cpp
tests/unit_tests/server/ut-input-monitor.cpp
tests/unit_tests/server/ut-server.cpp
tests/unit_tests/ut.cpp
tests/unit_tests/utils/scoped-daemon.cpp
tests/unit_tests/utils/ut-callback-guard.cpp
tests/unit_tests/utils/ut-fs.cpp
tests/unit_tests/utils/ut-glib-loop.cpp
tests/unit_tests/utils/ut-paths.cpp

index 8c987dd..18786ef 100644 (file)
@@ -35,14 +35,21 @@ IF(NOT CMAKE_BUILD_TYPE)
 ENDIF(NOT CMAKE_BUILD_TYPE)
 MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
 
+# special case for a GCC < 4.7, assume rest is fine
+IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
+    SET(CXX_11_STD "c++0x")
+else()
+    SET(CXX_11_STD "c++11")
+endif()
+
 SET(CMAKE_C_FLAGS_PROFILING    "-g -O0 -pg")
-SET(CMAKE_CXX_FLAGS_PROFILING  "-g -std=c++11 -O0 -pg")
+SET(CMAKE_CXX_FLAGS_PROFILING  "-g -std=${CXX_11_STD} -O0 -pg")
 SET(CMAKE_C_FLAGS_DEBUG        "-g -O0 -ggdb")
-SET(CMAKE_CXX_FLAGS_DEBUG      "-g -std=c++11 -O0 -ggdb")
+SET(CMAKE_CXX_FLAGS_DEBUG      "-g -std=${CXX_11_STD} -O0 -ggdb")
 SET(CMAKE_C_FLAGS_RELEASE      "-g -O2 -DNDEBUG")
-SET(CMAKE_CXX_FLAGS_RELEASE    "-g -std=c++11 -O2 -DNDEBUG")
+SET(CMAKE_CXX_FLAGS_RELEASE    "-g -std=${CXX_11_STD} -O2 -DNDEBUG")
 SET(CMAKE_C_FLAGS_CCOV         "-g -O2 --coverage")
-SET(CMAKE_CXX_FLAGS_CCOV       "-g -std=c++11 -O2 --coverage")
+SET(CMAKE_CXX_FLAGS_CCOV       "-g -std=${CXX_11_STD} -O2 --coverage")
 
 ADD_DEFINITIONS("-fPIC")   # Position Independent Code
 ADD_DEFINITIONS("-Werror") # Make all warnings into errors
index b458d59..b4420e8 100644 (file)
@@ -36,7 +36,8 @@ namespace security_containers {
  * @brief Base class for exceptions in Security Containers Client
  */
 struct ClientException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    ClientException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 
index e21be98..1c02567 100644 (file)
  *  limitations under the License
  */
 
-
 /**
  * @file
  * @author  Jan Olszak (j.olszak@samsung.com)
  * @brief   Main file for the Security Containers Client
  */
 
+#include "config.hpp"
+
 #include "exception.hpp"
index e9f12f8..15fd3e0 100644 (file)
@@ -27,6 +27,7 @@
 #define COMMON_BASE_EXCEPTION_HPP
 
 #include <stdexcept>
+#include <string>
 
 
 namespace security_containers {
@@ -36,9 +37,8 @@ namespace security_containers {
  * Base class security containers exceptions
  */
 struct SecurityContainersException: public std::runtime_error {
-    using std::runtime_error::runtime_error;
 
-    SecurityContainersException() : std::runtime_error(std::string()) {}
+    SecurityContainersException(const std::string& error = "") : std::runtime_error(error) {}
 };
 
 
diff --git a/common/config.hpp b/common/config.hpp
new file mode 100644 (file)
index 0000000..567ef8e
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *  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   Configuration file for the code
+ */
+
+
+#ifndef COMMON_CONFIG_HPP
+#define COMMON_CONFIG_HPP
+
+
+#ifdef __clang__
+#define CLANG_VERSION (__clang__major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
+#endif // __clang__
+
+#if defined __GNUC__ && !defined __clang__  // clang also defines GCC versions
+#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
+#endif // __GNUC__
+
+
+#ifdef GCC_VERSION
+
+#if GCC_VERSION < 40800
+// GCC 4.8 is the first where those defines are not required for
+// std::this_thread::sleep_for() and ::yield(). They might exist though
+// in previous versions depending on the build configuration of the GCC.
+#ifndef _GLIBCXX_USE_NANOSLEEP
+#define _GLIBCXX_USE_NANOSLEEP
+#endif // _GLIBCXX_USE_NANOSLEEP
+#ifndef _GLIBCXX_USE_SCHED_YIELD
+#define _GLIBCXX_USE_SCHED_YIELD
+#endif // _GLIBCXX_USE_SCHED_YIELD
+#endif // GCC_VERSION < 40800
+
+#if GCC_VERSION < 40700
+// Those appeared in 4.7 with full c++11 support
+#define final
+#define override
+#define thread_local __thread  // use GCC extension instead of C++11
+#endif // GCC_VERSION < 40700
+
+#endif // GCC_VERSION
+
+
+#endif // COMMON_CONFIG_HPP
index fe22aa1..8e093a6 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Helper class for parsing and storing configurations in JSON format.
  */
 
+#include "config.hpp"
 #include "log/logger.hpp"
 #include "config/configuration.hpp"
 #include "config/exception.hpp"
index 0d0b014..aa644b1 100644 (file)
@@ -39,7 +39,8 @@ namespace config {
  * e.g. syntax error
  */
 struct ConfigException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    ConfigException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 
index af48230..03513cc 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Dbus connection class
  */
 
+#include "config.hpp"
 #include "dbus/connection.hpp"
 #include "dbus/exception.hpp"
 #include "utils/callback-wrapper.hpp"
@@ -138,6 +139,8 @@ DbusConnection::DbusConnection(const std::string& address)
     const GDBusConnectionFlags flags =
         static_cast<GDBusConnectionFlags>(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
                                           G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION);
+    // TODO: this is possible deadlock if the dbus
+    // socket exists but there is no dbus-daemon
     mConnection = g_dbus_connection_new_for_address_sync(address.c_str(),
                                                          flags,
                                                          NULL,
index 21f0506..32cf87c 100644 (file)
@@ -37,35 +37,40 @@ namespace dbus {
  * Base class for dbus exceptions
  */
 struct DbusException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    DbusException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 /**
  * Dbus IO exception (connection failed, connection lost, etc)
  */
 struct DbusIOException: public DbusException {
-    using DbusException::DbusException;
+
+    DbusIOException(const std::string& error = "") : DbusException(error) {}
 };
 
 /**
  * Dbus operation failed exception
  */
 struct DbusOperationException: public DbusException {
-    using DbusException::DbusException;
+
+    DbusOperationException(const std::string& error = "") : DbusException(error) {}
 };
 
 /**
  * Dbus custom exception triggered by user logic
  */
 struct DbusCustomException: public DbusException {
-    using DbusException::DbusException;
+
+    DbusCustomException(const std::string& error = "") : DbusException(error) {}
 };
 
 /**
  * Dbus invalid argument exception
  */
 struct DbusInvalidArgumentException: public DbusException {
-    using DbusException::DbusException;
+
+    DbusInvalidArgumentException(const std::string& error = "") : DbusException(error) {}
 };
 
 
index 1fada98..a7eab47 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Implementation of the class wrapping connection to libvirtd
  */
 
+#include "config.hpp"
 #include "log/logger.hpp"
 #include "libvirt/helpers.hpp"
 #include "libvirt/connection.hpp"
@@ -33,6 +34,7 @@ namespace libvirt {
 
 
 LibvirtConnection::LibvirtConnection(const std::string& uri)
+    : mCon(NULL)
 {
     libvirtInitialize();
 
index 70cfa3d..e0e1520 100644 (file)
@@ -54,7 +54,7 @@ public:
     operator bool() const;
 
 private:
-    virConnectPtr mCon = NULL;
+    virConnectPtr mCon;
 };
 
 
index ae25a02..e813d83 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Implementation of the class wrapping libvirt domain
  */
 
+#include "config.hpp"
 #include "log/logger.hpp"
 #include "libvirt/domain.hpp"
 #include "libvirt/helpers.hpp"
@@ -33,7 +34,7 @@ namespace libvirt {
 
 
 LibvirtDomain::LibvirtDomain(const std::string& configXML)
-    : mCon(LIBVIRT_LXC_ADDRESS)
+    : mCon(LIBVIRT_LXC_ADDRESS), mDom(NULL)
 {
     mDom = virDomainDefineXML(mCon.get(), configXML.c_str());
 
index 4e22a3f..6da6d48 100644 (file)
@@ -55,7 +55,7 @@ public:
 
 private:
     LibvirtConnection mCon;
-    virDomainPtr mDom = NULL;
+    virDomainPtr mDom;
 };
 
 
index aea145e..f207b56 100644 (file)
@@ -36,7 +36,8 @@ namespace security_containers {
  * Base class for exceptions in libvirt
  */
 struct LibvirtOperationException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    LibvirtOperationException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 
index 36b49e5..7948a6a 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   A function helpers for the libvirt library
  */
 
+#include "config.hpp"
 #include "libvirt/helpers.hpp"
 #include "log/logger.hpp"
 
index fdb9126..8b0e37e 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Implementation of the class wrapping libvirt network
  */
 
+#include "config.hpp"
+
 #include "log/logger.hpp"
 #include "libvirt/network.hpp"
 #include "libvirt/helpers.hpp"
@@ -33,7 +35,7 @@ namespace libvirt {
 
 
 LibvirtNetwork::LibvirtNetwork(const std::string& configXML)
-    : mCon(LIBVIRT_LXC_ADDRESS)
+    : mCon(LIBVIRT_LXC_ADDRESS), mNet(NULL)
 {
     mNet = virNetworkDefineXML(mCon.get(), configXML.c_str());
 
index 5c5626e..92a73da 100644 (file)
@@ -52,7 +52,7 @@ public:
 
 private:
     LibvirtConnection mCon;
-    virNetworkPtr mNet = NULL;
+    virNetworkPtr mNet;
 };
 
 
index 7f43419..1f043c1 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Systemd journal backend for logger
  */
 
+#include "config.hpp"
 #include "log/backend-journal.hpp"
 
 #define SD_JOURNAL_SUPPRESS_LOCATION
index fe1c5bc..471106c 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Stderr backend for logger
  */
 
+#include "config.hpp"
 #include "log/backend-stderr.hpp"
 #include "log/formatter.hpp"
 
index 703fcfc..128f11f 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Console color for StderrBackend logger
  */
 
+#include "config.hpp"
 #include "log/ccolor.hpp"
 
 #include <stdio.h>
index ef93146..dc37458 100644 (file)
@@ -39,12 +39,12 @@ enum class Color : unsigned int {
     BLUE        = 94,
     MAGENTA     = 95,
     CYAN        = 96,
-    WHITE       = 97,
+    WHITE       = 97
 };
 
 enum class Attributes : unsigned int {
     DEFAULT     = 0,
-    BOLD        = 1,
+    BOLD        = 1
 };
 
 std::string getConsoleEscapeSequence(Attributes attr, Color color);
index 1bfe905..c0b0f7c 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Helper formatter for logger
  */
 
+#include "config.hpp"
 #include "log/formatter.hpp"
 #include "log/ccolor.hpp"
 
index 5fc3c2a..3897726 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Functions to handle LogLevel
  */
 
+#include "config.hpp"
 
 #include "log/level.hpp"
 
index f834557..83298ed 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Logger
  */
 
+#include "config.hpp"
 #include "log/logger.hpp"
 #include "log/formatter.hpp"
 #include "log/backend-null.hpp"
index d1e7084..3a35098 100644 (file)
@@ -22,7 +22,8 @@
  * @brief   Callback guard
  */
 
-#include "callback-guard.hpp"
+#include "config.hpp"
+#include "utils/callback-guard.hpp"
 #include "log/logger.hpp"
 
 #include <mutex>
@@ -36,6 +37,8 @@ namespace utils {
 // Reference counting class like shared_ptr but with the ability to wait for it.
 class CallbackGuard::SharedState {
 public:
+    SharedState() : mCounter(0) {}
+
     void inc()
     {
         std::unique_lock<std::mutex> lock(mMutex);
@@ -65,7 +68,7 @@ public:
 private:
     std::mutex mMutex;
     std::condition_variable mEmptyCondition;
-    long mCounter = 0;
+    long mCounter;
 };
 
 namespace {
index 366da77..e093f90 100644 (file)
@@ -36,7 +36,8 @@ namespace security_containers {
  * Base class for exceptions in utils
  */
 struct UtilsException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    UtilsException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 
index cc468e3..9f210f1 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Wait for file utility function
  */
 
+#include "config.hpp"
 #include "utils/exception.hpp"
 #include "utils/file-wait.hpp"
 
index c8e0029..6d2ce18 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   File utility functions
  */
 
+#include "config.hpp"
 #include "log/logger.hpp"
 #include "utils/fs.hpp"
 #include "utils/paths.hpp"
index 193a25f..6a60778 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   C++ wrapper of glib main loop
  */
 
+#include "config.hpp"
 #include "utils/glib-loop.hpp"
 #include "utils/callback-wrapper.hpp"
 
index a35aee5..2ae240d 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Synchronization latch
  */
 
+#include "config.hpp"
 #include "utils/latch.hpp"
 
 #include <cassert>
index 5f1d110..e357c71 100644 (file)
@@ -81,8 +81,10 @@ inline void removeDuplicateSlashes(std::string& path)
 
 inline void removeTrailingSlash(std::string& path)
 {
-    if (path.size() > 1 && *(path.end() - 1) == '/') {
-        path.pop_back();
+    size_t size = path.size();
+
+    if (size > 1 && path[size - 1] == '/') {
+        path.resize(size - 1);
     }
 }
 
index fa3f839..7fe08ad 100644 (file)
@@ -22,7 +22,8 @@
  * @brief   Synchronization latch
  */
 
-#include "typeinfo.hpp"
+#include "config.hpp"
+#include "utils/typeinfo.hpp"
 
 #include <string>
 #include <cxxabi.h>
index 714d987..4f66e37 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Dbus API for the Container Daemon
  */
 
+#include "config.hpp"
+
 #include "daemon-connection.hpp"
 #include "daemon-dbus-definitions.hpp"
 #include "exception.hpp"
index 9dd1c7b..d79762f 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Definition of the Daemon, implementation of the logic of the daemon.
  */
 
+#include "config.hpp"
+
 #include "daemon.hpp"
 
 #include "log/logger.hpp"
index 2cd4d12..898d202 100644 (file)
@@ -36,7 +36,8 @@ namespace container_daemon {
  * Base class for exceptions in Security Containers Container Daemon
  */
 struct ContainerDaemonException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    ContainerDaemonException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 
index 769de69..8dcdbad 100644 (file)
 #define LOG_TO_CONSOLE
 #endif
 
+#include "config.hpp"
+
+#include "exception.hpp"
+#include "runner.hpp"
+
 #include "log/logger.hpp"
 #include "log/backend-stderr.hpp"
 #include "log/backend-journal.hpp"
 #include "utils/typeinfo.hpp"
 
-#include "exception.hpp"
-#include "runner.hpp"
-
 #include <boost/program_options.hpp>
 #include <iostream>
 
index 5c5f9b0..13d3f21 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Definition of the Runner class, that manages daemon's lifetime
  */
 
+#include "config.hpp"
+
 #include "runner.hpp"
 #include "daemon.hpp"
 
index 657ab4f..ed7b878 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Implementation of class for administrating one container
  */
 
+#include "config.hpp"
+
 #include "container-admin.hpp"
 #include "exception.hpp"
 
@@ -72,7 +74,7 @@ ContainerAdmin::ContainerAdmin(ContainerConfig& config)
       mDetachOnExit(false),
       mLifecycleCallbackId(-1),
       mRebootCallbackId(-1),
-      mNextIdForListener(0)
+      mNextIdForListener(1)
 {
     LOGD(mId << ": Instantiating ContainerAdmin object");
 
@@ -185,10 +187,10 @@ void ContainerAdmin::stop()
         }
     };
 
-    ListenerId id = registerListener(setStopped, nullptr);
+    ListenerId id = registerLifecycleListener(setStopped, nullptr);
     shutdown();
     bool stopped = stoppedOccured.wait(SHUTDOWN_WAIT);
-    removeListener<LifecycleListener>(id);
+    removeListener(id);
 
     if (!stopped) {
         LOGW(mId << ": Gracefull shutdown timed out, the container is still running, destroying");
@@ -405,6 +407,39 @@ std::int64_t ContainerAdmin::getSchedulerQuota()
     return quota;
 }
 
+ContainerAdmin::ListenerId ContainerAdmin::registerLifecycleListener(const ContainerAdmin::LifecycleListener& listener,
+                                                                     const utils::CallbackGuard::Tracker& tracker)
+{
+
+    utils::CallbackWrapper<LifecycleListener> wrap(listener, tracker);
+
+    std::unique_lock<std::mutex> lock(mListenerMutex);
+    unsigned int id = mNextIdForListener++;
+    mLifecycleListeners.insert(LifecycleListenerMap::value_type(id, std::move(wrap)));
+
+    return id;
+}
+
+ContainerAdmin::ListenerId ContainerAdmin::registerRebootListener(const ContainerAdmin::RebootListener& listener,
+                                                                  const utils::CallbackGuard::Tracker& tracker)
+{
+
+    utils::CallbackWrapper<RebootListener> wrap(listener, tracker);
+
+    std::unique_lock<std::mutex> lock(mListenerMutex);
+    unsigned int id = mNextIdForListener++;
+    mRebootListeners.insert(RebootListenerMap::value_type(id, std::move(wrap)));
+
+    return id;
+}
+
+void ContainerAdmin::removeListener(const ContainerAdmin::ListenerId id)
+{
+    std::unique_lock<std::mutex> lock(mListenerMutex);
+    mLifecycleListeners.erase(id);
+    mRebootListeners.erase(id);
+}
+
 int ContainerAdmin::libvirtLifecycleCallback(virConnectPtr /*con*/,
                                              virDomainPtr /*dom*/,
                                              int event,
@@ -444,19 +479,5 @@ void ContainerAdmin::libvirtRebootCallback(virConnectPtr /*con*/,
     }
 }
 
-template<>
-ContainerAdmin::ListenerMap<ContainerAdmin::LifecycleListener>&
-ContainerAdmin::getListenerMap<ContainerAdmin::LifecycleListener>()
-{
-    return mLifecycleListeners;
-}
-
-template<>
-ContainerAdmin::ListenerMap<ContainerAdmin::RebootListener>&
-ContainerAdmin::getListenerMap<ContainerAdmin::RebootListener>()
-{
-    return mRebootListeners;
-}
-
 
 } // namespace security_containers
index ed1e00e..8b2ec42 100644 (file)
@@ -58,6 +58,11 @@ public:
     typedef unsigned int ListenerId;
 
     /**
+     * An invalid ListenerId value.
+     */
+    static const ListenerId LISTENER_ID_INVALID = 0;
+
+    /**
      * A function type used for the lifecycle listener
      */
     typedef std::function<void(const int eventId, const int detailId)> LifecycleListener;
@@ -145,20 +150,28 @@ public:
     std::int64_t getSchedulerQuota();
 
     /**
-     * Sets a listener for a specific event.
+     * Sets a listener for a lifecycle event.
      * It's a caller's responsibility to remove the listener
      * prior to destroying the object.
      *
      * @return listener ID that can be used to remove.
      */
-    template <typename Listener>
-    ListenerId registerListener(const Listener& listener,
-                                const utils::CallbackGuard::Tracker& tracker);
+    ListenerId registerLifecycleListener(const LifecycleListener& listener,
+                                         const utils::CallbackGuard::Tracker& tracker);
+
+    /**
+     * Sets a listener for a reboot event.
+     * It's a caller's responsibility to remove the listener
+     * prior to destroying the object.
+     *
+     * @return listener ID that can be used to remove.
+     */
+    ListenerId registerRebootListener(const RebootListener& listener,
+                                      const utils::CallbackGuard::Tracker& tracker);
 
     /**
      * Remove a previously registered listener.
      */
-    template <typename Listener>
     void removeListener(const ListenerId id);
 
 private:
@@ -189,42 +202,15 @@ private:
 
     // for handling external listeners triggered from libvirt callbacks
     // TODO, the Listener type might not be unique, reimplement using proper listeners
-    template <typename Listener>
-    using ListenerMap = std::map<ListenerId, utils::CallbackWrapper<Listener>>;
+    typedef std::map<ListenerId, utils::CallbackWrapper<LifecycleListener>> LifecycleListenerMap;
+    typedef std::map<ListenerId, utils::CallbackWrapper<RebootListener>> RebootListenerMap;
 
     std::mutex mListenerMutex;
-    std::atomic<unsigned int> mNextIdForListener;
-    ListenerMap<LifecycleListener> mLifecycleListeners;
-    ListenerMap<RebootListener> mRebootListeners;
-
-    template <typename Listener>
-    ListenerMap<Listener>& getListenerMap();
+    unsigned int mNextIdForListener;
+    LifecycleListenerMap mLifecycleListeners;
+    RebootListenerMap mRebootListeners;
 };
 
-template <typename Listener>
-unsigned int ContainerAdmin::registerListener(const Listener& listener,
-                                              const utils::CallbackGuard::Tracker& tracker)
-{
-
-    ListenerMap<Listener>& map = getListenerMap<Listener>();
-    unsigned int id = mNextIdForListener++;
-    utils::CallbackWrapper<Listener> wrap(listener, tracker);
-
-    std::unique_lock<std::mutex> lock(mListenerMutex);
-    map.emplace(id, std::move(wrap));
-
-    return id;
-}
-
-template <typename Listener>
-void ContainerAdmin::removeListener(const ContainerAdmin::ListenerId id)
-{
-    ListenerMap<Listener>& map = getListenerMap<Listener>();
-
-    std::unique_lock<std::mutex> lock(mListenerMutex);
-    map.erase(id);
-}
-
 
 } // namespace security_containers
 
index 71594f6..9b8848d 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Implementation of a class for communication transport between container and server
  */
 
+#include "config.hpp"
+
 #include "container-connection-transport.hpp"
 #include "exception.hpp"
 
index b1c1df0..9f8a417 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Implementation of a class for communication between container and server
  */
 
+#include "config.hpp"
+
 #include "container-connection.hpp"
 #include "container-dbus-definitions.hpp"
 #include "exception.hpp"
index 6ce6f24..bb8bcd3 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Implementation of class for managing one container
  */
 
+#include "config.hpp"
+
 #include "container.hpp"
 
 #include "log/logger.hpp"
index 20c1f98..3b14c1b 100644 (file)
  * @brief   Definition of the class for managing containers
  */
 
+#include "config.hpp"
+
 #include "containers-manager.hpp"
 #include "container-admin.hpp"
 #include "exception.hpp"
+
 #include "utils/paths.hpp"
 #include "log/logger.hpp"
 
@@ -54,7 +57,7 @@ ContainersManager::ContainersManager(const std::string& managerConfigPath): mDet
         LOGD("Creating Container " << containerConfigPath);
         std::unique_ptr<Container> c(new Container(containerConfigPath));
         std::string id = c->getId();
-        mContainers.emplace(id, std::move(c));
+        mContainers.insert(ContainerMap::value_type(id, std::move(c)));
     }
 
     LOGD("ContainersManager object instantiated");
index 3291f79..37fa45e 100644 (file)
@@ -36,7 +36,8 @@ namespace security_containers {
  * Base class for exceptions in Security Containers Server
  */
 struct ServerException: public SecurityContainersException {
-    using SecurityContainersException::SecurityContainersException;
+
+    ServerException(const std::string& error = "") : SecurityContainersException(error) {}
 };
 
 /**
@@ -44,14 +45,16 @@ struct ServerException: public SecurityContainersException {
  * e.g. start, stop a container
  */
 struct ContainerOperationException: public ServerException {
-    using ServerException::ServerException;
+
+    ContainerOperationException(const std::string& error = "") : ServerException(error) {}
 };
 
 /**
  * Exception during performing an operation on a container connection
  */
 struct ContainerConnectionException: public ServerException {
-    using ServerException::ServerException;
+
+    ContainerConnectionException(const std::string& error = "") : ServerException(error) {}
 };
 
 /**
@@ -59,7 +62,8 @@ struct ContainerConnectionException: public ServerException {
 * e.g. create channel, register callback etc.
 */
 struct InputMonitorException: public ServerException {
-    using ServerException::ServerException;
+
+    InputMonitorException(const std::string& error = "") : ServerException(error) {}
 };
 
 
index 395d936..521f683 100644 (file)
 #define LOG_TO_CONSOLE
 #endif
 
+#include "config.hpp"
+
+#include "exception.hpp"
+#include "server.hpp"
+
 #include "log/logger.hpp"
 #include "log/backend-stderr.hpp"
 #include "log/backend-journal.hpp"
 #include "utils/typeinfo.hpp"
-#include "exception.hpp"
-#include "server.hpp"
 
 #include <boost/program_options.hpp>
 #include <iostream>
index 82d0a6d..d04d562 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Server class definition
  */
 
+#include "config.hpp"
+
 #include "server.hpp"
 #include "containers-manager.hpp"
 #include "exception.hpp"
index cbb84eb..470cbfd 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit test of ConfigurationBase
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 #include "config/configuration.hpp"
 
index f056765..59b6adb 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Example dbus api client
  */
 
+#include "config.hpp"
+
 #include "dbus/test-client.hpp"
 #include "dbus/test-common.hpp"
 
index aaa99b4..e245d4d 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Example dbus api server
  */
 
+#include "config.hpp"
+
 #include "dbus/test-server.hpp"
 #include "dbus/test-common.hpp"
 
index 6687f03..eed9af7 100644 (file)
@@ -22,6 +22,7 @@
  * @brief   Dbus connection unit tests
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 #include "dbus/test-server.hpp"
 #include "dbus/test-client.hpp"
index c4ba8ff..26f649b 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the LibvirtConnection class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "libvirt/connection.hpp"
index 2ecbcc3..4046460 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the LibvirtDomain class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "libvirt/domain.hpp"
index 7f862f7..04e396d 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the LibvirtNetwork class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "libvirt/network.hpp"
index 696bda5..b4aeb5d 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the log utility
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 #include "log/logger.hpp"
 #include "log/formatter.hpp"
index 5d4d09c..7d534d9 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the ContainerAdmin class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "container-admin.hpp"
@@ -96,7 +97,7 @@ BOOST_AUTO_TEST_CASE(MissingConfigTest)
 BOOST_AUTO_TEST_CASE(StartTest)
 {
     utils::Latch booted;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -105,7 +106,7 @@ BOOST_AUTO_TEST_CASE(StartTest)
             booted.set();
         }
     };
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(bootedListener, mGuard.spawn()));
+    BOOST_REQUIRE_NO_THROW(id = ca.registerLifecycleListener(bootedListener, mGuard.spawn()));
 
     BOOST_REQUIRE_NO_THROW(ca.start());
     ensureStarted();
@@ -113,13 +114,13 @@ BOOST_AUTO_TEST_CASE(StartTest)
     BOOST_CHECK(booted.wait(WAIT_TIMEOUT));
     BOOST_CHECK(ca.isRunning());
 
-    BOOST_REQUIRE_NO_THROW(ca.removeListener<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(ShutdownTest)
 {
     utils::Latch shutdown;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -132,20 +133,20 @@ BOOST_AUTO_TEST_CASE(ShutdownTest)
     BOOST_REQUIRE_NO_THROW(ca.start());
     ensureStarted();
     BOOST_REQUIRE(ca.isRunning());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(shutdownListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(DestroyTest)
 {
     utils::Latch destroyed;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -158,20 +159,20 @@ BOOST_AUTO_TEST_CASE(DestroyTest)
     BOOST_REQUIRE_NO_THROW(ca.start());
     ensureStarted();
     BOOST_REQUIRE(ca.isRunning());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(destroyedListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(StopShutdownTest)
 {
     utils::Latch shutdown;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -184,21 +185,21 @@ BOOST_AUTO_TEST_CASE(StopShutdownTest)
     BOOST_REQUIRE_NO_THROW(ca.start());
     ensureStarted();
     BOOST_REQUIRE(ca.isRunning());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(shutdownListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    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::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_NO_SHUTDOWN_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -211,20 +212,20 @@ BOOST_AUTO_TEST_CASE(StopDestroyTest)
     BOOST_REQUIRE_NO_THROW(ca.start());
     ensureStarted();
     BOOST_REQUIRE(ca.isRunning());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(destroyedListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(SuspendTest)
 {
     utils::Latch paused;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -237,20 +238,20 @@ BOOST_AUTO_TEST_CASE(SuspendTest)
     BOOST_REQUIRE_NO_THROW(ca.start())
     ensureStarted();
     BOOST_REQUIRE(ca.isRunning());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(pausedListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(ResumeTest)
 {
     utils::Latch unpaused;
-    ContainerAdmin::ListenerId id;
+    ContainerAdmin::ListenerId id = ContainerAdmin::LISTENER_ID_INVALID;
     ContainerConfig config; config.parseFile(TEST_CONFIG_PATH);
     ContainerAdmin ca(config);
 
@@ -265,14 +266,14 @@ BOOST_AUTO_TEST_CASE(ResumeTest)
     BOOST_REQUIRE(ca.isRunning());
     BOOST_REQUIRE_NO_THROW(ca.suspend())
     BOOST_REQUIRE(ca.isPaused());
-    BOOST_REQUIRE_NO_THROW(id = ca.registerListener(unpausedListener, mGuard.spawn()));
+    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<ContainerAdmin::LifecycleListener>(id));
+    BOOST_REQUIRE_NO_THROW(ca.removeListener(id));
 }
 
 BOOST_AUTO_TEST_CASE(SchedulerLevelTest)
index 5f485f4..4b757a3 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the ContainerConnection class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "container-connection.hpp"
index 09aa059..da6479e 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the Container class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "container.hpp"
index b0a96db..7004148 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the ContainersManager class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "containers-manager.hpp"
index 31b02ea..a31d783 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the InputMonitor class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "input-monitor.hpp"
index a185cf0..d7393b7 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of the Server class
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "server.hpp"
@@ -32,6 +33,7 @@
 #include <string>
 #include <future>
 
+
 BOOST_AUTO_TEST_SUITE(ServerSuite)
 
 using namespace security_containers;
index 0108456..de98558 100644 (file)
@@ -23,6 +23,8 @@
  * @brief   Main file for the Security Containers Daemon unit tests
  */
 
+#include "config.hpp"
+
 #include "log/logger.hpp"
 #include "log/backend-stderr.hpp"
 
index ca4fec6..077e19a 100644 (file)
@@ -22,6 +22,8 @@
  * @brief   Starts external daemon in constructor, stops it in destructor
  */
 
+#include "config.hpp"
+
 #include "utils/scoped-daemon.hpp"
 
 #include "log/logger.hpp"
index 429ed79..6a503b0 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of callback guard
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "utils/callback-guard.hpp"
@@ -71,7 +72,7 @@ BOOST_AUTO_TEST_CASE(ThreadTest)
     Latch trackerCreated;
     Latch trackerCanBeDestroyed;
 
-    std::future<bool> future = std::async(std::launch::async, [&] {
+    std::future<bool> future = std::async(std::launch::async, [&]() -> bool {
             CallbackGuard::Tracker tracker = guard.spawn();
             trackerCreated.set();
             if (!trackerCanBeDestroyed.wait(TIMEOUT)) {
index 845b0a4..adc8797 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of utils
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "utils/fs.hpp"
index f34fdbc..63fc3ee 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of glib-loop
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "utils/latch.hpp"
@@ -50,7 +51,7 @@ BOOST_AUTO_TEST_CASE(GlibTimerEventTest)
     CallbackGuard guard;
     Latch latch;
 
-    Glib::OnTimerEventCallback callback = [&] {
+    Glib::OnTimerEventCallback callback = [&]()->bool {
         static unsigned int counter = 0;
         latch.set();
         if (++counter >= TIMER_NUMBER) {
index 087a7ad..e32424b 100644 (file)
@@ -23,6 +23,7 @@
  * @brief   Unit tests of utils
  */
 
+#include "config.hpp"
 #include "ut.hpp"
 
 #include "utils/paths.hpp"