${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")
--- /dev/null
+/*
+ * 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<Connection::signalCallback*> (user_data);
+ (*func)(Variant(parameters));
+ delete func;
+}
+
+} // namespace
+
+Connection::Connection(const std::string& address) :
+ connection(nullptr)
+{
+ Error error;
+ const GDBusConnectionFlags flags = static_cast<GDBusConnectionFlags>(
+ 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
--- /dev/null
+/*
+ * 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 <string>
+#include <functional>
+
+#include <gio/gio.h>
+
+#include "dbus/variant.h"
+
+namespace dbus {
+
+class Connection {
+public:
+ typedef unsigned int subscriptionId;
+ typedef std::function<void(Variant)> 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__
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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 <iostream>
+#include <gio/gio.h>
+
+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__
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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 <string>
+#include <gio/gio.h>
+
+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__
#include <bundle.h>
#include <tzplatform_config.h>
-#include <gio/gio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/inotify.h>
#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
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 <typename... Args>
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;
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;