qemu.c 9.34 KB
Newer Older
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* this file contains various functions used by all libhardware modules
 * that support QEMU emulation
 */
#include "qemu.h"
#define  LOG_TAG  "hardware-qemu"
#include <cutils/log.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdarg.h>

#define  QEMU_DEBUG  0

#if QEMU_DEBUG
#  define  D(...)   LOGD(__VA_ARGS__)
#else
#  define  D(...)   ((void)0)
#endif

39
#include "hardware/qemu_pipe.h"
40 41 42 43 44 45 46 47 48 49 50 51 52 53

int
qemu_check(void)
{
    static int  in_qemu = -1;

    if (__builtin_expect(in_qemu < 0,0)) {
        char  propBuf[PROPERTY_VALUE_MAX];
        property_get("ro.kernel.qemu", propBuf, "");
        in_qemu = (propBuf[0] == '1');
    }
    return in_qemu;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static int
qemu_fd_write( int  fd, const char*  cmd, int  len )
{
    int  len2;
    do {
        len2 = write(fd, cmd, len);
    } while (len2 < 0 && errno == EINTR);
    return len2;
}

static int
qemu_fd_read( int  fd, char*  buff, int  len )
{
    int  len2;
    do {
        len2 = read(fd, buff, len);
    } while (len2 < 0 && errno == EINTR);
    return len2;
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static int
qemu_channel_open_qemud_pipe( QemuChannel*  channel,
                              const char*   name )
{
    int   fd;
    char  pipe_name[512];

    snprintf(pipe_name, sizeof(pipe_name), "qemud:%s", name);
    fd = qemu_pipe_open(pipe_name);
    if (fd < 0) {
        D("no qemud pipe: %s", strerror(errno));
        return -1;
    }

    channel->is_qemud = 1;
    channel->fd       = fd;
    return 0;
}
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

static int
qemu_channel_open_qemud( QemuChannel*  channel,
                         const char*   name )
{
    int   fd, ret, namelen = strlen(name);
    char  answer[2];

    fd = socket_local_client( "qemud",
                              ANDROID_SOCKET_NAMESPACE_RESERVED,
                              SOCK_STREAM );
    if (fd < 0) {
        D("no qemud control socket: %s", strerror(errno));
        return -1;
    }

    /* send service name to connect */
    if (qemu_fd_write(fd, name, namelen) != namelen) {
        D("can't send service name to qemud: %s",
           strerror(errno));
        close(fd);
        return -1;
    }

    /* read answer from daemon */
    if (qemu_fd_read(fd, answer, 2) != 2 ||
        answer[0] != 'O' || answer[1] != 'K') {
        D("cant' connect to %s service through qemud", name);
        close(fd);
        return -1;
    }

    channel->is_qemud = 1;
    channel->fd       = fd;
    return 0;
}


static int
qemu_channel_open_qemud_old( QemuChannel*  channel,
                             const char*   name )
{
    int  fd;

    snprintf(channel->device, sizeof channel->device,
                "qemud_%s", name);

    fd = socket_local_client( channel->device,
                              ANDROID_SOCKET_NAMESPACE_RESERVED,
                              SOCK_STREAM );
    if (fd < 0) {
        D("no '%s' control socket available: %s",
            channel->device, strerror(errno));
        return -1;
    }

    close(fd);
    channel->is_qemud_old = 1;
    return 0;
}


static int
qemu_channel_open_tty( QemuChannel*  channel,
                       const char*   name,
                       int           mode )
{
    char   key[PROPERTY_KEY_MAX];
    char   prop[PROPERTY_VALUE_MAX];
    int    ret;

    ret = snprintf(key, sizeof key, "ro.kernel.android.%s", name);
    if (ret >= (int)sizeof key)
        return -1;

    if (property_get(key, prop, "") == 0) {
        D("no kernel-provided %s device name", name);
        return -1;
    }

    ret = snprintf(channel->device, sizeof channel->device,
                    "/dev/%s", prop);
    if (ret >= (int)sizeof channel->device) {
        D("%s device name too long: '%s'", name, prop);
        return -1;
    }

    channel->is_tty = !memcmp("/dev/tty", channel->device, 8);
    return 0;
}
182 183 184 185 186 187 188 189 190 191 192

int
qemu_channel_open( QemuChannel*  channel,
                   const char*   name,
                   int           mode )
{
    int  fd = -1;

    /* initialize the channel is needed */
    if (!channel->is_inited)
    {
193
        channel->is_inited = 1;
194 195

        do {
196 197 198
            if (qemu_channel_open_qemud_pipe(channel, name) == 0)
                break;

199
            if (qemu_channel_open_qemud(channel, name) == 0)
200 201
                break;

202
            if (qemu_channel_open_qemud_old(channel, name) == 0)
203 204
                break;

205
            if (qemu_channel_open_tty(channel, name, mode) == 0)
206 207
                break;

208 209
            channel->is_available = 0;
            return -1;
210 211
        } while (0);

212
        channel->is_available = 1;
213 214 215 216 217
    }

    /* try to open the file */
    if (!channel->is_available) {
        errno = ENOENT;
218 219 220 221 222 223 224 225
        return -1;
    }

    if (channel->is_qemud) {
        return dup(channel->fd);
    }

    if (channel->is_qemud_old) {
226 227 228 229 230
        do {
            fd = socket_local_client( channel->device,
                                      ANDROID_SOCKET_NAMESPACE_RESERVED,
                                      SOCK_STREAM );
        } while (fd < 0 && errno == EINTR);
231 232 233
    }
    else /* /dev/ttySn ? */
    {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        do {
            fd = open(channel->device, mode);
        } while (fd < 0 && errno == EINTR);

        /* disable ECHO on serial lines */
        if (fd >= 0 && channel->is_tty) {
            struct termios  ios;
            tcgetattr( fd, &ios );
            ios.c_lflag = 0;  /* disable ECHO, ICANON, etc... */
            tcsetattr( fd, TCSANOW, &ios );
        }
    }
    return fd;
}


static int
251
qemu_command_vformat( char*        buffer,
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
                      int          buffer_size,
                      const char*  format,
                      va_list      args )
{
    char     header[5];
    int      len;

    if (buffer_size < 6)
        return -1;

    len = vsnprintf(buffer+4, buffer_size-4, format, args);
    if (len >= buffer_size-4)
        return -1;

    snprintf(header, sizeof header, "%04x", len);
    memcpy(buffer, header, 4);
    return len + 4;
}

extern int
272
qemu_command_format( char*        buffer,
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                     int          buffer_size,
                     const char*  format,
                     ... )
{
    va_list  args;
    int      ret;

    va_start(args, format);
    ret = qemu_command_format(buffer, buffer_size, format, args);
    va_end(args);
    return ret;
}


static int
qemu_control_fd(void)
{
    static QemuChannel  channel[1];
    int                 fd;

293
    fd = qemu_channel_open( channel, "hw-control", O_RDWR );
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    if (fd < 0) {
        D("%s: could not open control channel: %s", __FUNCTION__,
          strerror(errno));
    }
    return fd;
}

static int
qemu_control_send(const char*  cmd, int  len)
{
    int  fd, len2;

    if (len < 0) {
        errno = EINVAL;
        return -1;
    }

    fd = qemu_control_fd();
    if (fd < 0)
        return -1;

315
    len2 = qemu_fd_write(fd, cmd, len);
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
    close(fd);
    if (len2 != len) {
        D("%s: could not send everything %d < %d",
          __FUNCTION__, len2, len);
        return -1;
    }
    return 0;
}


int
qemu_control_command( const char*  fmt, ... )
{
    va_list  args;
    char     command[256];
    int      len, fd;

    va_start(args, fmt);
    len = qemu_command_vformat( command, sizeof command, fmt, args );
    va_end(args);

    if (len < 0 || len >= (int)sizeof command) {
        if (len < 0) {
            D("%s: could not send: %s", __FUNCTION__, strerror(errno));
        } else {
            D("%s: too large %d > %d", __FUNCTION__, len, (int)(sizeof command));
        }
        errno = EINVAL;
        return -1;
    }

    return qemu_control_send( command, len );
}

extern int  qemu_control_query( const char*  question, int  questionlen,
                                char*        answer,   int  answersize )
{
    int   ret, fd, len, result = -1;
    char  header[5], *end;

    if (questionlen <= 0) {
        errno = EINVAL;
        return -1;
    }

    fd = qemu_control_fd();
    if (fd < 0)
        return -1;

365
    ret = qemu_fd_write( fd, question, questionlen );
366 367 368 369 370 371 372
    if (ret != questionlen) {
        D("%s: could not write all: %d < %d", __FUNCTION__,
          ret, questionlen);
        goto Exit;
    }

    /* read a 4-byte header giving the length of the following content */
373
    ret = qemu_fd_read( fd, header, 4 );
374 375 376 377 378 379
    if (ret != 4) {
        D("%s: could not read header (%d != 4)",
          __FUNCTION__, ret);
        goto Exit;
    }

380
    header[4] = 0;
381 382 383 384 385 386 387 388
    len = strtol( header, &end,  16 );
    if ( len < 0 || end == NULL || end != header+4 || len > answersize ) {
        D("%s: could not parse header: '%s'",
          __FUNCTION__, header);
        goto Exit;
    }

    /* read the answer */
389
    ret = qemu_fd_read( fd, answer, len );
390 391 392 393 394 395 396 397 398 399 400 401
    if (ret != len) {
        D("%s: could not read all of answer %d < %d",
          __FUNCTION__, ret, len);
        goto Exit;
    }

    result = len;

Exit:
    close(fd);
    return result;
}