PROJECT(context-service)
INCLUDE(GNUInstallDirs)
-SET(target "contextd")
-
-SET(DEPS "libsystemd-login glib-2.0 gio-2.0 dlog capi-base-common alarm-service")
-SET(DEPS "${DEPS} context-common-server")
-SET(DEPS "${DEPS} context-app-history-server")
-SET(DEPS "${DEPS} context-sensor-recorder-server")
-SET(DEPS "${DEPS} context-store-server")
-SET(DEPS "${DEPS} context-job-scheduler-server")
-
-SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service")
-
-INCLUDE_DIRECTORIES(
- ${CMAKE_INSTALL_PREFIX}/${INCDIR}/private
-)
-
-FILE(GLOB_RECURSE SRCS src/*.cpp)
-MESSAGE("Sources: ${SRCS}")
-
ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden)
-ADD_DEFINITIONS(-DLOG_TAG="CONTEXT")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section")
-INCLUDE(FindPkgConfig)
-pkg_check_modules(DEPS_PKG REQUIRED ${DEPS})
-
-FOREACH(flag ${DEPS_PKG_CFLAGS})
- SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
-ENDFOREACH(flag)
-
-ADD_EXECUTABLE(${target} ${SRCS})
-TARGET_LINK_LIBRARIES(${target} ${DEPS_PKG_LDFLAGS})
-
-INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
+ADD_SUBDIRECTORY(src/server)
+ADD_SUBDIRECTORY(src/agent)
+++ /dev/null
-[Unit]
-Description=Contextual Service Framework Daemon
-
-[Service]
-User=service_fw
-Group=service_fw
-Type=dbus
-BusName=org.tizen.context
-ExecStart=/usr/bin/contextd
-Restart=on-failure
-RestartSec=1
-
-[Install]
-WantedBy=default.target
Group: Service/Context
License: Apache-2.0
Source0: %{name}-%{version}.tar.gz
-Source1: context-service.service
+Source1: contextd.service
Source2: org.tizen.context.conf
+Source3: contextd-agent.service
+Source4: org.tizen.contextagent.conf
+Source5: org.tizen.contextagent.service
BuildRequires: cmake
BuildRequires: pkgconfig(libsystemd-login)
%description
This is a systemd service that contains all features provided by the Tizen Contextual Service Framework.
+
+%package -n context-agent
+Summary: User-level agent service of the Tizen Contextual Service Framework
+
+%description -n context-agent
+This is a systemd service that runs in the user session to assist the main service daemon of the Contextual Service Framework.
+
+
%prep
%setup -q
mkdir -p %{buildroot}%{_unitdir}/default.target.wants
install -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}
-ln -s ../context-service.service %{buildroot}%{_unitdir}/default.target.wants/context-service.service
+ln -s ../contextd.service %{buildroot}%{_unitdir}/default.target.wants/contextd.service
mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d
install -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/dbus-1/system.d/
+mkdir -p %{buildroot}%{_unitdir_user}
+install -m 0644 %{SOURCE3} %{buildroot}%{_unitdir_user}
+
+mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/session.d
+install -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/dbus-1/session.d/
+
+mkdir -p %{buildroot}/usr/share/dbus-1/services
+install -m 0644 %{SOURCE5} %{buildroot}/usr/share/dbus-1/services/
+
%files
%manifest packaging/%{name}.manifest
%config %{_sysconfdir}/dbus-1/system.d/*
-%{_bindir}/*
-%{_unitdir}/context-service.service
-%{_unitdir}/*/context-service.service
+%{_bindir}/contextd
+%{_unitdir}/contextd.service
+%{_unitdir}/*/contextd.service
+%license LICENSE
+
+%files -n context-agent
+%manifest packaging/%{name}.manifest
+%config %{_sysconfdir}/dbus-1/session.d/*
+%{_bindir}/contextd-agent
+%{_unitdir_user}/contextd-agent.service
+/usr/share/dbus-1/services/*
%license LICENSE
--- /dev/null
+[Unit]
+Description=Contextual Service Framework User-Level Agent Daemon
+
+[Service]
+Type=dbus
+BusName=org.tizen.contextagent
+ExecStart=/usr/bin/contextd-agent
+Restart=on-failure
+RestartSec=1
--- /dev/null
+[Unit]
+Description=Contextual Service Framework Server Daemon
+
+[Service]
+User=service_fw
+Group=service_fw
+Type=dbus
+BusName=org.tizen.context
+ExecStart=/usr/bin/contextd
+Restart=on-failure
+RestartSec=1
+
+[Install]
+WantedBy=default.target
--- /dev/null
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+ <policy smack="System">
+ <deny own="org.tizen.contextagent"/>
+ <allow send_destination="org.tizen.contextagent"/>
+ </policy>
+ <policy smack="User">
+ <allow own="org.tizen.contextagent"/>
+ <allow send_destination="org.tizen.contextagent"/>
+ </policy>
+ <policy context="default">
+ <allow send_destination="org.tizen.contextagent"/>
+ </policy>
+</busconfig>
--- /dev/null
+[Unit]
+Description=Contextual Service Framework User-Level Agent D-Bus
+
+[D-BUS Service]
+Name=org.tizen.contextagent
+Exec=/bin/false
+SystemdService=contextd-agent.service
+++ /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 "ActiveUserMonitor.h"
-
-#define ROOT_UID 0
-
-using namespace ctx;
-
-ActiveUserMonitor::ActiveUserMonitor() :
- __connection(NULL),
- __activateUser(NULL),
- __deactivateUser(NULL),
- __activeUid(ROOT_UID),
- __userNewSignalId(0),
- __userRemovedSignalId(0)
-{
-}
-
-ActiveUserMonitor::~ActiveUserMonitor()
-{
-}
-
-void ActiveUserMonitor::start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser)
-{
- __connection = conn;
- __activateUser = activateUser;
- __deactivateUser = deactivateUser;
-
- __userNewSignalId = g_dbus_connection_signal_subscribe(__connection,
- NULL, "org.freedesktop.login1.Manager", "UserNew", NULL,
- NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserNew, this, NULL);
-
- __userRemovedSignalId = g_dbus_connection_signal_subscribe(__connection,
- NULL, "org.freedesktop.login1.Manager", "UserRemoved", NULL,
- NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserRemoved, this, NULL);
-
- g_timeout_add(200, __checkCurrentUser, this);
-}
-
-void ActiveUserMonitor::stop()
-{
- g_dbus_connection_signal_unsubscribe(__connection, __userNewSignalId);
- g_dbus_connection_signal_unsubscribe(__connection, __userRemovedSignalId);
-}
-
-void ActiveUserMonitor::__onUserNew(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData)
-{
- uint32_t uid = 0;
- g_variant_get_child(param, 0, "u", &uid);
- IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
-
- _D("UID: %u", uid);
-
- ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
-
- if (monitor->__activeUid == uid)
- return;
-
- if (monitor->__activeUid > ROOT_UID) {
- _W("Over-activation of the user %u", uid);
- monitor->__deactivateUser(monitor->__activeUid);
- }
-
- monitor->__activateUser(uid);
- monitor->__activeUid = uid;
-}
-
-void ActiveUserMonitor::__onUserRemoved(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData)
-{
- uint32_t uid = 0;
- g_variant_get_child(param, 0, "u", &uid);
- IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
-
- _D("UID: %u", uid);
-
- ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
-
- if (monitor->__activeUid == ROOT_UID) {
- _W("No active user");
- return;
- }
-
- if (monitor->__activeUid != uid) {
- _W("Mismatched uid");
- }
-
- monitor->__deactivateUser(uid);
- monitor->__activeUid = ROOT_UID;
-}
-
-gboolean ActiveUserMonitor::__checkCurrentUser(gpointer userData)
-{
- ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
- IF_FAIL_RETURN(monitor->__activeUid == ROOT_UID, G_SOURCE_REMOVE);
-
- uid_t* users = NULL;
- int numUsers = sd_get_active_uids(&users);
-
- if (numUsers > 0)
- monitor->__activeUid = users[0];
-
- g_free(users);
-
- if (monitor->__activeUid != ROOT_UID) {
- _D("UID: %u", monitor->__activeUid);
- monitor->__activateUser(monitor->__activeUid);
- }
-
- return G_SOURCE_REMOVE;
-}
+++ /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_ACTIVE_USER_MONITOR_H__
-#define __CONTEXT_ACTIVE_USER_MONITOR_H__
-
-#include <ContextTypes.h>
-
-namespace ctx {
-
- class ActiveUserMonitor {
-
- typedef void (*uid_cb_t)(uid_t);
-
- public:
- ActiveUserMonitor();
- ~ActiveUserMonitor();
-
- void start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser);
- void stop();
-
- private:
- static void __onUserNew(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData);
-
- static void __onUserRemoved(GDBusConnection* conn, const gchar* sender,
- const gchar* path, const gchar* iface, const gchar* name,
- GVariant* param, gpointer userData);
-
- static gboolean __checkCurrentUser(gpointer userData);
-
- GDBusConnection* __connection;
- uid_cb_t __activateUser;
- uid_cb_t __deactivateUser;
- uid_t __activeUid;
- guint __userNewSignalId;
- guint __userRemovedSignalId;
- };
-
-}
-
-#endif
+++ /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 <alarm.h>
-#include <ContextTypes.h>
-#include "AlarmInitializer.h"
-
-#define IDENTIFIER "contextd"
-
-using namespace ctx;
-
-static bool __initialized = false;
-
-AlarmInitializer::AlarmInitializer()
-{
- _I("Init Alarm");
- int result = alarmmgr_init(IDENTIFIER);
- if (result != ALARMMGR_RESULT_SUCCESS) {
- _E("Alarm manager initialization failed");
- return;
- }
- alarmmgr_remove_all();
- __initialized = true;
-}
-
-AlarmInitializer::~AlarmInitializer()
-{
- if (!__initialized)
- return;
-
- _I("Release Alarm");
- alarmmgr_remove_all();
- alarmmgr_fini();
- __initialized = false;
-}
+++ /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_ALARM_INITIALIZER_H__
-#define __CONTEXT_ALARM_INITIALIZER_H__
-
-namespace ctx {
-
- class AlarmInitializer {
- public:
- AlarmInitializer();
- ~AlarmInitializer();
- };
-
-}
-
-#endif
+++ /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 "DBusConnector.h"
-
-using namespace ctx;
-
-DBusConnector::DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb) :
- __owner(-1),
- __connection(NULL),
- __busAcquiredCb(acqCb),
- __busLostCb(lostCb)
-{
- __init();
-}
-
-DBusConnector::~DBusConnector()
-{
- __release();
-}
-
-void DBusConnector::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
- DBusConnector* connector = static_cast<DBusConnector*>(userData);
- connector->__connection = conn;
-}
-
-void DBusConnector::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
- _I(GREEN("Acquired"));
- DBusConnector* connector = static_cast<DBusConnector*>(userData);
- connector->__busAcquiredCb(conn);
-}
-
-void DBusConnector::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
- _E(RED("Lost"));
- DBusConnector* connector = static_cast<DBusConnector*>(userData);
- connector->__busLostCb(conn);
-}
-
-void DBusConnector::__init()
-{
- __owner = g_bus_own_name(G_BUS_TYPE_SYSTEM, CTX_DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
- __onBusAcquired, __onNameAcquired, __onNameLost, this, NULL);
-}
-
-void DBusConnector::__release()
-{
- if (!__connection)
- return;
-
- g_dbus_connection_close_sync(__connection, NULL, NULL);
- g_object_unref(__connection);
-}
+++ /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_DBUS_CONNECTOR_H__
-#define __CONTEXT_DBUS_CONNECTOR_H__
-
-#include <ContextTypes.h>
-
-namespace ctx {
-
- typedef void (*bus_acquired_cb_t)(GDBusConnection *conn);
- typedef void (*bus_lost_cb_t)(GDBusConnection *conn);
-
- class DBusConnector {
- public:
- DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb);
- ~DBusConnector();
-
- private:
- static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
- static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
- static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
-
- void __init();
- void __release();
-
- guint __owner;
- GDBusConnection *__connection;
- bus_acquired_cb_t __busAcquiredCb;
- bus_lost_cb_t __busLostCb;
-
- };
-
-}
-
-#endif
+++ /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 <cstdlib>
-#include <cstring>
-#include <ContextTypes.h>
-#include <Timer.h>
-#include "DBusConnector.h"
-#include "ServiceLoader.h"
-#include "ActiveUserMonitor.h"
-#include "AlarmInitializer.h"
-
-using namespace ctx;
-
-namespace {
- class MainLoop {
- public:
- static bool start();
- static void stop();
- private:
- MainLoop() {}
- static GMainLoop* __mainLoop;
- };
-}
-
-GMainLoop* MainLoop::__mainLoop = NULL;
-
-bool MainLoop::start()
-{
- __mainLoop = g_main_loop_new(NULL, FALSE);
- IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed");
-
- _I(CYAN("Starting..."));
- g_main_loop_run(__mainLoop);
-
- g_main_loop_unref(__mainLoop);
-
- return true;
-}
-
-void MainLoop::stop()
-{
- _I(PURPLE("Terminating..."));
- g_main_loop_quit(__mainLoop);
-}
-
-static ServiceLoader __serviceLoader;
-static ActiveUserMonitor __activeUserMonitor;
-static AlarmInitializer __alarmInit;
-
-static void __activateUser(uid_t uid)
-{
- __serviceLoader.startUser(uid);
-}
-
-static void __deactivateUser(uid_t uid)
-{
- __serviceLoader.stopUser();
-}
-
-static void __startService(GDBusConnection* conn)
-{
- Timer::setDBusConnection(conn);
- __activeUserMonitor.start(conn, __activateUser, __deactivateUser);
-
- _I("Loading services");
- if (!__serviceLoader.load(conn)) {
- _E(RED("No service loaded"));
- return;
- }
-
- _I("Service loading successful");
- __serviceLoader.startSystem();
-}
-
-static gboolean __stopService(gpointer data)
-{
- __activeUserMonitor.stop();
-
- _I("Unloading services");
- __serviceLoader.stopUser();
- __serviceLoader.stopSystem();
-
- MainLoop::stop();
- return G_SOURCE_REMOVE;
-}
-
-static void __busAcquired(GDBusConnection* conn)
-{
- __startService(conn);
-}
-
-static void __busLost(GDBusConnection* conn)
-{
- __stopService(NULL);
-}
-
-static void __signalHandler(int signum)
-{
- _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
- static bool terminated = false;
- if (!terminated) {
- g_idle_add(__stopService, NULL);
- terminated = true;
- }
-}
-
-int main(int argc, char* argv[])
-{
- static struct sigaction signalAction;
- signalAction.sa_handler = __signalHandler;
- sigemptyset(&signalAction.sa_mask);
-
- sigaction(SIGINT, &signalAction, NULL);
- sigaction(SIGHUP, &signalAction, NULL);
- sigaction(SIGTERM, &signalAction, NULL);
- sigaction(SIGQUIT, &signalAction, NULL);
- sigaction(SIGABRT, &signalAction, NULL);
-
- DBusConnector dbusConnector(__busAcquired, __busLost);
-
- MainLoop::start();
-
- return EXIT_SUCCESS;
-}
+++ /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 <AppHistoryService.h>
-#include <SensorRecorderService.h>
-#include <ContextStoreService.h>
-#include <JobSchedulerService.h>
-
-#include "ServiceLoader.h"
-
-#define ROOT_UID 0
-
-using namespace ctx;
-
-ServiceLoader::ServiceLoader() :
- __activeUser(ROOT_UID)
-{
-}
-
-ServiceLoader::~ServiceLoader()
-{
- for (auto& svc : __userServices) {
- delete svc;
- }
- for (auto& svc : __systemServices) {
- delete svc;
- }
-}
-
-bool ServiceLoader::load(GDBusConnection* conn)
-{
- __create<AppHistoryService>(conn);
- __create<SensorRecorderService>(conn);
- __create<ContextStoreService>(conn);
- __create<JobSchedulerService>(conn);
-
- return (!__systemServices.empty() || !__userServices.empty());
-}
-
-void ServiceLoader::startSystem()
-{
- for (auto& svc : __systemServices) {
- svc->start();
- }
-}
-
-void ServiceLoader::stopSystem()
-{
- for (auto& svc : __systemServices) {
- svc->stop();
- }
-}
-
-void ServiceLoader::startUser(uid_t uid)
-{
- IF_FAIL_VOID(__activeUser != uid);
- _I("Starting services for %u", static_cast<unsigned int>(uid));
-
- ServiceBase::setActiveUser(uid);
-
- for (auto& svc : __userServices) {
- svc->start();
- }
-
- for (auto& svc : __systemServices) {
- svc->notifyUserNew();
- }
-
- __activeUser = uid;
-}
-
-void ServiceLoader::stopUser()
-{
- IF_FAIL_VOID(__activeUser != ROOT_UID);
- _I("Stopping services for %u", static_cast<unsigned int>(__activeUser));
-
- for (auto& svc : __userServices) {
- svc->stop();
- }
-
- ServiceBase::setActiveUser(ROOT_UID);
-
- for (auto& svc : __systemServices) {
- svc->notifyUserRemoved();
- }
-
- __activeUser = ROOT_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.
- */
-
-#ifndef __CONTEXT_SERVICE_LOADER_H__
-#define __CONTEXT_SERVICE_LOADER_H__
-
-#include <vector>
-#include <ContextTypes.h>
-#include <ServiceBase.h>
-
-namespace ctx {
-
- class ServiceLoader {
- public:
- ServiceLoader();
- ~ServiceLoader();
-
- bool load(GDBusConnection* conn);
-
- void startUser(uid_t uid);
- void stopUser();
- void startSystem();
- void stopSystem();
-
- private:
- uid_t __activeUser;
- std::vector<ServiceBase*> __userServices;
- std::vector<ServiceBase*> __systemServices;
-
- template<typename ServiceType> void __create(GDBusConnection* conn)
- {
- ServiceBase *svc = NULL;
- try {
- svc = new ServiceType(conn);
- } catch (std::exception& e) {
- _W("Service creation failed (%s)", e.what());
- return;
- }
- if (svc->isUserService()) {
- __userServices.push_back(svc);
- } else {
- __systemServices.push_back(svc);
- }
- }
- };
-
-}
-
-#endif
--- /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 <cstdlib>
+
+int main(int argc, char* argv[])
+{
+ return EXIT_SUCCESS;
+}
--- /dev/null
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(target "contextd-agent")
+
+SET(DEPS glib-2.0 gio-2.0 dlog capi-base-common)
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(DEPS_AGENT_PKG REQUIRED ${DEPS})
+
+FOREACH(flag ${DEPS_AGENT_PKG_CFLAGS})
+ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_EXECUTABLE(${target} ${SRCS})
+SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT-AGENT\"")
+TARGET_LINK_LIBRARIES(${target} ${DEPS_AGENT_PKG_LDFLAGS})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
--- /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 "ActiveUserMonitor.h"
+
+#define ROOT_UID 0
+
+using namespace ctx;
+
+ActiveUserMonitor::ActiveUserMonitor() :
+ __connection(NULL),
+ __activateUser(NULL),
+ __deactivateUser(NULL),
+ __activeUid(ROOT_UID),
+ __userNewSignalId(0),
+ __userRemovedSignalId(0)
+{
+}
+
+ActiveUserMonitor::~ActiveUserMonitor()
+{
+}
+
+void ActiveUserMonitor::start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser)
+{
+ __connection = conn;
+ __activateUser = activateUser;
+ __deactivateUser = deactivateUser;
+
+ __userNewSignalId = g_dbus_connection_signal_subscribe(__connection,
+ NULL, "org.freedesktop.login1.Manager", "UserNew", NULL,
+ NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserNew, this, NULL);
+
+ __userRemovedSignalId = g_dbus_connection_signal_subscribe(__connection,
+ NULL, "org.freedesktop.login1.Manager", "UserRemoved", NULL,
+ NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserRemoved, this, NULL);
+
+ g_timeout_add(200, __checkCurrentUser, this);
+}
+
+void ActiveUserMonitor::stop()
+{
+ g_dbus_connection_signal_unsubscribe(__connection, __userNewSignalId);
+ g_dbus_connection_signal_unsubscribe(__connection, __userRemovedSignalId);
+}
+
+void ActiveUserMonitor::__onUserNew(GDBusConnection* conn, const gchar* sender,
+ const gchar* path, const gchar* iface, const gchar* name,
+ GVariant* param, gpointer userData)
+{
+ uint32_t uid = 0;
+ g_variant_get_child(param, 0, "u", &uid);
+ IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
+
+ _D("UID: %u", uid);
+
+ ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+
+ if (monitor->__activeUid == uid)
+ return;
+
+ if (monitor->__activeUid > ROOT_UID) {
+ _W("Over-activation of the user %u", uid);
+ monitor->__deactivateUser(monitor->__activeUid);
+ }
+
+ monitor->__activateUser(uid);
+ monitor->__activeUid = uid;
+}
+
+void ActiveUserMonitor::__onUserRemoved(GDBusConnection* conn, const gchar* sender,
+ const gchar* path, const gchar* iface, const gchar* name,
+ GVariant* param, gpointer userData)
+{
+ uint32_t uid = 0;
+ g_variant_get_child(param, 0, "u", &uid);
+ IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
+
+ _D("UID: %u", uid);
+
+ ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+
+ if (monitor->__activeUid == ROOT_UID) {
+ _W("No active user");
+ return;
+ }
+
+ if (monitor->__activeUid != uid) {
+ _W("Mismatched uid");
+ }
+
+ monitor->__deactivateUser(uid);
+ monitor->__activeUid = ROOT_UID;
+}
+
+gboolean ActiveUserMonitor::__checkCurrentUser(gpointer userData)
+{
+ ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+ IF_FAIL_RETURN(monitor->__activeUid == ROOT_UID, G_SOURCE_REMOVE);
+
+ uid_t* users = NULL;
+ int numUsers = sd_get_active_uids(&users);
+
+ if (numUsers > 0)
+ monitor->__activeUid = users[0];
+
+ g_free(users);
+
+ if (monitor->__activeUid != ROOT_UID) {
+ _D("UID: %u", monitor->__activeUid);
+ monitor->__activateUser(monitor->__activeUid);
+ }
+
+ return G_SOURCE_REMOVE;
+}
--- /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_ACTIVE_USER_MONITOR_H__
+#define __CONTEXT_ACTIVE_USER_MONITOR_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+ class ActiveUserMonitor {
+
+ typedef void (*uid_cb_t)(uid_t);
+
+ public:
+ ActiveUserMonitor();
+ ~ActiveUserMonitor();
+
+ void start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser);
+ void stop();
+
+ private:
+ static void __onUserNew(GDBusConnection* conn, const gchar* sender,
+ const gchar* path, const gchar* iface, const gchar* name,
+ GVariant* param, gpointer userData);
+
+ static void __onUserRemoved(GDBusConnection* conn, const gchar* sender,
+ const gchar* path, const gchar* iface, const gchar* name,
+ GVariant* param, gpointer userData);
+
+ static gboolean __checkCurrentUser(gpointer userData);
+
+ GDBusConnection* __connection;
+ uid_cb_t __activateUser;
+ uid_cb_t __deactivateUser;
+ uid_t __activeUid;
+ guint __userNewSignalId;
+ guint __userRemovedSignalId;
+ };
+
+}
+
+#endif
--- /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 <alarm.h>
+#include <ContextTypes.h>
+#include "AlarmInitializer.h"
+
+#define IDENTIFIER "contextd"
+
+using namespace ctx;
+
+static bool __initialized = false;
+
+AlarmInitializer::AlarmInitializer()
+{
+ _I("Init Alarm");
+ int result = alarmmgr_init(IDENTIFIER);
+ if (result != ALARMMGR_RESULT_SUCCESS) {
+ _E("Alarm manager initialization failed");
+ return;
+ }
+ alarmmgr_remove_all();
+ __initialized = true;
+}
+
+AlarmInitializer::~AlarmInitializer()
+{
+ if (!__initialized)
+ return;
+
+ _I("Release Alarm");
+ alarmmgr_remove_all();
+ alarmmgr_fini();
+ __initialized = false;
+}
--- /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_ALARM_INITIALIZER_H__
+#define __CONTEXT_ALARM_INITIALIZER_H__
+
+namespace ctx {
+
+ class AlarmInitializer {
+ public:
+ AlarmInitializer();
+ ~AlarmInitializer();
+ };
+
+}
+
+#endif
--- /dev/null
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(target "contextd")
+
+SET(DEPS libsystemd-login glib-2.0 gio-2.0 dlog capi-base-common alarm-service)
+SET(DEPS ${DEPS} context-common-server)
+SET(DEPS ${DEPS} context-app-history-server)
+SET(DEPS ${DEPS} context-sensor-recorder-server)
+SET(DEPS ${DEPS} context-store-server)
+SET(DEPS ${DEPS} context-job-scheduler-server)
+
+SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service")
+
+INCLUDE_DIRECTORIES(
+ ${CMAKE_INSTALL_PREFIX}/${INCDIR}/private
+)
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(DEPS_SERVER_PKG REQUIRED ${DEPS})
+
+FOREACH(flag ${DEPS_SERVER_PKG_CFLAGS})
+ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_EXECUTABLE(${target} ${SRCS})
+SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT\"")
+TARGET_LINK_LIBRARIES(${target} ${DEPS_SERVER_PKG_LDFLAGS})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
--- /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 "DBusConnector.h"
+
+using namespace ctx;
+
+DBusConnector::DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb) :
+ __owner(-1),
+ __connection(NULL),
+ __busAcquiredCb(acqCb),
+ __busLostCb(lostCb)
+{
+ __init();
+}
+
+DBusConnector::~DBusConnector()
+{
+ __release();
+}
+
+void DBusConnector::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ DBusConnector* connector = static_cast<DBusConnector*>(userData);
+ connector->__connection = conn;
+}
+
+void DBusConnector::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ _I(GREEN("Acquired"));
+ DBusConnector* connector = static_cast<DBusConnector*>(userData);
+ connector->__busAcquiredCb(conn);
+}
+
+void DBusConnector::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ _E(RED("Lost"));
+ DBusConnector* connector = static_cast<DBusConnector*>(userData);
+ connector->__busLostCb(conn);
+}
+
+void DBusConnector::__init()
+{
+ __owner = g_bus_own_name(G_BUS_TYPE_SYSTEM, CTX_DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
+ __onBusAcquired, __onNameAcquired, __onNameLost, this, NULL);
+}
+
+void DBusConnector::__release()
+{
+ if (!__connection)
+ return;
+
+ g_dbus_connection_close_sync(__connection, NULL, NULL);
+ g_object_unref(__connection);
+}
--- /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_DBUS_CONNECTOR_H__
+#define __CONTEXT_DBUS_CONNECTOR_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+ typedef void (*bus_acquired_cb_t)(GDBusConnection *conn);
+ typedef void (*bus_lost_cb_t)(GDBusConnection *conn);
+
+ class DBusConnector {
+ public:
+ DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb);
+ ~DBusConnector();
+
+ private:
+ static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+ static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+ static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
+
+ void __init();
+ void __release();
+
+ guint __owner;
+ GDBusConnection *__connection;
+ bus_acquired_cb_t __busAcquiredCb;
+ bus_lost_cb_t __busLostCb;
+
+ };
+
+}
+
+#endif
--- /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 <cstdlib>
+#include <cstring>
+#include <ContextTypes.h>
+#include <Timer.h>
+#include "DBusConnector.h"
+#include "ServiceLoader.h"
+#include "ActiveUserMonitor.h"
+#include "AlarmInitializer.h"
+
+using namespace ctx;
+
+namespace {
+ class MainLoop {
+ public:
+ static bool start();
+ static void stop();
+ private:
+ MainLoop() {}
+ static GMainLoop* __mainLoop;
+ };
+}
+
+GMainLoop* MainLoop::__mainLoop = NULL;
+
+bool MainLoop::start()
+{
+ __mainLoop = g_main_loop_new(NULL, FALSE);
+ IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed");
+
+ _I(CYAN("Starting..."));
+ g_main_loop_run(__mainLoop);
+
+ g_main_loop_unref(__mainLoop);
+
+ return true;
+}
+
+void MainLoop::stop()
+{
+ _I(PURPLE("Terminating..."));
+ g_main_loop_quit(__mainLoop);
+}
+
+static ServiceLoader __serviceLoader;
+static ActiveUserMonitor __activeUserMonitor;
+static AlarmInitializer __alarmInit;
+
+static void __activateUser(uid_t uid)
+{
+ __serviceLoader.startUser(uid);
+}
+
+static void __deactivateUser(uid_t uid)
+{
+ __serviceLoader.stopUser();
+}
+
+static void __startService(GDBusConnection* conn)
+{
+ Timer::setDBusConnection(conn);
+ __activeUserMonitor.start(conn, __activateUser, __deactivateUser);
+
+ _I("Loading services");
+ if (!__serviceLoader.load(conn)) {
+ _E(RED("No service loaded"));
+ return;
+ }
+
+ _I("Service loading successful");
+ __serviceLoader.startSystem();
+}
+
+static gboolean __stopService(gpointer data)
+{
+ __activeUserMonitor.stop();
+
+ _I("Unloading services");
+ __serviceLoader.stopUser();
+ __serviceLoader.stopSystem();
+
+ MainLoop::stop();
+ return G_SOURCE_REMOVE;
+}
+
+static void __busAcquired(GDBusConnection* conn)
+{
+ __startService(conn);
+}
+
+static void __busLost(GDBusConnection* conn)
+{
+ __stopService(NULL);
+}
+
+static void __signalHandler(int signum)
+{
+ _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
+ static bool terminated = false;
+ if (!terminated) {
+ g_idle_add(__stopService, NULL);
+ terminated = true;
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ static struct sigaction signalAction;
+ signalAction.sa_handler = __signalHandler;
+ sigemptyset(&signalAction.sa_mask);
+
+ sigaction(SIGINT, &signalAction, NULL);
+ sigaction(SIGHUP, &signalAction, NULL);
+ sigaction(SIGTERM, &signalAction, NULL);
+ sigaction(SIGQUIT, &signalAction, NULL);
+ sigaction(SIGABRT, &signalAction, NULL);
+
+ DBusConnector dbusConnector(__busAcquired, __busLost);
+
+ MainLoop::start();
+
+ return EXIT_SUCCESS;
+}
--- /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 <AppHistoryService.h>
+#include <SensorRecorderService.h>
+#include <ContextStoreService.h>
+#include <JobSchedulerService.h>
+
+#include "ServiceLoader.h"
+
+#define ROOT_UID 0
+
+using namespace ctx;
+
+ServiceLoader::ServiceLoader() :
+ __activeUser(ROOT_UID)
+{
+}
+
+ServiceLoader::~ServiceLoader()
+{
+ for (auto& svc : __userServices) {
+ delete svc;
+ }
+ for (auto& svc : __systemServices) {
+ delete svc;
+ }
+}
+
+bool ServiceLoader::load(GDBusConnection* conn)
+{
+ __create<AppHistoryService>(conn);
+ __create<SensorRecorderService>(conn);
+ __create<ContextStoreService>(conn);
+ __create<JobSchedulerService>(conn);
+
+ return (!__systemServices.empty() || !__userServices.empty());
+}
+
+void ServiceLoader::startSystem()
+{
+ for (auto& svc : __systemServices) {
+ svc->start();
+ }
+}
+
+void ServiceLoader::stopSystem()
+{
+ for (auto& svc : __systemServices) {
+ svc->stop();
+ }
+}
+
+void ServiceLoader::startUser(uid_t uid)
+{
+ IF_FAIL_VOID(__activeUser != uid);
+ _I("Starting services for %u", static_cast<unsigned int>(uid));
+
+ ServiceBase::setActiveUser(uid);
+
+ for (auto& svc : __userServices) {
+ svc->start();
+ }
+
+ for (auto& svc : __systemServices) {
+ svc->notifyUserNew();
+ }
+
+ __activeUser = uid;
+}
+
+void ServiceLoader::stopUser()
+{
+ IF_FAIL_VOID(__activeUser != ROOT_UID);
+ _I("Stopping services for %u", static_cast<unsigned int>(__activeUser));
+
+ for (auto& svc : __userServices) {
+ svc->stop();
+ }
+
+ ServiceBase::setActiveUser(ROOT_UID);
+
+ for (auto& svc : __systemServices) {
+ svc->notifyUserRemoved();
+ }
+
+ __activeUser = ROOT_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.
+ */
+
+#ifndef __CONTEXT_SERVICE_LOADER_H__
+#define __CONTEXT_SERVICE_LOADER_H__
+
+#include <vector>
+#include <ContextTypes.h>
+#include <ServiceBase.h>
+
+namespace ctx {
+
+ class ServiceLoader {
+ public:
+ ServiceLoader();
+ ~ServiceLoader();
+
+ bool load(GDBusConnection* conn);
+
+ void startUser(uid_t uid);
+ void stopUser();
+ void startSystem();
+ void stopSystem();
+
+ private:
+ uid_t __activeUser;
+ std::vector<ServiceBase*> __userServices;
+ std::vector<ServiceBase*> __systemServices;
+
+ template<typename ServiceType> void __create(GDBusConnection* conn)
+ {
+ ServiceBase *svc = NULL;
+ try {
+ svc = new ServiceType(conn);
+ } catch (std::exception& e) {
+ _W("Service creation failed (%s)", e.what());
+ return;
+ }
+ if (svc->isUserService()) {
+ __userServices.push_back(svc);
+ } else {
+ __systemServices.push_back(svc);
+ }
+ }
+ };
+
+}
+
+#endif