summaryrefslogtreecommitdiff
path: root/tizen/src/util/osutil-linux.c
blob: d9b3032d6be165672ebbdb0ed7c2635897bae507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * Emulator
 *
 * Copyright (C) 2012 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * MunKyu Im <munkyu.im@samsung.com>
 * GiWoong Kim <giwoong.kim@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 * HyunJun Son
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

/**
  @file     osutil-linux.c
  @brief    Collection of utilities for linux
 */

#include <png.h>
#include "osutil.h"
#include "emulator.h"
#include "emul_state.h"
#include "debug_ch.h"
#include "maru_err_table.h"
#include "sdb.h"

#ifndef CONFIG_LINUX
#error
#endif

#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <linux/version.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>

MULTI_DEBUG_CHANNEL(emulator, osutil);


static qemu_timeval tv = { 0, 0 };
static time_t ti;
static char buf_time[64];

static int g_shmid;
static char *g_shared_memory;
static int gproxytool = GSETTINGS;

extern char tizen_target_img_path[];

/* Getting proxy commands */
static const char* gproxycmds[][2] = {
    { "gconftool-2 -g /system/proxy/mode" , "gsettings get org.gnome.system.proxy mode" },
    { "gconftool-2 -g /system/proxy/autoconfig_url", "gsettings get org.gnome.system.proxy autoconfig-url" },
    { "gconftool-2 -g /system/http_proxy/host", "gsettings get org.gnome.system.proxy.http host" },
    { "gconftool-2 -g /system/http_proxy/port", "gsettings get org.gnome.system.proxy.http port"},
    { "gconftool-2 -g /system/proxy/secure_host", "gsettings get org.gnome.system.proxy.https host" },
    { "gconftool-2 -g /system/proxy/secure_port", "gsettings get org.gnome.system.proxy.https port" },
    { "gconftool-2 -g /system/proxy/ftp_host", "gsettings get org.gnome.system.proxy.ftp host" },
    { "gconftool-2 -g /system/proxy/ftp_port", "gsettings get org.gnome.system.proxy.ftp port" },
    { "gconftool-2 -g /system/proxy/socks_host", "gsettings get org.gnome.system.proxy.socks host" },
    { "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
};

void check_vm_lock_os(void)
{
    int shm_id;
    void *shm_addr;
    uint32_t port;
    int val;
    struct shmid_ds shm_info;

    for (port = 26100; port < 26200; port += 10) {
        shm_id = shmget((key_t)port, 0, 0);
        if (shm_id != -1) {
            shm_addr = shmat(shm_id, (void *)0, 0);
            if ((void *)-1 == shm_addr) {
                ERR("error occured at shmat()\n");
                break;
            }

            val = shmctl(shm_id, IPC_STAT, &shm_info);
            if (val != -1) {
                INFO("count of process that use shared memory : %d\n",
                    shm_info.shm_nattch);
                if ((shm_info.shm_nattch > 0) &&
                    g_strcmp0(tizen_target_img_path, (char *)shm_addr) == 0) {
                    if (check_port_bind_listen(port + 1) > 0) {
                        shmdt(shm_addr);
                        continue;
                    }
                    shmdt(shm_addr);
                    maru_register_exit_msg(MARU_EXIT_UNKNOWN,
                                        "Can not execute this VM.\n"
                                        "The same name is running now.");
                    exit(0);
                } else {
                    shmdt(shm_addr);
                }
            }
        }
    }
}

void make_vm_lock_os(void)
{
    int base_port;

    base_port = get_emul_vm_base_port();

    g_shmid = shmget((key_t)base_port, MAXLEN, 0666|IPC_CREAT);
    if (g_shmid == -1) {
        ERR("shmget failed\n");
        perror("osutil-linux: ");
        return;
    }

    g_shared_memory = shmat(g_shmid, (char *)0x00, 0);
    if (g_shared_memory == (void *)-1) {
        ERR("shmat failed\n");
        perror("osutil-linux: ");
        return;
    }

    g_sprintf(g_shared_memory, "%s", tizen_target_img_path);
    INFO("shared memory key: %d value: %s\n",
        base_port, (char *)g_shared_memory);

    if (shmdt(g_shared_memory) == -1) {
        ERR("shmdt failed\n");
        perror("osutil-linux: ");
    }
}

void remove_vm_lock_os(void)
{
    if (shmctl(g_shmid, IPC_RMID, 0) == -1) {
        ERR("shmctl failed\n");
        perror("osutil-linux: ");
    }
}


void set_bin_path_os(gchar * exec_argv)
{
    gchar link_path[PATH_MAX] = { 0, };
    char *file_name = NULL;

    ssize_t len = readlink("/proc/self/exe", link_path, sizeof(link_path) - 1);

    if (len < 0 || len > (sizeof(link_path) - 1)) {
        perror("set_bin_path error : ");
        return;
    }

    link_path[len] = '\0';

    file_name = g_strrstr(link_path, "/");
    g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);

    g_strlcat(bin_path, "/", PATH_MAX);
}

