skin: modified qHD skin name
[sdk/emulator/qemu.git] / tizen / src / osutil-linux.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-linux.c
34   @brief    Collection of utilities for linux
35  */
36
37 #include <png.h>
38 #include "maru_common.h"
39 #include "osutil.h"
40 #include "emulator.h"
41 #include "debug_ch.h"
42 #include "maru_err_table.h"
43 #include "sdb.h"
44
45 #ifndef CONFIG_LINUX
46 #error
47 #endif
48
49 #include <string.h>
50 #include <unistd.h>
51 #include <sys/shm.h>
52 #include <sys/ipc.h>
53 #include <linux/version.h>
54 #include <sys/utsname.h>
55 #include <sys/sysinfo.h>
56
57 MULTI_DEBUG_CHANNEL(emulator, osutil);
58
59 extern char tizen_target_img_path[];
60 extern int tizen_base_port;
61 int g_shmid;
62 char *g_shared_memory;
63 int gproxytool = GCONFTOOL;
64
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" },
77 };
78
79 void check_vm_lock_os(void)
80 {
81     int shm_id;
82     void *shm_addr;
83     uint32_t port;
84     int val;
85     struct shmid_ds shm_info;
86
87     for (port = 26100; port < 26200; port += 10) {
88         shm_id = shmget((key_t)port, 0, 0);
89         if (shm_id != -1) {
90             shm_addr = shmat(shm_id, (void *)0, 0);
91             if ((void *)-1 == shm_addr) {
92                 ERR("error occured at shmat()\n");
93                 break;
94             }
95
96             val = shmctl(shm_id, IPC_STAT, &shm_info);
97             if (val != -1) {
98                 INFO("count of process that use shared memory : %d\n",
99                     shm_info.shm_nattch);
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) {
103                         shmdt(shm_addr);
104                         continue;
105                     }
106                     shmdt(shm_addr);
107                     maru_register_exit_msg(MARU_EXIT_UNKNOWN,
108                                         "Can not execute this VM.\n"
109                                         "The same name is running now.");
110                     exit(0);
111                 } else {
112                     shmdt(shm_addr);
113                 }
114             }
115         }
116     }
117 }
118
119 void make_vm_lock_os(void)
120 {
121     g_shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
122     if (g_shmid == -1) {
123         ERR("shmget failed\n");
124         perror("osutil-linux: ");
125         return;
126     }
127
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: ");
132         return;
133     }
134
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);
138
139     if (shmdt(g_shared_memory) == -1) {
140         ERR("shmdt failed\n");
141         perror("osutil-linux: ");
142     }
143 }
144
145 void set_bin_path_os(gchar * exec_argv)
146 {
147     gchar link_path[PATH_MAX] = { 0, };
148     char *file_name = NULL;
149
150     ssize_t len = readlink("/proc/self/exe", link_path, sizeof(link_path) - 1);
151
152     if (len < 0 || len > sizeof(link_path)) {
153         perror("set_bin_path error : ");
154         return;
155     }
156
157     link_path[len] = '\0';
158
159     file_name = g_strrstr(link_path, "/");
160     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
161
162     g_strlcat(bin_path, "/", PATH_MAX);
163 }
164
165 void print_system_info_os(void)
166 {
167     INFO("* Linux\n");
168
169     INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
170
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);
176
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);
184     }
185
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);
191     }
192
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);
200     }
201
202     /* pci device description */
203     INFO("* Host PCI devices :\n");
204     char lspci_cmd[MAXLEN] = "lspci >> ";
205     strcat(lspci_cmd, log_path);
206
207     fflush(stdout);
208     if(system(lspci_cmd) < 0) {
209         INFO("system function command '%s' \
210             returns error !", lspci_cmd);
211     }
212 }
213
214 static void process_string(char *buf)
215 {
216     char tmp_buf[MAXLEN];
217
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);
223     }
224 }
225
226 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
227 {
228     char type[MAXLEN];
229     char proxy[MAXLEN];
230     char line[MAXLEN];
231     FILE *fp_pacfile;
232     char *p = NULL;
233     FILE *output;
234     char buf[MAXLEN];
235
236     output = popen(gproxycmds[GNOME_PROXY_AUTOCONFIG_URL][gproxytool], "r");
237     if(fscanf(output, "%s", buf) > 0) {
238         process_string(buf);
239         INFO("pac address: %s\n", buf);
240         download_url(buf);
241     }
242     pclose(output);
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);
249             }
250         }
251
252         if(g_str_has_prefix(type, DIRECT)) {
253             INFO("auto proxy is set to direct mode\n");
254             fclose(fp_pacfile);
255         }
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, "\";");
260             if(p != NULL) {
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);
266             }
267             fclose(fp_pacfile);
268         }
269         else
270         {
271             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
272             fclose(fp_pacfile);
273         }
274     } 
275     else {
276         ERR("fail to get pacfile fp\n");
277         return -1;
278     }
279
280     remove(pac_tempfile);
281     return 0;
282 }
283
284 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
285 {
286     char buf[MAXLEN] = {0,};
287     char buf_port[MAXPORTLEN] = {0,};
288     char buf_proxy[MAXLEN] = {0,};
289     char *buf_proxy_bak;
290     char *proxy;
291     FILE *output;
292     int MAXPROXYLEN = MAXLEN + MAXPORTLEN;
293
294     output = popen(gproxycmds[GNOME_PROXY_HTTP_HOST][gproxytool], "r");
295     if(fscanf(output, "%s", buf) > 0) {
296         process_string(buf);
297         snprintf(buf_proxy, MAXLEN, "%s", buf);
298     }
299     pclose(output);
300     
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);
311             free(proxy);
312         }
313         else {
314             INFO("http_proxy is not set on env.\n");
315             pclose(output);
316             return;
317         }
318
319     }
320     else {
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);
324     }
325     pclose(output);
326
327     memset(buf, 0, MAXLEN);
328
329     output = popen(gproxycmds[GNOME_PROXY_HTTPS_HOST][gproxytool], "r");
330     if(fscanf(output, "%s", buf) > 0) {
331         process_string(buf);
332         snprintf(buf_proxy, MAXLEN, "%s", buf);
333     }
334     pclose(output);
335
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);
339     }
340     pclose(output);
341     memset(buf, 0, MAXLEN);
342     memset(buf_proxy, 0, MAXLEN);
343     INFO("https_proxy : %s\n", https_proxy);
344
345     output = popen(gproxycmds[GNOME_PROXY_FTP_HOST][gproxytool], "r");
346     if(fscanf(output, "%s", buf) > 0) {
347         process_string(buf);
348         snprintf(buf_proxy, MAXLEN, "%s", buf);
349     }
350     pclose(output);
351
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);
355     }
356     pclose(output);
357     memset(buf, 0, MAXLEN);
358     memset(buf_proxy, 0, MAXLEN);
359     INFO("ftp_proxy : %s\n", ftp_proxy);
360
361     output = popen(gproxycmds[GNOME_PROXY_SOCKS_HOST][gproxytool], "r");
362     if(fscanf(output, "%s", buf) > 0) {
363         process_string(buf);
364         snprintf(buf_proxy, MAXLEN, "%s", buf);
365     }
366     pclose(output);
367
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);
371     }
372     pclose(output);
373     INFO("socks_proxy : %s\n", socks_proxy);
374 }
375
376
377 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
378 {
379     char buf[MAXLEN];
380     FILE *output;
381     int ret;
382
383     output = popen(gproxycmds[GNOME_PROXY_MODE][gproxytool], "r");
384     ret = fscanf(output, "%s", buf);
385     if (ret <= 0) {
386         pclose(output);
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);
391     }
392     if (ret > 0) {
393         process_string(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);
398         }
399         else if (strcmp(buf, "manual") == 0) {
400             INFO("MANUAL PROXY MODE\n");
401             get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
402         }
403         else if (strcmp(buf, "none") == 0) {
404             INFO("DIRECT PROXY MODE\n");
405         }
406     }
407     pclose(output);
408 }