summaryrefslogtreecommitdiff
path: root/tizen/src/util/osutil-linux.c
blob: d0cd9cedfbb8f3f49760fa8f630d5d555dfa8c8b (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
/*
 * 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 <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>

#include "qemu/sockets.h"
#include "qemu/error-report.h"

#include "osutil.h"
#include "emulator.h"
#include "emul_state.h"
#include "net_helper.h"
#include "emulator_options.h"

#ifndef CONFIG_LINUX
#error
#endif

#include "debug_ch.h"
MULTI_DEBUG_CHANNEL(emulator, osutil);

static int g_shmid;
static char *g_shared_memory;

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(get_drive_image_file(), (char *)shm_addr) == 0) {
                    int ret = 0;
                    if ((ret = check_port_bind_listen(port + 1)) > 0) {
                        shmdt(shm_addr);
                        closesocket(ret);
                        continue;
                    }
                    shmdt(shm_addr);
                    // FIXME: VM lock logic should be refactored ASAP.
#if 0
                    error_report("Can not execute this VM. "
                            "The same name is running now.");
                    exit(1);
#endif
                } else {
                    shmdt(shm_addr);
                }
            }
        }
    }
}

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

static void notify_remove_lock(Notifier *notifier, void *data)
{
    remove_vm_lock_os();
}

static Notifier remove_lock = { .notify = notify_remove_lock };

void make_vm_lock_os(void)
{
    int base_port;

    check_vm_lock_os();

    base_port = get_vm_base_port();

    g_shmid = shmget((key_t)base_port, getpagesize(), 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", get_drive_image_file());
    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: ");
    }

    emulator_add_exit_notifier(&remove_lock);
}

void set_bin_path_os(char const *const exec_argv)
{
    char link_path[PATH_MAX] = { 0, };
    char bin_path_os[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_os, link_path, strlen(link_path) - strlen(file_name) + 1);

    g_strlcat(bin_path_os, "/", PATH_MAX);

    bin_path = g_strdup(bin_path_os);
}

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");
    char const *const lsb_release_cmd = "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");
    char const *const lspci_cmd = "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);
}

bool make_sdcard_lock_os(char *sdcard)
{
    return make_sdcard_lock_posix(sdcard);
}


int remove_sdcard_lock_os(char *sdcard)
{
    return remove_sdcard_lock_posix(sdcard);
}