osutil: modfiy vm lock module
authorjihye <jihye424.kim@samsung.com>
Thu, 29 Sep 2016 03:08:50 +0000 (12:08 +0900)
committerjihye <jihye424.kim@samsung.com>
Thu, 29 Sep 2016 06:45:00 +0000 (15:45 +0900)
Change-Id: I33e811ee554fe86b0ee6997d2fdc017df867a26b
Signed-off-by: jihye <jihye424.kim@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 8216832992b44799d01c6735cc115e19a97301b5..93dda3d7afded57a44a38f6c3c10ee4fe71c29f2 100644 (file)
@@ -287,7 +287,6 @@ const char *prepare_maru(const gchar * const kernel_cmdline)
 
 void prepare_maru_after_device_init(void)
 {
-    make_vm_lock_os();
     maru_device_hotplug_init();
     qemu_add_opts(&qemu_ecs_opts);
     start_ecs();
@@ -370,6 +369,7 @@ static int emulator_main(int argc, char *argv[], char **envp)
 
     set_bin_path(_qemu_argv[0]);
 
+    make_vm_lock_os(g_path_get_dirname(conf));
     if (!load_conf(conf)) {
         return -1;
     }
index 2ebc6c4fa108010aa0b4dc03d58afb604f7f3f7a..a09df652852d061aa6ab308b4e2dde96dbe9d77f 100644 (file)
@@ -46,8 +46,8 @@
 
 MULTI_DEBUG_CHANNEL(qemu, osutil);
 
-void make_vm_lock_os(void) {
-    make_vm_lock_posix();
+void make_vm_lock_os(gchar *vm_path) {
+    make_vm_lock_posix(vm_path);
 }
 
 static CFDictionaryRef proxySettings;
index 164e92c9609dd479bc3921dc81e6fb4abb0fc139..eb20628b3cd882d43100a69e28c79aa6a4fff199 100644 (file)
@@ -65,9 +65,9 @@ static const char* gproxycmds[][2] = {
     { "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
 };
 
-void make_vm_lock_os(void)
+void make_vm_lock_os(gchar *vm_path)
 {
-    make_vm_lock_posix();
+    make_vm_lock_posix(vm_path);
 }
 
 void set_bin_path_os(char const *const exec_argv)
index e460011f77bad66d45c7e03bbe2a6fac55245afb..f0b2f0d57f9dafa7e226528b560d0ebd857964e1 100644 (file)
@@ -81,15 +81,15 @@ 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 *vm_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", vm_path, VMLOCK_FILE);
 
     if (g_mkdir_with_parents(g_path_get_dirname(lock_filename), 0777)) {
-        ERR("Can not create directory for lock file: %ld\n",
+        ERR("Cannot create directory for lock file: %ld\n",
                     GetLastError());
     }
     lock_file = CreateFile(lock_filename,
@@ -105,7 +105,7 @@ void make_vm_lock_os(void)
         // naturally unless FILE_SHARE_* attribute is set.
         if (error == ERROR_SHARING_VIOLATION) {
             maru_register_exit_msg(MARU_EXIT_UNKNOWN,
-                    "Can not execute this VM.\n"
+                    "Cannot execute this VM.\n"
                     "The same VM may be running now.");
             exit(1);
         }
index e8a3a5d186620127fef0e22af4abf1a31acc0d44..6a0b8cdc794d2a031b3f088adb757419a3115c87 100644 (file)
@@ -46,6 +46,7 @@ MULTI_DEBUG_CHANNEL(emulator, osutil);
 #include "maru_err_table.h"
 
 static int lock_file = -1;
+static gchar *lock_filename = NULL;
 static struct flock _lock = {
     .l_type = F_WRLCK,
     .l_start = 0,
@@ -162,6 +163,7 @@ static bool fd_checklock(int fd)
 
 static void remove_vm_lock_posix(void)
 {
+    int error = 0;
     if (lock_file == -1) {
         return;
     }
@@ -171,6 +173,15 @@ static void remove_vm_lock_posix(void)
     }
     close(lock_file);
 
+    if (lock_filename != NULL) {
+        if (unlink(lock_filename) == -1) {
+            error = errno;
+            ERR("Failed to unlink lock file(%d): %s\n",
+                        error, strerror(error));
+        }
+        g_free(lock_filename);
+        lock_filename = NULL;
+    }
     lock_file = -1;
 }
 
@@ -181,21 +192,25 @@ 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;
 
-    g_assert(lock_file == -1);
-    g_assert(image_file != NULL);
+    lock_filename = g_strdup_printf("%s/%s", vms_path, VMLOCK_FILE);
+
+    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;
-        ERR("Failed to open image file for lock: %s\n",
-            strerror(error));
-        return;
+        ERR("Failed to create file for lock(%d): %s\n",
+           error, strerror(error));
+        maru_register_exit_msg(MARU_EXIT_UNKNOWN,
+                    "Cannot execute this VM.\n"
+                    "Failed to create file for lock.");
+        exit(1);
+
     }
 
     if (fd_lock(lock_file) == -1) {
@@ -209,8 +224,8 @@ retry:
                 goto retry;
             }
             maru_register_exit_msg(MARU_EXIT_UNKNOWN,
-                        "Can not execute this VM.\n"
-                        "The same name is running now.");
+                        "Cannot execute this VM.\n"
+                        "The same VM may be running now.");
             exit(1);
         }
 
index ce63254a9c4b0c186b010f2f725468be07ef61de..8dbe58e735ce76722adef00449a63f871124b2b2 100644 (file)
 #define ERR_UNLCK 4
 #define ERR_LCK   5
 #define ERR_NOENT 6
+#define VMLOCK_FILE "vm.lock"
 
 extern const char *pac_tempfile;
 
-void make_vm_lock_os(void);
+void make_vm_lock_os(gchar *vm_path);
 bool make_sdcard_lock_os(char *sdcard);
 int remove_sdcard_lock_os(char *sdcard);
 
@@ -84,7 +85,7 @@ typedef struct sdcard_info
 
 
 #ifndef CONFIG_WIN32
-void make_vm_lock_posix(void);
+void make_vm_lock_posix(gchar *vm_path);
 
 bool make_sdcard_lock_posix(char *sdcard);
 int remove_sdcard_lock_posix(char *sdcard);