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 darwin
37 #include "maru_common.h"
41 #include "maru_err_table.h"
50 #include <sys/sysctl.h>
51 #include <SystemConfiguration/SystemConfiguration.h>
53 MULTI_DEBUG_CHANNEL(qemu, osutil);
55 extern int tizen_base_port;
56 CFDictionaryRef proxySettings;
58 static char *cfstring_to_cstring(CFStringRef str) {
63 CFIndex length = CFStringGetLength(str);
64 CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
65 char *buffer = (char *)malloc(maxSize);
66 if (CFStringGetCString(str, buffer, maxSize, kCFStringEncodingUTF8))
71 static int cfnumber_to_int(CFNumberRef num) {
76 CFNumberGetValue(num, kCFNumberIntType, &value);
80 void check_vm_lock_os(void)
85 void make_vm_lock_os(void)
90 shmid = shmget((key_t)SHMKEY, MAXLEN, 0666|IPC_CREAT);
92 ERR("shmget failed\n");
93 perror("osutil-darwin: ");
97 shared_memory = shmat(shmid, (char *)0x00, 0);
98 if (shared_memory == (void *)-1) {
99 ERR("shmat failed\n");
100 perror("osutil-darwin: ");
103 sprintf(shared_memory, "%d", tizen_base_port + 2);
104 INFO("shared memory key: %d, value: %s\n", SHMKEY, (char *)shared_memory);
106 if (shmdt(shared_memory) == -1) {
107 ERR("shmdt failed\n");
108 perror("osutil-darwin: ");
112 void set_bin_path_os(gchar * exec_argv)
114 gchar *file_name = NULL;
120 char *data = g_strdup(exec_argv);
122 ERR("Fail to strdup for paring a binary directory.\n");
126 file_name = g_strrstr(data, "/");
132 g_strlcpy(bin_path, data, strlen(data) - strlen(file_name) + 1);
134 g_strlcat(bin_path, "/", PATH_MAX);
139 void print_system_info_os(void)
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);
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);
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);
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);
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);
191 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
199 CFStringRef pacURL = (CFStringRef)CFDictionaryGetValue(proxySettings,
200 kSCPropNetProxiesProxyAutoConfigURLString);
202 char url[MAXLEN] = {};
203 CFStringGetCString(pacURL, url, sizeof url, kCFStringEncodingASCII);
204 INFO("pac address: %s\n", (char*)url);
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);
217 if(g_str_has_prefix(type, DIRECT)) {
218 INFO("auto proxy is set to direct mode\n");
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, "\";");
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);
236 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
241 ERR("fail to get pacfile fp\n");
245 remove(pac_tempfile);
249 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
253 CFNumberRef isEnable;
254 CFStringRef proxyHostname;
255 CFNumberRef proxyPort;
256 CFDictionaryRef proxySettings;
257 proxySettings = SCDynamicStoreCopyProxies(NULL);
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);
265 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPPort);
266 port = cfnumber_to_int(proxyPort);
267 // Save hostname & port
268 snprintf(http_proxy, MAXLEN, "%s:%d", hostname, port);
272 INFO("http proxy is null\n");
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);
281 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSPort);
282 port = cfnumber_to_int(proxyPort);
283 // Save hostname & port
284 snprintf(https_proxy, MAXLEN, "%s:%d", hostname, port);
288 INFO("https proxy is null\n");
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);
297 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPPort);
298 port = cfnumber_to_int(proxyPort);
299 // Save hostname & port
300 snprintf(ftp_proxy, MAXLEN, "%s:%d", hostname, port);
304 INFO("ftp proxy is null\n");
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);
313 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSPort);
314 port = cfnumber_to_int(proxyPort);
315 // Save hostname & port
316 snprintf(socks_proxy, MAXLEN, "%s:%d", hostname, port);
320 INFO("socks proxy is null\n");
322 CFRelease(proxySettings);
325 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
328 proxySettings = SCDynamicStoreCopyProxies(NULL);
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);