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