osutil: adjust vm lock module
authorMunkyu Im <munkyu.im@samsung.com>
Mon, 26 Sep 2016 09:14:21 +0000 (18:14 +0900)
committerMunkyu Im <munkyu.im@samsung.com>
Mon, 26 Sep 2016 09:23:39 +0000 (18:23 +0900)
- move it to an earlier time.(early launching time)
- make the same lock file(vm.lock) on each host OS.

Change-Id: I80744500bf7c25c536061d2952f7f6552a0406f1
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
tizen/src/emulator.c
tizen/src/util/osutil-darwin.c
tizen/src/util/osutil-linux.c
tizen/src/util/osutil-win32.c
tizen/src/util/osutil.c
tizen/src/util/osutil.h

index b1d6bab..940e0d1 100644 (file)
@@ -240,7 +240,6 @@ const char *prepare_maru(const char * const kernel_cmdline)
 
 void prepare_maru_after_device_init(void)
 {
-    make_vm_lock_os();
     init_device_hotplug();
     start_ecs();
 
@@ -308,6 +307,7 @@ static int emulator_main(int argc, char *argv[], char **envp)
     set_bin_path_os(_qemu_argv[0]);
 
     if (launch_conf_file) {
+        make_vm_lock_os(g_path_get_dirname(launch_conf_file));
         if (!load_conf(launch_conf_file)) {
             return -1;
         }
index b27ed7c..c797ca8 100644 (file)
@@ -47,8 +47,8 @@
 #include "new_debug_ch.h"
 DECLARE_DEBUG_CHANNEL(osutil);
 
-void make_vm_lock_os(void) {
-    make_vm_lock_posix();
+void make_vm_lock_os(gchar *vms_path) {
+    make_vm_lock_posix(vms_path);
 }
 
 void set_bin_path_os(char const *const exec_argv)
index 8e432f9..c46e461 100644 (file)
@@ -50,8 +50,8 @@
 #include "new_debug_ch.h"
 DECLARE_DEBUG_CHANNEL(osutil);
 
-void make_vm_lock_os(void) {
-    make_vm_lock_posix();
+void make_vm_lock_os(gchar *vms_path) {
+    make_vm_lock_posix(vms_path);
 }
 
 void set_bin_path_os(char const *const exec_argv)
index a76edfc..bced9f8 100644 (file)
@@ -83,12 +83,13 @@ image file and kernel log file are aleady opened with exclusive write
 lock by pre-executed emulator. But it is still useful for
 emulator-manager.
 */
-void make_vm_lock_os(void)
+void make_vm_lock_os(gchar *vms_path)
 {
+
     g_assert(lock_file == INVALID_HANDLE_VALUE);
     g_assert(lock_filename == NULL);
 
-    lock_filename = g_strdup_printf("%s.lock", get_drive_image_file());
+    lock_filename = g_strdup_printf("%s\\%s", vms_path, VMLOCK_FILE);
 
     if (g_mkdir_with_parents(g_path_get_dirname(lock_filename), 0777)) {
         LOG_WARNING("Can not create directory for lock file: %ld\n",
index 099e178..de78c53 100644 (file)
@@ -46,6 +46,7 @@ DECLARE_DEBUG_CHANNEL(osutil)
 
 
 static int lock_file = -1;
+static gchar *lock_filename = NULL;
 static struct flock _lock = {
     .l_type = F_WRLCK,
     .l_start = 0,
@@ -101,6 +102,7 @@ static bool fd_checklock(int fd)
 
 static void remove_vm_lock_posix(void)
 {
+    int error = 0;
     if (lock_file == -1) {
         return;
     }
@@ -110,6 +112,16 @@ static void remove_vm_lock_posix(void)
     }
     close(lock_file);
 
+    if (lock_filename != NULL) {
+        if (unlink(lock_filename) == -1) {
+            error = errno;
+            LOG_WARNING("Failed to unlink lock file(%d): %s\n",
+                    error, strerror(error));
+        }
+        g_free(lock_filename);
+        lock_filename = NULL;
+    }
+
     lock_file = -1;
 }
 
@@ -120,21 +132,22 @@ static void notify_remove_lock(Notifier *notifier, void *data)
 
 static Notifier remove_lock = { .notify = notify_remove_lock };
 
-void make_vm_lock_posix(void)
+void make_vm_lock_posix(gchar *vms_path)
 {
-    const char *image_file = get_drive_image_file();
     int error = 0;
+    lock_filename = g_strdup_printf("%s/%s", vms_path, VMLOCK_FILE);
 
-    g_assert(lock_file == -1);
-    g_assert(image_file != NULL);
+    g_assert(lock_filename != NULL);
 
 retry:
-    lock_file = open(image_file, O_RDWR);
+    lock_file = open(lock_filename, O_RDWR|O_CREAT, 0666);
     if (lock_file == -1) {
         error = errno;
-        LOG_WARNING("Failed to open image file for lock: %s\n",
-                    strerror(error));
-        return;
+        LOG_WARNING("Failed to open file for lock(%d): %s\n",
+                error, strerror(error));
+        error_report("Can not execute this VM. "
+                     "The same VM may be running now.");
+        exit(1);
     }
 
     if (fd_lock(lock_file) == -1) {
index 8e9d2ac..7dc9fea 100644 (file)
 #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. */
+#define VMLOCK_FILE "vm.lock"
 
 extern const char *pac_tempfile;
 
-void make_vm_lock_os(void);
+void make_vm_lock_os(gchar *vms_path);
 bool make_sdcard_lock_os(char *sdcard);
 int remove_sdcard_lock_os(char *sdcard);
 
 #ifndef CONFIG_WIN32
-void make_vm_lock_posix(void);
+void make_vm_lock_posix(gchar *dirname);
 
 bool make_sdcard_lock_posix(char *sdcard);
 int remove_sdcard_lock_posix(char *sdcard);