int get_number_of_processors(void)
{
    int num_processors = 0;

    num_processors = sysconf(_SC_NPROCESSORS_ONLN);
    if (num_processors < 1) {
        num_processors = 1;
    }
    TRACE("Number of processors : %d\n", num_processors);

    return num_processors;
}

void print_system_info_os(void)
{
    INFO("* Linux\n");

    INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);

    /* depends on building */
    INFO("* QEMU build machine linux kernel version : (%d, %d, %d)\n",
        LINUX_VERSION_CODE >> 16,
        (LINUX_VERSION_CODE >> 8) & 0xff,
        LINUX_VERSION_CODE & 0xff);

     /* depends on launching */
    struct utsname host_uname_buf;
    if (uname(&host_uname_buf) == 0) {
        INFO("* Host machine uname : %s %s %s %s %s\n",
            host_uname_buf.sysname, host_uname_buf.nodename,
            host_uname_buf.release, host_uname_buf.version,
            host_uname_buf.machine);
    }

    struct sysinfo sys_info;
    if (sysinfo(&sys_info) == 0) {
        INFO("* Total Ram : %llu kB, Free: %llu kB\n",
            sys_info.totalram * (unsigned long long)sys_info.mem_unit / 1024,
            sys_info.freeram * (unsigned long long)sys_info.mem_unit / 1024);
    }

    /* get linux distribution information */
    INFO("* Linux distribution infomation :\n");
    const gchar lsb_release_cmd[MAXLEN] = "lsb_release -d -r -c";
    gchar *buffer = NULL;
    gint buffer_size = strlen(lsb_release_cmd) + 1;

    buffer = g_malloc(buffer_size);

    g_snprintf(buffer, buffer_size, "%s", lsb_release_cmd);

    if (system(buffer) < 0) {
        INFO("system function command '%s' \
                returns error !", buffer);
    }
    g_free(buffer);

    /* pci device description */
    INFO("* Host PCI devices :\n");
    const gchar lspci_cmd[MAXLEN] = "lspci";
    buffer_size = strlen(lspci_cmd) + 1;

    buffer = g_malloc(buffer_size);

    g_snprintf(buffer, buffer_size, "%s", lspci_cmd);

    fflush(stdout);
    if (system(buffer) < 0) {
        INFO("system function command '%s' \
                returns error !", buffer);
    }
    g_free(buffer);
}

char *get_timeofday(void)
{
    qemu_gettimeofday(&tv);
    ti = tv.tv_sec;

    struct tm tm;
    localtime_r(&ti, &tm);
    strftime(buf_time, sizeof(buf_time),
             "%H:%M:%S", &tm);

    return buf_time;
}

static void process_string(char *buf)
{
    char tmp_buf[MAXLEN];

    /* remove single quotes of strings gotten by gsettings */
    if (gproxytool == GSETTINGS) {
        remove_string(buf, tmp_buf, "\'");
        memset(buf, 0, MAXLEN);
        strncpy(buf, tmp_buf, strlen(tmp_buf)-1);
    }
}

