update source for tizen_2.1
[sdk/emulator/qemu.git] / tizen / src / osutil-darwin.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-darwin.c
34   @brief    Collection of utilities for darwin
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_DARWIN
45 #error
46 #endif
47
48 #include <string.h>
49 #include <sys/shm.h>
50 #include <sys/sysctl.h>
51 #include <SystemConfiguration/SystemConfiguration.h>
52
53 MULTI_DEBUG_CHANNEL(qemu, osutil);
54
55 extern int tizen_base_port;
56 CFDictionaryRef proxySettings;
57
58 static char *cfstring_to_cstring(CFStringRef str) {
59     if (str == NULL) {
60         return NULL;
61     }
62
63     CFIndex length = CFStringGetLength(str);
64     CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
65     char *buffer = (char *)malloc(maxSize);
66     if (CFStringGetCString(str, buffer, maxSize, kCFStringEncodingUTF8))
67         return buffer;
68     return NULL;
69 }
70
71 static int cfnumber_to_int(CFNumberRef num) {
72     if (!num)
73         return 0;
74
75     int value;
76     CFNumberGetValue(num, kCFNumberIntType, &value);
77     return value;
78 }
79
80 void check_vm_lock_os(void)
81 {
82     /* TODO: */
83 }
84
85 void make_vm_lock_os(void)
86 {
87     int shmid;
88     char *shared_memory;
89
90     shmid = shmget((key_t)SHMKEY, MAXLEN, 0666|IPC_CREAT);
91     if (shmid == -1) {
92         ERR("shmget failed\n");
93         perror("osutil-darwin: ");
94         return;
95     }
96
97     shared_memory = shmat(shmid, (char *)0x00, 0);
98     if (shared_memory == (void *)-1) {
99         ERR("shmat failed\n");
100         perror("osutil-darwin: ");
101         return;
102     }
103     sprintf(shared_memory, "%d", tizen_base_port + 2);
104     INFO("shared memory key: %d, value: %s\n", SHMKEY, (char *)shared_memory);
105     
106     if (shmdt(shared_memory) == -1) {
107         ERR("shmdt failed\n");
108         perror("osutil-darwin: ");
109     }
110 }
111
112 void set_bin_path_os(gchar * exec_argv)
113 {
114     gchar *file_name = NULL;
115
116     if (!exec_argv) {
117         return;
118     }
119
120     char *data = g_strdup(exec_argv);
121     if (!data) {
122         ERR("Fail to strdup for paring a binary directory.\n");
123         return;
124     }
125
126     file_name = g_strrstr(data, "/");
127     if (!file_name) {
128         free(data);
129         return;
130     }
131
132     g_strlcpy(bin_path, data, strlen(data) - strlen(file_name) + 1);
133
134     g_strlcat(bin_path, "/", PATH_MAX);
135     free(data);
136 }
137
138
139 void print_system_info_os(void)
140 {
141   INFO("* Mac\n");
142
143     /* uname */
144     INFO("* Host machine uname :\n");
145     char uname_cmd[MAXLEN] = "uname -a >> ";
146     strcat(uname_cmd, log_path);
147     if(system(uname_cmd) < 0) {
148         INFO("system function command '%s' \
149             returns error !", uname_cmd);
150     }
151
152     /* hw information */
153     int mib[2];
154     size_t len;
155     char *sys_info;
156     int sys_num = 0;
157
158     mib[0] = CTL_HW;
159     mib[1] = HW_MODEL;
160     sysctl(mib, 2, NULL, &len, NULL, 0);
161     sys_info = malloc(len * sizeof(char));
162     if (sysctl(mib, 2, sys_info, &len, NULL, 0) >= 0) {
163         INFO("* Machine model : %s\n", sys_info);
164     }
165     free(sys_info);
166
167     mib[0] = CTL_HW;
168     mib[1] = HW_MACHINE;
169     sysctl(mib, 2, NULL, &len, NULL, 0);
170     sys_info = malloc(len * sizeof(char));
171     if (sysctl(mib, 2, sys_info, &len, NULL, 0) >= 0) {
172         INFO("* Machine class : %s\n", sys_info);
173     }
174     free(sys_info);
175
176     mib[0] = CTL_HW;
177     mib[1] = HW_NCPU;
178     len = sizeof(sys_num);
179     if (sysctl(mib, 2, &sys_num, &len, NULL, 0) >= 0) {
180         INFO("* Number of processors : %d\n", sys_num);
181     }
182
183     mib[0] = CTL_HW;
184     mib[1] = HW_PHYSMEM;
185     len = sizeof(sys_num);
186     if (sysctl(mib, 2, &sys_num, &len, NULL, 0) >= 0) {
187         INFO("* Total memory : %llu bytes\n", sys_num);
188     }
189 }
190
191 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
192 {
193     char type[MAXLEN];
194     char proxy[MAXLEN];
195     char line[MAXLEN];
196     FILE *fp_pacfile;
197     char *p = NULL;
198
199     CFStringRef pacURL = (CFStringRef)CFDictionaryGetValue(proxySettings,
200                                 kSCPropNetProxiesProxyAutoConfigURLString);
201         if (pacURL) {
202                 char url[MAXLEN] = {};
203                 CFStringGetCString(pacURL, url, sizeof url, kCFStringEncodingASCII);
204                 INFO("pac address: %s\n", (char*)url);
205                 download_url(url);
206         }
207
208     fp_pacfile = fopen(pac_tempfile, "r");
209     if(fp_pacfile != NULL) {
210         while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
211             if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
212                 INFO("line found %s", line);
213                 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
214             }
215         }
216
217         if(g_str_has_prefix(type, DIRECT)) {
218             INFO("auto proxy is set to direct mode\n");
219             fclose(fp_pacfile);
220         }
221         else if(g_str_has_prefix(type, PROXY)) {
222             INFO("auto proxy is set to proxy mode\n");
223             INFO("type: %s, proxy: %s\n", type, proxy);
224             p = strtok(proxy, "\";");
225             if(p != NULL) {
226                 INFO("auto proxy to set: %s\n",p);
227                 strcpy(http_proxy, p);
228                 strcpy(https_proxy, p);
229                 strcpy(ftp_proxy, p);
230                 strcpy(socks_proxy, p);
231             }
232             fclose(fp_pacfile);
233         }
234         else
235         {
236             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
237             fclose(fp_pacfile);
238         }
239     }
240     else {
241         ERR("fail to get pacfile fp\n");
242         return -1;
243     }
244
245     remove(pac_tempfile);
246     return 0;
247 }
248
249 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
250 {
251     char *hostname;
252     int port;
253     CFNumberRef isEnable;
254     CFStringRef proxyHostname;
255     CFNumberRef proxyPort;
256     CFDictionaryRef proxySettings;
257     proxySettings = SCDynamicStoreCopyProxies(NULL);
258
259     isEnable  = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPEnable);
260     if (cfnumber_to_int(isEnable)) {
261         // Get proxy hostname
262         proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPProxy);
263         hostname = cfstring_to_cstring(proxyHostname);
264         // Get proxy port
265         proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPPort);
266         port = cfnumber_to_int(proxyPort);
267         // Save hostname & port
268         snprintf(http_proxy, MAXLEN, "%s:%d", hostname, port);
269
270         free(hostname);
271     } else {
272         INFO("http proxy is null\n");
273     }
274
275     isEnable  = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSEnable);
276     if (cfnumber_to_int(isEnable)) {
277         // Get proxy hostname
278         proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSProxy);
279         hostname = cfstring_to_cstring(proxyHostname);
280         // Get proxy port
281         proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSPort);
282         port = cfnumber_to_int(proxyPort);
283         // Save hostname & port
284         snprintf(https_proxy, MAXLEN, "%s:%d", hostname, port);
285
286         free(hostname);
287     } else {
288         INFO("https proxy is null\n");
289     }
290
291     isEnable  = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPEnable);
292     if (cfnumber_to_int(isEnable)) {
293         // Get proxy hostname
294         proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPProxy);
295         hostname = cfstring_to_cstring(proxyHostname);
296         // Get proxy port
297         proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPPort);
298         port = cfnumber_to_int(proxyPort);
299         // Save hostname & port
300         snprintf(ftp_proxy, MAXLEN, "%s:%d", hostname, port);
301
302         free(hostname);
303     } else {
304         INFO("ftp proxy is null\n");
305     }
306
307     isEnable  = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSEnable);
308     if (cfnumber_to_int(isEnable)) {
309         // Get proxy hostname
310         proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSProxy);
311         hostname = cfstring_to_cstring(proxyHostname);
312         // Get proxy port
313         proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSPort);
314         port = cfnumber_to_int(proxyPort);
315         // Save hostname & port
316         snprintf(socks_proxy, MAXLEN, "%s:%d", hostname, port);
317
318         free(hostname);
319     } else {
320         INFO("socks proxy is null\n");
321     }
322     CFRelease(proxySettings);
323 }
324
325 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
326 {
327     int ret;
328     proxySettings = SCDynamicStoreCopyProxies(NULL);
329     if(proxySettings) {
330         INFO("AUTO PROXY MODE\n");
331         ret = get_auto_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
332         if(strlen(http_proxy) == 0 && ret < 0) {
333             INFO("MANUAL PROXY MODE\n");
334                 get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
335             }
336     }
337 }