aboutsummaryrefslogtreecommitdiffstats
path: root/android/core-init-utils.c
blob: 97d5f05ce874264eed422d446b9b0220d4e9f9f2 (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
/* Copyright (C) 2010 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** 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.
*/

/*
 * Contains implementation of routines and that are used in the course
 * of the emulator's core initialization.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "qemu-common.h"
#include "sockets.h"
#include "android/android.h"
#include "android/core-init-utils.h"
#include "android/utils/bufprint.h"

extern char* android_op_ui_port;

/* Sends core initialization status message back to the UI that started this
 * core process.
 * Param:
 *  msg - Message to send. On success, message must begin with "ok:", followed
 *      by "port=<console port number>". On initialization failure, message must
 *      begin with "ko:", followed by a string describing the reason of failure.
 */
static void
android_core_send_init_response(const char* msg)
{
    int fd;
    int ui_port;

    if (android_op_ui_port == NULL) {
        // No socket - no reply.
        return;
    }

    ui_port = atoi(android_op_ui_port);
    if (ui_port >= 0) {
        // At this point UI always starts the core on the same workstation.
        fd = socket_loopback_client(ui_port, SOCKET_STREAM);
        if (fd == -1) {
            fprintf(stderr, "Unable to create UI socket client for port %s: %s\n",
                    android_op_ui_port, errno_str);
            return;
        }
        socket_send(fd, msg, strlen(msg) + 1);
        socket_close(fd);
    } else {
        fprintf(stderr, "Invalid -ui-port parameter: %s\n", android_op_ui_port);
    }
}

void
android_core_init_completed(void)
{
    char msg[32];
    snprintf(msg, sizeof(msg), "ok:port=%d", android_base_port);
    android_core_send_init_response(msg);
}

void
android_core_init_failure(const char* fmt, ...)
{
    va_list  args;
    char msg[4096];

    // Format "ko" message to send back to the UI.
    snprintf(msg, sizeof(msg), "ko:");

    va_start(args, fmt);
    vbufprint(msg + strlen(msg), msg + sizeof(msg), fmt, args);
    va_end(args);

    // Send message back to the UI, print it to the error stdout, and exit the
    // process.
    android_core_send_init_response(msg);
    fprintf(stderr, "%s\n", msg);
    exit(1);
}

void
android_core_init_exit(int exit_status)
{
    char msg[32];
    // Build "ok" message with the exit status, and send it back to the UI.
    snprintf(msg, sizeof(msg), "ok:status=%d", exit_status);
    android_core_send_init_response(msg);
    exit(exit_status);
}