+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 __CONTEXT_CLIENT_BASE_H__
-#define __CONTEXT_CLIENT_BASE_H__
-
-#include <vector>
-#include <string>
-#include <ContextTypes.h>
-
-namespace ctx {
-
- /* Forward declarations */
- class ServiceBase;
- class MethodCall;
- class Credential;
-
- class EXPORT_API ClientBase {
- public:
- virtual ~ClientBase();
-
- virtual void onMethodCalled(MethodCall* methodCall) = 0;
-
- virtual void onDisconnected() = 0;
-
- const std::string& getBusName() const;
-
- // If the GVariant 'param' is floating, it is consumed.
- void publish(const std::string& signalName, GVariant* param);
-
- ServiceBase& getHostService();
-
- bool isVerified();
-
- bool hasPrivilege(const char* privil) const;
-
- bool hasPrivileges(const std::vector<std::string>& privil) const;
-
- uid_t getUid() const;
-
- std::string getClientId() const;
-
- bool isSystem() const;
-
- static bool isSystemUid(uid_t uid);
-
- protected:
- ClientBase(ServiceBase* hostService, const std::string& busName);
-
- private:
- bool __getCredential();
-
- ServiceBase* __hostService;
- std::string __busName;
- Credential* __credential;
- };
-
-}
-
-#endif /* __CONTEXT_CLIENT_BASE_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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 __CONTEXT_I_CLIENT_H__
+#define __CONTEXT_I_CLIENT_H__
+
+#include <string>
+#include <vector>
+#include <ContextTypes.h>
+
+namespace ctx {
+
+ class IService;
+
+ class IClient {
+ public:
+ virtual ~IClient() {}
+
+ virtual const std::string& getBusName() = 0;
+
+ virtual const std::string& getId() = 0;
+
+ virtual uid_t getUid() = 0;
+
+ virtual bool isSystem() = 0;
+
+ virtual bool isVerified() = 0;
+
+ virtual bool hasPrivilege(const char* privil) = 0;
+
+ virtual bool hasPrivileges(const std::vector<std::string>& privil) = 0;
+
+ // If the GVariant 'param' is floating, it is consumed.
+ virtual void publish(const std::string& signalName, GVariant* param) = 0;
+
+ virtual IService* getHostService() = 0;
+ };
+
+}
+
+#endif /* __CONTEXT_I_CLIENT_H__ */
* limitations under the License.
*/
-#ifndef __CONTEXT_DBUS_METHOD_CALL_H__
-#define __CONTEXT_DBUS_METHOD_CALL_H__
+#ifndef __CONTEXT_I_METHOD_CALL_H__
+#define __CONTEXT_I_METHOD_CALL_H__
#include <string>
#include <ContextTypes.h>
namespace ctx {
- /* Forward declaration */
- class ClientBase;
+ class IClient;
- class EXPORT_API MethodCall {
+ class IMethodCall {
public:
- MethodCall(ClientBase* client, const std::string& methodName, GVariant* param, GDBusMethodInvocation* invocation);
- ~MethodCall();
+ virtual ~IMethodCall() {}
- const std::string& getMethodName() const;
+ virtual const std::string& getMethodName() = 0;
// The returned GVariant object should be not freed.
- GVariant* getParam();
+ virtual GVariant* getParam() = 0;
- bool reply(GVariant* param);
- bool reply(int error);
+ virtual bool reply(GVariant* param) = 0;
+ virtual bool reply(int error) = 0;
- void publish(const std::string& signalName, GVariant* param);
+ virtual void publish(const std::string& signalName, GVariant* param) = 0;
- bool hasPrivilege(const char* privil);
+ virtual bool hasPrivilege(const char* privil) = 0;
- uid_t getUid();
+ virtual uid_t getUid() = 0;
- std::string getClientId();
+ virtual const std::string& getCallerId() = 0;
- bool isSystem();
+ virtual bool isSystem() = 0;
- ClientBase& getSender();
-
- private:
- ClientBase* __client;
- std::string __methodName;
- GVariant* __param;
- GDBusMethodInvocation* __invocation;
+ virtual IClient& getCaller() = 0;
};
}
-#endif /* __CONTEXT_DBUS_METHOD_CALL_H__ */
+#endif /* __CONTEXT_I_METHOD_CALL_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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 __CONTEXT_I_METHOD_CALL_HANDLER_H__
+#define __CONTEXT_I_METHOD_CALL_HANDLER_H__
+
+namespace ctx {
+
+ class IClient;
+ class IMethodCall;
+
+ class IMethodCallHandler {
+ public:
+ virtual ~IMethodCallHandler() {}
+
+ virtual void setCaller(IClient* client) = 0;
+
+ virtual void onMethodCalled(IMethodCall* methodCall) = 0;
+
+ virtual void onDisconnected() = 0;
+ };
+
+}
+
+#endif /* __CONTEXT_I_METHOD_CALL_HANDLER_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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 __CONTEXT_I_SERVICE_H__
+#define __CONTEXT_I_SERVICE_H__
+
+namespace ctx {
+
+ class IServiceRunner;
+ class IMethodCallHandler;
+
+ class IService {
+ public:
+ virtual ~IService() {}
+
+ virtual void setServiceRunner(IServiceRunner* runner) = 0;
+
+ virtual bool isUserService() = 0;
+
+ virtual const char* getServiceName() = 0;
+
+ virtual const char* getMethodSpecs() = 0;
+
+ virtual bool prepare() = 0;
+
+ virtual void cleanup() = 0;
+
+ virtual void onUserActivated() = 0;
+
+ virtual void onUserDeactivated() = 0;
+
+ virtual IMethodCallHandler* createMethodCallHandler() = 0;
+ };
+
+}
+
+#endif /* __CONTEXT_I_SERVICE_H__ */
* limitations under the License.
*/
-#ifndef __CONTEXT_CREDENTIAL_H__
-#define __CONTEXT_CREDENTIAL_H__
+#ifndef __CONTEXT_I_SERVICE_RUNNER_H__
+#define __CONTEXT_I_SERVICE_RUNNER_H__
#include <string>
#include <ContextTypes.h>
-#define ROOT_UID 0
-
namespace ctx {
- class EXPORT_API Credential {
+ class IServiceRunner {
public:
- Credential(GDBusConnection* conn, const std::string& busName);
- ~Credential();
-
- bool valid() const;
- bool hasPrivilege(const char* privil) const;
+ virtual ~IServiceRunner() {}
- uid_t getUid() const;
- std::string getClientId() const;
- bool isSystem() const;
+ // If the GVariant 'param' is floating, it is consumed.
+ virtual void publish(const std::string& busName, const std::string& signalName, GVariant* param) = 0;
- static bool isSystemUid(uid_t uid);
+ virtual GMainContext* getMainContext() = 0;
- private:
- pid_t __pid;
- uid_t __uid;
- char* __clientId; /* Tizen-Default: Smack label */
- char* __session;
- char* __user; /* Tizen-Default: UID */
- bool __valid;
+ virtual GDBusConnection* getConnection() = 0;
};
}
-#endif
+#endif /* __CONTEXT_I_SERVICE_RUNNER_H__ */
class EXPORT_API PlatformDatabase : public Database {
public:
PlatformDatabase(const std::string& dbName, uid_t uid);
- PlatformDatabase(const std::string& dbName, const ClientBase& client);
- PlatformDatabase(const std::string& dbName, const ClientBase* client);
PlatformDatabase(const std::string& dbName);
};
namespace ctx { namespace util {
+ uid_t setActiveUid(uid_t uid);
+
+ uid_t getActiveUid();
+
+ bool isSystemUid(uid_t uid);
+
std::string getSystemPath(enum tzplatform_variable id, const std::string& path);
std::string getUserPath(enum tzplatform_variable id, const std::string& path);
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 __CONTEXT_SERVICE_BASE_H__
-#define __CONTEXT_SERVICE_BASE_H__
-
-#include <atomic>
-#include <string>
-#include <map>
-#include <ContextTypes.h>
-
-namespace ctx {
-
- /* Forward declarations */
- class ClientBase;
- class MethodCall;
-
- class EXPORT_API ServiceBase {
- public:
- virtual ~ServiceBase();
-
- // These functions should not be called from individual services
- bool start();
- void stop();
- void notifyUserNew();
- void notifyUserRemoved();
- static void setActiveUser(uid_t uid);
- // ---
-
- // If the GVariant 'param' is floating, it is consumed.
- void publish(const std::string& busName, const std::string& signalName, GVariant* param);
-
- GMainContext* getMainContext();
-
- GDBusConnection* getConnection();
-
- static uid_t getActiveUser();
-
- virtual bool isUserService() = 0;
-
- protected:
- ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs);
-
- virtual bool prepare() = 0;
-
- virtual void cleanup() = 0;
-
- virtual ClientBase* createClient(const std::string& busName) = 0;
-
- virtual void onUserActivated();
-
- virtual void onUserDeactivated();
-
- private:
- static gpointer __threadFunc(gpointer data);
-
- static void __onMethodCalled(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, GDBusMethodInvocation* invocation, gpointer userData);
-
- static void __onNameOwnerChanged(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData);
-
- static gboolean __onUserActivated(gpointer data);
-
- static gboolean __onUserDeactivated(gpointer data);
-
- static gboolean __stopMainLoop(gpointer data);
-
- void __onMethodCalled(const std::string& sender,
- const std::string& name, GVariant* param, GDBusMethodInvocation* invocation);
-
- void __run();
- bool __init();
- void __release();
-
- ClientBase* __getClient(const std::string& busName);
- void __removeClient(const std::string& busName);
- unsigned int __watch(const std::string& busName, ClientBase* client);
- void __unwatch(unsigned int watchId);
-
- bool __started;
- std::atomic_bool __threadRunning;
- const char* __serviceName;
-
- GMainContext* __mainContext;
- GMainLoop* __mainLoop;
- GThread* __gthread;
-
- GDBusConnection* __connection;
- const char* __methodSpecs;
- std::string __objPath;
- std::string __interface;
-
- GDBusNodeInfo* __nodeInfo;
- guint __registrationId;
-
- struct __ClientInfo {
- ClientBase* client;
- unsigned int watchId;
- };
-
- std::map<std::string, __ClientInfo> __clients;
- };
-
-}
-
-#endif /* __CONTEXT_SERVICE_BASE_H__ */
namespace ctx {
- class ServiceBase;
class ITimerListener;
/* All timers expire in the context where the Timer object is created. */
class EXPORT_API Timer {
public:
- Timer(ServiceBase* hostService);
Timer(GMainContext* mainContext);
~Timer();
Name: context-common
Summary: Tizen Context-Service Internal Shared Library
-Version: 1.0.3
+Version: 1.1.0
Release: 1
Group: Service Framework/Context
License: Apache-2.0
BuildRequires: pkgconfig(capi-base-common)
BuildRequires: pkgconfig(alarm-service)
BuildRequires: pkgconfig(libtzplatform-config)
-BuildRequires: pkgconfig(cynara-creds-gdbus)
-BuildRequires: pkgconfig(cynara-client)
-BuildRequires: pkgconfig(cynara-session)
%description
Tizen Context-Service Internal Shared Library
SET(target "${target_prefix}-server")
-SET(DEPS "${DEPS} sqlite3 libsystemd-login libtzplatform-config alarm-service cynara-creds-gdbus cynara-client cynara-session")
+SET(DEPS "${DEPS} sqlite3 libsystemd-login libtzplatform-config alarm-service")
FILE(GLOB_RECURSE SRCS *.cpp)
MESSAGE("Sources: ${SRCS}")
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 <ServiceBase.h>
-#include <MethodCall.h>
-#include <ClientBase.h>
-#include "Credential.h"
-
-using namespace ctx;
-
-ClientBase::ClientBase(ServiceBase* hostService, const std::string& busName) :
- __hostService(hostService),
- __busName(busName),
- __credential(NULL)
-{
-}
-
-ClientBase::~ClientBase()
-{
- delete __credential;
-}
-
-const std::string& ClientBase::getBusName() const
-{
- return __busName;
-}
-
-bool ClientBase::__getCredential()
-{
- if (__credential)
- return true;
-
- __credential = new Credential(__hostService->getConnection(), __busName);
-
- return true;
-}
-
-ServiceBase& ClientBase::getHostService()
-{
- return *__hostService;
-}
-
-void ClientBase::publish(const std::string& signalName, GVariant* param)
-{
- getHostService().publish(__busName, signalName, param);
-}
-
-bool ClientBase::isVerified()
-{
- IF_FAIL_RETURN(__getCredential(), false);
- return __credential->valid();
-}
-
-bool ClientBase::hasPrivilege(const char* privil) const
-{
- return __credential->hasPrivilege(privil);
-}
-
-bool ClientBase::hasPrivileges(const std::vector<std::string>& privil) const
-{
- for (auto& item : privil) {
- if (!hasPrivilege(item.c_str()))
- return false;
- }
- return true;
-}
-
-uid_t ClientBase::getUid() const
-{
- return __credential->getUid();
-}
-
-std::string ClientBase::getClientId() const
-{
- return __credential->getClientId();
-}
-
-bool ClientBase::isSystem() const
-{
- return __credential->isSystem();
-}
-
-bool ClientBase::isSystemUid(uid_t uid)
-{
- return Credential::isSystemUid(uid);
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 <systemd/sd-login.h>
-#include <cynara-creds-gdbus.h>
-#include <cynara-session.h>
-#include <cynara-client.h>
-#include <ScopeMutex.h>
-#include "Credential.h"
-
-#define CACHE_SIZE 100
-
-using namespace ctx;
-
-namespace {
-
- GMutex __cynaraMutex;
-
- class PrivilegeChecker {
- private:
- cynara* __cynara;
-
- public:
- PrivilegeChecker() :
- __cynara(NULL)
- {
- ScopeMutex sm(&__cynaraMutex);
-
- int err;
- cynara_configuration* conf = NULL;
-
- err = cynara_configuration_create(&conf);
- IF_FAIL_VOID_TAG(err == CYNARA_API_SUCCESS, _E, "Cynara configuration creation failed");
-
- err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
- if (err != CYNARA_API_SUCCESS) {
- _E("Cynara cache size set failed");
- cynara_configuration_destroy(conf);
- return;
- }
-
- err = cynara_initialize(&__cynara, conf);
- cynara_configuration_destroy(conf);
- if (err != CYNARA_API_SUCCESS) {
- _E("Cynara initialization failed");
- __cynara = NULL;
- return;
- }
-
- _I("Cynara initialized");
- }
-
- ~PrivilegeChecker()
- {
- ScopeMutex sm(&__cynaraMutex);
-
- if (__cynara)
- cynara_finish(__cynara);
-
- _I("Cynara deinitialized");
- }
-
- bool hasPrivilege(const char* client, const char* session, const char* user, const char* privil)
- {
- ScopeMutex sm(&__cynaraMutex);
-
- IF_FAIL_RETURN_TAG(__cynara, false, _E, "Cynara not initialized");
- int ret = cynara_check(__cynara, client, session, user, privil);
- return (ret == CYNARA_API_ACCESS_ALLOWED);
- }
- };
-}
-
-Credential::Credential(GDBusConnection* conn, const std::string& busName) :
- __pid(0),
- __uid(ROOT_UID),
- __clientId(NULL),
- __session(NULL),
- __user(NULL),
- __valid(true)
-{
- ScopeMutex sm(&__cynaraMutex);
-
- if (cynara_creds_gdbus_get_pid(conn, busName.c_str(), &__pid) == CYNARA_API_SUCCESS)
- __session = cynara_session_from_pid(__pid);
-
- cynara_creds_gdbus_get_client(conn, busName.c_str(), CLIENT_METHOD_DEFAULT, &__clientId);
- cynara_creds_gdbus_get_user(conn, busName.c_str(), USER_METHOD_DEFAULT, &__user);
-
- _SD("%d, %s, %s, %s", __pid, __clientId, __session, __user);
-
- if (!__clientId || !__session || !__user) {
- _E("Peer credentialing failed");
- __valid = false;
- }
-
- if (__user)
- __uid = static_cast<uid_t>(std::atoll(__user));
-}
-
-Credential::~Credential()
-{
- g_free(__clientId);
- g_free(__session);
- g_free(__user);
-}
-
-bool Credential::valid() const
-{
- return __valid;
-}
-
-bool Credential::hasPrivilege(const char* privil) const
-{
- IF_FAIL_RETURN(privil, true);
- IF_FAIL_RETURN_TAG(valid(), false, _W, "Unidentified peer");
-
- static PrivilegeChecker privilegeChecker;
-
- _D("Checking '%s' for '%s'", privil, __clientId);
- if (!privilegeChecker.hasPrivilege(__clientId, __session, __user, privil)) {
- _W("Privilege denied");
- return false;
- }
- return true;
-}
-
-uid_t Credential::getUid() const
-{
- return __uid;
-}
-
-std::string Credential::getClientId() const
-{
- return __clientId ? __clientId : "";
-}
-
-bool Credential::isSystem() const
-{
- return isSystemUid(__uid);
-}
-
-bool Credential::isSystemUid(uid_t uid)
-{
- int type = sd_get_uid_type(uid);
- return (type != NORMAL_USER_TYPE && type != CONTAINER_TYPE);
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 <SharedUtil.h>
-#include <ClientBase.h>
-#include <MethodCall.h>
-
-using namespace ctx;
-
-MethodCall::MethodCall(ClientBase* client, const std::string& methodName, GVariant* param, GDBusMethodInvocation* invocation) :
- __client(client),
- __methodName(methodName),
- __param(param),
- __invocation(invocation)
-{
- g_variant_ref(__param);
-}
-
-MethodCall::~MethodCall()
-{
- g_variant_unref(__param);
-
- if (!__invocation)
- return;
-
- _E("Method call '%s' from '%s' is not handled yet", __methodName.c_str(), __client->getBusName().c_str());
-
- g_dbus_method_invocation_return_error_literal(__invocation, CTX_ERROR_DOMAIN, E_FAILED, "");
-}
-
-ClientBase& MethodCall::getSender()
-{
- return *__client;
-}
-
-const std::string& MethodCall::getMethodName() const
-{
- return __methodName;
-}
-
-GVariant* MethodCall::getParam()
-{
- return __param;
-}
-
-bool MethodCall::reply(GVariant* param)
-{
- IF_FAIL_RETURN_TAG(__invocation, false, _E, "Replied already");
-
- g_dbus_method_invocation_return_value(__invocation, param);
- __invocation = NULL;
-
- return true;
-}
-
-bool MethodCall::reply(int error)
-{
- IF_FAIL_RETURN_TAG(__invocation, false, _D, "Replied already");
-
- if (error == E_NONE)
- return reply(static_cast<GVariant*>(NULL));
-
- g_dbus_method_invocation_return_error_literal(__invocation, CTX_ERROR_DOMAIN, error, "");
- __invocation = NULL;
-
- return true;
-}
-
-void MethodCall::publish(const std::string& signalName, GVariant* param)
-{
- getSender().publish(signalName, param);
-}
-
-bool MethodCall::hasPrivilege(const char* privil)
-{
- return getSender().hasPrivilege(privil);
-}
-
-uid_t MethodCall::getUid()
-{
- return getSender().getUid();
-}
-
-std::string MethodCall::getClientId()
-{
- return getSender().getClientId();
-}
-
-bool MethodCall::isSystem()
-{
- return getSender().isSystem();
-}
*/
#include <Tuple.h>
-#include <ClientBase.h>
#include <ServerUtil.h>
#include <PlatformDatabase.h>
{
std::string path = "." + dbName + ".db";
- if (ClientBase::isSystemUid(uid)) {
+ if (util::isSystemUid(uid)) {
return util::getSystemPath(TZ_SYS_DB, path);
} else {
return util::getUserPath(uid, TZ_USER_DB, path);
PlatformDatabase(dbName, ROOT_UID)
{
}
-
-PlatformDatabase::PlatformDatabase(const std::string& dbName, const ClientBase& client) :
- PlatformDatabase(dbName, client.getUid())
-{
-}
-
-PlatformDatabase::PlatformDatabase(const std::string& dbName, const ClientBase* client) :
- PlatformDatabase(dbName, client->getUid())
-{
-}
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
+#include <atomic>
+#include <systemd/sd-login.h>
#include <ScopeMutex.h>
-#include <ClientBase.h>
-#include <ServiceBase.h>
#include <ServerUtil.h>
using namespace ctx;
static GMutex __pathMutex;
+static std::atomic_uint __activeUid;
+
+EXPORT_API uid_t util::setActiveUid(uid_t uid)
+{
+ __activeUid.store(uid);
+ return uid;
+}
+
+EXPORT_API uid_t util::getActiveUid()
+{
+ return __activeUid.load();
+}
+
+EXPORT_API bool util::isSystemUid(uid_t uid)
+{
+ int type = sd_get_uid_type(uid);
+ return (type != NORMAL_USER_TYPE && type != CONTAINER_TYPE);
+}
EXPORT_API std::string util::getSystemPath(enum tzplatform_variable id, const std::string& path)
{
EXPORT_API std::string util::getUserPath(enum tzplatform_variable id, const std::string& path)
{
- return getUserPath(ServiceBase::getActiveUser(), id, path);
+ return getUserPath(util::getActiveUid(), id, path);
}
EXPORT_API std::string util::getUserPath(uid_t uid, enum tzplatform_variable id, const std::string& path)
{
- IF_FAIL_RETURN_TAG(!ClientBase::isSystemUid(uid), EMPTY_STR, _E, "Invalid UID");
+ IF_FAIL_RETURN_TAG(!util::isSystemUid(uid), EMPTY_STR, _E, "Invalid UID");
tzplatform_context* context = NULL;
tzplatform_context_create(&context);
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * 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 <atomic>
-#include <ScopeMutex.h>
-#include <ClientBase.h>
-#include <MethodCall.h>
-#include <ServiceBase.h>
-
-using namespace ctx;
-
-static std::atomic_uint __activeUid;
-
-ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
- __started(false),
- __threadRunning(false),
- __serviceName(serviceName),
- __mainContext(NULL),
- __mainLoop(NULL),
- __gthread(NULL),
- __connection(conn),
- __methodSpecs(methodSpecs),
- __objPath(CTX_DBUS_PATH),
- __interface(CTX_DBUS_IFACE),
- __nodeInfo(NULL),
- __registrationId(0)
-{
- __objPath += serviceName;
- __interface += serviceName;
-}
-
-ServiceBase::~ServiceBase()
-{
-}
-
-GMainContext* ServiceBase::getMainContext()
-{
- return __mainContext;
-}
-
-GDBusConnection* ServiceBase::getConnection()
-{
- return __connection;
-}
-
-bool ServiceBase::start()
-{
- IF_FAIL_RETURN(!__started, true);
-
- _I("Preparing '%s'", __serviceName);
-
- __gthread = g_thread_new(__serviceName, __threadFunc, this);
- IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
-
- __started = true;
- return true;
-}
-
-void ServiceBase::stop()
-{
- IF_FAIL_VOID(__started);
- __started = false;
-
- IF_FAIL_VOID(__threadRunning.load());
-
- GSource *gSrc = g_idle_source_new();
- if (gSrc) {
- // Tries to stop the main loop within its thread.
- // In this way, already scheduled idle tasks are not discarded.
- g_source_set_callback(gSrc, __stopMainLoop, this, NULL);
- g_source_attach(gSrc, __mainContext);
- g_source_unref(gSrc);
- } else {
- __stopMainLoop(this);
- }
-
- _I("Joining the thread of '%s'", __serviceName);
- g_thread_join(__gthread);
- __gthread = NULL;
-}
-
-gboolean ServiceBase::__stopMainLoop(gpointer data)
-{
- ServiceBase* svc = static_cast<ServiceBase*>(data);
- _I(PURPLE("Stopping '%s'"), svc->__serviceName);
- g_main_loop_quit(svc->__mainLoop);
- return G_SOURCE_REMOVE;
-}
-
-void ServiceBase::publish(const std::string& busName, const std::string& signalName, GVariant* param)
-{
- GError* gerr = NULL;
- g_dbus_connection_emit_signal(__connection,
- busName.c_str(), __objPath.c_str(), __interface.c_str(),
- signalName.c_str(), param, &gerr);
- HANDLE_GERROR(gerr);
-}
-
-uid_t ServiceBase::getActiveUser()
-{
- return __activeUid.load();
-}
-
-void ServiceBase::setActiveUser(uid_t uid)
-{
- __activeUid.store(uid);
-}
-
-void ServiceBase::onUserActivated()
-{
-}
-
-void ServiceBase::onUserDeactivated()
-{
-}
-
-void ServiceBase::notifyUserNew()
-{
- IF_FAIL_VOID(__started);
- IF_FAIL_VOID(__threadRunning.load());
-
- GSource* gSrc = g_idle_source_new();
- IF_FAIL_VOID_TAG(gSrc, _E, E_STR_ALLOC);
-
- g_source_set_callback(gSrc, __onUserActivated, this, NULL);
- g_source_attach(gSrc, __mainContext);
- g_source_unref(gSrc);
-}
-
-void ServiceBase::notifyUserRemoved()
-{
- IF_FAIL_VOID(__started);
- IF_FAIL_VOID(__threadRunning.load());
-
- GSource* gSrc = g_idle_source_new();
- IF_FAIL_VOID_TAG(gSrc, _E, E_STR_ALLOC);
-
- g_source_set_callback(gSrc, __onUserDeactivated, this, NULL);
- g_source_attach(gSrc, __mainContext);
- g_source_unref(gSrc);
-}
-
-gboolean ServiceBase::__onUserActivated(gpointer data)
-{
- ServiceBase* svc = static_cast<ServiceBase*>(data);
- svc->onUserActivated();
- return G_SOURCE_REMOVE;
-}
-
-gboolean ServiceBase::__onUserDeactivated(gpointer data)
-{
- ServiceBase* svc = static_cast<ServiceBase*>(data);
- svc->onUserDeactivated();
- return G_SOURCE_REMOVE;
-}
-
-gpointer ServiceBase::__threadFunc(gpointer data)
-{
- ServiceBase* service = static_cast<ServiceBase*>(data);
- service->__run();
- return NULL;
-}
-
-void ServiceBase::__run()
-{
- if (!__init()) {
- _E("Starting '%s' failed", __serviceName);
- __release();
- return;
- }
-
- __threadRunning.store(true);
-
- _I(CYAN("Starting '%s'"), __serviceName);
- g_main_loop_run(__mainLoop);
-
- __threadRunning.store(false);
-
- __release();
-}
-
-bool ServiceBase::__init()
-{
- GError* gerr = NULL;
- GDBusInterfaceVTable vtable;
-
- vtable.method_call = __onMethodCalled;
- vtable.get_property = NULL;
- vtable.set_property = NULL;
-
- __mainContext = g_main_context_new();
- IF_FAIL_RETURN_TAG(__mainContext, false, _E, "MainContext creation failed");
-
- g_main_context_push_thread_default(__mainContext);
-
- __mainLoop = g_main_loop_new(__mainContext, FALSE);
- IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "MainLoop creation failed");
-
- std::string introspection("<node><interface name='");
- introspection += __interface + "'>" + __methodSpecs + "</interface></node>";
-
- __nodeInfo = g_dbus_node_info_new_for_xml(introspection.c_str(), NULL);
- IF_FAIL_RETURN_TAG(__nodeInfo, false, _E, "NodeInfo creation failed");
-
- __registrationId = g_dbus_connection_register_object(__connection,
- __objPath.c_str(), __nodeInfo->interfaces[0], &vtable, this, NULL, &gerr);
- HANDLE_GERROR(gerr);
- IF_FAIL_RETURN_TAG(__registrationId > 0, false, _E, "Object registration failed");
-
- return prepare();
-}
-
-void ServiceBase::__release()
-{
- _D("Releasing '%s'", __serviceName);
-
- for (auto iter = __clients.begin(); iter != __clients.end(); ++iter) {
- iter->second.client->onDisconnected();
- delete iter->second.client;
- }
-
- __clients.clear();
-
- cleanup();
-
- if (__registrationId > 0)
- g_dbus_connection_unregister_object(__connection, __registrationId);
-
- if (__nodeInfo)
- g_dbus_node_info_unref(__nodeInfo);
-
- if (__mainLoop)
- g_main_loop_unref(__mainLoop);
-
- if (__mainContext)
- g_main_context_unref(__mainContext);
-}
-
-void ServiceBase::__onMethodCalled(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, GDBusMethodInvocation* invocation, gpointer userData)
-{
- ServiceBase* service = static_cast<ServiceBase*>(userData);
- service->__onMethodCalled(sender, name, param, invocation);
-}
-
-void ServiceBase::__onMethodCalled(const std::string& sender,
- const std::string& name, GVariant* param, GDBusMethodInvocation* invocation)
-{
- _I("'%s' called '%s.%s'", sender.c_str(), __serviceName, name.c_str());
-
- ClientBase* client = __getClient(sender);
- IF_FAIL_VOID(client);
-
- client->onMethodCalled(new MethodCall(client, name, param, invocation));
-}
-
-ClientBase* ServiceBase::__getClient(const std::string& busName)
-{
- auto iter = __clients.find(busName);
-
- if (iter != __clients.end())
- return iter->second.client;
-
- ClientBase* client = createClient(busName);
- IF_FAIL_RETURN_TAG(client, NULL, _E, "Client creation failed");
-
- if (!client->isVerified()) {
- delete client;
- return NULL;
- }
-
- __ClientInfo info = {client, __watch(busName, client)};
- __clients[busName] = info;
-
- return client;
-}
-
-void ServiceBase::__onNameOwnerChanged(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData)
-{
- ClientBase* client = static_cast<ClientBase*>(userData);
- client->getHostService().__removeClient(client->getBusName());
-}
-
-void ServiceBase::__removeClient(const std::string& busName)
-{
- _I("'%s' lost '%s'", __serviceName, busName.c_str());
-
- auto iter = __clients.find(busName);
- IF_FAIL_VOID(iter != __clients.end());
-
- __ClientInfo info = iter->second;
- __clients.erase(iter);
-
- __unwatch(info.watchId);
- info.client->onDisconnected();
- delete info.client;
-}
-
-unsigned int ServiceBase::__watch(const std::string& busName, ClientBase* client)
-{
- return g_dbus_connection_signal_subscribe(__connection,
- "org.freedesktop.DBus", "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus",
- busName.c_str(), G_DBUS_SIGNAL_FLAGS_NONE, __onNameOwnerChanged, client, NULL);
-}
-
-void ServiceBase::__unwatch(unsigned int watchId)
-{
- g_dbus_connection_signal_unsubscribe(__connection, watchId);
-}
#include <atomic>
#include <alarm.h>
-#include <ServiceBase.h>
#include <ITimerListener.h>
#include <Timer.h>
{
}
-Timer::Timer(ServiceBase* hostService) :
- Timer(hostService->getMainContext())
-{
-}
-
Timer::~Timer()
{
cancelAll();