Delay server ready timing until creating cache
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / create_cache_request_handler.cc
index 6cebec6..f70cf2d 100644 (file)
@@ -1,9 +1,11 @@
-// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
 // Use of this source code is governed by an apache-2.0 license that can be
 // found in the LICENSE file.
 
 #include "create_cache_request_handler.hh"
 
+#include <sys/resource.h>
+
 #include <string>
 
 #include "cache_db_handler.hh"
@@ -14,83 +16,73 @@ namespace psd = pkgmgr_server::database;
 namespace pkgmgr_server {
 namespace request_handler {
 
-CreateCacheRequestHandler::Scheduler::Scheduler(pid_t tid) : tid_(tid) {
-  Get(&policy_, &param_);
+CreateCacheRequestHandler::Scheduler::Scheduler(pid_t tid)
+    : tid_(tid), priority_(0) {
+  if (Get(&priority_) != 0)
+    priority_ = 0;
 }
 
-void CreateCacheRequestHandler::Scheduler::ChangePolicy() {
-  struct sched_param param {
-    .sched_priority = 1,
-  };
-  if (Set(SCHED_RR, &param) != 0)
+void CreateCacheRequestHandler::Scheduler::ChangePriority() {
+  if (Set(-10) != 0)
     return;
 
-  int policy = -1;
-  Get(&policy, &param);
-  LOG(WARNING) << "Current policy: " << policy
-               << ", priority: " << param.sched_priority;
+  int priority = 0;
+  if (Get(&priority) == 0)
+    LOG(WARNING) << "Current priority: " << priority;
 }
 
-void CreateCacheRequestHandler::Scheduler::ResetPolicy() {
-  if (Set(policy_, &param_) != 0)
+void CreateCacheRequestHandler::Scheduler::ResetPriority() {
+  if (Set(priority_) != 0)
     return;
 
-  struct sched_param param = {
-    .sched_priority = 0,
-  };
-  int policy = -1;
-  Get(&policy, &param);
-  LOG(WARNING) << "Current policy: " << policy
-               << ", priority: " << param.sched_priority;
+  int priority = 0;
+  if (Get(&priority) == 0)
+    LOG(WARNING) << "Current priority: " << priority;
 }
 
-void CreateCacheRequestHandler::Scheduler::Get(int* policy,
-    struct sched_param* param) {
-  if (sched_getparam(tid_, param) != 0)
-    LOG(ERROR) << "sched_getparam() is failed. errno: " << errno;
+int CreateCacheRequestHandler::Scheduler::Get(int* priority) {
+  errno = 0;
+  *priority = getpriority(PRIO_PROCESS, 0);
+  if (errno != 0) {
+    LOG(ERROR) << "getpriority() is failed. errno: " << errno;
+    return -1;
+  }
 
-  *policy = sched_getscheduler(tid_);
-  if (*policy < 0)
-    LOG(ERROR) << "sched_getscheduler() is failed. errno: " << errno;
+  return 0;
 }
 
-int CreateCacheRequestHandler::Scheduler::Set(int policy,
-    struct sched_param* param) {
-  if (sched_setscheduler(tid_, policy, param) != 0) {
-    LOG(ERROR) << "sched_setscheduler() is failed. policy: " << policy
-               << ", errno: " << errno;
+int CreateCacheRequestHandler::Scheduler::Set(int priority) {
+  if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
+    LOG(ERROR) << "setpriority() is failed. , errno: " << errno;
     return -1;
   }
 
-  LOG(WARNING) << "policy: " << policy
-               << ", sched_priority: " << param->sched_priority;
+  LOG(WARNING) << "priority: " << priority;
   return 0;
 }
 
 CreateCacheRequestHandler::CreateCacheRequestHandler(): scheduler_(gettid()) {
 }
 
-bool CreateCacheRequestHandler::HandleRequest(unsigned char* data, int size,
+bool CreateCacheRequestHandler::HandleRequest(const unsigned char* data, int size,
                                             const std::string& locale) {
-  // TODO(ilho159.kim) need to get logined user id
-  psd::CacheDBHandler db(5001, GetPID());
+  psd::CacheDBHandler db(GetUID(), GetPID());
   db.SetLocale(locale);
 
-  int ret = db.Execute();
-
-  return ret == PMINFO_R_OK;
+  success_ = (db.Execute() == PMINFO_R_OK);
+  return success_;
 }
 
 std::vector<uint8_t> CreateCacheRequestHandler::ExtractResult() {
-  return {};
+  return { static_cast<uint8_t>(success_) };
 }
 
 void CreateCacheRequestHandler::PreExec() {
-  scheduler_.ChangePolicy();
+  scheduler_.ChangePriority();
 }
 
 void CreateCacheRequestHandler::PostExec() {
-  scheduler_.ResetPolicy();
+  scheduler_.ResetPriority();
 }
 
 }  // namespace request_handler