static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
{
    char type[MAXLEN];
    char proxy[MAXLEN];
    char line[MAXLEN];
    FILE *fp_pacfile;
    char *p = NULL;
    FILE *output;
    char buf[MAXLEN];

    output = popen(gproxycmds[GNOME_PROXY_AUTOCONFIG_URL][gproxytool], "r");
    if (fscanf(output, "%s", buf) > 0) {
        process_string(buf);
        INFO("pac address: %s\n", buf);
        download_url(buf);
    }

    pclose(output);
    fp_pacfile = fopen(pac_tempfile, "r");
    if (fp_pacfile != NULL) {
        while (fgets(line, MAXLEN, fp_pacfile) != NULL) {
            if ((strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
                INFO("line found %s", line);
                sscanf(line, "%*[^\"]\"%s %s", type, proxy);
            }
        }

        if (g_str_has_prefix(type, DIRECT)) {
            INFO("auto proxy is set to direct mode\n");
            fclose(fp_pacfile);
        } else if (g_str_has_prefix(type, PROXY)) {
            INFO("auto proxy is set to proxy mode\n");
            INFO("type: %s, proxy: %s\n", type, proxy);

            p = strtok(proxy, "\";");
            if (p != NULL) {
                INFO("auto proxy to set: %s\n",p);
                strcpy(http_proxy, p);
                strcpy(https_proxy, p);
                strcpy(ftp_proxy, p);
                strcpy(socks_proxy, p);
            }
            fclose(fp_pacfile);
        } else {
            ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
            fclose(fp_pacfile);
        }
    } else {
        ERR("fail to get pacfile fp\n");
        return -1;
    }

    if (remove(pac_tempfile) < 0) {
        WARN("fail to remove the temporary pacfile\n");
    }

    return 0;
}

static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
{
    char buf[MAXLEN] = {0,};
    char buf_port[MAXPORTLEN] = {0,};
    char buf_proxy[MAXLEN] = {0,};
    char *buf_proxy_bak;
    char *proxy;
    FILE *output;
    int MAXPROXYLEN = MAXLEN + MAXPORTLEN;

    output = popen(gproxycmds[GNOME_PROXY_HTTP_HOST][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        process_string(buf);
        snprintf(buf_proxy, MAXLEN, "%s", buf);
    }
    pclose(output);

    output = popen(gproxycmds[GNOME_PROXY_HTTP_PORT][gproxytool], "r");
    if(fscanf(output, "%s", buf_port) <= 0) {
        //for abnormal case: if can't find the key of http port, get from environment value.
        buf_proxy_bak = getenv("http_proxy");
        INFO("http_proxy from env: %s\n", buf_proxy_bak);
        if(buf_proxy_bak != NULL) {
            proxy = malloc(MAXLEN);
            remove_string(buf_proxy_bak, proxy, HTTP_PREFIX);
            strncpy(http_proxy, proxy, strlen(proxy)-1);
            INFO("final http_proxy value: %s\n", http_proxy);
            free(proxy);
        }
        else {
            INFO("http_proxy is not set on env.\n");
            pclose(output);
            return;
        }

    }
    else {
        snprintf(http_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf_port);
        memset(buf_proxy, 0, MAXLEN);
        INFO("http_proxy: %s\n", http_proxy);
    }
    pclose(output);

    memset(buf, 0, MAXLEN);

    output = popen(gproxycmds[GNOME_PROXY_HTTPS_HOST][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        process_string(buf);
        snprintf(buf_proxy, MAXLEN, "%s", buf);
    }
    pclose(output);

    output = popen(gproxycmds[GNOME_PROXY_HTTPS_PORT][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        snprintf(https_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
    }
    pclose(output);
    memset(buf, 0, MAXLEN);
    memset(buf_proxy, 0, MAXLEN);
    INFO("https_proxy : %s\n", https_proxy);

    output = popen(gproxycmds[GNOME_PROXY_FTP_HOST][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        process_string(buf);
        snprintf(buf_proxy, MAXLEN, "%s", buf);
    }
    pclose(output);

    output = popen(gproxycmds[GNOME_PROXY_FTP_PORT][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        snprintf(ftp_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
    }
    pclose(output);
    memset(buf, 0, MAXLEN);
    memset(buf_proxy, 0, MAXLEN);
    INFO("ftp_proxy : %s\n", ftp_proxy);

    output = popen(gproxycmds[GNOME_PROXY_SOCKS_HOST][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        process_string(buf);
        snprintf(buf_proxy, MAXLEN, "%s", buf);
    }
    pclose(output);

    output = popen(gproxycmds[GNOME_PROXY_SOCKS_PORT][gproxytool], "r");
    if(fscanf(output, "%s", buf) > 0) {
        snprintf(socks_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
    }
    pclose(output);
    INFO("socks_proxy : %s\n", socks_proxy);
}


void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
{
    char buf[MAXLEN];
    FILE *output;
    int ret;

    output = popen(gproxycmds[GNOME_PROXY_MODE][gproxytool], "r");
    ret = fscanf(output, "%s", buf);
    if (ret <= 0) {
        pclose(output);
        INFO("Try to use gsettings to get proxy\n");
        gproxytool = GSETTINGS;
        output = popen(gproxycmds[GNOME_PROXY_MODE][gproxytool], "r");
        ret = fscanf(output, "%s", buf);
    }
    if (ret > 0) {
        process_string(buf);
        //priority : auto > manual > none
        if (strcmp(buf, "auto") == 0) {
            INFO("AUTO PROXY MODE\n");
            get_auto_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
        }
        else if (strcmp(buf, "manual") == 0) {
            INFO("MANUAL PROXY MODE\n");
            get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
        }
        else if (strcmp(buf, "none") == 0) {
            INFO("DIRECT PROXY MODE\n");
        }
    }
    pclose(output);
}