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
38 #include "maru_common.h"
42 #include "maru_err_table.h"
53 #include <linux/version.h>
54 #include <sys/utsname.h>
55 #include <sys/sysinfo.h>
57 MULTI_DEBUG_CHANNEL(emulator, osutil);
59 extern char tizen_target_img_path[];
60 extern int tizen_base_port;
62 char *g_shared_memory;
63 int gproxytool = GCONFTOOL;
65 /* Getting proxy commands */
66 static const char* gproxycmds[][2] = {
67 { "gconftool-2 -g /system/proxy/mode" , "gsettings get org.gnome.system.proxy mode" },
68 { "gconftool-2 -g /system/proxy/autoconfig_url", "gsettings get org.gnome.system.proxy autoconfig-url" },
69 { "gconftool-2 -g /system/http_proxy/host", "gsettings get org.gnome.system.proxy.http host" },
70 { "gconftool-2 -g /system/http_proxy/port", "gsettings get org.gnome.system.proxy.http port"},
71 { "gconftool-2 -g /system/proxy/secure_host", "gsettings get org.gnome.system.proxy.https host" },
72 { "gconftool-2 -g /system/proxy/secure_port", "gsettings get org.gnome.system.proxy.https port" },
73 { "gconftool-2 -g /system/proxy/ftp_host", "gsettings get org.gnome.system.proxy.ftp host" },
74 { "gconftool-2 -g /system/proxy/ftp_port", "gsettings get org.gnome.system.proxy.ftp port" },
75 { "gconftool-2 -g /system/proxy/socks_host", "gsettings get org.gnome.system.proxy.socks host" },
76 { "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
79 void check_vm_lock_os(void)
85 struct shmid_ds shm_info;
87 for (port = 26100; port < 26200; port += 10) {
88 shm_id = shmget((key_t)port, 0, 0);
90 shm_addr = shmat(shm_id, (void *)0, 0);
91 if ((void *)-1 == shm_addr) {
92 ERR("error occured at shmat()\n");
96 val = shmctl(shm_id, IPC_STAT, &shm_info);
98 INFO("count of process that use shared memory : %d\n",
100 if ((shm_info.shm_nattch > 0) &&
101 g_strcmp0(tizen_target_img_path, (char *)shm_addr) == 0) {
102 if (check_port_bind_listen(port + 1) > 0) {
107 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
108 "Can not execute this VM.\n"
109 "The same name is running now.");
119 void make_vm_lock_os(void)
121 g_shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
123 ERR("shmget failed\n");
124 perror("osutil-linux: ");
128 g_shared_memory = shmat(g_shmid, (char *)0x00, 0);
129 if (g_shared_memory == (void *)-1) {
130 ERR("shmat failed\n");
131 perror("osutil-linux: ");
135 g_sprintf(g_shared_memory, "%s", tizen_target_img_path);
136 INFO("shared memory key: %d value: %s\n",
137 tizen_base_port, (char *)g_shared_memory);
139 if (shmdt(g_shared_memory) == -1) {
140 ERR("shmdt failed\n");
141 perror("osutil-linux: ");
145 void set_bin_path_os(gchar * exec_argv)
147 gchar link_path[PATH_MAX] = { 0, };
148 char *file_name = NULL;
150 ssize_t len = readlink("/proc/self/exe", link_path, sizeof(link_path) - 1);
152 if (len < 0 || len > sizeof(link_path)) {
153 perror("set_bin_path error : ");
157 link_path[len] = '\0';
159 file_name = g_strrstr(link_path, "/");
160 g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
162 g_strlcat(bin_path, "/", PATH_MAX);
165 void print_system_info_os(void)
169 INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
171 /* depends on building */
172 INFO("* QEMU build machine linux kernel version : (%d, %d, %d)\n",
173 LINUX_VERSION_CODE >> 16,
174 (LINUX_VERSION_CODE >> 8) & 0xff,
175 LINUX_VERSION_CODE & 0xff);
177 /* depends on launching */
178 struct utsname host_uname_buf;
179 if (uname(&host_uname_buf) == 0) {
180 INFO("* Host machine uname : %s %s %s %s %s\n",
181 host_uname_buf.sysname, host_uname_buf.nodename,
182 host_uname_buf.release, host_uname_buf.version,
183 host_uname_buf.machine);
186 struct sysinfo sys_info;
187 if (sysinfo(&sys_info) == 0) {
188 INFO("* Total Ram : %llu kB, Free: %llu kB\n",
189 sys_info.totalram * (unsigned long long)sys_info.mem_unit / 1024,
190 sys_info.freeram * (unsigned long long)sys_info.mem_unit / 1024);
193 /* get linux distribution information */
194 INFO("* Linux distribution infomation :\n");
195 char lsb_release_cmd[MAXLEN] = "lsb_release -d -r -c >> ";
196 strcat(lsb_release_cmd, log_path);
197 if(system(lsb_release_cmd) < 0) {
198 INFO("system function command '%s' \
199 returns error !", lsb_release_cmd);
202 /* pci device description */
203 INFO("* Host PCI devices :\n");
204 char lspci_cmd[MAXLEN] = "lspci >> ";
205 strcat(lspci_cmd, log_path);
208 if(system(lspci_cmd) < 0) {
209 INFO("system function command '%s' \
210 returns error !", lspci_cmd);
214 static void process_string(char *buf)
216 char tmp_buf[MAXLEN];
218 /* remove single quotes of strings gotten by gsettings */
219 if (gproxytool == GSETTINGS) {
220 remove_string(buf, tmp_buf, "\'");
221 memset(buf, 0, MAXLEN);
222 strncpy(buf, tmp_buf, strlen(tmp_buf)-1);
226 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
236 output = popen(gproxycmds[GNOME_PROXY_AUTOCONFIG_URL][gproxytool], "r");
237 if(fscanf(output, "%s", buf) > 0) {
239 INFO("pac address: %s\n", buf);
243 fp_pacfile = fopen(pac_tempfile, "r");
244 if(fp_pacfile != NULL) {
245 while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
246 if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
247 INFO("line found %s", line);
248 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
252 if(g_str_has_prefix(type, DIRECT)) {
253 INFO("auto proxy is set to direct mode\n");
256 else if(g_str_has_prefix(type, PROXY)) {
257 INFO("auto proxy is set to proxy mode\n");
258 INFO("type: %s, proxy: %s\n", type, proxy);
259 p = strtok(proxy, "\";");
261 INFO("auto proxy to set: %s\n",p);
262 strcpy(http_proxy, p);
263 strcpy(https_proxy, p);
264 strcpy(ftp_proxy, p);
265 strcpy(socks_proxy, p);
271 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
276 ERR("fail to get pacfile fp\n");
280 remove(pac_tempfile);
284 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
286 char buf[MAXLEN] = {0,};
287 char buf_port[MAXPORTLEN] = {0,};
288 char buf_proxy[MAXLEN] = {0,};
292 int MAXPROXYLEN = MAXLEN + MAXPORTLEN;
294 output = popen(gproxycmds[GNOME_PROXY_HTTP_HOST][gproxytool], "r");
295 if(fscanf(output, "%s", buf) > 0) {
297 snprintf(buf_proxy, MAXLEN, "%s", buf);
301 output = popen(gproxycmds[GNOME_PROXY_HTTP_PORT][gproxytool], "r");
302 if(fscanf(output, "%s", buf_port) <= 0) {
303 //for abnormal case: if can't find the key of http port, get from environment value.
304 buf_proxy_bak = getenv("http_proxy");
305 INFO("http_proxy from env: %s\n", buf_proxy_bak);
306 if(buf_proxy_bak != NULL) {
307 proxy = malloc(MAXLEN);
308 remove_string(buf_proxy_bak, proxy, HTTP_PREFIX);
309 strncpy(http_proxy, proxy, strlen(proxy)-1);
310 INFO("final http_proxy value: %s\n", http_proxy);
314 INFO("http_proxy is not set on env.\n");
321 snprintf(http_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf_port);
322 memset(buf_proxy, 0, MAXLEN);
323 INFO("http_proxy: %s\n", http_proxy);
327 memset(buf, 0, MAXLEN);
329 output = popen(gproxycmds[GNOME_PROXY_HTTPS_HOST][gproxytool], "r");
330 if(fscanf(output, "%s", buf) > 0) {
332 snprintf(buf_proxy, MAXLEN, "%s", buf);
336 output = popen(gproxycmds[GNOME_PROXY_HTTPS_PORT][gproxytool], "r");
337 if(fscanf(output, "%s", buf) > 0) {
338 snprintf(https_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
341 memset(buf, 0, MAXLEN);
342 memset(buf_proxy, 0, MAXLEN);
343 INFO("https_proxy : %s\n", https_proxy);
345 output = popen(gproxycmds[GNOME_PROXY_FTP_HOST][gproxytool], "r");
346 if(fscanf(output, "%s", buf) > 0) {
348 snprintf(buf_proxy, MAXLEN, "%s", buf);
352 output = popen(gproxycmds[GNOME_PROXY_FTP_PORT][gproxytool], "r");
353 if(fscanf(output, "%s", buf) > 0) {
354 snprintf(ftp_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
357 memset(buf, 0, MAXLEN);
358 memset(buf_proxy, 0, MAXLEN);
359 INFO("ftp_proxy : %s\n", ftp_proxy);
361 output = popen(gproxycmds[GNOME_PROXY_SOCKS_HOST][gproxytool], "r");
362 if(fscanf(output, "%s", buf) > 0) {
364 snprintf(buf_proxy, MAXLEN, "%s", buf);
368 output = popen(gproxycmds[GNOME_PROXY_SOCKS_PORT][gproxytool], "r");
369 if(fscanf(output, "%s", buf) > 0) {
370 snprintf(socks_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
373 INFO("socks_proxy : %s\n", socks_proxy);
377 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
383 output = popen(gproxycmds[GNOME_PROXY_MODE][gproxytool], "r");
384 ret = fscanf(output, "%s", buf);
387 INFO("Try to use gsettings to get proxy\n");
388 gproxytool = GSETTINGS;
389 output = popen(gproxycmds[GNOME_PROXY_MODE][gproxytool], "r");
390 ret = fscanf(output, "%s", buf);
394 //priority : auto > manual > none
395 if (strcmp(buf, "auto") == 0) {
396 INFO("AUTO PROXY MODE\n");
397 get_auto_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
399 else if (strcmp(buf, "manual") == 0) {
400 INFO("MANUAL PROXY MODE\n");
401 get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
403 else if (strcmp(buf, "none") == 0) {
404 INFO("DIRECT PROXY MODE\n");