update source for tizen_2.1
[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 "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_LINUX
45 #error
46 #endif
47
48 #include <string.h>
49 #include <unistd.h>
50 #include <sys/shm.h>
51 #include <sys/ipc.h>
52 #include <linux/version.h>
53 #include <sys/utsname.h>
54 #include <sys/sysinfo.h>
55
56 MULTI_DEBUG_CHANNEL(emulator, osutil);
57
58 extern char tizen_target_img_path[];
59 extern int tizen_base_port;
60 int g_shmid;
61 char *g_shared_memory;
62
63 void check_vm_lock_os(void)
64 {
65     int shm_id;
66     void *shm_addr;
67     uint32_t port;
68     int val;
69     struct shmid_ds shm_info;
70
71     for (port = 26100; port < 26200; port += 10) {
72         shm_id = shmget((key_t)port, 0, 0);
73         if (shm_id != -1) {
74             shm_addr = shmat(shm_id, (void *)0, 0);
75             if ((void *)-1 == shm_addr) {
76                 ERR("error occured at shmat()\n");
77                 break;
78             }
79
80             val = shmctl(shm_id, IPC_STAT, &shm_info);
81             if (val != -1) {
82                 INFO("count of process that use shared memory : %d\n",
83                     shm_info.shm_nattch);
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) {
87                         shmdt(shm_addr);
88                         continue;
89                     }
90                     shmdt(shm_addr);
91                     maru_register_exit_msg(MARU_EXIT_UNKNOWN,
92                                         "Can not execute this VM.\n"
93                                         "The same name is running now.");
94                     exit(0);
95                 } else {
96                     shmdt(shm_addr);
97                 }
98             }
99         }
100     }
101 }
102
103 void make_vm_lock_os(void)
104 {
105
106     g_shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
107     if (g_shmid == -1) {
108         ERR("shmget failed\n");
109         return;
110     }
111
112     g_shared_memory = shmat(g_shmid, (char *)0x00, 0);
113     if (g_shared_memory == (void *)-1) {
114         ERR("shmat failed\n");
115         return;
116     }
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);
120 }
121
122 void set_bin_path_os(gchar * exec_argv)
123 {
124     gchar link_path[PATH_MAX] = { 0, };
125     char *file_name = NULL;
126
127     ssize_t len = readlink("/proc/self/exe", link_path, sizeof(link_path) - 1);
128
129     if (len < 0 || len > sizeof(link_path)) {
130         perror("set_bin_path error : ");
131         return;
132     }
133
134     link_path[len] = '\0';
135
136     file_name = g_strrstr(link_path, "/");
137     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
138
139     g_strlcat(bin_path, "/", PATH_MAX);
140 }
141
142 void print_system_info_os(void)
143 {
144     INFO("* Linux\n");
145
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);
151
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);
159     }
160
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);
166     }
167
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);
175     }
176
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);
184     }
185 }
186
187 static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
188 {
189     char type[MAXLEN];
190     char proxy[MAXLEN];
191     char line[MAXLEN];
192     FILE *fp_pacfile;
193     char *p = NULL;
194     FILE *output;
195     char buf[MAXLEN];
196
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);
200         download_url(buf);
201     }
202     pclose(output);
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);
209             }
210         }
211
212         if(g_str_has_prefix(type, DIRECT)) {
213             INFO("auto proxy is set to direct mode\n");
214             fclose(fp_pacfile);
215         }
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, "\";");
220             if(p != NULL) {
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);
226             }
227             fclose(fp_pacfile);
228         }
229         else
230         {
231             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
232             fclose(fp_pacfile);
233         }
234     } 
235     else {
236         ERR("fail to get pacfile fp\n");
237         return -1;
238     }
239
240     remove(pac_tempfile);
241     return 0;
242 }
243
244 static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
245 {
246     char buf[MAXLEN] = {0,};
247     char buf_port[MAXPORTLEN] = {0,};
248     char buf_proxy[MAXLEN] = {0,};
249     char *buf_proxy_bak;
250     char *proxy;
251     FILE *output;
252     int MAXPROXYLEN = MAXLEN + MAXPORTLEN;
253
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);
257     }
258     pclose(output);
259     
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);
270             free(proxy);
271         }
272         else {
273             INFO("http_proxy is not set on env.\n");
274             pclose(output);
275             return;
276         }
277
278     }
279     else {
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);
283     }
284     pclose(output);
285
286     memset(buf, 0, MAXLEN);
287
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);
291     }
292     pclose(output);
293
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);
297     }
298     pclose(output);
299     memset(buf, 0, MAXLEN);
300     memset(buf_proxy, 0, MAXLEN);
301     INFO("https_proxy : %s\n", https_proxy);
302
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);
306     }
307     pclose(output);
308
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);
312     }
313     pclose(output);
314     memset(buf, 0, MAXLEN);
315     memset(buf_proxy, 0, MAXLEN);
316     INFO("ftp_proxy : %s\n", ftp_proxy);
317
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);
321     }
322     pclose(output);
323
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);
327     }
328     pclose(output);
329     INFO("socks_proxy : %s\n", socks_proxy);
330 }
331
332
333 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
334 {
335     char buf[MAXLEN];
336     FILE *output;
337
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);
344         }
345         else if (strcmp(buf, "manual") == 0) {
346             INFO("MANUAL PROXY MODE\n");
347             get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
348         }
349         else if (strcmp(buf, "none") == 0) {
350             INFO("DIRECT PROXY MODE\n");
351         }
352     }
353     pclose(output);
354 }