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);
53 extern char tizen_target_img_path[];
55 static const char *pactempfile = ".autoproxy";
57 void check_vm_lock_os(void)
60 char *base_port = NULL;
64 for (port = 26100; port < 26200; port += 10) {
65 base_port = g_strdup_printf("%d", port);
66 hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
67 if (hMapFile == NULL) {
68 INFO("port %s is not used.\n", base_port);
71 pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
73 ERR("Could not map view of file (%d).\n", GetLastError());
74 CloseHandle(hMapFile);
77 if (strcmp(pBuf, tizen_target_img_path) == 0) {
78 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
79 "Can not execute this VM.\n"
80 "The same name is running now.");
81 UnmapViewOfFile(pBuf);
82 CloseHandle(hMapFile);
86 UnmapViewOfFile(pBuf);
90 CloseHandle(hMapFile);
95 void make_vm_lock_os(void)
103 base_port = get_emul_vm_base_port();
104 shared_memory = g_strdup_printf("%s", tizen_target_img_path);
105 port_in_use = g_strdup_printf("%d", base_port);
106 hMapFile = CreateFileMapping(
107 INVALID_HANDLE_VALUE, /* use paging file */
108 NULL, /* default security */
109 PAGE_READWRITE, /* read/write access */
110 0, /* maximum object size (high-order DWORD) */
111 50, /* maximum object size (low-order DWORD) */
112 port_in_use); /* name of mapping object */
113 if (hMapFile == NULL) {
114 ERR("Could not create file mapping object (%d).\n", GetLastError());
117 pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
120 ERR("Could not map view of file (%d).\n", GetLastError());
121 CloseHandle(hMapFile);
125 CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
130 void set_bin_path_os(gchar * exec_argv)
132 gchar link_path[PATH_MAX] = { 0, };
133 gchar *file_name = NULL;
135 if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
139 file_name = g_strrstr(link_path, "\\");
140 g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
142 g_strlcat(bin_path, "\\", PATH_MAX);
145 void print_system_info_os(void)
149 INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
151 /* Retrieves information about the current os */
153 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
154 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
156 if (GetVersionEx(&osvi)) {
157 INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, "
158 "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
159 osvi.dwMinorVersion, osvi.dwBuildNumber,
160 osvi.dwPlatformId, osvi.szCSDVersion);
163 /* Retrieves information about the current system */
165 ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
167 GetSystemInfo(&sysi);
168 INFO("* Processor type : %d, Number of processors : %d\n",
169 sysi.dwProcessorType, sysi.dwNumberOfProcessors);
171 MEMORYSTATUSEX memInfo;
172 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
173 GlobalMemoryStatusEx(&memInfo);
174 INFO("* Total Ram : %llu kB, Free: %lld kB\n",
175 memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
178 static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
186 INFO("pac address: %s\n", (char*)url);
187 download_url((char*)url);
189 fp_pacfile = fopen(pactempfile, "r");
190 if(fp_pacfile != NULL) {
191 while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
192 if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
193 INFO("line found %s", line);
194 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
198 if(g_str_has_prefix(type, DIRECT)) {
199 INFO("auto proxy is set to direct mode\n");
202 else if(g_str_has_prefix(type, PROXY)) {
203 INFO("auto proxy is set to proxy mode\n");
204 INFO("type: %s, proxy: %s\n", type, proxy);
205 p = strtok(proxy, "\";");
207 INFO("auto proxy to set: %s\n",p);
208 strcpy(http_proxy, p);
209 strcpy(https_proxy, p);
210 strcpy(ftp_proxy, p);
211 strcpy(socks_proxy, p);
217 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
222 ERR("fail to get pacfile fp\n");
231 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
236 BYTE *proxyenable, *proxyserver;
242 nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
243 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
244 0, KEY_QUERY_VALUE, &hKey);
245 if (nRet != ERROR_SUCCESS) {
246 ERR("Failed to open registry from %s\n",
247 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
250 //check auto proxy key exists
251 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
252 if (lRet != ERROR_SUCCESS && dwLength == 0) {
253 ERR("Failed to query value from %s\n",
254 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
258 url = (char*)malloc(dwLength);
260 ERR( "Failed to allocate a buffer\n");
263 memset(url, 0x00, dwLength);
264 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
265 if (lRet == ERROR_SUCCESS && dwLength != 0) {
266 get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
272 //check manual proxy key exists
273 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
274 if (lRet != ERROR_SUCCESS && dwLength == 0) {
275 ERR(stderr, "Failed to query value from %s\n",
276 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
280 proxyenable = (BYTE*)malloc(dwLength);
281 if (proxyenable == NULL) {
282 ERR( "Failed to allocate a buffer\n");
287 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
288 if (lRet != ERROR_SUCCESS) {
290 ERR("Failed to query value from %s\n",
291 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
295 if (*(char*)proxyenable == 0) {
302 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
303 if (lRet != ERROR_SUCCESS && dwLength == 0) {
304 ERR("Failed to query value from from %s\n",
305 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
310 proxyserver = (BYTE*)malloc(dwLength);
311 if (proxyserver == NULL) {
312 ERR( "Failed to allocate a buffer\n");
317 memset(proxyserver, 0x00, dwLength);
318 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
319 if (lRet != ERROR_SUCCESS) {
321 ERR( "Failed to query value from from %s\n",
322 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
327 if((char*)proxyserver != NULL) {
328 INFO("proxy value: %s\n", (char*)proxyserver);
329 real_proxy = malloc(MAXLEN);
331 for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
332 if(strstr(p, HTTP_PROTOCOL)) {
333 remove_string(p, real_proxy, HTTP_PROTOCOL);
334 strcpy(http_proxy, real_proxy);
336 else if(strstr(p, HTTPS_PROTOCOL)) {
337 remove_string(p, real_proxy, HTTPS_PROTOCOL);
338 strcpy(https_proxy, real_proxy);
340 else if(strstr(p, FTP_PROTOCOL)) {
341 remove_string(p, real_proxy, FTP_PROTOCOL);
342 strcpy(ftp_proxy, real_proxy);
344 else if(strstr(p, SOCKS_PROTOCOL)) {
345 remove_string(p, real_proxy, SOCKS_PROTOCOL);
346 strcpy(socks_proxy, real_proxy);
349 INFO("all protocol uses the same proxy server: %s\n", p);
350 strcpy(http_proxy, p);
351 strcpy(https_proxy, p);
352 strcpy(ftp_proxy, p);
353 strcpy(socks_proxy, p);
359 INFO("proxy is null\n");