+++ /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 configuration
- */
-
-
-#ifndef COMMON_CONFIG_EXCEPTION_HPP
-#define COMMON_CONFIG_EXCEPTION_HPP
-
-#include "base-exception.hpp"
-
-
-namespace security_containers {
-namespace config {
-
-
-/**
- * Base class for exceptions in configuration.
- * Error occured during a config file parsing,
- * e.g. syntax error
- */
-struct ConfigException: public SecurityContainersException {
-
- ConfigException(const std::string& error = "") : SecurityContainersException(error) {}
-};
-
-
-} // namespace config
-} // namespace security_containers
-
-
-#endif // COMMON_CONFIG_EXCEPTION_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz (p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Macros for registering configuration fields
- */
-
-#ifndef COMMON_CONFIG_FIELDS_HPP
-#define COMMON_CONFIG_FIELDS_HPP
-
-#include <boost/preprocessor/variadic/to_list.hpp>
-#include <boost/preprocessor/list/for_each.hpp>
-
-#if BOOST_PP_VARIADICS != 1
-#error variadic macros not supported
-#endif
-
-/**
- * Use this macro to register config fields.
- *
- * Example:
- * struct Foo
- * {
- * std::string bar;
- * std::vector<int> tab;
- *
- * // SubConfigA must also register config fields
- * SubConfigA sub_a;
- *
- * // SubConfigB must also register config fields
- * std::vector<SubConfigB> tab_sub;
- *
- * CONFIG_REGISTER
- * (
- * bar,
- * tab,
- * sub_a
- * )
- * };
- */
-#define CONFIG_REGISTER(...) \
- template<typename Visitor> \
- void accept(Visitor v) { \
- GENERATE_ELEMENTS__(__VA_ARGS__) \
- } \
- template<typename Visitor> \
- void accept(Visitor v) const { \
- GENERATE_ELEMENTS__(__VA_ARGS__) \
- } \
-
-#define GENERATE_ELEMENTS__(...) \
- BOOST_PP_LIST_FOR_EACH(GENERATE_ELEMENT__, \
- _, \
- BOOST_PP_VARIADIC_TO_LIST(__VA_ARGS__)) \
-
-#define GENERATE_ELEMENT__(r, _, element) \
- v.visit(#element, element); \
-
-
-#endif // COMMON_CONFIG_FIELDS_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz (p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief JSON visitor
- */
-
-#ifndef COMMON_CONFIG_FROM_JSON_VISITOR_HPP
-#define COMMON_CONFIG_FROM_JSON_VISITOR_HPP
-
-#include "config/is-visitable.hpp"
-#include "config/exception.hpp"
-
-#include <json/json.h>
-#include <string>
-#include <vector>
-
-namespace security_containers {
-namespace config {
-
-class FromJsonVisitor {
-public:
- explicit FromJsonVisitor(const std::string& jsonString)
- : mObject(nullptr)
- {
- mObject = json_tokener_parse(jsonString.c_str());
- if (mObject == nullptr) {
- throw ConfigException("Json parsing error");
- }
- }
-
- FromJsonVisitor(const FromJsonVisitor& visitor)
- : mObject(json_object_get(visitor.mObject))
- {
- }
-
- ~FromJsonVisitor()
- {
- json_object_put(mObject);
- }
-
- template<class T>
- void visit(const std::string& name, T& value)
- {
- json_object* object = nullptr;
- if (!json_object_object_get_ex(mObject, name.c_str(), &object)) {
- throw ConfigException("Missing field '" + name + "' in json");
- }
- fromJsonObject(object, value);
- }
-
-private:
- json_object* mObject;
-
- FromJsonVisitor& operator=(const FromJsonVisitor&) = delete;
-
- explicit FromJsonVisitor(json_object* object)
- : mObject(json_object_get(object))
- {
- }
-
- static void checkType(json_object* object, json_type type)
- {
- if (type != json_object_get_type(object)) {
- throw ConfigException("Invalid field type");
- }
- }
-
- static void fromJsonObject(json_object* object, int& value)
- {
- checkType(object, json_type_int);
- std::int64_t value64 = json_object_get_int64(object);
- if (value64 > INT32_MAX || value64 < INT32_MIN) {
- throw ConfigException("Value out of range");
- }
- value = static_cast<int>(value64);
- }
-
- static void fromJsonObject(json_object* object, std::int64_t& value)
- {
- checkType(object, json_type_int);
- value = json_object_get_int64(object);
- }
-
- static void fromJsonObject(json_object* object, bool& value)
- {
- checkType(object, json_type_boolean);
- value = json_object_get_boolean(object);
- }
-
- static void fromJsonObject(json_object* object, double& value)
- {
- checkType(object, json_type_double);
- value = json_object_get_double(object);
- }
-
- static void fromJsonObject(json_object* object, std::string& value)
- {
- checkType(object, json_type_string);
- value = json_object_get_string(object);
- }
-
- template<class T>
- static void fromJsonObject(json_object* object, std::vector<T>& value)
- {
- checkType(object, json_type_array);
- int length = json_object_array_length(object);
- value.resize(static_cast<size_t>(length));
- for (int i = 0; i < length; ++i) {
- fromJsonObject(json_object_array_get_idx(object, i), value[static_cast<size_t>(i)]);
- }
- }
-
- template<class T, class = typename std::enable_if<isVisitable<T>::value>::type>
- static void fromJsonObject(json_object* object, T& value)
- {
- checkType(object, json_type_object);
- FromJsonVisitor visitor(object);
- value.accept(visitor);
- }
-};
-
-} // namespace config
-} // namespace security_containers
-
-#endif // COMMON_CONFIG_FROM_JSON_VISITOR_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz (p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Internal configuration helper
- */
-
-#ifndef COMMON_CONFIG_IS_VISITABLE_HPP
-#define COMMON_CONFIG_IS_VISITABLE_HPP
-
-#include <type_traits>
-
-namespace security_containers {
-namespace config {
-
-template <typename T>
-struct isVisitableHelper__ {
- struct Visitor {};
-
- template <typename C> static std::true_type
- test(decltype(std::declval<C>().template accept(Visitor()))*);
-
- template <typename C> static std::false_type
- test(...);
-
- static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>::value;
-};
-
-/**
- * Helper for compile-time checking against existance of template method 'accept'.
- */
-template <typename T>
-struct isVisitable : public std::integral_constant<bool, isVisitableHelper__<T>::value> {};
-
-} // namespace config
-} // namespace security_containers
-
-#endif // COMMON_CONFIG_IS_VISITABLE_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz (p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Configuration management functions
- */
-
-#ifndef COMMON_CONFIG_MANAGER_HPP
-#define COMMON_CONFIG_MANAGER_HPP
-
-#include "config/to-json-visitor.hpp"
-#include "config/from-json-visitor.hpp"
-#include "config/is-visitable.hpp"
-#include "utils/fs.hpp"
-
-namespace security_containers {
-namespace config {
-
-template <class Config>
-void loadFromString(const std::string& jsonString, Config& config)
-{
- static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
-
- FromJsonVisitor visitor(jsonString);
- config.accept(visitor);
-}
-
-template <class Config>
-std::string saveToString(const Config& config)
-{
- static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
-
- ToJsonVisitor visitor;
- config.accept(visitor);
- return visitor.toString();
-}
-
-template <class Config>
-void loadFromFile(const std::string& filename, Config& config)
-{
- std::string content;
- if (!utils::readFileContent(filename, content)) {
- throw ConfigException("Could not load " + filename);
- }
- loadFromString(content, config);
-}
-
-template <class Config>
-void saveToFile(const std::string& filename, const Config& config)
-{
- const std::string content = saveToString(config);
- if (!utils::saveFileContent(filename, content)) {
- throw ConfigException("Could not save " + filename);
- }
-}
-
-} // namespace config
-} // namespace security_containers
-
-#endif // COMMON_CONFIG_MANAGER_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz (p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief JSON visitor
- */
-
-#ifndef COMMON_CONFIG_TO_JSON_VISITOR_HPP
-#define COMMON_CONFIG_TO_JSON_VISITOR_HPP
-
-#include "config/is-visitable.hpp"
-
-#include <json/json.h>
-#include <string>
-#include <vector>
-
-namespace security_containers {
-namespace config {
-
-class ToJsonVisitor {
-
-public:
- ToJsonVisitor()
- : mObject(json_object_new_object())
- {
- }
-
- ToJsonVisitor(const ToJsonVisitor& visitor)
- : mObject(json_object_get(visitor.mObject))
- {
- }
-
- ~ToJsonVisitor()
- {
- json_object_put(mObject);
- }
-
- std::string toString() const
- {
- return json_object_to_json_string(mObject);
- }
-
- template<class T>
- void visit(const std::string& name, const T& value)
- {
- json_object_object_add(mObject, name.c_str(), toJsonObject(value));
- }
-private:
- json_object* mObject;
-
- ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
-
- json_object* detach()
- {
- json_object* ret = mObject;
- mObject = nullptr;
- return ret;
- }
-
- static json_object* toJsonObject(int value)
- {
- return json_object_new_int(value);
- }
-
- static json_object* toJsonObject(std::int64_t value)
- {
- return json_object_new_int64(value);
- }
-
- static json_object* toJsonObject(bool value)
- {
- return json_object_new_boolean(value);
- }
-
- static json_object* toJsonObject(double value)
- {
- return json_object_new_double(value);
- }
-
- static json_object* toJsonObject(const std::string& value)
- {
- return json_object_new_string(value.c_str());
- }
-
- template<class T>
- static json_object* toJsonObject(const std::vector<T>& value)
- {
- json_object* array = json_object_new_array();
- for (const T& v : value) {
- json_object_array_add(array, toJsonObject(v));
- }
- return array;
- }
-
- template<class T, class = typename std::enable_if<isVisitable<T>::value>::type>
- static json_object* toJsonObject(const T& value)
- {
- ToJsonVisitor visitor;
- value.accept(visitor);
- return visitor.detach();
- }
-};
-
-} // namespace config
-} // namespace security_containers
-
-#endif // COMMON_CONFIG_TO_JSON_VISITOR_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz <p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Dbus connection class
- */
-
-#include "config.hpp"
-#include "dbus/connection.hpp"
-#include "dbus/exception.hpp"
-#include "utils/callback-wrapper.hpp"
-#include "utils/scoped-gerror.hpp"
-#include "log/logger.hpp"
-
-using namespace security_containers::utils;
-
-namespace security_containers {
-namespace dbus {
-
-
-namespace {
-
-const std::string SYSTEM_BUS_ADDRESS = "unix:path=/var/run/dbus/system_bus_socket";
-const std::string INTROSPECT_INTERFACE = "org.freedesktop.DBus.Introspectable";
-const std::string INTROSPECT_METHOD = "Introspect";
-
-const int CALL_METHOD_TIMEOUT_MS = 1000;
-
-class MethodResultBuilderImpl : public MethodResultBuilder {
-public:
- MethodResultBuilderImpl(GDBusMethodInvocation* invocation)
- : mInvocation(invocation), mResultSet(false) {}
- ~MethodResultBuilderImpl()
- {
- if (!mResultSet) {
- setError("org.freedesktop.DBus.Error.UnknownMethod", "Not implemented");
- }
- }
- void set(GVariant* parameters)
- {
- g_dbus_method_invocation_return_value(mInvocation, parameters);
- mResultSet = true;
- }
- void setVoid()
- {
- set(NULL);
- }
- void setError(const std::string& name, const std::string& message)
- {
- g_dbus_method_invocation_return_dbus_error(mInvocation, name.c_str(), message.c_str());
- mResultSet = true;
- }
-private:
- GDBusMethodInvocation* mInvocation;
- bool mResultSet;
-};
-
-void throwDbusException(const ScopedGError& e)
-{
- if (e->domain == g_io_error_quark()) {
- if (e->code == G_IO_ERROR_DBUS_ERROR) {
- throw DbusCustomException(e->message);
- } else {
- throw DbusIOException(e->message);
- }
- } else if (e->domain == g_dbus_error_quark()) {
- throw DbusOperationException(e->message);
- } else if (e->domain == g_markup_error_quark()) {
- throw DbusInvalidArgumentException(e->message);
- } else {
- throw DbusException(e->message);
- }
-}
-
-class AsyncMethodCallResultImpl : public AsyncMethodCallResult {
-public:
- AsyncMethodCallResultImpl(GVariant* result, const ScopedGError& error)
- : mResult(result), mError(error) {}
- ~AsyncMethodCallResultImpl()
- {
- if (mResult) {
- g_variant_unref(mResult);
- }
- }
- GVariant* get()
- {
- if (mError) {
- throwDbusException(mError);
- }
- return mResult;
- }
-private:
- GVariant* mResult;
- const ScopedGError& mError;
-};
-
-} // namespace
-
-DbusConnection::Pointer DbusConnection::create(const std::string& address)
-{
- return Pointer(new DbusConnection(address));
-}
-
-DbusConnection::Pointer DbusConnection::createSystem()
-{
- return create(SYSTEM_BUS_ADDRESS);
-}
-
-DbusConnection::DbusConnection(const std::string& address)
- : mConnection(NULL)
- , mNameId(0)
-{
- ScopedGError error;
- 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,
- NULL,
- &error);
- if (error) {
- error.strip();
- LOGE("Could not connect to " << address << "; " << error);
- throwDbusException(error);
- }
-}
-
-DbusConnection::~DbusConnection()
-{
- if (mNameId) {
- g_bus_unown_name(mNameId);
- }
- g_object_unref(mConnection);
- LOGT("Connection deleted");
-}
-
-void DbusConnection::setName(const std::string& name,
- const VoidCallback& onNameAcquired,
- const VoidCallback& onNameLost)
-{
- mNameId = g_bus_own_name_on_connection(mConnection,
- name.c_str(),
- G_BUS_NAME_OWNER_FLAGS_NONE,
- &DbusConnection::onNameAcquired,
- &DbusConnection::onNameLost,
- utils::createCallbackWrapper(
- NameCallbacks(onNameAcquired, onNameLost),
- mGuard.spawn()),
- &utils::deleteCallbackWrapper<NameCallbacks>);
-}
-
-void DbusConnection::onNameAcquired(GDBusConnection*, const gchar* name, gpointer userData)
-{
- LOGD("Name acquired " << name);
- const NameCallbacks& callbacks = utils::getCallbackFromPointer<NameCallbacks>(userData);
- if (callbacks.nameAcquired) {
- callbacks.nameAcquired();
- }
-}
-
-void DbusConnection::onNameLost(GDBusConnection*, const gchar* name, gpointer userData)
-{
- LOGD("Name lost " << name);
- const NameCallbacks& callbacks = utils::getCallbackFromPointer<NameCallbacks>(userData);
- if (callbacks.nameLost) {
- callbacks.nameLost();
- }
-}
-
-void DbusConnection::emitSignal(const std::string& objectPath,
- const std::string& interface,
- const std::string& name,
- GVariant* parameters)
-{
- ScopedGError error;
- g_dbus_connection_emit_signal(mConnection,
- NULL,
- objectPath.c_str(),
- interface.c_str(),
- name.c_str(),
- parameters,
- &error);
- if (error) {
- error.strip();
- LOGE("Emit signal failed; " << error);
- throwDbusException(error);
- }
-}
-
-void DbusConnection::signalSubscribe(const SignalCallback& callback,
- const std::string& senderBusName)
-{
- g_dbus_connection_signal_subscribe(mConnection,
- senderBusName.empty() ? NULL : senderBusName.c_str(),
- NULL,
- NULL,
- NULL,
- NULL,
- G_DBUS_SIGNAL_FLAGS_NONE,
- &DbusConnection::onSignal,
- utils::createCallbackWrapper(callback, mGuard.spawn()),
- &utils::deleteCallbackWrapper<SignalCallback>);
-}
-
-void DbusConnection::onSignal(GDBusConnection*,
- const gchar* sender,
- const gchar* object,
- const gchar* interface,
- const gchar* name,
- GVariant* parameters,
- gpointer userData)
-{
- const SignalCallback& callback = utils::getCallbackFromPointer<SignalCallback>(userData);
-
- LOGD("Signal: " << sender << "; " << object << "; " << interface << "; " << name);
-
- if (callback) {
- callback(sender, object, interface, name, parameters);
- }
-}
-
-std::string DbusConnection::introspect(const std::string& busName, const std::string& objectPath)
-{
- GVariantPtr result = DbusConnection::callMethod(busName,
- objectPath,
- INTROSPECT_INTERFACE,
- INTROSPECT_METHOD,
- NULL,
- "(s)");
- const gchar* xml;
- g_variant_get(result.get(), "(&s)", &xml);
- return xml;
-}
-
-void DbusConnection::registerObject(const std::string& objectPath,
- const std::string& objectDefinitionXml,
- const MethodCallCallback& callback)
-{
- ScopedGError error;
- GDBusNodeInfo* nodeInfo = g_dbus_node_info_new_for_xml(objectDefinitionXml.c_str(), &error);
- if (nodeInfo != NULL && (nodeInfo->interfaces == NULL ||
- nodeInfo->interfaces[0] == NULL ||
- nodeInfo->interfaces[1] != NULL)) {
- g_dbus_node_info_unref(nodeInfo);
- g_set_error(&error,
- G_MARKUP_ERROR,
- G_MARKUP_ERROR_INVALID_CONTENT,
- "Expected exactly one interface");
- }
- if (error) {
- error.strip();
- LOGE("Invalid xml; " << error);
- throwDbusException(error);
- }
- GDBusInterfaceInfo* interfaceInfo = nodeInfo->interfaces[0];
-
- GDBusInterfaceVTable vtable;
- vtable.method_call = &DbusConnection::onMethodCall;
- vtable.get_property = NULL;
- vtable.set_property = NULL;
-
- g_dbus_connection_register_object(mConnection,
- objectPath.c_str(),
- interfaceInfo,
- &vtable,
- utils::createCallbackWrapper(callback, mGuard.spawn()),
- &utils::deleteCallbackWrapper<MethodCallCallback>,
- &error);
- g_dbus_node_info_unref(nodeInfo);
- if (error) {
- error.strip();
- LOGE("Register object failed; " << error);
- throwDbusException(error);
- }
-}
-
-void DbusConnection::onMethodCall(GDBusConnection*,
- const gchar*,
- const gchar* objectPath,
- const gchar* interface,
- const gchar* method,
- GVariant* parameters,
- GDBusMethodInvocation* invocation,
- gpointer userData)
-{
- const MethodCallCallback& callback = utils::getCallbackFromPointer<MethodCallCallback>(userData);
-
- LOGD("MethodCall: " << objectPath << "; " << interface << "; " << method);
-
- MethodResultBuilder::Pointer resultBuilder(new MethodResultBuilderImpl(invocation));
- if (callback) {
- callback(objectPath, interface, method, parameters, resultBuilder);
- }
-}
-
-GVariantPtr DbusConnection::callMethod(const std::string& busName,
- const std::string& objectPath,
- const std::string& interface,
- const std::string& method,
- GVariant* parameters,
- const std::string& replyType)
-{
- ScopedGError error;
- GVariant* result = g_dbus_connection_call_sync(mConnection,
- busName.c_str(),
- objectPath.c_str(),
- interface.c_str(),
- method.c_str(),
- parameters,
- replyType.empty() ? NULL
- : G_VARIANT_TYPE(replyType.c_str()),
- G_DBUS_CALL_FLAGS_NONE,
- CALL_METHOD_TIMEOUT_MS,
- NULL,
- &error);
- if (error) {
- error.strip();
- LOGE("Call method failed; " << error);
- throwDbusException(error);
- }
- return GVariantPtr(result, g_variant_unref);
-}
-
-void DbusConnection::callMethodAsync(const std::string& busName,
- const std::string& objectPath,
- const std::string& interface,
- const std::string& method,
- GVariant* parameters,
- const std::string& replyType,
- const AsyncMethodCallCallback& callback)
-{
- g_dbus_connection_call(mConnection,
- busName.c_str(),
- objectPath.c_str(),
- interface.c_str(),
- method.c_str(),
- parameters,
- replyType.empty() ? NULL
- : G_VARIANT_TYPE(replyType.c_str()),
- G_DBUS_CALL_FLAGS_NONE,
- CALL_METHOD_TIMEOUT_MS,
- NULL,
- &DbusConnection::onAsyncReady,
- utils::createCallbackWrapper(callback, mGuard.spawn()));
-}
-
-void DbusConnection::onAsyncReady(GObject* source,
- GAsyncResult* asyncResult,
- gpointer userData)
-{
- std::unique_ptr<void, void(*)(void*)>
- autoDeleteCallback(userData, &utils::deleteCallbackWrapper<AsyncMethodCallCallback>);
- GDBusConnection* connection = reinterpret_cast<GDBusConnection*>(source);
- const AsyncMethodCallCallback& callback =
- utils::getCallbackFromPointer<AsyncMethodCallCallback>(userData);
-
- ScopedGError error;
- GVariant* result = g_dbus_connection_call_finish(connection, asyncResult, &error);
- if (error) {
- error.strip();
- LOGE("Call method failed; " << error);
- }
- AsyncMethodCallResultImpl asyncMethodCallResult(result, error);
- if (callback) {
- callback(asyncMethodCallResult);
- }
-}
-
-
-} // namespace dbus
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz <p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Dbus connection class
- */
-
-#ifndef COMMON_DBUS_CONNECTION_HPP
-#define COMMON_DBUS_CONNECTION_HPP
-
-#include "utils/callback-guard.hpp"
-
-#include <memory>
-#include <string>
-#include <functional>
-#include <gio/gio.h>
-
-
-namespace security_containers {
-namespace dbus {
-
-
-typedef std::unique_ptr<GVariant, void(*)(GVariant*)> GVariantPtr;
-
-/**
- * An interface used to set a result to a method call.
- */
-class MethodResultBuilder {
-public:
- typedef std::shared_ptr<MethodResultBuilder> Pointer;
-
- virtual ~MethodResultBuilder() {}
- virtual void set(GVariant* parameters) = 0;
- virtual void setVoid() = 0;
- virtual void setError(const std::string& name, const std::string& message) = 0;
-};
-
-/**
- * An interface used to get result from async response.
- */
-class AsyncMethodCallResult {
-public:
- virtual ~AsyncMethodCallResult() {}
- virtual GVariant* get() = 0; // throws DbusException on error
-};
-
-/**
- * Dbus connection.
- * Provides a functionality that allows to call dbus methods,
- * register dbus interfaces, etc.
- *
- * TODO divide to interface and implementation header
- */
-class DbusConnection {
-public:
- typedef std::unique_ptr<DbusConnection> Pointer;
-
- typedef std::function<void()> VoidCallback;
-
- typedef std::function<void(const std::string& objectPath,
- const std::string& interface,
- const std::string& methodName,
- GVariant* parameters,
- MethodResultBuilder::Pointer result
- )> MethodCallCallback;
-
- typedef std::function<void(const std::string& senderBusName,
- const std::string& objectPath,
- const std::string& interface,
- const std::string& signalName,
- GVariant* parameters
- )> SignalCallback;
-
- typedef std::function<void(AsyncMethodCallResult& asyncMethodCallResult
- )> AsyncMethodCallCallback;
-
- /**
- * Creates a connection to the dbus with given address.
- */
- static Pointer create(const std::string& address);
-
- /**
- * Creates a connection to the system dbus.
- */
- static Pointer createSystem();
-
- ~DbusConnection();
-
- /**
- * Sets a name to the dbus connection.
- * It allows other client to call methods using this name.
- */
- void setName(const std::string& name,
- const VoidCallback& onNameAcquired,
- const VoidCallback& onNameLost);
-
- /**
- * Emits dbus signal.
- */
- void emitSignal(const std::string& objectPath,
- const std::string& interface,
- const std::string& name,
- GVariant* parameters);
-
- /**
- * Subscribes to a signal.
- * Empty sender means subscribe to all signals
- */
- void signalSubscribe(const SignalCallback& callback, const std::string& senderBusName);
-
- /**
- * Registers an object with given definition.
- * Api calls will be handled by given callback.
- */
- void registerObject(const std::string& objectPath,
- const std::string& objectDefinitionXml,
- const MethodCallCallback& callback);
-
- /**
- * Call a dbus method
- */
- GVariantPtr callMethod(const std::string& busName,
- const std::string& objectPath,
- const std::string& interface,
- const std::string& method,
- GVariant* parameters,
- const std::string& replyType);
-
- /**
- * Async call a dbus method
- */
- void callMethodAsync(const std::string& busName,
- const std::string& objectPath,
- const std::string& interface,
- const std::string& method,
- GVariant* parameters,
- const std::string& replyType,
- const AsyncMethodCallCallback& callback);
-
- /**
- * Returns an xml with meta description of specified dbus object.
- */
- std::string introspect(const std::string& busName, const std::string& objectPath);
-
-private:
- struct NameCallbacks {
- VoidCallback nameAcquired;
- VoidCallback nameLost;
-
- NameCallbacks(const VoidCallback& acquired, const VoidCallback& lost)
- : nameAcquired(acquired), nameLost(lost) {}
- };
-
- utils::CallbackGuard mGuard;
- GDBusConnection* mConnection;
- guint mNameId;
-
- DbusConnection(const std::string& address);
-
- static void onNameAcquired(GDBusConnection* connection, const gchar* name, gpointer userData);
- static void onNameLost(GDBusConnection* connection, const gchar* name, gpointer userData);
- static void onSignal(GDBusConnection* connection,
- const gchar* sender,
- const gchar* object,
- const gchar* interface,
- const gchar* name,
- GVariant* parameters,
- gpointer userData);
- static void onMethodCall(GDBusConnection* connection,
- const gchar* sender,
- const gchar* objectPath,
- const gchar* interface,
- const gchar* method,
- GVariant* parameters,
- GDBusMethodInvocation* invocation,
- gpointer userData);
- static void onAsyncReady(GObject* source,
- GAsyncResult* asyncResult,
- gpointer userData);
-};
-
-
-} // namespace dbus
-} // namespace security_containers
-
-
-#endif // COMMON_DBUS_CONNECTION_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Piotr Bartosiewicz <p.bartosiewi@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 Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
- * @brief Dbus exceptions
- */
-
-
-#ifndef COMMON_DBUS_EXCEPTION_HPP
-#define COMMON_DBUS_EXCEPTION_HPP
-
-#include "base-exception.hpp"
-
-
-namespace security_containers {
-namespace dbus {
-
-
-/**
- * Base class for dbus exceptions
- */
-struct DbusException: public SecurityContainersException {
-
- DbusException(const std::string& error = "") : SecurityContainersException(error) {}
-};
-
-/**
- * Dbus IO exception (connection failed, connection lost, etc)
- */
-struct DbusIOException: public DbusException {
-
- DbusIOException(const std::string& error = "") : DbusException(error) {}
-};
-
-/**
- * Dbus operation failed exception
- */
-struct DbusOperationException: public DbusException {
-
- DbusOperationException(const std::string& error = "") : DbusException(error) {}
-};
-
-/**
- * Dbus custom exception triggered by user logic
- */
-struct DbusCustomException: public DbusException {
-
- DbusCustomException(const std::string& error = "") : DbusException(error) {}
-};
-
-/**
- * Dbus invalid argument exception
- */
-struct DbusInvalidArgumentException: public DbusException {
-
- DbusInvalidArgumentException(const std::string& error = "") : DbusException(error) {}
-};
-
-
-} // namespace dbus
-} // namespace security_containers
-
-
-#endif // COMMON_DBUS_EXCEPTION_HPP
*/
#include "config.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "libvirt/helpers.hpp"
#include "libvirt/connection.hpp"
#include "libvirt/exception.hpp"
*/
#include "config.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "libvirt/domain.hpp"
#include "libvirt/helpers.hpp"
#include "libvirt/exception.hpp"
#include "config.hpp"
#include "libvirt/helpers.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <mutex>
#include <libvirt/virterror.h>
#include "config.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "libvirt/network.hpp"
#include "libvirt/helpers.hpp"
#include "libvirt/exception.hpp"
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Systemd journal backend for logger
- */
-
-#include "config.hpp"
-#include "log/backend-journal.hpp"
-
-#define SD_JOURNAL_SUPPRESS_LOCATION
-#include <systemd/sd-journal.h>
-
-namespace security_containers {
-namespace log {
-
-namespace {
-
-inline int toJournalPriority(LogLevel logLevel)
-{
- switch (logLevel) {
- case LogLevel::ERROR:
- return LOG_ERR; // 3
- case LogLevel::WARN:
- return LOG_WARNING; // 4
- case LogLevel::INFO:
- return LOG_INFO; // 6
- case LogLevel::DEBUG:
- return LOG_DEBUG; // 7
- case LogLevel::TRACE:
- return LOG_DEBUG; // 7
- default:
- return LOG_DEBUG;
- }
-}
-
-} // namespace
-
-void SystemdJournalBackend::log(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func,
- const std::string& message)
-{
- sd_journal_send("PRIORITY=%d", toJournalPriority(logLevel),
- "CODE_FILE=%s", file.c_str(),
- "CODE_LINE=%d", line,
- "CODE_FUNC=%s", func.c_str(),
- "MESSAGE=%s", message.c_str(),
- NULL);
-}
-
-} // namespace log
-} // namespace security_containers
-
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Systemd journal backend for logger
- */
-
-#ifndef COMMON_LOG_BACKEND_JOURNAL_HPP
-#define COMMON_LOG_BACKEND_JOURNAL_HPP
-
-#include "log/backend.hpp"
-
-
-namespace security_containers {
-namespace log {
-
-
-/**
- systemd journal logging backend
- */
-class SystemdJournalBackend : public LogBackend {
-public:
- void log(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func,
- const std::string& message) override;
-};
-
-
-} // namespace log
-} // namespace security_containers
-
-
-#endif // COMMON_LOG_BACKEND_JOURNAL_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Pawel Broda <p.broda@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 Pawel Broda (p.broda@partner.samsung.com)
- * @brief Null backend for logger
- */
-
-
-#ifndef COMMON_LOG_BACKEND_NULL_HPP
-#define COMMON_LOG_BACKEND_NULL_HPP
-
-#include "log/backend.hpp"
-
-
-namespace security_containers {
-namespace log {
-
-
-/**
- Null logging backend
- */
-class NullLogger : public LogBackend {
-public:
- void log(LogLevel /*logLevel*/,
- const std::string& /*file*/,
- const unsigned int& /*line*/,
- const std::string& /*func*/,
- const std::string& /*message*/) override {}
-};
-
-
-} // namespace log
-} // namespace security_containers
-
-
-#endif // COMMON_LOG_BACKEND_NULL_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Pawel Broda <p.broda@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 Pawel Broda (p.broda@partner.samsung.com)
- * @brief Stderr backend for logger
- */
-
-#include "config.hpp"
-#include "log/backend-stderr.hpp"
-#include "log/formatter.hpp"
-
-#include <boost/tokenizer.hpp>
-
-namespace security_containers {
-namespace log {
-
-void StderrBackend::log(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func,
- const std::string& message)
-{
- typedef boost::char_separator<char> charSeparator;
- typedef boost::tokenizer<charSeparator> tokenizer;
-
- // example log string
- // 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
-
- const std::string logColor = LogFormatter::getConsoleColor(logLevel);
- const std::string defaultColor = LogFormatter::getDefaultConsoleColor();
- const std::string header = LogFormatter::getHeader(logLevel, file, line, func);
- tokenizer tokens(message, charSeparator("\n"));
- for (const auto& messageLine : tokens) {
- if (!messageLine.empty()) {
- fprintf(stderr,
- "%s%s%s%s\n",
- logColor.c_str(),
- header.c_str(),
- messageLine.c_str(),
- defaultColor.c_str());
- }
- }
-}
-
-
-} // namespace log
-} // namespace security_containers
-
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Pawel Broda <p.broda@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 Pawel Broda (p.broda@partner.samsung.com)
- * @brief Stderr backend for logger
- */
-
-#ifndef COMMON_LOG_BACKEND_STDERR_HPP
-#define COMMON_LOG_BACKEND_STDERR_HPP
-
-#include "log/backend.hpp"
-
-
-namespace security_containers {
-namespace log {
-
-
-/**
- Stderr logging backend
- */
-class StderrBackend : public LogBackend {
-public:
- void log(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func,
- const std::string& message) override;
-};
-
-
-} // namespace log
-} // namespace security_containers
-
-
-#endif // COMMON_LOG_BACKEND_STDERR_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Pawel Broda <p.broda@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 Pawel Broda (p.broda@partner.samsung.com)
- * @brief Logging backend
- */
-
-
-#ifndef COMMON_LOG_BACKEND_HPP
-#define COMMON_LOG_BACKEND_HPP
-
-#include "log/level.hpp"
-
-#include <string>
-
-namespace security_containers {
-namespace log {
-
-
-/**
- Abstract class for logger
- */
-class LogBackend {
-public:
- virtual void log(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func,
- const std::string& message) = 0;
- virtual ~LogBackend() {}
-};
-
-
-} // namespace log
-} // namespace security_containers
-
-
-#endif // COMMON_LOG_BACKEND_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Console color for StderrBackend logger
- */
-
-#include "config.hpp"
-#include "log/ccolor.hpp"
-
-#include <stdio.h>
-
-namespace security_containers {
-namespace log {
-
-std::string getConsoleEscapeSequence(Attributes attr, Color color)
-{
- char command[10];
-
- // Command is the control command to the terminal
- snprintf(command, sizeof(command), "%c[%d;%dm", 0x1B, attr, color);
- return std::string(command);
-}
-
-} // namespace log
-} // namespace security_containers
-
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Console color for StderrBackend logger
- */
-
-#ifndef COMMON_LOG_CCOLOR_HPP
-#define COMMON_LOG_CCOLOR_HPP
-
-#include <string>
-
-namespace security_containers {
-namespace log {
-
-enum class Color : unsigned int {
- DEFAULT = 0,
- BLACK = 90,
- RED = 91,
- GREEN = 92,
- YELLOW = 93,
- BLUE = 94,
- MAGENTA = 95,
- CYAN = 96,
- WHITE = 97
-};
-
-enum class Attributes : unsigned int {
- DEFAULT = 0,
- BOLD = 1
-};
-
-std::string getConsoleEscapeSequence(Attributes attr, Color color);
-
-} // namespace log
-} // namespace security_containers
-
-#endif // COMMON_LOG_CCOLOR_HPP
-
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Helper formatter for logger
- */
-
-#include "config.hpp"
-#include "log/formatter.hpp"
-#include "log/ccolor.hpp"
-
-#include <sys/time.h>
-#include <cassert>
-#include <sstream>
-#include <iomanip>
-#include <thread>
-#include <atomic>
-
-namespace security_containers {
-namespace log {
-
-namespace {
-
-const int TIME_COLUMN_LENGTH = 12;
-const int SEVERITY_COLUMN_LENGTH = 8;
-const int THREAD_COLUMN_LENGTH = 3;
-const int FILE_COLUMN_LENGTH = 60;
-
-std::atomic<unsigned int> gNextThreadId(1);
-thread_local unsigned int gThisThreadId(0);
-
-} // namespace
-
-unsigned int LogFormatter::getCurrentThread(void)
-{
- unsigned int id = gThisThreadId;
- if (id == 0) {
- gThisThreadId = id = gNextThreadId++;
- }
-
- return id;
-}
-
-std::string LogFormatter::getCurrentTime(void)
-{
- char time[TIME_COLUMN_LENGTH + 1];
- struct timeval tv;
- gettimeofday(&tv, NULL);
- struct tm* tm = localtime(&tv.tv_sec);
- snprintf(time,
- sizeof(time),
- "%02d:%02d:%02d.%03d",
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- int(tv.tv_usec / 1000));
-
- return std::string(time);
-}
-
-std::string LogFormatter::getConsoleColor(LogLevel logLevel)
-{
- switch (logLevel) {
- case LogLevel::ERROR:
- return getConsoleEscapeSequence(Attributes::BOLD, Color::RED);
- case LogLevel::WARN:
- return getConsoleEscapeSequence(Attributes::BOLD, Color::YELLOW);
- case LogLevel::INFO:
- return getConsoleEscapeSequence(Attributes::BOLD, Color::BLUE);
- case LogLevel::DEBUG:
- return getConsoleEscapeSequence(Attributes::DEFAULT, Color::GREEN);
- case LogLevel::TRACE:
- return getConsoleEscapeSequence(Attributes::DEFAULT, Color::BLACK);
- default:
- return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
- }
-}
-
-std::string LogFormatter::getDefaultConsoleColor(void)
-{
- return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
-}
-
-std::string LogFormatter::stripProjectDir(const std::string& file)
-{
- const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/";
- // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...)
- assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR));
- return file.substr(SOURCE_DIR.size());
-}
-
-std::string LogFormatter::getHeader(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func)
-{
- std::ostringstream logLine;
- logLine << getCurrentTime() << ' '
- << std::left << std::setw(SEVERITY_COLUMN_LENGTH) << '[' + toString(logLevel) + ']'
- << std::right << std::setw(THREAD_COLUMN_LENGTH) << getCurrentThread() << ": "
- << std::left << std::setw(FILE_COLUMN_LENGTH)
- << file + ':' + std::to_string(line) + ' ' + func + ':';
- return logLine.str();
-}
-
-} // namespace log
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief Helper formatter for logger
- */
-
-#ifndef COMMON_LOG_FORMATTER_HPP
-#define COMMON_LOG_FORMATTER_HPP
-
-#include "log/level.hpp"
-
-#include <string>
-
-namespace security_containers {
-namespace log {
-
-class LogFormatter {
-public:
- static unsigned int getCurrentThread(void);
- static std::string getCurrentTime(void);
- static std::string getConsoleColor(LogLevel logLevel);
- static std::string getDefaultConsoleColor(void);
- static std::string stripProjectDir(const std::string& file);
- static std::string getHeader(LogLevel logLevel,
- const std::string& file,
- const unsigned int& line,
- const std::string& func);
-};
-
-} // namespace log
-} // namespace security_containers
-
-#endif // COMMON_LOG_FORMATTER_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 Functions to handle LogLevel
- */
-
-#include "config.hpp"
-
-#include "log/level.hpp"
-
-#include <stdexcept>
-#include <boost/algorithm/string.hpp>
-
-namespace security_containers {
-namespace log {
-
-LogLevel parseLogLevel(const std::string& level)
-{
- if (boost::iequals(level, "ERROR")) {
- return LogLevel::ERROR;
- } else if (boost::iequals(level, "WARN")) {
- return LogLevel::WARN;
- } else if (boost::iequals(level, "INFO")) {
- return LogLevel::INFO;
- } else if (boost::iequals(level, "DEBUG")) {
- return LogLevel::DEBUG;
- } else if (boost::iequals(level, "TRACE")) {
- return LogLevel::TRACE;
- } else {
- throw std::runtime_error("Invalid LogLevel to parse");
- }
-}
-
-std::string toString(const LogLevel logLevel)
-{
- switch (logLevel) {
- case LogLevel::ERROR:
- return "ERROR";
- case LogLevel::WARN:
- return "WARN";
- case LogLevel::INFO:
- return "INFO";
- case LogLevel::DEBUG:
- return "DEBUG";
- case LogLevel::TRACE:
- return "TRACE";
- default:
- return "UNKNOWN";
- }
-}
-} // namespace log
-} // namespace security_containers
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Dariusz Michaluk (d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
- * @brief LogLevel
- */
-
-#ifndef COMMON_LOG_LEVEL_HPP
-#define COMMON_LOG_LEVEL_HPP
-
-#include <string>
-
-
-namespace security_containers {
-namespace log {
-
-enum class LogLevel {
- TRACE,
- DEBUG,
- INFO,
- WARN,
- ERROR
-};
-
-/**
- * @param logLevel LogLevel
- * @return std::sting representation of the LogLevel value
- */
-std::string toString(const LogLevel logLevel);
-
-/**
- * @param level string representation of log level
- * @return parsed LogLevel value
- */
-LogLevel parseLogLevel(const std::string& level);
-
-
-} // namespace log
-} // namespace security_containers
-
-#endif // COMMON_LOG_LEVEL_HPP
+++ /dev/null
-/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Pawel Broda <p.broda@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 Pawel Broda (p.broda@partner.samsung.com)
- * @brief Logger
- */
-
-#include "config.hpp"
-#include "log/logger.hpp"
-#include "log/formatter.hpp"
-#include "log/backend-null.hpp"
-
-#include <memory>
-#include <mutex>
-
-namespace security_containers {
-namespace log {
-
-
-namespace {
-
-volatile LogLevel gLogLevel = LogLevel::DEBUG;
-std::unique_ptr<LogBackend> gLogBackendPtr(new NullLogger());
-std::mutex gLogMutex;
-
-} // namespace
-
-Logger::Logger(LogLevel logLevel,
- const std::string& file,
- const unsigned int line,
- const std::string& func)
- : mLogLevel(logLevel),
- mFile(LogFormatter::stripProjectDir(file)),
- mLine(line),
- mFunc(func)
-{
-
-}
-
-void Logger::logMessage(const std::string& message)
-{
- std::unique_lock<std::mutex> lock(gLogMutex);
- gLogBackendPtr->log(mLogLevel, mFile, mLine, mFunc, message);
-}
-
-void Logger::setLogLevel(const LogLevel level)
-{
- gLogLevel = level;
-}
-
-void Logger::setLogLevel(const std::string& level)
-{
- gLogLevel = parseLogLevel(level);
-}
-
-LogLevel Logger::getLogLevel(void)
-{
- return gLogLevel;
-}
-
-void Logger::setLogBackend(LogBackend* pBackend)
-{
- std::unique_lock<std::mutex> lock(gLogMutex);
- gLogBackendPtr.reset(pBackend);
-}
-
-} // namespace log
-} // 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 Logger
- */
-
-
-#ifndef COMMON_LOG_LOGGER_HPP
-#define COMMON_LOG_LOGGER_HPP
-
-#include "log/level.hpp"
-
-#include <sstream>
-#include <string>
-
-
-namespace security_containers {
-namespace log {
-
-class LogBackend;
-
-class Logger {
-public:
- Logger(LogLevel logLevel,
- const std::string& file,
- const unsigned int line,
- const std::string& func);
-
- void logMessage(const std::string& message);
-
- static void setLogLevel(const LogLevel level);
- static void setLogLevel(const std::string& level);
- static LogLevel getLogLevel(void);
- static void setLogBackend(LogBackend* pBackend);
-
-private:
- LogLevel mLogLevel;
- std::string mFile;
- unsigned int mLine;
- std::string mFunc;
-};
-
-} // namespace log
-} // namespace security_containers
-
-
-#define LOG(SEVERITY, MESSAGE) \
- do { \
- if (security_containers::log::Logger::getLogLevel() <= \
- security_containers::log::LogLevel::SEVERITY) { \
- std::ostringstream messageStream__; \
- messageStream__ << MESSAGE; \
- security_containers::log::Logger logger(security_containers::log::LogLevel::SEVERITY, \
- __FILE__, \
- __LINE__, \
- __func__); \
- logger.logMessage(messageStream__.str()); \
- } \
- } while(0)
-
-#define LOGE(MESSAGE) LOG(ERROR, MESSAGE)
-#define LOGW(MESSAGE) LOG(WARN, MESSAGE)
-#define LOGI(MESSAGE) LOG(INFO, MESSAGE)
-#define LOGD(MESSAGE) LOG(DEBUG, MESSAGE)
-#define LOGT(MESSAGE) LOG(TRACE, MESSAGE)
-
-
-#endif // COMMON_LOG_LOGGER_HPP
-
#include "config.hpp"
#include "utils/callback-guard.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <mutex>
#include <condition_variable>
#include "config.hpp"
#include "utils/environment.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <cap-ng.h>
#include <grp.h>
*/
#include "config.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/fs.hpp"
#include "utils/paths.hpp"
#include "utils/exception.hpp"
## Link libraries ##############################################################
FIND_PACKAGE (Boost COMPONENTS program_options system filesystem)
-PKG_CHECK_MODULES(CONTAINER_DAEMON_DEPS REQUIRED gio-2.0 libsystemd-journal libcap-ng)
+PKG_CHECK_MODULES(CONTAINER_DAEMON_DEPS REQUIRED gio-2.0 libsystemd-journal libcap-ng
+ libLogger libSimpleDbus libConfig)
INCLUDE_DIRECTORIES(${COMMON_FOLDER})
INCLUDE_DIRECTORIES(SYSTEM ${CONTAINER_DAEMON_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${CONTAINER_DAEMON_CODENAME} ${CONTAINER_DAEMON_DEPS_LIBRARIES} ${Boost_LIBRARIES})
#include "daemon-dbus-definitions.hpp"
#include "exception.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
namespace security_containers {
#include "daemon.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
namespace security_containers {
#include "exception.hpp"
#include "runner.hpp"
-#include "log/logger.hpp"
-#include "log/backend-stderr.hpp"
-#include "log/backend-journal.hpp"
+#include "logger/logger.hpp"
+#include "logger/backend-stderr.hpp"
+#include "logger/backend-journal.hpp"
#include "utils/typeinfo.hpp"
#include <boost/program_options.hpp>
#include <iostream>
-using namespace security_containers::log;
+using namespace logger;
using namespace security_containers;
namespace po = boost::program_options;
#include "runner.hpp"
#include "daemon.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/glib-loop.hpp"
#include "utils/latch.hpp"
BuildRequires: libvirt-devel
BuildRequires: libjson-devel >= 0.10
BuildRequires: libcap-ng-devel
+BuildRequires: pkgconfig(libConfig)
+BuildRequires: pkgconfig(libLogger)
+BuildRequires: pkgconfig(libSimpleDbus)
BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(libsystemd-journal)
BuildRequires: pkgconfig(libvirt-glib-1.0)
## 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
- libcap-ng)
+ libcap-ng libLogger libSimpleDbus libConfig)
INCLUDE_DIRECTORIES(${COMMON_FOLDER})
INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
#include "exception.hpp"
#include "libvirt/helpers.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/fs.hpp"
#include "utils/latch.hpp"
#include "utils/callback-wrapper.hpp"
#include "utils/file-wait.hpp"
#include "utils/fs.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <boost/filesystem.hpp>
#include <boost/system/system_error.hpp>
// TODO: Switch to real power-manager dbus defs when they will be implemented in power-manager
#include "fake-power-manager-dbus-definitions.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
namespace security_containers {
#include "config.hpp"
#include "container.hpp"
+#include "base-exception.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/paths.hpp"
#include "config/manager.hpp"
#include "exception.hpp"
#include "utils/paths.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "config/manager.hpp"
#include "dbus/exception.hpp"
+#include "utils/fs.hpp"
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include "host-dbus-definitions.hpp"
#include "exception.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
namespace security_containers {
#include "input-monitor.hpp"
#include "exception.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/exception.hpp"
#include "utils/fs.hpp"
#include "utils/callback-wrapper.hpp"
#include "exception.hpp"
#include "server.hpp"
-#include "log/logger.hpp"
-#include "log/backend-stderr.hpp"
-#include "log/backend-journal.hpp"
+#include "logger/logger.hpp"
+#include "logger/backend-stderr.hpp"
+#include "logger/backend-journal.hpp"
#include "utils/typeinfo.hpp"
#include <boost/program_options.hpp>
#include <iostream>
-using namespace security_containers::log;
+using namespace logger;
using namespace security_containers;
namespace po = boost::program_options;
#include "exception.hpp"
#include "libvirt/helpers.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/fs.hpp"
#include <cassert>
#include "exception.hpp"
#include "config/manager.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include "utils/glib-loop.hpp"
#include "utils/environment.hpp"
+#include "utils/fs.hpp"
#include <csignal>
#include <cerrno>
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
- libsystemd-journal libcap-ng)
+ libsystemd-journal libcap-ng libLogger libSimpleDbus libConfig)
INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_FOLDER} ${UNIT_TESTS_FOLDER})
INCLUDE_DIRECTORIES(SYSTEM ${UT_SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} ${UT_SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
#include "config/fields.hpp"
#include "config/manager.hpp"
-using namespace security_containers;
-using namespace security_containers::config;
+using namespace config;
BOOST_AUTO_TEST_SUITE(ConfigurationSuite)
#include "dbus/connection.hpp"
#include "dbus/exception.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
namespace security_containers {
#include "utils/file-wait.hpp"
#include "utils/latch.hpp"
#include "utils/fs.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <boost/filesystem.hpp>
#include <thread>
using namespace security_containers;
using namespace security_containers::utils;
-using namespace security_containers::dbus;
+using namespace dbus;
namespace {
#include "config.hpp"
#include "ut.hpp"
-#include "log/logger.hpp"
-#include "log/formatter.hpp"
-#include "log/backend.hpp"
-#include "log/backend-stderr.hpp"
+#include "logger/logger.hpp"
+#include "logger/formatter.hpp"
+#include "logger/backend.hpp"
+#include "logger/backend-stderr.hpp"
#include <stdexcept>
BOOST_AUTO_TEST_SUITE(LogSuite)
-using namespace security_containers::log;
+using namespace logger;
namespace {
using namespace security_containers;
using namespace security_containers::utils;
-using namespace security_containers::dbus;
+using namespace dbus;
namespace {
using namespace security_containers;
-using namespace security_containers::config;
+using namespace config;
namespace {
#include <boost/filesystem.hpp>
using namespace security_containers;
-using namespace security_containers::config;
+using namespace config;
using namespace security_containers::utils;
-using namespace security_containers::dbus;
+using namespace dbus;
namespace {
}
struct Fixture {
- utils::ScopedGlibLoop mLoop;
+ security_containers::utils::ScopedGlibLoop mLoop;
};
} // namespace
BOOST_AUTO_TEST_SUITE(ServerSuite)
using namespace security_containers;
-using namespace security_containers::config;
+using namespace config;
const std::string TEST_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-server/test-daemon.conf";
const std::string BUGGY_CONFIG_PATH = SC_TEST_CONFIG_INSTALL_DIR "/server/ut-server/buggy-daemon.conf";
#include "config.hpp"
-#include "log/logger.hpp"
-#include "log/backend-stderr.hpp"
+#include "logger/logger.hpp"
+#include "logger/backend-stderr.hpp"
#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;
-using namespace security_containers::log;
+using namespace logger;
test_suite* init_unit_test_suite(int /*argc*/, char** /*argv*/)
{
#include "utils/scoped-daemon.hpp"
-#include "log/logger.hpp"
+#include "logger/logger.hpp"
#include <unistd.h>
#include <sys/wait.h>