Fix delivering PID logic
authorJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 9 Mar 2021 08:24:01 +0000 (17:24 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 9 Mar 2021 08:24:01 +0000 (17:24 +0900)
- Make pkg list as static to share it via instances.
- Remove hashtable destroy logic if given request has failed.
- Change parameter to get sender pid of socket properly.

Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/common/database/db_handle_provider.cc
src/common/database/db_handle_provider.hh
src/common/database/pkg_get_db_handler.cc
src/common/socket/abstract_socket.cc

index 19b4dce..2c881d9 100644 (file)
@@ -56,6 +56,7 @@ namespace pkgmgr_common {
 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
     DBHandleProvider::provider_;
 bool DBHandleProvider::is_memory_ = false;
+std::vector<pid_t> DBHandleProvider::pid_list_;
 
 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
     global_parser_memory_db_handle_(nullptr, sqlite3_close_v2),
@@ -95,13 +96,17 @@ DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
 std::vector<std::string> DBHandleProvider::GetParserDBPath(pid_t pid) {
   std::unique_lock<std::mutex> u(lock_);
   std::vector<std::string> db_path_list;
-  bool is_file_access = is_memory_;
-  std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
-      pid_list_.end(), pid);
-  if (it == pid_list_.end())
-    is_file_access = true;
+  bool tmp_is_memory = is_memory_;
 
-  if (!is_file_access) {
+  if (tmp_is_memory) {
+    std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
+        pid_list_.end(), pid);
+    if (it != pid_list_.end())
+      tmp_is_memory = false;
+  }
+
+
+  if (tmp_is_memory) {
     db_path_list.emplace_back(parser_memory_db_path_);
     if (IsUserRequest(uid_))
       db_path_list.emplace_back(global_parser_memory_db_path_);
@@ -115,13 +120,15 @@ std::vector<std::string> DBHandleProvider::GetParserDBPath(pid_t pid) {
 
 std::string DBHandleProvider::GetCertDBPath(pid_t pid) {
   std::unique_lock<std::mutex> u(lock_);
-  bool is_file_access = is_memory_;
-  std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
-      pid_list_.end(), pid);
-  if (it == pid_list_.end())
-    is_file_access = true;
+  bool tmp_is_memory = is_memory_;
 
-  if (!is_file_access)
+  if (tmp_is_memory) {
+    std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
+        pid_list_.end(), pid);
+    if (it != pid_list_.end())
+      tmp_is_memory = false;
+  
+  if (tmp_is_memory)
     return cert_memory_db_path_;
   else
     return cert_file_db_path_;
index cf74af0..6968bef 100644 (file)
@@ -61,7 +61,7 @@ class EXPORT_API DBHandleProvider {
   std::string cert_memory_db_path_;
   std::string cert_file_db_path_;
   static bool is_memory_;
-  std::vector<pid_t> pid_list_;
+  static std::vector<pid_t> pid_list_;
   std::mutex lock_;
 };
 
index 2b822de..e93d4c2 100644 (file)
@@ -63,10 +63,8 @@ int PkgGetDBHandler::Execute() {
   for (auto& conn : conn_list) {
     ret = pkginfo_internal_filter_get_list(conn, filter_, uid_,
                                             GetLocale().c_str(), &list);
-    if (ret == PMINFO_R_ERROR) {
-      g_hash_table_destroy(list);
+    if (ret == PMINFO_R_ERROR)
       return ret;
-    }
   }
 
   // TODO: possible memory leak?
index a5e8225..f8e0e20 100644 (file)
@@ -94,7 +94,11 @@ pid_t AbstractSocket::GetPID() {
   struct ucred cred;
   socklen_t len;
 
-  r = getsockopt(fd_, SOCK_STREAM, SO_PEERCRED, &cred, &len);
+  r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
+  if (r < 0) {
+    LOGE("getsockopt has failed, errno[%d]", errno);
+    return -1;
+  }
 
   return cred.pid;
 }