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 linux
37 #include "maru_common.h"
41 #include "maru_err_table.h"
52 #include <linux/version.h>
53 #include <sys/utsname.h>
54 #include <sys/sysinfo.h>
56 MULTI_DEBUG_CHANNEL(emulator, osutil);
58 extern char tizen_target_img_path[];
59 extern int tizen_base_port;
61 char *g_shared_memory;
63 void check_vm_lock_os(void)
69 struct shmid_ds shm_info;
71 for (port = 26100; port < 26200; port += 10) {
72 shm_id = shmget((key_t)port, 0, 0);
74 shm_addr = shmat(shm_id, (void *)0, 0);
75 if ((void *)-1 == shm_addr) {
76 ERR("error occured at shmat()\n");
80 val = shmctl(shm_id, IPC_STAT, &shm_info);
82 INFO("count of process that use shared memory : %d\n",
84 if ((shm_info.shm_nattch > 0) &&
85 g_strcmp0(tizen_target_img_path, (char *)shm_addr) == 0) {
86 if (check_port_bind_listen(port + 1) > 0) {
91 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
92 "Can not execute this VM.\n"
93 "The same name is running now.");
103 void make_vm_lock_os(void)
106 g_shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
108 ERR("shmget failed\n");
112 g_shared_memory = shmat(g_shmid, (char *)0x00, 0);
113 if (g_shared_memory == (void *)-1) {
114 ERR("shmat failed\n");
117 g_sprintf(g_shared_memory, "%s", tizen_target_img_path);
118 INFO("shared memory key: %d value: %s\n",
119 tizen_base_port, (char *)g_shared_memory);
122 void set_bin_path_os(gchar * exec_argv)
124 gchar link_path[PATH_MAX] = { 0, };
125 char *file_name = NULL;
127 ssize_t len = readlink("/proc/self/exe", link_path, sizeof(link_path) - 1);
129 if (len < 0 || len > sizeof(link_path)) {
130 perror("set_bin_path error : ");
134 link_path[len] = '\0';
136 file_name = g_strrstr(link_path, "/");
137 g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
139 g_strlcat(bin_path, "/", PATH_MAX);
142 void print_system_info_os(void)
146 /* depends on building */
147 INFO("* QEMU build machine linux kernel version : (%d, %d, %d)\n",
148 LINUX_VERSION_CODE >> 16,
149 (LINUX_VERSION_CODE >> 8) & 0xff,
150 LINUX_VERSION_CODE & 0xff);
152 /* depends on launching */
153 struct utsname host_uname_buf;
154 if (uname(&host_uname_buf) == 0) {
155 INFO("* Host machine uname : %s %s %s %s %s\n",
156 host_uname_buf.sysname, host_uname_buf.nodename,
157 host_uname_buf.release, host_uname_buf.version,
158 host_uname_buf.machine);
161 struct sysinfo sys_info;
162 if (sysinfo(&sys_info) == 0) {
163 INFO("* Total Ram : %llu kB, Free: %llu kB\n",
164 sys_info.totalram * (unsigned long long)sys_info.mem_unit / 1024,
165 sys_info.freeram * (unsigned long long)sys_info.mem_unit / 1024);
168 /* get linux distribution information */
169 INFO("* Linux distribution infomation :\n");
170 char lsb_release_cmd[MAXLEN] = "lsb_release -d -r -c >> ";
171 strcat(lsb_release_cmd, log_path);
172 if(system(lsb_release_cmd) < 0) {
173 INFO("system function command '%s' \
174 returns error !", lsb_release_cmd);
177 /* pci device description */
178 INFO("* PCI devices :\n");
179 char lspci_cmd[MAXLEN] = "lspci >> ";
180 strcat(lspci_cmd, log_path);
181 if(system(lspci_cmd) < 0) {
182 INFO("system function command '%s' \
183 returns error !", lspci_cmd);
187 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
197 output = popen("gconftool-2 --get /system/proxy/autoconfig_url", "r");
198 if(fscanf(output, "%s", buf) > 0) {
199 INFO("pac address: %s\n", buf);
203 fp_pacfile = fopen(pac_tempfile, "r");
204 if(fp_pacfile != NULL) {
205 while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
206 if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
207 INFO("line found %s", line);
208 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
212 if(g_str_has_prefix(type, DIRECT)) {
213 INFO("auto proxy is set to direct mode\n");
216 else if(g_str_has_prefix(type, PROXY)) {
217 INFO("auto proxy is set to proxy mode\n");
218 INFO("type: %s, proxy: %s\n", type, proxy);
219 p = strtok(proxy, "\";");
221 INFO("auto proxy to set: %s\n",p);
222 strcpy(http_proxy, p);
223 strcpy(https_proxy, p);
224 strcpy(ftp_proxy, p);
225 strcpy(socks_proxy, p);
231 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
236 ERR("fail to get pacfile fp\n");
240 remove(pac_tempfile);
244 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
246 char buf[MAXLEN] = {0,};
247 char buf_port[MAXPORTLEN] = {0,};
248 char buf_proxy[MAXLEN] = {0,};
252 int MAXPROXYLEN = MAXLEN + MAXPORTLEN;
254 output = popen("gconftool-2 --get /system/http_proxy/host", "r");
255 if(fscanf(output, "%s", buf) > 0) {
256 snprintf(buf_proxy, MAXLEN, "%s", buf);
260 output = popen("gconftool-2 --get /system/http_proxy/port", "r");
261 if(fscanf(output, "%s", buf_port) <= 0) {
262 //for abnormal case: if can't find the key of http port, get from environment value.
263 buf_proxy_bak = getenv("http_proxy");
264 INFO("http_proxy from env: %s\n", buf_proxy_bak);
265 if(buf_proxy_bak != NULL) {
266 proxy = malloc(MAXLEN);
267 remove_string(buf_proxy_bak, proxy, HTTP_PREFIX);
268 strncpy(http_proxy, proxy, strlen(proxy)-1);
269 INFO("final http_proxy value: %s\n", http_proxy);
273 INFO("http_proxy is not set on env.\n");
280 snprintf(http_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf_port);
281 memset(buf_proxy, 0, MAXLEN);
282 INFO("http_proxy: %s\n", http_proxy);
286 memset(buf, 0, MAXLEN);
288 output = popen("gconftool-2 --get /system/proxy/secure_host", "r");
289 if(fscanf(output, "%s", buf) > 0) {
290 snprintf(buf_proxy, MAXLEN, "%s", buf);
294 output = popen("gconftool-2 --get /system/proxy/secure_port", "r");
295 if(fscanf(output, "%s", buf) > 0) {
296 snprintf(https_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
299 memset(buf, 0, MAXLEN);
300 memset(buf_proxy, 0, MAXLEN);
301 INFO("https_proxy : %s\n", https_proxy);
303 output = popen("gconftool-2 --get /system/proxy/ftp_host", "r");
304 if(fscanf(output, "%s", buf) > 0) {
305 snprintf(buf_proxy, MAXLEN, "%s", buf);
309 output = popen("gconftool-2 --get /system/proxy/ftp_port", "r");
310 if(fscanf(output, "%s", buf) > 0) {
311 snprintf(ftp_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
314 memset(buf, 0, MAXLEN);
315 memset(buf_proxy, 0, MAXLEN);
316 INFO("ftp_proxy : %s\n", ftp_proxy);
318 output = popen("gconftool-2 --get /system/proxy/socks_host", "r");
319 if(fscanf(output, "%s", buf) > 0) {
320 snprintf(buf_proxy, MAXLEN, "%s", buf);
324 output = popen("gconftool-2 --get /system/proxy/socks_port", "r");
325 if(fscanf(output, "%s", buf) > 0) {
326 snprintf(socks_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
329 INFO("socks_proxy : %s\n", socks_proxy);
333 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
338 output = popen("gconftool-2 --get /system/proxy/mode", "r");
339 if(fscanf(output, "%s", buf) > 0) {
340 //priority : auto > manual > none
341 if (strcmp(buf, "auto") == 0) {
342 INFO("AUTO PROXY MODE\n");
343 get_auto_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
345 else if (strcmp(buf, "manual") == 0) {
346 INFO("MANUAL PROXY MODE\n");
347 get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
349 else if (strcmp(buf, "none") == 0) {
350 INFO("DIRECT PROXY MODE\n");