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