From: Sangho Park Date: Mon, 1 Feb 2016 02:11:26 +0000 (+0900) Subject: osutil: fix bugs on locking sdcard in Windows X-Git-Tag: Tizen_Studio_1.3_Release_p2.3.2~73 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1fb4635887d48d563cd1396fc45f966f4de88236;p=sdk%2Femulator%2Fqemu.git osutil: fix bugs on locking sdcard in Windows * Remove 'pid' on filename of sdcard lock. It makes that only a single emulator can attach the same sdcard image. * Check whether sdcard is already attached or not when try to attach sdcard * Check whether sdcard is attached and same when try to detach sdcard * Refactoring ** Just CloseHandle() when detach sdcard. It is sufficient to remove the lock file ** Remove redundant memory allocation Change-Id: Ib6611dceffa5b9e6dfaf10fb450b9c7b218687b7 Signed-off-by: Sangho Park --- diff --git a/tizen/src/util/osutil-win32.c b/tizen/src/util/osutil-win32.c index f1e3f80d7c..388d7dfa05 100644 --- a/tizen/src/util/osutil-win32.c +++ b/tizen/src/util/osutil-win32.c @@ -51,8 +51,10 @@ DECLARE_DEBUG_CHANNEL(osutil); static char *lock_filename; static HANDLE lock_file = INVALID_HANDLE_VALUE; -static char g_sdcard[256] = {0,}; -static sdcard_info info; +static sdcard_info info = { + .handle = INVALID_HANDLE_VALUE, + .lock_file = NULL, +}; static void remove_vm_lock_os(void) { @@ -181,61 +183,67 @@ void print_system_info_os(void) bool make_sdcard_lock_os(char *sdcard) { - char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid()); - - HANDLE hFile = CreateFile(lock_file, GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_DELETE, - 0, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, - NULL); - - if (hFile == INVALID_HANDLE_VALUE) { - if (GetLastError() == ERROR_SHARING_VIOLATION) { - if (strcmp(g_sdcard, sdcard) == 0) { - LOG_INFO("try to mount same sdcard!\n"); - } - return false; - } - return true; + HANDLE h; + char *fname; + + if (info.handle != INVALID_HANDLE_VALUE) { + g_assert(info.lock_file != NULL); + LOG_INFO("sdcard(%s) is already attached\n", info.lock_file); + return false; + } + + fname = g_strdup_printf("%s.lck", sdcard); + h = CreateFile(fname, + GENERIC_READ, + 0, // No share + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, + NULL); + + if (h == INVALID_HANDLE_VALUE) { + LOG_WARNING("Failed to CreateFile a sdcard lock file: %d\n", + (DWORD)GetLastError()); + g_free(fname); + return false; + } + + /* If the lock file exists and CreateFile() succeeded, just go on. + * If other emulator locks the sdcard, CreateFile failed owing to + * ERROR_SHARING_VIOLATION. Therefore, no emulator opened the lock file + * and just go on attaching sdcard. + */ + if (GetLastError() != ERROR_ALREADY_EXISTS) { + LOG_WARNING("The lock file exists but CreateFile succeeded. Just go on\n"); } - LOG_INFO("Get file lock: %s\n", lock_file); - strncpy(g_sdcard, sdcard, strlen(sdcard)); - info.handle = hFile; + + info.handle = h; + info.lock_file = fname; + LOG_INFO("Locked sdcard: %s\n", fname); return true; } int remove_sdcard_lock_os(char *sdcard) { - char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid()); - HANDLE hFile2; - hFile2 = CreateFile(lock_file, GENERIC_READ, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - /* to check if previous lock exists */ - if (hFile2 == INVALID_HANDLE_VALUE) { - if (GetLastError() == ERROR_SHARING_VIOLATION) { - if (strncmp(g_sdcard, sdcard, strlen(sdcard)) != 0) { - LOG_INFO("not same sdcard!!!\n"); - return ERR_UNLCK; - } - - LOG_INFO("unlock success: %s\n", lock_file); - g_sdcard[0] = '\0'; - CloseHandle(info.handle); - return ERR_SUCCESS; - } else { - return ERR_UNLCK; - } - } else { - LOG_TRACE("lockfile exists but, it is not locked.\n"); - CloseHandle(hFile2); - return ERR_UNLCK; + /* check whether the sdcard is attached or not */ + if (info.handle == INVALID_HANDLE_VALUE) { + LOG_INFO("No attached sdcard\n"); + return ERR_NODEV; } + + if (memcmp(sdcard, info.lock_file, strlen(sdcard)) != 0) { + LOG_INFO("%s is already attached\n", info.lock_file); + return ERR_NOENT; + } + + /* CloseHandle(info.handle) will remove the lock file */ + LOG_INFO("Unlocked sdcard\n"); + CloseHandle(info.handle); + info.handle = INVALID_HANDLE_VALUE; + g_free(info.lock_file); + info.lock_file = NULL; + return ERR_SUCCESS; } /* Gets the JavaHome path from the windows registry.