4 * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
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>
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.
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.
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.
34 @brief Collection of utilities for win32
38 #include "maru_common.h"
42 #include "maru_err_table.h"
51 MULTI_DEBUG_CHANNEL (emulator, osutil);
54 static qemu_timeval tv = { 0, 0 };
56 static char buf_time[64];
58 extern char tizen_target_img_path[];
60 static const char *pactempfile = ".autoproxy";
62 void check_vm_lock_os(void)
65 char *base_port = NULL;
69 for (port = 26100; port < 26200; port += 10) {
70 base_port = g_strdup_printf("%d", port);
71 hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
72 if (hMapFile == NULL) {
73 INFO("port %s is not used.\n", base_port);
76 pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
78 ERR("Could not map view of file (%d).\n", GetLastError());
79 CloseHandle(hMapFile);
82 if (strcmp(pBuf, tizen_target_img_path) == 0) {
83 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
84 "Can not execute this VM.\n"
85 "The same name is running now.");
86 UnmapViewOfFile(pBuf);
87 CloseHandle(hMapFile);
91 UnmapViewOfFile(pBuf);
95 CloseHandle(hMapFile);
100 void make_vm_lock_os(void)
108 base_port = get_emul_vm_base_port();
109 shared_memory = g_strdup_printf("%s", tizen_target_img_path);
110 port_in_use = g_strdup_printf("%d", base_port);
111 hMapFile = CreateFileMapping(
112 INVALID_HANDLE_VALUE, /* use paging file */
113 NULL, /* default security */
114 PAGE_READWRITE, /* read/write access */
115 0, /* maximum object size (high-order DWORD) */
116 50, /* maximum object size (low-order DWORD) */
117 port_in_use); /* name of mapping object */
118 if (hMapFile == NULL) {
119 ERR("Could not create file mapping object (%d).\n", GetLastError());
122 pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
125 ERR("Could not map view of file (%d).\n", GetLastError());
126 CloseHandle(hMapFile);
130 CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
135 void set_bin_path_os(gchar * exec_argv)
137 gchar link_path[PATH_MAX] = { 0, };
138 gchar *file_name = NULL;
140 if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
144 file_name = g_strrstr(link_path, "\\");
145 g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
147 g_strlcat(bin_path, "\\", PATH_MAX);
150 int get_number_of_processors(void)
153 int num_processors = 0;
155 GetSystemInfo(&sysi);
156 TRACE("Processor type: %d, Core number: %d\n",
157 sysi.dwProcessorType, sysi.dwNumberOfProcessors);
159 num_processors = sysi.dwNumberOfProcessors;
160 if (num_processors < 1) {
164 return num_processors;
167 void print_system_info_os(void)
171 INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
173 /* Retrieves information about the current os */
175 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
176 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
178 if (GetVersionEx(&osvi)) {
179 INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, "
180 "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
181 osvi.dwMinorVersion, osvi.dwBuildNumber,
182 osvi.dwPlatformId, osvi.szCSDVersion);
185 /* Retrieves information about the current system */
187 ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
190 GetSystemInfo(&sysi);
191 INFO("* Processor type : %d, Number of processors : %d\n",
192 sysi.dwProcessorType, sysi.dwNumberOfProcessors);
194 get_number_of_processors();
196 MEMORYSTATUSEX memInfo;
197 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
198 GlobalMemoryStatusEx(&memInfo);
199 INFO("* Total Ram : %llu kB, Free: %lld kB\n",
200 memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
203 char *get_timeofday(void)
205 qemu_gettimeofday(&tv);
208 struct tm *ptm = localtime(&ti);
209 strftime(buf_time, sizeof(buf_time),
215 static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
223 INFO("pac address: %s\n", (char*)url);
224 download_url((char*)url);
226 fp_pacfile = fopen(pactempfile, "r");
227 if(fp_pacfile != NULL) {
228 while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
229 if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
230 INFO("line found %s", line);
231 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
235 if(g_str_has_prefix(type, DIRECT)) {
236 INFO("auto proxy is set to direct mode\n");
239 else if(g_str_has_prefix(type, PROXY)) {
240 INFO("auto proxy is set to proxy mode\n");
241 INFO("type: %s, proxy: %s\n", type, proxy);
242 p = strtok(proxy, "\";");
244 INFO("auto proxy to set: %s\n",p);
245 strcpy(http_proxy, p);
246 strcpy(https_proxy, p);
247 strcpy(ftp_proxy, p);
248 strcpy(socks_proxy, p);
254 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
259 ERR("fail to get pacfile fp\n");
268 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
273 BYTE *proxyenable, *proxyserver;
279 nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
280 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
281 0, KEY_QUERY_VALUE, &hKey);
282 if (nRet != ERROR_SUCCESS) {
283 ERR("Failed to open registry from %s\n",
284 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
287 //check auto proxy key exists
288 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
289 if (lRet != ERROR_SUCCESS && dwLength == 0) {
290 ERR("Failed to query value from %s\n",
291 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
295 url = (char*)malloc(dwLength);
297 ERR( "Failed to allocate a buffer\n");
300 memset(url, 0x00, dwLength);
301 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
302 if (lRet == ERROR_SUCCESS && dwLength != 0) {
303 get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
309 //check manual proxy key exists
310 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
311 if (lRet != ERROR_SUCCESS && dwLength == 0) {
312 ERR(stderr, "Failed to query value from %s\n",
313 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
317 proxyenable = (BYTE*)malloc(dwLength);
318 if (proxyenable == NULL) {
319 ERR( "Failed to allocate a buffer\n");
324 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
325 if (lRet != ERROR_SUCCESS) {
327 ERR("Failed to query value from %s\n",
328 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
332 if (*(char*)proxyenable == 0) {
339 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
340 if (lRet != ERROR_SUCCESS && dwLength == 0) {
341 ERR("Failed to query value from from %s\n",
342 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
347 proxyserver = (BYTE*)malloc(dwLength);
348 if (proxyserver == NULL) {
349 ERR( "Failed to allocate a buffer\n");
354 memset(proxyserver, 0x00, dwLength);
355 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
356 if (lRet != ERROR_SUCCESS) {
358 ERR( "Failed to query value from from %s\n",
359 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
364 if((char*)proxyserver != NULL) {
365 INFO("proxy value: %s\n", (char*)proxyserver);
366 real_proxy = malloc(MAXLEN);
368 for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
369 if(strstr(p, HTTP_PROTOCOL)) {
370 remove_string(p, real_proxy, HTTP_PROTOCOL);
371 strcpy(http_proxy, real_proxy);
373 else if(strstr(p, HTTPS_PROTOCOL)) {
374 remove_string(p, real_proxy, HTTPS_PROTOCOL);
375 strcpy(https_proxy, real_proxy);
377 else if(strstr(p, FTP_PROTOCOL)) {
378 remove_string(p, real_proxy, FTP_PROTOCOL);
379 strcpy(ftp_proxy, real_proxy);
381 else if(strstr(p, SOCKS_PROTOCOL)) {
382 remove_string(p, real_proxy, SOCKS_PROTOCOL);
383 strcpy(socks_proxy, real_proxy);
386 INFO("all protocol uses the same proxy server: %s\n", p);
387 strcpy(http_proxy, p);
388 strcpy(https_proxy, p);
389 strcpy(ftp_proxy, p);
390 strcpy(socks_proxy, p);
396 INFO("proxy is null\n");