Patch for semaphore deadlock bug while plugins installation.
authorTaejeong Lee <taejeong.lee@samsung.com>
Mon, 21 Jan 2013 05:45:58 +0000 (14:45 +0900)
committerTaejeong Lee <taejeong.lee@samsung.com>
Mon, 21 Jan 2013 06:15:18 +0000 (15:15 +0900)
[Issue#] N/A
[Problem] wrt-installer does not progress after plugins installation crash occurred on wrt-installer.
[Cause] Semaphore is not deleted when crash occur.
[Solution] Semaphore was replaced to flock api. The lock by flock is release automatically when the owner process is terminated.

Change-Id: I7f345a18a1041c2cb68416a34a2c8b5719e3bc84

src/wrt-installer/plugin_utils.cpp

index ae29f5e..2b6bc3c 100644 (file)
  */
 
 #include "plugin_utils.h"
-#include <dpl/semaphore.h>
 #include <dpl/exception.h>
 #include <dpl/log/log.h>
 #include <dpl/wrt-dao-ro/global_config.h>
+#include <sys/file.h>
 
 using namespace WrtDB;
 
 namespace PluginUtils {
-const char PLUGIN_INSTALL_SEMAPHORE[] = "/.wrt_plugin_install_lock";
+const char* PLUGIN_INSTALL_LOCK_FILE = "/tmp/.wrt_plugin_install_lock";
 
-static DPL::Semaphore semaphore(PLUGIN_INSTALL_SEMAPHORE);
+static int s_plugin_install_lock_fd = -1;
 
 bool lockPluginInstallation()
 {
-    Try {
-        semaphore.Lock();
-        return true;
+    int ret = 0;
+
+    LogInfo("Try to lock for plugins installation.");
+
+    s_plugin_install_lock_fd =
+        open(PLUGIN_INSTALL_LOCK_FILE, O_RDONLY|O_CREAT, 0666);
+
+    if (s_plugin_install_lock_fd == -1)
+    {
+        LogError("Lock file open failed!");
+
+        return false;
     }
-    Catch(DPL::Semaphore::Exception::Base){
+
+    ret = flock(s_plugin_install_lock_fd, LOCK_EX); //lock with waiting
+
+    if (ret == -1)
+    {
+        LogError("Lock failed!");
+
+        close(s_plugin_install_lock_fd);
+        s_plugin_install_lock_fd = -1;
+
         return false;
     }
+
+    return true;
 }
 
 bool unlockPluginInstallation()
 {
-    Try {
-        semaphore.Unlock();
+    LogInfo("Unlock for plugins installation.");
+
+    if (s_plugin_install_lock_fd != -1)
+    {
+        int ret = 0;
+
+        ret = flock(s_plugin_install_lock_fd, LOCK_UN); //unlock
+
+        if (ret == -1)
+        {
+            LogError("Unlock failed!");
+        }
+
+        close(s_plugin_install_lock_fd);
+        s_plugin_install_lock_fd = -1;
+
         return true;
     }
-    Catch(DPL::Semaphore::Exception::Base){
-        return false;
+    else
+    {
+        LogError("Lock file was not created!");
     }
+
+    return false;
 }
 
 bool checkPluginInstallationRequired()