emulator: Cleans up code and introduce osutil
[sdk/emulator/qemu.git] / tizen / src / osutil-win32.c
1 /* 
2  * Emulator
3  *
4  * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * SeokYeon Hwang <syeon.hwang@samsung.com>
8  * MunKyu Im <munkyu.im@samsung.com>
9  * GiWoong Kim <giwoong.kim@samsung.com>
10  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11  * HyunJun Son
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  *
30  */
31
32 /**
33   @file     osutil-win32.c
34   @brief    Collection of utilities for win32
35  */
36
37 #include "maru_common.h"
38 #include "osutil.h"
39 #include "emulator.h"
40 #include "debug_ch.h"
41 #include "maru_err_table.h"
42 #include "sdb.h"
43
44 #include <windows.h>
45
46 MULTI_DEBUG_CHANNEL (emulator, osutil);
47
48 extern char tizen_target_img_path[];
49 extern int tizen_base_port;
50
51 void check_vm_lock_os(void)
52 {
53     uint32_t port;
54     char *base_port = NULL;
55     char *pBuf;
56     HANDLE hMapFile;
57     for (port = 26100; port < 26200; port += 10) {
58         base_port = g_strdup_printf("%d", port);
59         hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
60         if (hMapFile == NULL) {
61             INFO("port %s is not used.\n", base_port);
62             continue;
63         } else {
64              pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
65             if (pBuf == NULL) {
66                 ERR("Could not map view of file (%d).\n", GetLastError());
67                 CloseHandle(hMapFile);
68             }
69
70             if (strcmp(pBuf, tizen_target_img_path) == 0) {
71                 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
72                     "Can not execute this VM.\n"
73                     "The same name is running now.");
74                 UnmapViewOfFile(pBuf);
75                 CloseHandle(hMapFile);
76                 free(base_port);
77                 exit(0);
78             } else {
79                 UnmapViewOfFile(pBuf);
80             }
81         }
82
83         CloseHandle(hMapFile);
84         free(base_port);
85     }
86 }
87
88 void make_vm_lock_os(void)
89 {
90     HANDLE hMapFile;
91     char *pBuf;
92     char *port_in_use;
93     char *shared_memory;
94
95     shared_memory = g_strdup_printf("%s", tizen_target_img_path);
96     port_in_use =  g_strdup_printf("%d", tizen_base_port);
97     hMapFile = CreateFileMapping(
98                  INVALID_HANDLE_VALUE, /* use paging file */
99                  NULL,                 /* default security */
100                  PAGE_READWRITE,       /* read/write access */
101                  0,                /* maximum object size (high-order DWORD) */
102                  50,               /* maximum object size (low-order DWORD) */
103                  port_in_use);         /* name of mapping object */
104     if (hMapFile == NULL) {
105         ERR("Could not create file mapping object (%d).\n", GetLastError());
106         return;
107     }
108     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
109
110     if (pBuf == NULL) {
111         ERR("Could not map view of file (%d).\n", GetLastError());
112         CloseHandle(hMapFile);
113         return;
114     }
115
116     CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
117     free(port_in_use);
118     free(shared_memory);
119 }
120
121 void set_bin_path_os(gchar * exec_argv)
122 {
123     gchar link_path[PATH_MAX] = { 0, };
124     gchar *file_name = NULL;
125
126     if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
127         return;
128     }
129
130     file_name = g_strrstr(link_path, "\\");
131     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
132
133     g_strlcat(bin_path, "\\", PATH_MAX);
134 }
135
136 void print_system_info_os(void)
137 {
138     INFO("* Windows\n");
139
140     /* Retrieves information about the current os */
141     OSVERSIONINFO osvi;
142     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
143     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
144
145     if (GetVersionEx(&osvi)) {
146         INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, \
147             PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
148             osvi.dwMinorVersion, osvi.dwBuildNumber,
149             osvi.dwPlatformId, osvi.szCSDVersion);
150     }
151
152     /* Retrieves information about the current system */
153     SYSTEM_INFO sysi;
154     ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
155
156     GetSystemInfo(&sysi);
157     INFO("* Processor type : %d, Number of processors : %d\n",
158             sysi.dwProcessorType, sysi.dwNumberOfProcessors);
159
160     MEMORYSTATUSEX memInfo;
161     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
162     GlobalMemoryStatusEx(&memInfo);
163     INFO("* Total Ram : %llu kB, Free: %lld kB\n",
164             memInfo.ullTotalPhys / DIV, memInfo.ullAvailPhys / DIV);
165 }