diff options
author | SeokYeon Hwang <syeon.hwang@samsung.com> | 2016-01-06 19:00:49 +0900 |
---|---|---|
committer | SeokYeon Hwang <syeon.hwang@samsung.com> | 2016-01-26 16:04:19 +0900 |
commit | a1d119b70e8fa17a34d8d024ac543f7adeb24561 (patch) | |
tree | 0916ec5a252208e09d766cc9a15d1e5494a13cb2 /tizen | |
parent | cd433418a715611ea692db3432458a67d962b15e (diff) | |
download | qemu-a1d119b70e8fa17a34d8d024ac543f7adeb24561.tar.gz qemu-a1d119b70e8fa17a34d8d024ac543f7adeb24561.tar.bz2 qemu-a1d119b70e8fa17a34d8d024ac543f7adeb24561.zip |
osutil: change locking mechanism
Using file lock mechanism instead of shared memory. Emulator-manager
can share same file lock for checking whether same VM is running or not.
On Linux and MacOSX, POSIX file lock is used. If it is failed to lock file
and the file is not locked with valid type and record, it will try again to
lock file after sleeping.
On Windows, emulator will stop before checking lock because platform
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.
In addition, header files are cleaned up.
Change-Id: Ibedfac3503fb0d19956a7fbed6736337be8f2223
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
Signed-off-by: Sangho Park <sangho.p@samsung.com>
Diffstat (limited to 'tizen')
-rw-r--r-- | tizen/src/util/osutil-darwin.c | 97 | ||||
-rw-r--r-- | tizen/src/util/osutil-linux.c | 137 | ||||
-rw-r--r-- | tizen/src/util/osutil-win32.c | 165 | ||||
-rw-r--r-- | tizen/src/util/osutil.c | 86 | ||||
-rw-r--r-- | tizen/src/util/osutil.h | 29 |
5 files changed, 186 insertions, 328 deletions
diff --git a/tizen/src/util/osutil-darwin.c b/tizen/src/util/osutil-darwin.c index 62ae4a30d6..cdf492c13b 100644 --- a/tizen/src/util/osutil-darwin.c +++ b/tizen/src/util/osutil-darwin.c @@ -1,14 +1,11 @@ /* - * Emulator + * osutil * - * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: * SeokYeon Hwang <syeon.hwang@samsung.com> * MunKyu Im <munkyu.im@samsung.com> - * GiWoong Kim <giwoong.kim@samsung.com> - * YeongKyoon Lee <yeongkyoon.lee@samsung.com> - * HyunJun Son * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,73 +31,22 @@ @brief Collection of utilities for darwin */ -#include "emulator_common.h" +#include <sys/sysctl.h> + +#include "png.h" + #include "osutil.h" -#include "emulator.h" #include "emul_state.h" -#include "debug_ch.h" -#include "emulator_options.h" #ifndef CONFIG_DARWIN #error #endif -#include <string.h> -#include <unistd.h> -#include <sys/shm.h> -#include <sys/sysctl.h> - -MULTI_DEBUG_CHANNEL(qemu, osutil); - -static int g_shmid; - -void check_vm_lock_os(void) -{ - /* TODO: */ -} - -static void remove_vm_lock_os(void) -{ - if (shmctl(g_shmid, IPC_RMID, 0) == -1) { - ERR("shmctl failed\n"); - perror("osutil-linux: "); - } -} - -static void notify_remove_lock(Notifier *notifier, void *data) -{ - remove_vm_lock_os(); -} - -static Notifier remove_lock = { .notify = notify_remove_lock }; - -void make_vm_lock_os(void) -{ - char *shared_memory; - int base_port; - base_port = get_vm_base_port(); - g_shmid = shmget((key_t)base_port, getpagesize(), 0666|IPC_CREAT); - if (g_shmid == -1) { - ERR("shmget failed\n"); - perror("osutil-darwin: "); - return; - } - - shared_memory = shmat(g_shmid, (char *)0x00, 0); - if (shared_memory == (void *)-1) { - ERR("shmat failed\n"); - perror("osutil-darwin: "); - return; - } - g_sprintf(shared_memory, "%s", get_drive_image_file()); - INFO("shared memory key: %d, value: %s\n", base_port, (char *)shared_memory); - - if (shmdt(shared_memory) == -1) { - ERR("shmdt failed\n"); - perror("osutil-darwin: "); - } +#include "new_debug_ch.h" +DECLARE_DEBUG_CHANNEL(osutil); - emulator_add_exit_notifier(&remove_lock); +void make_vm_lock_os(void) { + make_vm_lock_posix(); } void set_bin_path_os(char const *const exec_argv) @@ -114,7 +60,7 @@ void set_bin_path_os(char const *const exec_argv) char *data = g_strdup(exec_argv); if (!data) { - ERR("Fail to strdup for paring a binary directory.\n"); + LOG_WARNING("Fail to strdup for paring a binary directory.\n"); return; } @@ -149,20 +95,21 @@ int get_number_of_processors(void) sys_num = 1; } } - INFO("* Number of processors : %d\n", sys_num); + LOG_INFO("* Number of processors : %d\n", sys_num); return sys_num; } void print_system_info_os(void) { - INFO("* Mac\n"); + LOG_INFO("* Mac\n"); + LOG_INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING); /* uname */ - INFO("* Host machine uname :\n"); + LOG_INFO("* Host machine uname :\n"); char const *const uname_cmd = "uname -a"; if(system(uname_cmd) < 0) { - INFO("system function command '%s' \ + LOG_INFO("system function command '%s' \ returns error !", uname_cmd); } @@ -177,7 +124,7 @@ void print_system_info_os(void) sysctl(mib, 2, NULL, &len, NULL, 0); sys_info = malloc(len * sizeof(char)); if (sysctl(mib, 2, sys_info, &len, NULL, 0) >= 0) { - INFO("* Machine model : %s\n", sys_info); + LOG_INFO("* Machine model : %s\n", sys_info); } free(sys_info); @@ -186,7 +133,7 @@ void print_system_info_os(void) sysctl(mib, 2, NULL, &len, NULL, 0); sys_info = malloc(len * sizeof(char)); if (sysctl(mib, 2, sys_info, &len, NULL, 0) >= 0) { - INFO("* Machine class : %s\n", sys_info); + LOG_INFO("* Machine class : %s\n", sys_info); } free(sys_info); @@ -195,7 +142,7 @@ void print_system_info_os(void) mib[1] = HW_NCPU; len = sizeof(sys_num); if (sysctl(mib, 2, &sys_num, &len, NULL, 0) >= 0) { - INFO("* Number of processors : %d\n", sys_num); + LOG_INFO("* Number of processors : %d\n", sys_num); } #endif get_number_of_processors(); @@ -204,16 +151,16 @@ void print_system_info_os(void) mib[1] = HW_PHYSMEM; len = sizeof(sys_num); if (sysctl(mib, 2, &sys_num, &len, NULL, 0) >= 0) { - INFO("* Total memory : %llu bytes\n", sys_num); + LOG_INFO("* Total memory : %llu bytes\n", sys_num); } /* java version */ - INFO("* Java version :\n"); + LOG_INFO("* Java version :\n"); char const *const lspci_cmd = "java -version"; fflush(stdout); if(system(lspci_cmd) < 0) { - INFO("system function command '%s' \ + LOG_INFO("system function command '%s' \ returns error !", lspci_cmd); } } diff --git a/tizen/src/util/osutil-linux.c b/tizen/src/util/osutil-linux.c index d0cd9cedfb..9dc506382a 100644 --- a/tizen/src/util/osutil-linux.c +++ b/tizen/src/util/osutil-linux.c @@ -1,14 +1,11 @@ /* - * Emulator + * osutil * * Copyright (C) 2012 - 2014 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: * SeokYeon Hwang <syeon.hwang@samsung.com> * MunKyu Im <munkyu.im@samsung.com> - * GiWoong Kim <giwoong.kim@samsung.com> - * YeongKyoon Lee <yeongkyoon.lee@samsung.com> - * HyunJun Son * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,125 +31,24 @@ @brief Collection of utilities for linux */ -#include <png.h> -#include <string.h> -#include <unistd.h> -#include <sys/shm.h> -#include <sys/ipc.h> #include <linux/version.h> #include <sys/utsname.h> #include <sys/sysinfo.h> -#include "qemu/sockets.h" -#include "qemu/error-report.h" +#include "png.h" #include "osutil.h" -#include "emulator.h" #include "emul_state.h" -#include "net_helper.h" -#include "emulator_options.h" #ifndef CONFIG_LINUX #error #endif -#include "debug_ch.h" -MULTI_DEBUG_CHANNEL(emulator, osutil); +#include "new_debug_ch.h" +DECLARE_DEBUG_CHANNEL(osutil); -static int g_shmid; -static char *g_shared_memory; - -void check_vm_lock_os(void) -{ - int shm_id; - void *shm_addr; - uint32_t port; - int val; - struct shmid_ds shm_info; - - for (port = 26100; port < 26200; port += 10) { - shm_id = shmget((key_t)port, 0, 0); - if (shm_id != -1) { - shm_addr = shmat(shm_id, (void *)0, 0); - if ((void *)-1 == shm_addr) { - ERR("error occured at shmat()\n"); - break; - } - - val = shmctl(shm_id, IPC_STAT, &shm_info); - if (val != -1) { - INFO("count of process that use shared memory: %d\n", - shm_info.shm_nattch); - if ((shm_info.shm_nattch > 0) && - g_strcmp0(get_drive_image_file(), (char *)shm_addr) == 0) { - int ret = 0; - if ((ret = check_port_bind_listen(port + 1)) > 0) { - shmdt(shm_addr); - closesocket(ret); - continue; - } - shmdt(shm_addr); - // FIXME: VM lock logic should be refactored ASAP. -#if 0 - error_report("Can not execute this VM. " - "The same name is running now."); - exit(1); -#endif - } else { - shmdt(shm_addr); - } - } - } - } -} - -static void remove_vm_lock_os(void) -{ - if (shmctl(g_shmid, IPC_RMID, 0) == -1) { - ERR("shmctl failed\n"); - perror("osutil-linux: "); - } -} - -static void notify_remove_lock(Notifier *notifier, void *data) -{ - remove_vm_lock_os(); -} - -static Notifier remove_lock = { .notify = notify_remove_lock }; - -void make_vm_lock_os(void) -{ - int base_port; - - check_vm_lock_os(); - - base_port = get_vm_base_port(); - - g_shmid = shmget((key_t)base_port, getpagesize(), 0666|IPC_CREAT); - if (g_shmid == -1) { - ERR("shmget failed\n"); - perror("osutil-linux: "); - return; - } - - g_shared_memory = shmat(g_shmid, (char *)0x00, 0); - if (g_shared_memory == (void *)-1) { - ERR("shmat failed\n"); - perror("osutil-linux: "); - return; - } - - g_sprintf(g_shared_memory, "%s", get_drive_image_file()); - INFO("shared memory key: %d value: %s\n", - base_port, (char *)g_shared_memory); - - if (shmdt(g_shared_memory) == -1) { - ERR("shmdt failed\n"); - perror("osutil-linux: "); - } - - emulator_add_exit_notifier(&remove_lock); +void make_vm_lock_os(void) { + make_vm_lock_posix(); } void set_bin_path_os(char const *const exec_argv) @@ -186,19 +82,18 @@ int get_number_of_processors(void) if (num_processors < 1) { num_processors = 1; } - TRACE("Number of processors : %d\n", num_processors); + LOG_TRACE("Number of processors : %d\n", num_processors); return num_processors; } void print_system_info_os(void) { - INFO("* Linux\n"); - - INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING); + LOG_INFO("* Linux\n"); + LOG_INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING); /* depends on building */ - INFO("* QEMU build machine linux kernel version : %d.%d.%d\n", + LOG_INFO("* QEMU build machine linux kernel version : %d.%d.%d\n", LINUX_VERSION_CODE >> 16, (LINUX_VERSION_CODE >> 8) & 0xff, LINUX_VERSION_CODE & 0xff); @@ -206,7 +101,7 @@ void print_system_info_os(void) /* depends on launching */ struct utsname host_uname_buf; if (uname(&host_uname_buf) == 0) { - INFO("* Host machine uname : %s %s %s %s %s\n", + LOG_INFO("* Host machine uname : %s %s %s %s %s\n", host_uname_buf.sysname, host_uname_buf.nodename, host_uname_buf.release, host_uname_buf.version, host_uname_buf.machine); @@ -214,13 +109,13 @@ void print_system_info_os(void) struct sysinfo sys_info; if (sysinfo(&sys_info) == 0) { - INFO("* Total Ram : %llu kB, Free: %llu kB\n", + LOG_INFO("* Total Ram : %llu kB, Free: %llu kB\n", sys_info.totalram * (unsigned long long)sys_info.mem_unit / 1024, sys_info.freeram * (unsigned long long)sys_info.mem_unit / 1024); } /* get linux distribution information */ - INFO("* Linux distribution infomation :\n"); + LOG_INFO("* Linux distribution infomation :\n"); char const *const lsb_release_cmd = "lsb_release -d -r -c"; gchar *buffer = NULL; gint buffer_size = strlen(lsb_release_cmd) + 1; @@ -230,13 +125,13 @@ void print_system_info_os(void) g_snprintf(buffer, buffer_size, "%s", lsb_release_cmd); if (system(buffer) < 0) { - INFO("system function command '%s' \ + LOG_INFO("system function command '%s' \ returns error !", buffer); } g_free(buffer); /* pci device description */ - INFO("* Host PCI devices :\n"); + LOG_INFO("* Host PCI devices :\n"); char const *const lspci_cmd = "lspci"; buffer_size = strlen(lspci_cmd) + 1; @@ -246,7 +141,7 @@ void print_system_info_os(void) fflush(stdout); if (system(buffer) < 0) { - INFO("system function command '%s' \ + LOG_INFO("system function command '%s' \ returns error !", buffer); } g_free(buffer); diff --git a/tizen/src/util/osutil-win32.c b/tizen/src/util/osutil-win32.c index 70ce715d61..a4c522ae2f 100644 --- a/tizen/src/util/osutil-win32.c +++ b/tizen/src/util/osutil-win32.c @@ -1,14 +1,11 @@ /* - * Emulator + * osutil * - * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: * SeokYeon Hwang <syeon.hwang@samsung.com> * MunKyu Im <munkyu.im@samsung.com> - * GiWoong Kim <giwoong.kim@samsung.com> - * YeongKyoon Lee <yeongkyoon.lee@samsung.com> - * HyunJun Son * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,74 +31,37 @@ @brief Collection of utilities for win32 */ -#include <png.h> -#include "emulator_common.h" +#include <windows.h> + +#include "png.h" +#include "qemu/error-report.h" + +#include "qemu-common.h" #include "osutil.h" #include "emulator.h" #include "emul_state.h" -#include "emulator_options.h" #ifndef CONFIG_WIN32 #error #endif -#include <windows.h> -#include "qemu/error-report.h" +#include "new_debug_ch.h" +DECLARE_DEBUG_CHANNEL(osutil); -#include "debug_ch.h" -MULTI_DEBUG_CHANNEL (emulator, osutil); - -static HANDLE g_hMapFile; -static char *g_pBuf; +static char *lock_filename; +static HANDLE lock_file = INVALID_HANDLE_VALUE; static char g_sdcard[256] = {0,}; static sdcard_info info; -void check_vm_lock_os(void) +static void remove_vm_lock_os(void) { - uint32_t port; - char *base_port = NULL; - char *pBuf; - HANDLE hMapFile; - - for (port = 26100; port < 26200; port += 10) { - base_port = g_strdup_printf("%d", port); - hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port); - if (hMapFile == NULL) { - INFO("port %s is not used.\n", base_port); - continue; - } else { - pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50); - if (pBuf == NULL) { - ERR("Could not map view of file (%d).\n", GetLastError()); - CloseHandle(hMapFile); - } + g_assert(lock_file != INVALID_HANDLE_VALUE); - if (strcmp(pBuf, get_drive_image_file()) == 0) { - error_report("Can not execute this VM. " - "The same name is running now."); - UnmapViewOfFile(pBuf); - CloseHandle(hMapFile); - free(base_port); - exit(0); - } else { - UnmapViewOfFile(pBuf); - } - } + CloseHandle(lock_file); - CloseHandle(hMapFile); - free(base_port); - } -} - -static void remove_vm_lock_os(void) -{ - if (g_pBuf != NULL) { - UnmapViewOfFile(g_pBuf); - } - if (g_hMapFile != NULL) { - CloseHandle(g_hMapFile); - } + lock_file = INVALID_HANDLE_VALUE; + g_free(lock_filename); } static void notify_remove_lock(Notifier *notifier, void *data) @@ -111,39 +71,39 @@ static void notify_remove_lock(Notifier *notifier, void *data) static Notifier remove_lock = { .notify = notify_remove_lock }; +/* +On Windows, emulator will stop before checking lock because platform +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) { - char *port_in_use; - char *shared_memory; - int base_port; - - check_vm_lock_os(); - - base_port = get_vm_base_port(); - shared_memory = g_strdup_printf("%s", get_drive_image_file()); - port_in_use = g_strdup_printf("%d", base_port); - g_hMapFile = CreateFileMapping( - INVALID_HANDLE_VALUE, /* use paging file */ - NULL, /* default security */ - PAGE_READWRITE, /* read/write access */ - 0, /* maximum object size (high-order DWORD) */ - 50, /* maximum object size (low-order DWORD) */ - port_in_use); /* name of mapping object */ - if (g_hMapFile == NULL) { - ERR("Could not create file mapping object (%d).\n", GetLastError()); - return; - } + char *drive_image_filename = g_strdup(get_drive_image_file()); - g_pBuf = MapViewOfFile(g_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50); - if (g_pBuf == NULL) { - ERR("Could not map view of file (%d).\n", GetLastError()); - CloseHandle(g_hMapFile); - return; + lock_filename = g_strdup_printf("%s.lock", drive_image_filename); + + g_free(drive_image_filename); + + if(g_mkdir_with_parents(g_path_get_dirname(lock_filename), 0777)) { + LOG_WARNING("Can not create lock file: %ld\n", GetLastError()); } + lock_file = CreateFile(lock_filename, GENERIC_READ | GENERIC_WRITE, 0, + NULL, CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL); + if (lock_file == INVALID_HANDLE_VALUE) { + DWORD error = GetLastError(); + // On Windows, the file opened by CreateFile has exclusive lock + // naturally unless FILE_SHARE_* attribute is set. + if (error == ERROR_SHARING_VIOLATION) { + error_report("Can not execute this VM. " + "The same VM may be running now."); + exit(1); + } - CopyMemory((PVOID)g_pBuf, shared_memory, strlen(shared_memory)); - free(port_in_use); - free(shared_memory); + LOG_WARNING("Can not create lock file: %0xlx\n", error); + + } emulator_add_exit_notifier(&remove_lock); } @@ -171,7 +131,7 @@ int get_number_of_processors(void) int num_processors = 0; GetSystemInfo(&sysi); - TRACE("Processor type: %d, Core number: %d\n", + LOG_TRACE("Processor type: %d, Core number: %d\n", sysi.dwProcessorType, sysi.dwNumberOfProcessors); num_processors = sysi.dwNumberOfProcessors; @@ -187,16 +147,15 @@ OSVERSIONINFO osvi; void print_system_info_os(void) { - INFO("* Windows\n"); - - INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING); + LOG_INFO("* Windows\n"); + LOG_INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING); /* Retrieves information about the current os */ ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (GetVersionEx(&osvi)) { - INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, " + LOG_INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, " "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, osvi.dwPlatformId, osvi.szCSDVersion); @@ -208,7 +167,7 @@ void print_system_info_os(void) #if 0 GetSystemInfo(&sysi); - INFO("* Processor type : %d, Number of processors : %d\n", + LOG_INFO("* Processor type : %d, Number of processors : %d\n", sysi.dwProcessorType, sysi.dwNumberOfProcessors); #endif get_number_of_processors(); @@ -216,7 +175,7 @@ void print_system_info_os(void) MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); - INFO("* Total Ram : %llu kB, Free: %lld kB\n", + LOG_INFO("* Total Ram : %llu kB, Free: %lld kB\n", memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024); } @@ -232,16 +191,15 @@ bool make_sdcard_lock_os(char *sdcard) NULL); if (hFile == INVALID_HANDLE_VALUE) { - ERR("file open error : (%d)\n", GetLastError()); if (GetLastError() == ERROR_SHARING_VIOLATION) { if (strcmp(g_sdcard, sdcard) == 0) { - INFO("try to mount same sdcard!\n"); + LOG_INFO("try to mount same sdcard!\n"); } return false; } return true; } - INFO("Get file lock: %s\n", lock_file); + LOG_INFO("Get file lock: %s\n", lock_file); strncpy(g_sdcard, sdcard, strlen(sdcard)); info.handle = hFile; return true; @@ -260,14 +218,13 @@ int remove_sdcard_lock_os(char *sdcard) NULL); /* to check if previous lock exists */ if (hFile2 == INVALID_HANDLE_VALUE) { - ERR("CreateFile() error : (%d)\n", GetLastError()); if (GetLastError() == ERROR_SHARING_VIOLATION) { if (strncmp(g_sdcard, sdcard, strlen(sdcard)) != 0) { - INFO("not same sdcard!!!\n"); + LOG_INFO("not same sdcard!!!\n"); return ERR_UNLCK; } - INFO("unlock success: %s\n", lock_file); + LOG_INFO("unlock success: %s\n", lock_file); g_sdcard[0] = '\0'; CloseHandle(info.handle); return ERR_SUCCESS; @@ -275,7 +232,7 @@ int remove_sdcard_lock_os(char *sdcard) return ERR_UNLCK; } } else { - ERR("lockfile exists but, it is not locked.\n"); + LOG_TRACE("lockfile exists but, it is not locked.\n"); CloseHandle(hFile2); return ERR_UNLCK; } @@ -297,7 +254,7 @@ static int is_wow64(void) GetModuleHandle(TEXT("kernel32")), "IsWow64Process"); if (!fnIsWow64Process) { - WARN("GetProcAddress(IsWow64Process) failed, %s return false\n", + LOG_WARNING("GetProcAddress(IsWow64Process) failed, %s return false\n", __func__); return 0; } @@ -349,7 +306,7 @@ void get_java_path_win32(const char **java_path) MY_KEY_WOW64_64KEY, &hKey); if (res != ERROR_SUCCESS) { - WARN("Java Runtime Environment key not found\n"); + LOG_WARNING("Java Runtime Environment key not found\n"); goto javahome_not_found; } @@ -362,7 +319,7 @@ void get_java_path_win32(const char **java_path) &dwBufLen); RegCloseKey(hKey); if (res != ERROR_SUCCESS) { - WARN("JRE CurrentVersion not found\n"); + LOG_WARNING("JRE CurrentVersion not found\n"); goto javahome_not_found; } @@ -391,13 +348,13 @@ javahome_not_found: strJavaHome, PATH_MAX); if (dwBufLen == 0) { - WARN("There is no JavaHome\n"); + LOG_WARNING("There is no JavaHome\n"); // try it with "javaw.exe" return; } } g_sprintf(wow64_java_path, "\"%s\\bin\\javaw.exe\"", strJavaHome); - INFO("JavaHome: %s\n", wow64_java_path); + LOG_INFO("JavaHome: %s\n", wow64_java_path); *java_path = wow64_java_path; #endif diff --git a/tizen/src/util/osutil.c b/tizen/src/util/osutil.c index a9260ece81..2c13652940 100644 --- a/tizen/src/util/osutil.c +++ b/tizen/src/util/osutil.c @@ -1,14 +1,11 @@ /* - * Emulator + * osutil * - * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: * SeokYeon Hwang <syeon.hwang@samsung.com> * MunKyu Im <munkyu.im@samsung.com> - * GiWoong Kim <giwoong.kim@samsung.com> - * YeongKyoon Lee <yeongkyoon.lee@samsung.com> - * HyunJun Son * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,13 +31,22 @@ @brief Common functions for osutil */ +#include "qemu-common.h" + +#ifndef CONFIG_WIN32 + +#include "qemu/error-report.h" + #include "osutil.h" -#include "debug_ch.h" +#include "emulator.h" +#include "emul_state.h" -#include <string.h> +#include "new_debug_ch.h" +DECLARE_DEBUG_CHANNEL(osutil) + + +static int lock_file = -1; -#ifndef CONFIG_WIN32 -MULTI_DEBUG_CHANNEL(emulator, osutil); static sdcard_info info; static int fd_lock(int fd) @@ -69,6 +75,64 @@ static int fd_unlock(int fd) return fcntl(fd, F_SETLK, &lock); } +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"); + } + close(lock_file); + + lock_file = -1; +} + +static void notify_remove_lock(Notifier *notifier, void *data) +{ + remove_vm_lock_posix(); +} + +static Notifier remove_lock = { .notify = notify_remove_lock }; + +#define RETRY_COUNT 10 +void make_vm_lock_posix(void) +{ + const char *image_file = get_drive_image_file(); + int error = 0, i; + + g_assert(lock_file == -1); + g_assert(image_file != NULL); + + for (i = 0; i < RETRY_COUNT; ++i) { + lock_file = open(image_file, O_RDWR); + if (lock_file == -1) { + error = errno; + LOG_WARNING("Failed to open image file for lock: %s\n", + strerror(error)); + return; + } + + if (fd_lock(lock_file) == -1) { + error = errno; + if (error == EAGAIN || error == EACCES) { + if (i == RETRY_COUNT - 1) { + error_report("Can not execute this VM. " + "The same VM may be running now."); + exit(1); + } + g_usleep(10000); /* 10 msec */ + } + + LOG_WARNING("Failed to lock image file: %s\n", strerror(error)); + close(lock_file); + lock_file = -1; + return; + } + } + + emulator_add_exit_notifier(&remove_lock); +} + bool make_sdcard_lock_posix(char *sdcard) { char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid()); @@ -85,7 +149,7 @@ bool make_sdcard_lock_posix(char *sdcard) return false; } info.fd = fd; - INFO("Get file lock: %s\n", lock_file); + LOG_INFO("Get file lock: %s\n", lock_file); return true; } @@ -116,7 +180,7 @@ int remove_sdcard_lock_posix(char *sdcard) return ERR_UNLCK; } - INFO("unlock success: %s\n", lock_file); + LOG_INFO("unlock success: %s\n", lock_file); close(fd); close(info.fd); return ERR_SUCCESS; diff --git a/tizen/src/util/osutil.h b/tizen/src/util/osutil.h index 66a20dd3b0..74cb7df579 100644 --- a/tizen/src/util/osutil.h +++ b/tizen/src/util/osutil.h @@ -1,14 +1,11 @@ /* - * Emulator + * osutil * - * Copyright (C) 2011, 2012 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2012-2016 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: * SeokYeon Hwang <syeon.hwang@samsung.com> * MunKyu Im <munkyu.im@samsung.com> - * GiWoong Kim <giwoong.kim@samsung.com> - * YeongKyoon Lee <yeongkyoon.lee@samsung.com> - * HyunJun Son * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,17 +29,14 @@ #ifndef __OSUTIL_H__ #define __OSUTIL_H__ -#include <unistd.h> -#include <fcntl.h> - #include "emulator_common.h" + #ifdef CONFIG_WIN32 #include "sysemu/os-win32.h" #else #include "sysemu/os-posix.h" #endif -#define DEFAULTBUFLEN 512 #define ERR_SUCCESS 0 #define ERR_UNLCK 4 #define ERR_LCK 5 @@ -50,11 +44,19 @@ extern const char *pac_tempfile; -void check_vm_lock_os(void); void make_vm_lock_os(void); bool make_sdcard_lock_os(char *sdcard); int remove_sdcard_lock_os(char *sdcard); +#ifndef CONFIG_WIN32 +void make_vm_lock_posix(void); + +bool make_sdcard_lock_posix(char *sdcard); +int remove_sdcard_lock_posix(char *sdcard); +#else +void get_java_path_win32(const char **java_path); +#endif + void set_bin_path_os(char const *const); typedef struct sdcard_info { @@ -66,13 +68,6 @@ typedef struct sdcard_info char* lock_file; /* reserved for future use */ } sdcard_info; -#ifndef CONFIG_WIN32 -bool make_sdcard_lock_posix(char *sdcard); -int remove_sdcard_lock_posix(char *sdcard); -#else -void get_java_path_win32(const char **java_path); -#endif - void print_system_info_os(void); int get_number_of_processors(void); |