From: Jaemin Ryu Date: Wed, 20 Apr 2016 10:31:32 +0000 (+0900) Subject: Add DBUS wrapper X-Git-Tag: submit/rmi/20160518.105607~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bd53c3b953a6f2b99da09f1372dff41a329f5cfb;p=platform%2Fcore%2Fsecurity%2Fdevice-policy-manager.git Add DBUS wrapper Change-Id: Ibc4c8d72b3de8d13c62c4f151086ea7a65d92b7d Signed-off-by: Jaemin Ryu --- diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index cb76978..a727335 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -43,7 +43,9 @@ SET (COMMON_SOURCES ${DPM_COMMON}/error.cpp ${DPM_COMMON}/auth/user.cpp ${DPM_COMMON}/auth/group.cpp ${DPM_COMMON}/auth/shadow.cpp - + ${DPM_COMMON}/dbus/error.cpp + ${DPM_COMMON}/dbus/variant.cpp + ${DPM_COMMON}/dbus/connection.cpp ) SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack") diff --git a/common/dbus/connection.cpp b/common/dbus/connection.cpp new file mode 100644 index 0000000..dddb99d --- /dev/null +++ b/common/dbus/connection.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + +#include "connection.h" + +#include "exception.h" +#include "dbus/error.h" +#include "dbus/variant.h" +#include "dbus/connection.h" +#include "audit/logger.h" + +namespace dbus { + +namespace { + +const std::string DBUS_SYSTEM_BUS_ADDRESS = "unix:path=/var/run/dbus/system_bus_socket"; + +void defaultCallback (GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) { + Connection::signalCallback* func = reinterpret_cast (user_data); + (*func)(Variant(parameters)); + delete func; +} + +} // namespace + +Connection::Connection(const std::string& address) : + connection(nullptr) +{ + Error error; + const GDBusConnectionFlags flags = static_cast( + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION + ); + + connection = g_dbus_connection_new_for_address_sync(address.c_str(), flags, NULL, NULL, &error); + if (error) { + ERROR(error->message); + throw runtime::Exception(error->message); + } +} + +Connection::Connection(Connection&& other) : + connection(other.connection) +{ + other.connection = nullptr; +} + +Connection::~Connection() +{ + if (connection) { + g_dbus_connection_close_sync(connection, NULL, NULL); + g_object_unref(connection); + } +} + +Connection& Connection::getSystem() +{ + static Connection __instance__(DBUS_SYSTEM_BUS_ADDRESS); + return __instance__; +} + +void Connection::emitSignal(const std::string& busName, + const std::string& object, + const std::string& interface, + const std::string& name, + const std::string& paramType, + ...) +{ + Error error; + va_list ap; + + va_start(ap, paramType); + g_dbus_connection_emit_signal(connection, + busName.empty() ? NULL : busName.c_str(), + object.c_str(), + interface.c_str(), + name.c_str(), + paramType.empty() ? NULL : + g_variant_new(paramType.c_str(), NULL, &ap), + &error); + va_end(ap); + + if (error) { + ERROR(error->message); + throw runtime::Exception(error->message); + } +} + +Connection::subscriptionId Connection::subscribeSignal(const std::string& sender, + const std::string& interface, + const std::string& object, + const std::string& member, + const signalCallback callback) +{ + subscriptionId id; + id = g_dbus_connection_signal_subscribe(connection, + sender.empty() ? NULL : sender.c_str(), + interface.empty() ? NULL : interface.c_str(), + object.empty() ? NULL : object.c_str(), + member.empty() ? NULL : member.c_str(), + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + defaultCallback, + new signalCallback(callback), + NULL); + return id; +} + +void Connection::unsubscribeSignal(Connection::subscriptionId id) +{ + g_dbus_connection_signal_unsubscribe(connection, id); +} + +const Variant Connection::methodcall(const std::string& busName, + const std::string& object, + const std::string& interface, + const std::string& method, + int timeout, + const std::string& replyType, + const std::string& paramType, + ...) +{ + Variant result; + Error error; + va_list ap; + + va_start(ap, paramType); + result = g_dbus_connection_call_sync(connection, + busName.empty() ? NULL : + busName.c_str(), + object.c_str(), + interface.c_str(), + method.c_str(), + paramType.empty() ? NULL : + g_variant_new_va(paramType.c_str(), NULL, &ap), + replyType.empty() ? NULL : + G_VARIANT_TYPE(replyType.c_str()), + G_DBUS_CALL_FLAGS_NONE, + timeout, + NULL, + &error); + va_end(ap); + + if (error) { + ERROR(error->message); + throw runtime::Exception(error->message); + } + + return result; +} + +} // namespace dbus diff --git a/common/dbus/connection.h b/common/dbus/connection.h new file mode 100644 index 0000000..89b02ff --- /dev/null +++ b/common/dbus/connection.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + + #ifndef __RUNTIME_DBUS_CONNECTION_H__ + #define __RUNTIME_DBUS_CONNECTION_H__ + +#include +#include + +#include + +#include "dbus/variant.h" + +namespace dbus { + +class Connection { +public: + typedef unsigned int subscriptionId; + typedef std::function signalCallback; + + Connection() = delete; + Connection(const Connection&) = delete; + Connection(Connection&&); + ~Connection(); + + Connection& operator=(const Connection&) = delete; + + static Connection& getSystem(); + + void emitSignal(const std::string& busName, + const std::string& object, + const std::string& interface, + const std::string& name, + const std::string& paramType, + ...); + + subscriptionId subscribeSignal(const std::string& sender, + const std::string& interface, + const std::string& object, + const std::string& member, + const signalCallback callback); + + + void unsubscribeSignal(subscriptionId id); + + const Variant methodcall(const std::string& busName, + const std::string& object, + const std::string& interface, + const std::string& method, + int timeout, + const std::string& replyType, + const std::string& paramType, + ...); + +private: + Connection(const std::string& address); + + GDBusConnection* connection; +}; + +} // namespace dbus + + #endif //! __RUNTIME_DBUS_CONNECTION_H__ diff --git a/common/dbus/error.cpp b/common/dbus/error.cpp new file mode 100644 index 0000000..8a5b2f2 --- /dev/null +++ b/common/dbus/error.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + +#include "dbus/error.h" + +namespace dbus { + +Error::Error() : + error(nullptr) +{ +} + +Error::~Error() +{ + if (error) { + g_error_free(error); + } +} + +GError** Error::operator& () +{ + return &error; +} + +const GError* Error::operator-> () const +{ + return error; +} + +Error::operator bool () const +{ + return error != nullptr; +} + +std::ostream& operator<<(std::ostream& os, const Error& e) +{ + return os << e->message; +} + +} // namespace dbus diff --git a/common/dbus/error.h b/common/dbus/error.h new file mode 100644 index 0000000..f2fa38b --- /dev/null +++ b/common/dbus/error.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + +#ifndef __RUNTIME_DBUS_ERROR_H__ +#define __RUNTIME_DBUS_ERROR_H__ + +#include +#include + +namespace dbus{ + +class Error { +public: + Error(); + ~Error(); + + GError** operator& (); + const GError* operator-> () const; + operator bool () const; + + friend std::ostream& operator<<(std::ostream& os, const Error& e); + +private: + GError* error; +}; + +} // namespace dbus + +#endif //!__RUNTIME_DBUS_ERROR_H__ diff --git a/common/dbus/variant.cpp b/common/dbus/variant.cpp new file mode 100644 index 0000000..48cc20e --- /dev/null +++ b/common/dbus/variant.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + +#include "dbus/variant.h" + +namespace dbus { + +Variant::Variant(GVariant* var) : + variant(var) +{ +} + +Variant::Variant(Variant&& var) : + variant(var.variant) +{ + var.variant = nullptr; +} + +Variant::Variant() : + variant(nullptr) +{ +} + +Variant::~Variant() +{ + if (variant) { + g_variant_unref(variant); + } +} + +Variant& Variant::operator=(GVariant* var) +{ + variant = var; + return *this; +} + +Variant::operator bool () const +{ + return variant != nullptr; +} + +void Variant::get(const std::string& format, ...) const +{ + va_list ap; + + va_start(ap, format); + g_variant_get_va(variant, format.c_str(), NULL, &ap); + va_end(ap); +} + +} // namespace dbus diff --git a/common/dbus/variant.h b/common/dbus/variant.h new file mode 100644 index 0000000..3695751 --- /dev/null +++ b/common/dbus/variant.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + */ + +#ifndef __RUNTIME_DBUS_VARIANT_H__ +#define __RUNTIME_DBUS_VARIANT_H__ + +#include +#include + +namespace dbus{ + +class Variant { +public: + Variant(GVariant* var); + Variant(Variant&& var); + Variant(); + ~Variant(); + + Variant& operator=(GVariant* var); + operator bool () const; + + void get(const std::string& format, ...) const; +private: + GVariant* variant; +}; + +} // namespace dbus + +#endif //!__RUNTIME_DBUS_VARIANT_H__ diff --git a/server/zone.cpp b/server/zone.cpp index d5b1574..cd0218b 100644 --- a/server/zone.cpp +++ b/server/zone.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include #include @@ -33,6 +32,7 @@ #include "xml/parser.h" #include "xml/document.h" #include "audit/logger.h" +#include "dbus/connection.h" #define ZONE_UID_MIN 60001 #define ZONE_UID_MAX 65000 @@ -56,41 +56,11 @@ namespace DevicePolicyManager { static const char *defaultGroups[] = {"audio", "video", "display", "log", NULL}; static const char *defaultAppDir[] = {"cache", "data", "shared", NULL}; -static int setZoneState(uid_t id, int state) +static void setZoneState(uid_t id, int state) { - GDBusConnection* connection; - GError* error = NULL; - GVariant* param = g_variant_new("(ubb)", id, state, 1); - - connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (connection == NULL) { - ERROR(std::string("Failed to get system DBUS : ") + error->message); - g_error_free(error); - return -1; - } - - GVariant* var; - var = g_dbus_connection_call_sync(connection, FREEDESKTOP_LOGIN_INTERFACE, - "SetUserLinger", param, - NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, - &error); - if (var == NULL) { - ERROR(std::string("Failed to call Setlinger: ") + error->message); - g_error_free(error); - return -1; - } else { - g_variant_unref(var); - } - - /* - ret = g_dbus_connection_close_sync(connection, NULL, &error); - if (ret == FALSE) { - g_printerr("Failed to close system DBUS : %s\n", error->message); - g_error_free(error); - return -1; - } - */ - return 0; + dbus::Connection& systemDBus = dbus::Connection::getSystem(); + systemDBus.methodcall(FREEDESKTOP_LOGIN_INTERFACE, "SetUserLinger", + -1, "", "(ubb)", id, state, 1); } template @@ -380,15 +350,9 @@ int ZonePolicy::removeZone(const std::string& name) int ZonePolicy::lockZone(const std::string& name) { - int result; - try { runtime::User user(name); - - result = setZoneState(user.getUid(), 0); - if (result != 0) { - return -1; - } + setZoneState(user.getUid(), 0); } catch (runtime::Exception& e) { ERROR(e.what()); return -1; @@ -399,15 +363,9 @@ int ZonePolicy::lockZone(const std::string& name) int ZonePolicy::unlockZone(const std::string& name) { - int result; - try { runtime::User user(name); - - result = setZoneState(user.getUid(), 1); - if (result != 0) { - return -1; - } + setZoneState(user.getUid(), 1); } catch (runtime::Exception& e) { ERROR(e.what()); return -1;