aboutsummaryrefslogtreecommitdiffstats
path: root/monitor.c
blob: 7189c5f865ea3c9fec1388f3a8b7ad9da35f4f58 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/socket.h>

#include <gtk/gtk.h>
#include <vte/vte.h>

#include "qemu-gtk.h"
#include "tcp.h"

/* ----------------------------------------------------------------- */

static void show_error(struct qemu_window *win, char *cmd, char *reply)
{
    /* FIXME: show error dialog */
    fprintf(stderr, "ERROR: %s: %s\n", cmd, reply);
}

static int monitor_parse(struct qemu_window *win, char *buf, int len)
{
    char *reply, *prompt, *cmd;
    int off;

    /* handle vnc password change */
    if (win->vnc_send_password && win->mrun &&
        !strcmp(win->mrun->cmd, "change vnc password") &&
        strstr(buf, "Password: "))
    {
        char buf[64];
        int len;

        if (debug)
            fprintf(stderr, "%s: password prompt\n", __FUNCTION__);
        len = snprintf(buf, sizeof(buf), "%s\n", win->vnc_password);
        write(win->monitor.handle, buf, len);
        win->vnc_send_password = 0;
        return 0;
    }

    /* parse buffer */
    prompt = strstr(buf, "(qemu) ");
    if (NULL == prompt) {
        return 0;
    }
    off = prompt - buf;
    if (prompt > buf+1 && prompt[-2] == '\r' && prompt[-1] == '\n')
        prompt -= 2;
    *prompt = 0;

    if (!win->mrun) {
        if (debug)
            fprintf(stderr, "%s: no cmd in flight\n", __FUNCTION__);
        goto out;
    }
    cmd = win->mrun->cmd;
    reply = strstr(buf, "\r\n");
    if (!reply) {
        if (debug)
            fprintf(stderr, "%s: no reply for \"%s\"\n", __FUNCTION__, cmd);
        goto out;
    }
    reply += 2;
    if (debug)
        fprintf(stderr, "%s: \"%s\" -> [%s]\n", __FUNCTION__, cmd, reply);

    /* parse reply */
    if (0 == strcmp(cmd, "info version")) {
        snprintf(win->version, sizeof(win->version), "%s", reply);
        update_status(win);

    } else if (0 == strcmp(cmd, "info name")) {
        snprintf(win->name, sizeof(win->name), "%s", reply);
        update_status(win);

    } else if (0 == strcmp(cmd, "info vnc")) {
        if (1 == sscanf(reply, "VNC server active on: %127[^\r\n]",
                        win->vnc_display) &&
            strstr(reply, "No client connected")) {
            if (strstr(win->vnc_display, ",password")) {
                win->vnc_password = malloc(32);
#if 1
                /* FIXME: do something less predictable */
                srand(time(NULL)+getpid());
                snprintf(win->vnc_password, 32, "%x", rand() & 0xfffffff);
#endif
                win->vnc_send_password = 1;
                monitor_append(win, "change vnc password");
            } else {
                vnc_connect(win);
            }
        }

    } else if (0 == strcmp(cmd, "change vnc password")) {
        vnc_connect(win);

    } else if (0 == strcmp(cmd, "info block")) {
        devices_parse_info_block(win, reply);

    } else if (0 == strncmp(cmd, "eject ", 6) ||
               0 == strncmp(cmd, "change ", 7)) {
        show_error(win, cmd, reply);
    }

out:
    return off + 7;
}

/* ----------------------------------------------------------------- */

static void monitor_next(struct qemu_window *win)
{
    char buf[256];
    int len;

    if (win->mrun)
        free(win->mrun);
    win->mrun = win->mqueue;
    if (!win->mrun)
        return;
    win->mqueue = win->mrun->next;
    len = snprintf(buf, sizeof(buf), "%s\n", win->mrun->cmd);
    write(win->monitor.handle, buf, len);
}

void monitor_append(struct qemu_window *win, char *cmd)
{
    struct qemu_mq *mq;
    int try_send = 0;

    if (win->mqueue) {
        for (mq = win->mqueue; mq->next; mq = mq->next)
            ;
        mq->next = malloc(sizeof(*mq));
        mq = mq->next;
        if (debug)
            fprintf(stderr, "%s: tail: %s\n", __FUNCTION__, cmd);
    } else {
        win->mqueue = malloc(sizeof(*mq));
        mq = win->mqueue;
        if (!win->mrun && !win->mused && win->msize)
            try_send = 1;
        if (debug)
            fprintf(stderr, "%s: %s: %s\n", __FUNCTION__,
                    try_send ? "send" : "head", cmd);
    }
    mq->next = NULL;
    snprintf(mq->cmd, sizeof(mq->cmd), "%s", cmd);

    if (try_send)
        monitor_next(win);
}

static gboolean monitor_watch(GIOChannel *source, GIOCondition condition,
                              gpointer userdata)
{
    struct qemu_window *win = userdata;
    int rc;

    if (win->mused == win->msize) {
        if (!win->msize)
            win->msize = 4;
        win->msize *= 2;
        win->mbuf = realloc(win->mbuf, win->msize +1);
    }

    rc = read(win->monitor.handle, win->mbuf + win->mused, win->msize - win->mused);
    switch(rc) {
    case -1:
        if (EINTR == errno)
            break;
        perror("monitor: read");
        goto close;
    case 0:
        fprintf(stderr, "monitor: EOF\n");
        goto close;
    default:
        if (win->monitor.vte)
            vte_terminal_feed(VTE_TERMINAL(win->monitor.vte), win->mbuf + win->mused, rc);
        win->mused += rc;
        win->mbuf[win->mused] = 0;
        rc = monitor_parse(win, win->mbuf, win->mused);
        if (rc) {
            if (rc < win->mused)
                memmove(win->mbuf, win->mbuf + rc, win->mused - rc);
            win->mused -= rc;
        }
        break;
    }

    if (0 == win->mused)
        monitor_next(win);
    return TRUE;

close:
    if (win->monitor.vte)
        vte_terminal_feed(VTE_TERMINAL(win->monitor.vte), "\r\n=== CLOSED ===", 16);
    close(win->monitor.handle);
    win->monitor.handle = -1;
    if (win->quit_on_shutdown)
        gtk_widget_destroy(win->toplevel);
    else
        update_status(win);
    return FALSE;
}

/* ----------------------------------------------------------------- */

int monitor_connect(struct qemu_window *win, char *dest)
{
    int fd;

    fd = conn_init(&win->monitor, "monitor", dest);
    if (-1 == fd)
        return -1;

    win->monitor.ch = g_io_channel_unix_new(fd);
    win->monitor.id = g_io_add_watch(win->monitor.ch, G_IO_IN, monitor_watch, win);

    monitor_append(win, "info version");
    monitor_append(win, "info name");
    monitor_append(win, "info vnc");
    monitor_append(win, "info block");
    return fd;
}