Fix implementation (#147)
author최창규/Tizen Platform Lab(SR)/Engineer/삼성전자 <changyu.choi@samsung.com>
Fri, 12 Mar 2021 04:12:03 +0000 (13:12 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 12 Mar 2021 04:12:03 +0000 (13:12 +0900)
* Fix implementation

Changes:
 * Add global_lock_ into GetParserDBPath
 * Add error handling for socket fd.

Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
* Remove wrong break

Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
* Fix Wreorder warning

src/common/database/db_handle_provider.cc
src/common/database/db_handle_provider.hh
src/common/database/query_handler.cc
src/server/runner.cc
src/server/runner.hh

index 28c7c0d..67bfc5a 100644 (file)
@@ -63,7 +63,7 @@ std::string DBHandleProvider::cert_memory_db_path_ =
     "file:certdb?mode=memory&cache=shared";
 std::string DBHandleProvider::cert_file_db_path_;
 std::unordered_set<pid_t> DBHandleProvider::pid_list_;
-std::mutex DBHandleProvider::global_lock_;
+std::recursive_mutex DBHandleProvider::global_lock_;
 
 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
     is_memory_(false), parser_memory_db_handle_(nullptr, sqlite3_close_v2) {
@@ -100,6 +100,7 @@ DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
 
 std::vector<std::string> DBHandleProvider::GetParserDBPath(pid_t pid) {
   std::unique_lock<std::recursive_mutex> u(lock_);
+  std::unique_lock<std::recursive_mutex> gu(global_lock_);
   std::vector<std::string> db_path_list;
   if (is_memory_ != is_memory_global_)
     SetMemoryMode(pid, is_memory_global_);
@@ -126,6 +127,7 @@ std::vector<std::string> DBHandleProvider::GetParserDBPath(pid_t pid) {
 
 std::string DBHandleProvider::GetCertDBPath(pid_t pid) {
   std::unique_lock<std::recursive_mutex> u(lock_);
+  std::unique_lock<std::recursive_mutex> gu(global_lock_);
   if (is_memory_ != is_memory_global_)
     SetMemoryMode(pid, is_memory_global_);
 
@@ -171,8 +173,8 @@ sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
 }
 
 void DBHandleProvider::SetMemoryMode(pid_t pid, bool flag) {
-  std::unique_lock<std::recursive_mutex>(lock_);
-  std::unique_lock<std::recursive_mutex>(global_lock_);
+  std::unique_lock<std::recursive_mutex> u(lock_);
+  std::unique_lock<std::recursive_mutex> gu(global_lock_);
   if (flag == is_memory_global_ && flag == is_memory_)
     return;
 
index 2e963a6..e315b64 100644 (file)
@@ -61,13 +61,13 @@ class EXPORT_API DBHandleProvider {
   static std::string cert_memory_db_path_;
   static std::string cert_file_db_path_;
   static std::unordered_set<pid_t> pid_list_;
-  static std::mutex global_lock_;
+  static std::recursive_mutex global_lock_;
 
   uid_t uid_;
+  bool is_memory_;
   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
   std::string parser_memory_db_path_;
   std::string parser_file_db_path_;
-  bool is_memory_;
   std::recursive_mutex lock_;
 };
 
index ba7dda4..20c1312 100644 (file)
@@ -76,7 +76,6 @@ int QueryHandler::Execute() {
       }
 
       g_list_free(list);
-      break;
     }
     return ret;
   } else {
index fb7afa0..878081a 100644 (file)
@@ -46,6 +46,21 @@ Runner::~Runner() {
   vconf_ignore_key_changed(VCONFKEY_LANGSET, OnLanguageChange);
 }
 
+int Runner::FdErrorHandler(int fd, GIOCondition cond, void* user_data) {
+  auto runner = static_cast<Runner*>(user_data);
+  if (static_cast<int>(cond) & (G_IO_ERR | G_IO_HUP)) {
+    auto it = runner->sid_map_.find(fd);
+    if (it != runner->sid_map_.end()) {
+      LOGW("Socket disconnect. fd(%d) condition(%d)", fd, static_cast<int>(cond));
+      g_source_remove(it->second);
+      runner->sid_map_.erase(it);
+    }
+    close(fd);
+  }
+
+  return G_SOURCE_CONTINUE;
+}
+
 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
   auto runner = static_cast<Runner*>(user_data);
   int client_fd = runner->server_->Accept();
@@ -54,6 +69,9 @@ int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
     return G_SOURCE_CONTINUE;
   }
 
+  auto condition = static_cast<GIOCondition>(G_IO_ERR | G_IO_HUP);
+  int sid = g_unix_fd_add(client_fd, condition, FdErrorHandler, runner);
+  runner->sid_map_[client_fd] = sid;
   runner->QueueRequest(client_fd);
   return G_SOURCE_CONTINUE;
 }
index 52ba888..eff5994 100644 (file)
 #define SERVER_RUNNER_HH_
 
 #include <gio/gio.h>
+#include <glib-unix.h>
 #include <vconf.h>
 
 #include <memory>
+#include <unordered_map>
 
 #include "server_socket.hh"
 #include "worker_thread.hh"
@@ -40,10 +42,12 @@ class EXPORT_API Runner {
   static int OnReceiveRequest(int fd, GIOCondition cond, void* user_data);
   static void OnLanguageChange(keynode_t* key, void* user_data);
   bool QueueRequest(int client_fd);
+  static int FdErrorHandler(int fd, GIOCondition cond, void* user_data);
 
  private:
   int sid_;
   int thread_num_;
+  std::unordered_map<int, int> sid_map_;
   std::unique_ptr<pkgmgr_common::socket::ServerSocket> server_;
   std::unique_ptr<WorkerThread> thread_pool_;
 };