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)
{
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.