osutil: lock the sdcard image instead of lock file
authorSangho Park <sangho.p@samsung.com>
Thu, 28 Jan 2016 04:03:18 +0000 (13:03 +0900)
committerSangho Park <sangho.p@samsung.com>
Fri, 29 Jan 2016 03:24:00 +0000 (12:24 +0900)
Lock a sdcard image file to use and prevent other emulator from
using the sdcard image. Additional lock file is not needed.

Change-Id: Ie39cf7d1a66c7047593968caec3970c907eee935
Signed-off-by: Sangho Park <sangho.p@samsung.com>
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
tizen/src/ecs/ecs_sdcard.h
tizen/src/util/osutil.c
tizen/src/util/osutil.h

index 6d3ddfe..337c16e 100644 (file)
@@ -45,9 +45,9 @@
 #define ACT_SDCARD_ATTACH           1
 #define ACT_SDCARD_DETACH_STATUS    2
 #define ACT_SDCARD_ATTACH_STATUS    3
-#define ACT_SDCARD_DETACH_FAIL      4
-#define ACT_SDCARD_ATTACH_FAIL      5
-#define ACT_SDCARD_NO_ATTACH_FOUND  6
+#define ACT_SDCARD_DETACH_FAIL      4       /* No sdcard attached */
+#define ACT_SDCARD_ATTACH_FAIL      5       /* Already sdcard attached */
+#define ACT_SDCARD_NO_ATTACH_FOUND  6       /* Other sdcard attached */
 #define MAXBUFLEN 1024
 void handle_sdcard(char* dataBuf, size_t dataLen);
 
index 442d123..f3e0abf 100644 (file)
@@ -40,7 +40,6 @@
 #include "osutil.h"
 #include "emulator.h"
 #include "emul_state.h"
-
 #include "new_debug_ch.h"
 DECLARE_DEBUG_CHANNEL(osutil)
 
@@ -53,7 +52,10 @@ static struct flock _lock = {
     .l_len = 0,
 };
 
-static sdcard_info info;
+static sdcard_info info = {
+    .fd = -1,
+    .lock_file = NULL,
+};
 
 static int fd_lock(int fd)
 {
@@ -101,7 +103,7 @@ static void remove_vm_lock_posix(void)
     g_assert(lock_file != -1);
 
     if (fd_unlock(lock_file)) {
-        LOG_WARNING("Failed to remove lock from lock file");
+        LOG_INFO("Failed to remove lock from lock file");
     }
     close(lock_file);
 
@@ -158,54 +160,59 @@ retry:
 
 bool make_sdcard_lock_posix(char *sdcard)
 {
-    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
-    int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
+    int fd;
+
+    if (info.fd != -1) {
+        g_assert(info.lock_file != NULL);
+        LOG_INFO("sdcard(%s) is already attached\n", info.lock_file);
         return false;
     }
-    if (fd_lock(fd) == -1)
-    {
-        perror("file is lock ");
-        close(fd);
+
+    fd = qemu_open(sdcard, O_WRONLY);
+    if (fd == -1) {
+        LOG_WARNING("Failed to open sdcard file: %s\n", strerror(errno));
         return false;
     }
+
+    if (fd_lock(fd) == -1) {
+        LOG_WARNING("Failed to lock sdcard file: %s\n", strerror(errno));
+        qemu_close(fd);
+        return false;
+    }
+
     info.fd = fd;
-    LOG_INFO("Get file lock: %s\n", lock_file);
+    info.lock_file = g_strdup(sdcard);
+    LOG_INFO("Locked sdcard\n");
     return true;
 }
 
 int remove_sdcard_lock_posix(char *sdcard)
 {
-    errno = 0;
-    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
-    int fd = open(lock_file, O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
-        if(errno == ENOENT) {
-            return ERR_NOENT;
-        }
-        return ERR_UNLCK;
+    /* check whether the sdcard is attached or not */
+    if (info.fd == -1) {
+        LOG_INFO("No attached sdcard\n");
+        return ERR_NODEV;
     }
 
-    if (fd_unlock(fd) == -1)
-    {
-        perror("file unlock error ");
-        close(fd);
-        return ERR_UNLCK;
+    if (strcmp(sdcard, info.lock_file) != 0) {
+        LOG_INFO("%s is already attached\n", info.lock_file);
+        return ERR_NOENT;
     }
 
-    if (unlink(lock_file) < 0) {
-        perror("unlink error ");
-        close(fd);
-        return ERR_UNLCK;
+    /*
+     * Even though we failed to unlock the file, fall through and close fd.
+     * Closing fd makes the any lock on the file released.
+     */
+    if (fd_unlock(info.fd) == -1) {
+        LOG_INFO("Failed to unlock sdcard file, %d: %s\n",
+                    info.fd, strerror(errno));
     }
 
-    LOG_INFO("unlock success: %s\n", lock_file);
-    close(fd);
-    close(info.fd);
+    LOG_INFO("Unlocked sdcard\n");
+    qemu_close(info.fd);
+    info.fd = -1;
+    g_free(info.lock_file);
+    info.lock_file = NULL;
     return ERR_SUCCESS;
 }
 #endif
index 74cb7df..c1a15cf 100644 (file)
@@ -38,9 +38,9 @@
 #endif
 
 #define ERR_SUCCESS 0
-#define ERR_UNLCK 4
-#define ERR_LCK   5
-#define ERR_NOENT 6
+#define ERR_NODEV   4   /* ACT_SDCARD_DETACH_FAIL. No sdcard attached. */
+#define ERR_BUSY    5   /* ACT_SDCARD_ATTACH_FAIL. Already sdcard attached. */
+#define ERR_NOENT   6   /* ACT_SDCARD_NO_ATTACH_FOUND. Other sdcard attached. */
 
 extern const char *pac_tempfile;