Merge branch 'tizen_qemu_1.6' into develop
[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 <png.h>
38 #include "maru_common.h"
39 #include "osutil.h"
40 #include "emulator.h"
41 #include "debug_ch.h"
42 #include "maru_err_table.h"
43 #include "sdb.h"
44
45 #ifndef CONFIG_WIN32
46 #error
47 #endif
48
49 #include <windows.h>
50
51 MULTI_DEBUG_CHANNEL (emulator, osutil);
52
53 extern char tizen_target_img_path[];
54
55 static const char *pactempfile = ".autoproxy";
56
57 void check_vm_lock_os(void)
58 {
59     uint32_t port;
60     char *base_port = NULL;
61     char *pBuf;
62     HANDLE hMapFile;
63
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);
69             continue;
70         } else {
71              pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
72             if (pBuf == NULL) {
73                 ERR("Could not map view of file (%d).\n", GetLastError());
74                 CloseHandle(hMapFile);
75             }
76
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);
83                 free(base_port);
84                 exit(0);
85             } else {
86                 UnmapViewOfFile(pBuf);
87             }
88         }
89
90         CloseHandle(hMapFile);
91         free(base_port);
92     }
93 }
94
95 void make_vm_lock_os(void)
96 {
97     HANDLE hMapFile;
98     char *pBuf;
99     char *port_in_use;
100     char *shared_memory;
101     int base_port;
102
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());
115         return;
116     }
117     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
118
119     if (pBuf == NULL) {
120         ERR("Could not map view of file (%d).\n", GetLastError());
121         CloseHandle(hMapFile);
122         return;
123     }
124
125     CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
126     free(port_in_use);
127     free(shared_memory);
128 }
129
130 void set_bin_path_os(gchar * exec_argv)
131 {
132     gchar link_path[PATH_MAX] = { 0, };
133     gchar *file_name = NULL;
134
135     if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
136         return;
137     }
138
139     file_name = g_strrstr(link_path, "\\");
140     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
141
142     g_strlcat(bin_path, "\\", PATH_MAX);
143 }
144
145 int get_number_of_processors(void)
146 {
147     SYSTEM_INFO sysi;
148     int num_processors = 0;
149
150     GetSystemInfo(&sysi);
151     TRACE("Processor type: %d, Core number: %d\n",
152         sysi.dwProcessorType, sysi.dwNumberOfProcessors);
153
154     num_processors = sysi.dwNumberOfProcessors;
155     if (num_processors < 1) {
156         num_processors = 1;
157     }
158
159     return num_processors;
160 }
161
162 void print_system_info_os(void)
163 {
164     INFO("* Windows\n");
165
166     INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
167
168     /* Retrieves information about the current os */
169     OSVERSIONINFO osvi;
170     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
171     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
172
173     if (GetVersionEx(&osvi)) {
174         INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, "
175             "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
176             osvi.dwMinorVersion, osvi.dwBuildNumber,
177             osvi.dwPlatformId, osvi.szCSDVersion);
178     }
179
180     /* Retrieves information about the current system */
181     SYSTEM_INFO sysi;
182     ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
183
184 #if 0
185     GetSystemInfo(&sysi);
186     INFO("* Processor type : %d, Number of processors : %d\n",
187             sysi.dwProcessorType, sysi.dwNumberOfProcessors);
188 #endif
189     get_number_of_processors();
190
191     MEMORYSTATUSEX memInfo;
192     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
193     GlobalMemoryStatusEx(&memInfo);
194     INFO("* Total Ram : %llu kB, Free: %lld kB\n",
195             memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
196 }
197
198 static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
199 {
200     char type[MAXLEN];
201     char proxy[MAXLEN];
202     char line[MAXLEN];
203     FILE *fp_pacfile;
204     char *p = NULL;
205
206     INFO("pac address: %s\n", (char*)url);
207     download_url((char*)url);
208
209     fp_pacfile = fopen(pactempfile, "r");
210     if(fp_pacfile != NULL) {
211         while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
212             if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
213                 INFO("line found %s", line);
214                 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
215             }
216         }
217
218         if(g_str_has_prefix(type, DIRECT)) {
219             INFO("auto proxy is set to direct mode\n");
220             fclose(fp_pacfile);
221         }
222         else if(g_str_has_prefix(type, PROXY)) {
223             INFO("auto proxy is set to proxy mode\n");
224             INFO("type: %s, proxy: %s\n", type, proxy);
225             p = strtok(proxy, "\";");
226             if(p != NULL) {
227                 INFO("auto proxy to set: %s\n",p);
228                 strcpy(http_proxy, p);
229                 strcpy(https_proxy, p);
230                 strcpy(ftp_proxy, p);
231                 strcpy(socks_proxy, p);
232             }
233             fclose(fp_pacfile);
234         }
235         else
236         {
237             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
238             fclose(fp_pacfile);
239         }
240     }
241     else {
242         ERR("fail to get pacfile fp\n");
243         return -1;
244     }
245
246     remove(pactempfile);
247
248     return 0;
249 }
250
251 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
252 {
253     HKEY hKey;
254     int nRet;
255     LONG lRet;
256     BYTE *proxyenable, *proxyserver;
257     char *p;
258     char *real_proxy;
259     BYTE *url;
260
261     DWORD dwLength = 0;
262     nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
263             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
264             0, KEY_QUERY_VALUE, &hKey);
265     if (nRet != ERROR_SUCCESS) {
266         ERR("Failed to open registry from %s\n",
267                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
268         return 0;
269     }
270     //check auto proxy key exists
271     lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
272     if (lRet != ERROR_SUCCESS && dwLength == 0) {
273         ERR("Failed to query value from %s\n",
274                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
275     }
276     else {
277         //if exists
278         url = (char*)malloc(dwLength);
279         if (url == NULL) {
280             ERR( "Failed to allocate a buffer\n");
281         }
282         else {
283             memset(url, 0x00, dwLength);
284             lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
285             if (lRet == ERROR_SUCCESS && dwLength != 0) {
286                 get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
287                 RegCloseKey(hKey);
288                 return 0;
289             }
290         }
291     }
292     //check manual proxy key exists
293     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
294     if (lRet != ERROR_SUCCESS && dwLength == 0) {
295         ERR(stderr, "Failed to query value from %s\n",
296                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
297         RegCloseKey(hKey);
298         return 0;
299     }
300     proxyenable = (BYTE*)malloc(dwLength);
301     if (proxyenable == NULL) {
302         ERR( "Failed to allocate a buffer\n");
303         RegCloseKey(hKey);
304         return 0;
305     }
306
307     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
308     if (lRet != ERROR_SUCCESS) {
309         free(proxyenable);
310         ERR("Failed to query value from %s\n",
311                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
312         RegCloseKey(hKey);
313         return 0;
314     }
315     if (*(char*)proxyenable == 0) {
316         free(proxyenable);
317         RegCloseKey(hKey);
318         return 0;
319     }
320
321     dwLength = 0;
322     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
323     if (lRet != ERROR_SUCCESS && dwLength == 0) {
324         ERR("Failed to query value from from %s\n",
325                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
326         RegCloseKey(hKey);
327         return 0;
328     }
329
330     proxyserver = (BYTE*)malloc(dwLength);
331     if (proxyserver == NULL) {
332         ERR( "Failed to allocate a buffer\n");
333         RegCloseKey(hKey);
334         return 0;
335     }
336
337     memset(proxyserver, 0x00, dwLength);
338     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
339     if (lRet != ERROR_SUCCESS) {
340         free(proxyserver);
341         ERR( "Failed to query value from from %s\n",
342                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
343         RegCloseKey(hKey);
344         return 0;
345     }
346
347     if((char*)proxyserver != NULL) {
348         INFO("proxy value: %s\n", (char*)proxyserver);
349         real_proxy = malloc(MAXLEN);
350
351         for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
352             if(strstr(p, HTTP_PROTOCOL)) {
353                 remove_string(p, real_proxy, HTTP_PROTOCOL);
354                 strcpy(http_proxy, real_proxy);
355             }
356             else if(strstr(p, HTTPS_PROTOCOL)) {
357                 remove_string(p, real_proxy, HTTPS_PROTOCOL);
358                 strcpy(https_proxy, real_proxy);
359             }
360             else if(strstr(p, FTP_PROTOCOL)) {
361                 remove_string(p, real_proxy, FTP_PROTOCOL);
362                 strcpy(ftp_proxy, real_proxy);
363             }
364             else if(strstr(p, SOCKS_PROTOCOL)) {
365                 remove_string(p, real_proxy, SOCKS_PROTOCOL);
366                 strcpy(socks_proxy, real_proxy);
367             }
368             else {
369                 INFO("all protocol uses the same proxy server: %s\n", p);
370                 strcpy(http_proxy, p);
371                 strcpy(https_proxy, p);
372                 strcpy(ftp_proxy, p);
373                 strcpy(socks_proxy, p);
374             }
375         }
376         free(real_proxy);
377     }
378     else {
379         INFO("proxy is null\n");
380         return 0;
381     }
382     RegCloseKey(hKey);
383 }