From: Mu-Woong Lee Date: Tue, 8 Aug 2017 10:50:23 +0000 (+0900) Subject: Add OrphanedJobCleaner to remove the jobs owned by uninstalled apps X-Git-Tag: submit/tizen/20170809.045126^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=42e45bd29c2f4d92c99df4ae307558c93aa38dc3;p=platform%2Fcore%2Fcontext%2Fjob-scheduler.git Add OrphanedJobCleaner to remove the jobs owned by uninstalled apps Change-Id: I8176b3558e915ad18f41c6852f39f41b011c8791 Signed-off-by: Mu-Woong Lee --- diff --git a/include/private/JobSchedulerService.h b/include/private/JobSchedulerService.h index 5f76842..31af550 100644 --- a/include/private/JobSchedulerService.h +++ b/include/private/JobSchedulerService.h @@ -17,6 +17,7 @@ #ifndef __CONTEXT_JOB_SCHEDULER_SERVICE_H__ #define __CONTEXT_JOB_SCHEDULER_SERVICE_H__ +#include #include #include #include @@ -45,6 +46,7 @@ namespace ctx { // Own members JobManager* getJobManager(uid_t uid); + std::vector getAllJobManager(); private: IServiceRunner* __serviceRunner; diff --git a/src/server/JobManager.cpp b/src/server/JobManager.cpp index 306b03d..c07d317 100644 --- a/src/server/JobManager.cpp +++ b/src/server/JobManager.cpp @@ -283,6 +283,29 @@ std::vector JobManager::getAllJobInfo(IClient* owner) return jobInfos; } +void JobManager::purgeJobOf(const std::string& ownerId) +{ + _I("Purge all jobs of %s from %u", ownerId.c_str(), __uid); + + std::vector toPurge; + + for (auto& runner : __jobRunners) { + if (runner->getOwner() == ownerId) { + toPurge.push_back(runner); + } + } + + for (auto& runner : toPurge) { + if (runner->isStarted()) + runner->stop(); + + if (runner->isPersistent()) + __jobInfoDatabase.remove(runner->getJobId()); + + __removeRunner(runner); + } +} + void JobManager::addCustom(const std::string& uri, const std::string& ownerId) { std::string key = uri + "@" + ownerId; diff --git a/src/server/JobManager.h b/src/server/JobManager.h index 3ac2270..86920b9 100644 --- a/src/server/JobManager.h +++ b/src/server/JobManager.h @@ -50,6 +50,8 @@ namespace ctx { JobInfo* getJobInfo(int jobId, IClient* owner); std::vector getAllJobInfo(IClient* owner); + void purgeJobOf(const std::string& ownerId); + // For supporting Context-Trigger's custom context registration APIs void addCustom(const std::string& uri, const std::string& ownerId); void removeCustom(const std::string& uri, const std::string& ownerId); diff --git a/src/server/JobSchedulerService.cpp b/src/server/JobSchedulerService.cpp index 78eedcb..d23bcb9 100644 --- a/src/server/JobSchedulerService.cpp +++ b/src/server/JobSchedulerService.cpp @@ -22,12 +22,14 @@ #include "MethodCallHandler.h" #include "ContextManager.h" #include "SchedTimer.h" +#include "OrphanedJobCleaner.h" #define SYSTEM_UID 0 using namespace ctx; static std::map __jobManagers; +static OrphanedJobCleaner* __orphanedJobCleaner = NULL; JobSchedulerService::JobSchedulerService() : __serviceRunner(NULL) @@ -63,11 +65,13 @@ bool JobSchedulerService::prepare() ContextManager::init(); SchedTimer::init(); __jobManagers.emplace(SYSTEM_UID, new JobManager(SYSTEM_UID)); + __orphanedJobCleaner = new OrphanedJobCleaner(this); return true; } void JobSchedulerService::cleanup() { + delete __orphanedJobCleaner; for (auto& it : __jobManagers) { delete it.second; } @@ -100,3 +104,14 @@ JobManager* JobSchedulerService::getJobManager(uid_t uid) return it->second; } + +std::vector JobSchedulerService::getAllJobManager() +{ + std::vector managers; + + for (auto& it : __jobManagers) { + managers.push_back(it.second); + } + + return managers; +} diff --git a/src/server/OrphanedJobCleaner.cpp b/src/server/OrphanedJobCleaner.cpp new file mode 100644 index 0000000..66eabe9 --- /dev/null +++ b/src/server/OrphanedJobCleaner.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016 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 +#include +#include +#include "JobManager.h" +#include "OrphanedJobCleaner.h" + +#define GLOBAL_APP_UID 376 + +using namespace ctx; + +OrphanedJobCleaner::OrphanedJobCleaner(JobSchedulerService* hostService) : + __hostService(hostService) +{ + __dbusMonitor.subscribe(NULL, + "/org/tizen/pkgmgr/signal", "org.tizen.pkgmgr.signal", "uninstall", this); +} + +OrphanedJobCleaner::~OrphanedJobCleaner() +{ +} + +void OrphanedJobCleaner::onSignal(const std::string& sender, const std::string& objPath, + const std::string& interface, const std::string& signalName, GVariant *param) +{ + uint32_t uid = 0; + const char *pkgId = NULL; + const char *key = NULL; + const char *val = NULL; + + g_variant_get(param, "(u&s&s&s&s&s&s)", &uid, NULL, NULL, &pkgId, NULL, &key, &val); + IF_FAIL_VOID(pkgId && STR_EQ(key, "end") && STR_EQ(val, "ok")); + + _I("%s has been removed from %u", pkgId, uid); + + if (uid != GLOBAL_APP_UID) { + JobManager* manager = __hostService->getJobManager(static_cast(uid)); + if (manager) + manager->purgeJobOf(pkgId); + return; + } + + std::vector managers = __hostService->getAllJobManager(); + + for (auto& manager : managers) { + manager->purgeJobOf(pkgId); + } +} diff --git a/src/server/OrphanedJobCleaner.h b/src/server/OrphanedJobCleaner.h new file mode 100644 index 0000000..58c52cd --- /dev/null +++ b/src/server/OrphanedJobCleaner.h @@ -0,0 +1,42 @@ +/* + * 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_JOB_SCHEDULER_ORPHANED_JOB_CLEANER_H__ +#define __CONTEXT_JOB_SCHEDULER_ORPHANED_JOB_CLEANER_H__ + +#include +#include + +namespace ctx { + + class JobSchedulerService; + + class OrphanedJobCleaner : public IDBusSignalListener { + public: + OrphanedJobCleaner(JobSchedulerService* hostService); + ~OrphanedJobCleaner(); + + void onSignal(const std::string& sender, const std::string& objPath, + const std::string& interface, const std::string& signalName, GVariant *param); + + private: + JobSchedulerService* __hostService; + DBusMonitor __dbusMonitor; + }; + +} + +#endif