2016-12-12 23:41:00 +08:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-02-01 19:18:46 +08:00
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
#include "qemu/osdep.h"
|
2017-01-26 21:19:46 +08:00
|
|
|
#include "chardev/char.h"
|
2016-12-12 23:41:00 +08:00
|
|
|
#include "io/channel-socket.h"
|
|
|
|
#include "io/channel-tls.h"
|
2017-12-18 21:54:17 +08:00
|
|
|
#include "io/net-listener.h"
|
2016-12-12 23:41:00 +08:00
|
|
|
#include "qemu/error-report.h"
|
2018-02-01 19:18:46 +08:00
|
|
|
#include "qemu/option.h"
|
2016-12-12 23:41:00 +08:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qapi/clone-visitor.h"
|
2018-02-11 17:36:01 +08:00
|
|
|
#include "qapi/qapi-visit-sockets.h"
|
2018-03-08 22:07:14 +08:00
|
|
|
#include "sysemu/sysemu.h"
|
2016-12-12 23:41:00 +08:00
|
|
|
|
2017-01-26 21:19:46 +08:00
|
|
|
#include "chardev/char-io.h"
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
/***********************************************************/
|
|
|
|
/* TCP Net console */
|
|
|
|
|
|
|
|
#define TCP_MAX_FDS 16
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
typedef struct {
|
|
|
|
char buf[21];
|
|
|
|
size_t buflen;
|
|
|
|
} TCPChardevTelnetInit;
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
typedef struct {
|
|
|
|
Chardev parent;
|
|
|
|
QIOChannel *ioc; /* Client I/O channel */
|
|
|
|
QIOChannelSocket *sioc; /* Client master channel */
|
2017-12-18 21:54:17 +08:00
|
|
|
QIONetListener *listener;
|
2018-01-25 21:51:29 +08:00
|
|
|
GSource *hup_source;
|
2016-12-12 23:41:00 +08:00
|
|
|
QCryptoTLSCreds *tls_creds;
|
|
|
|
int connected;
|
|
|
|
int max_size;
|
|
|
|
int do_telnetopt;
|
|
|
|
int do_nodelay;
|
|
|
|
int *read_msgfds;
|
|
|
|
size_t read_msgfds_num;
|
|
|
|
int *write_msgfds;
|
|
|
|
size_t write_msgfds_num;
|
|
|
|
|
|
|
|
SocketAddress *addr;
|
|
|
|
bool is_listen;
|
|
|
|
bool is_telnet;
|
2016-09-23 14:06:11 +08:00
|
|
|
bool is_tn3270;
|
2018-03-06 13:33:15 +08:00
|
|
|
GSource *telnet_source;
|
|
|
|
TCPChardevTelnetInit *telnet_init;
|
2016-12-12 23:41:00 +08:00
|
|
|
|
2018-01-04 22:18:35 +08:00
|
|
|
GSource *reconnect_timer;
|
2016-12-12 23:41:00 +08:00
|
|
|
int64_t reconnect_time;
|
|
|
|
bool connect_err_reported;
|
|
|
|
} SocketChardev;
|
|
|
|
|
|
|
|
#define SOCKET_CHARDEV(obj) \
|
|
|
|
OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
|
|
|
|
|
|
|
|
static gboolean socket_reconnect_timeout(gpointer opaque);
|
2018-03-06 13:33:15 +08:00
|
|
|
static void tcp_chr_telnet_init(Chardev *chr);
|
2016-12-12 23:41:00 +08:00
|
|
|
|
2018-01-04 22:18:35 +08:00
|
|
|
static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
|
|
|
|
{
|
|
|
|
if (s->reconnect_timer) {
|
|
|
|
g_source_destroy(s->reconnect_timer);
|
|
|
|
g_source_unref(s->reconnect_timer);
|
|
|
|
s->reconnect_timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
static void qemu_chr_socket_restart_timer(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
assert(s->connected == 0);
|
|
|
|
name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
|
2018-01-04 22:18:35 +08:00
|
|
|
s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
|
|
|
|
s->reconnect_time * 1000,
|
|
|
|
socket_reconnect_timeout,
|
|
|
|
chr);
|
|
|
|
g_source_set_name(s->reconnect_timer, name);
|
2016-12-12 23:41:00 +08:00
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_report_connect_error(Chardev *chr,
|
|
|
|
Error *err)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
if (!s->connect_err_reported) {
|
|
|
|
error_report("Unable to connect character device %s: %s",
|
|
|
|
chr->label, error_get_pretty(err));
|
|
|
|
s->connect_err_reported = true;
|
|
|
|
}
|
|
|
|
qemu_chr_socket_restart_timer(chr);
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:54:17 +08:00
|
|
|
static void tcp_chr_accept(QIONetListener *listener,
|
|
|
|
QIOChannelSocket *cioc,
|
|
|
|
void *opaque);
|
2016-12-12 23:41:00 +08:00
|
|
|
|
2017-02-02 22:26:29 +08:00
|
|
|
static int tcp_chr_read_poll(void *opaque);
|
|
|
|
static void tcp_chr_disconnect(Chardev *chr);
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
/* Called with chr_write_lock held. */
|
|
|
|
static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
if (s->connected) {
|
|
|
|
int ret = io_channel_send_full(s->ioc, buf, len,
|
|
|
|
s->write_msgfds,
|
|
|
|
s->write_msgfds_num);
|
|
|
|
|
|
|
|
/* free the written msgfds, no matter what */
|
|
|
|
if (s->write_msgfds_num) {
|
|
|
|
g_free(s->write_msgfds);
|
|
|
|
s->write_msgfds = 0;
|
|
|
|
s->write_msgfds_num = 0;
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:26:29 +08:00
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
|
|
|
if (tcp_chr_read_poll(chr) <= 0) {
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
return len;
|
|
|
|
} /* else let the read handler finish it properly */
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
/* XXX: indicate an error ? */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tcp_chr_read_poll(void *opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(opaque);
|
|
|
|
if (!s->connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s->max_size = qemu_chr_be_can_write(chr);
|
|
|
|
return s->max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcp_chr_process_IAC_bytes(Chardev *chr,
|
|
|
|
SocketChardev *s,
|
|
|
|
uint8_t *buf, int *size)
|
|
|
|
{
|
2016-09-23 14:06:11 +08:00
|
|
|
/* Handle any telnet or tn3270 client's basic IAC options.
|
|
|
|
* For telnet options, it satisfies char by char mode with no echo.
|
|
|
|
* For tn3270 options, it satisfies binary mode with EOR.
|
|
|
|
* All IAC options will be removed from the buf and the do_opt
|
|
|
|
* pointer will be used to track the state of the width of the
|
|
|
|
* IAC information.
|
2016-12-12 23:41:00 +08:00
|
|
|
*
|
2016-09-23 14:06:11 +08:00
|
|
|
* RFC854: "All TELNET commands consist of at least a two byte sequence.
|
|
|
|
* The commands dealing with option negotiation are three byte sequences,
|
|
|
|
* the third byte being the code for the option referenced."
|
|
|
|
* "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
|
|
|
|
* "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
|
|
|
|
* for tn3270.
|
|
|
|
* NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
|
|
|
|
* session, and NOP and IP need to be done later.
|
2016-12-12 23:41:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
if (s->do_telnetopt > 1) {
|
|
|
|
if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
|
|
|
|
/* Double IAC means send an IAC */
|
|
|
|
if (j != i) {
|
|
|
|
buf[j] = buf[i];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
s->do_telnetopt = 1;
|
|
|
|
} else {
|
|
|
|
if ((unsigned char)buf[i] == IAC_BREAK
|
|
|
|
&& s->do_telnetopt == 2) {
|
|
|
|
/* Handle IAC break commands by sending a serial break */
|
|
|
|
qemu_chr_be_event(chr, CHR_EVENT_BREAK);
|
|
|
|
s->do_telnetopt++;
|
2016-09-23 14:06:11 +08:00
|
|
|
} else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
|
|
|
|
|| (unsigned char)buf[i] == IAC_SB
|
|
|
|
|| (unsigned char)buf[i] == IAC_SE)
|
|
|
|
&& s->do_telnetopt == 2) {
|
|
|
|
buf[j++] = IAC;
|
|
|
|
buf[j++] = buf[i];
|
|
|
|
s->do_telnetopt++;
|
|
|
|
} else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
|
|
|
|
|| (unsigned char)buf[i] == IAC_NOP)
|
|
|
|
&& s->do_telnetopt == 2) {
|
|
|
|
/* TODO: IP and NOP need to be implemented later. */
|
|
|
|
s->do_telnetopt++;
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
s->do_telnetopt++;
|
|
|
|
}
|
|
|
|
if (s->do_telnetopt >= 4) {
|
|
|
|
s->do_telnetopt = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((unsigned char)buf[i] == IAC) {
|
|
|
|
s->do_telnetopt = 2;
|
|
|
|
} else {
|
|
|
|
if (j != i) {
|
|
|
|
buf[j] = buf[i];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*size = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
|
|
|
|
|
|
|
|
assert(num <= TCP_MAX_FDS);
|
|
|
|
|
|
|
|
if (to_copy) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
|
|
|
|
|
|
|
|
/* Close unused fds */
|
|
|
|
for (i = to_copy; i < s->read_msgfds_num; i++) {
|
|
|
|
close(s->read_msgfds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(s->read_msgfds);
|
|
|
|
s->read_msgfds = 0;
|
|
|
|
s->read_msgfds_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
/* clear old pending fd array */
|
|
|
|
g_free(s->write_msgfds);
|
|
|
|
s->write_msgfds = NULL;
|
|
|
|
s->write_msgfds_num = 0;
|
|
|
|
|
|
|
|
if (!s->connected ||
|
|
|
|
!qio_channel_has_feature(s->ioc,
|
|
|
|
QIO_CHANNEL_FEATURE_FD_PASS)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num) {
|
|
|
|
s->write_msgfds = g_new(int, num);
|
|
|
|
memcpy(s->write_msgfds, fds, num * sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
s->write_msgfds_num = num;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
struct iovec iov = { .iov_base = buf, .iov_len = len };
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
int *msgfds = NULL;
|
|
|
|
size_t msgfds_num = 0;
|
|
|
|
|
|
|
|
if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
|
|
|
|
ret = qio_channel_readv_full(s->ioc, &iov, 1,
|
|
|
|
&msgfds, &msgfds_num,
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
ret = qio_channel_readv_full(s->ioc, &iov, 1,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == QIO_CHANNEL_ERR_BLOCK) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
ret = -1;
|
|
|
|
} else if (ret == -1) {
|
|
|
|
errno = EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msgfds_num) {
|
|
|
|
/* close and clean read_msgfds */
|
|
|
|
for (i = 0; i < s->read_msgfds_num; i++) {
|
|
|
|
close(s->read_msgfds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->read_msgfds_num) {
|
|
|
|
g_free(s->read_msgfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->read_msgfds = msgfds;
|
|
|
|
s->read_msgfds_num = msgfds_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s->read_msgfds_num; i++) {
|
|
|
|
int fd = s->read_msgfds[i];
|
|
|
|
if (fd < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
|
|
|
|
qemu_set_block(fd);
|
|
|
|
|
|
|
|
#ifndef MSG_CMSG_CLOEXEC
|
|
|
|
qemu_set_cloexec(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
return qio_channel_create_watch(s->ioc, cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcp_chr_free_connection(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (s->read_msgfds_num) {
|
|
|
|
for (i = 0; i < s->read_msgfds_num; i++) {
|
|
|
|
close(s->read_msgfds[i]);
|
|
|
|
}
|
|
|
|
g_free(s->read_msgfds);
|
|
|
|
s->read_msgfds = NULL;
|
|
|
|
s->read_msgfds_num = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:51:29 +08:00
|
|
|
if (s->hup_source != NULL) {
|
|
|
|
g_source_destroy(s->hup_source);
|
|
|
|
g_source_unref(s->hup_source);
|
|
|
|
s->hup_source = NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
tcp_set_msgfds(chr, NULL, 0);
|
2017-04-19 09:15:32 +08:00
|
|
|
remove_fd_in_watch(chr);
|
2016-12-12 23:41:00 +08:00
|
|
|
object_unref(OBJECT(s->sioc));
|
|
|
|
s->sioc = NULL;
|
|
|
|
object_unref(OBJECT(s->ioc));
|
|
|
|
s->ioc = NULL;
|
|
|
|
g_free(chr->filename);
|
|
|
|
chr->filename = NULL;
|
|
|
|
s->connected = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr,
|
|
|
|
bool is_listen, bool is_telnet)
|
|
|
|
{
|
|
|
|
switch (addr->type) {
|
2017-04-26 15:36:41 +08:00
|
|
|
case SOCKET_ADDRESS_TYPE_INET:
|
2016-12-12 23:41:00 +08:00
|
|
|
return g_strdup_printf("%s%s:%s:%s%s", prefix,
|
|
|
|
is_telnet ? "telnet" : "tcp",
|
2017-04-26 15:36:41 +08:00
|
|
|
addr->u.inet.host,
|
|
|
|
addr->u.inet.port,
|
2016-12-12 23:41:00 +08:00
|
|
|
is_listen ? ",server" : "");
|
|
|
|
break;
|
2017-04-26 15:36:41 +08:00
|
|
|
case SOCKET_ADDRESS_TYPE_UNIX:
|
2016-12-12 23:41:00 +08:00
|
|
|
return g_strdup_printf("%sunix:%s%s", prefix,
|
2017-04-26 15:36:41 +08:00
|
|
|
addr->u.q_unix.path,
|
2016-12-12 23:41:00 +08:00
|
|
|
is_listen ? ",server" : "");
|
|
|
|
break;
|
2017-04-26 15:36:41 +08:00
|
|
|
case SOCKET_ADDRESS_TYPE_FD:
|
|
|
|
return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.str,
|
2016-12-12 23:41:00 +08:00
|
|
|
is_listen ? ",server" : "");
|
|
|
|
break;
|
2017-04-26 15:36:41 +08:00
|
|
|
case SOCKET_ADDRESS_TYPE_VSOCK:
|
char: Fix socket with "type": "vsock" address
Watch this:
$ qemu-system-x86_64 -nodefaults -S -display none -qmp stdio
{"QMP": {"version": {"qemu": {"micro": 91, "minor": 8, "major": 2}, "package": " (v2.8.0-1195-gf84141e-dirty)"}, "capabilities": []}}
{ "execute": "qmp_capabilities" }
{"return": {}}
{ "execute": "chardev-add", "arguments": { "id": "chr0", "backend": { "type": "socket", "data": { "addr": { "type": "vsock", "data": { "cid": "CID", "port": "P" }}}}}}
Aborted (core dumped)
Crashes because SocketAddress_to_str() is blissfully unaware of
SOCKET_ADDRESS_KIND_VSOCK. Fix that. Pick the output format to match
socket_parse(), just like the existing formats.
Cc: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-id: 1490895797-29094-3-git-send-email-armbru@redhat.com
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2017-03-31 01:43:10 +08:00
|
|
|
return g_strdup_printf("%svsock:%s:%s", prefix,
|
2017-04-26 15:36:41 +08:00
|
|
|
addr->u.vsock.cid,
|
|
|
|
addr->u.vsock.port);
|
2016-12-12 23:41:00 +08:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:32:31 +08:00
|
|
|
static void update_disconnected_filename(SocketChardev *s)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(s);
|
|
|
|
|
|
|
|
g_free(chr->filename);
|
|
|
|
chr->filename = SocketAddress_to_str("disconnected:", s->addr,
|
|
|
|
s->is_listen, s->is_telnet);
|
|
|
|
}
|
|
|
|
|
2017-10-05 23:50:57 +08:00
|
|
|
/* NB may be called even if tcp_chr_connect has not been
|
|
|
|
* reached, due to TLS or telnet initialization failure,
|
|
|
|
* so can *not* assume s->connected == true
|
|
|
|
*/
|
2016-12-12 23:41:00 +08:00
|
|
|
static void tcp_chr_disconnect(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
2017-10-05 23:50:57 +08:00
|
|
|
bool emit_close = s->connected;
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
tcp_chr_free_connection(chr);
|
|
|
|
|
2017-12-18 21:54:17 +08:00
|
|
|
if (s->listener) {
|
2018-03-06 13:33:14 +08:00
|
|
|
qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
|
|
|
|
chr, NULL, chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
2016-12-20 22:32:31 +08:00
|
|
|
update_disconnected_filename(s);
|
2017-10-05 23:50:57 +08:00
|
|
|
if (emit_close) {
|
|
|
|
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
|
|
|
|
}
|
2016-12-12 23:41:00 +08:00
|
|
|
if (s->reconnect_time) {
|
|
|
|
qemu_chr_socket_restart_timer(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(opaque);
|
|
|
|
uint8_t buf[CHR_READ_BUF_LEN];
|
|
|
|
int len, size;
|
|
|
|
|
|
|
|
if (!s->connected || s->max_size <= 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
len = sizeof(buf);
|
|
|
|
if (len > s->max_size) {
|
|
|
|
len = s->max_size;
|
|
|
|
}
|
|
|
|
size = tcp_chr_recv(chr, (void *)buf, len);
|
2018-02-22 20:13:51 +08:00
|
|
|
if (size == 0 || (size == -1 && errno != EAGAIN)) {
|
2016-12-12 23:41:00 +08:00
|
|
|
/* connection closed */
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
} else if (size > 0) {
|
|
|
|
if (s->do_telnetopt) {
|
|
|
|
tcp_chr_process_IAC_bytes(chr, s, buf, &size);
|
|
|
|
}
|
|
|
|
if (size > 0) {
|
|
|
|
qemu_chr_be_write(chr, buf, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:51:29 +08:00
|
|
|
static gboolean tcp_chr_hup(QIOChannel *channel,
|
|
|
|
GIOCondition cond,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (!s->connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-07 01:03:53 +08:00
|
|
|
qio_channel_set_blocking(s->ioc, true, NULL);
|
2016-12-12 23:41:00 +08:00
|
|
|
size = tcp_chr_recv(chr, (void *) buf, len);
|
2017-07-07 01:03:53 +08:00
|
|
|
qio_channel_set_blocking(s->ioc, false, NULL);
|
2016-12-12 23:41:00 +08:00
|
|
|
if (size == 0) {
|
|
|
|
/* connection closed */
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
|
|
|
|
struct sockaddr_storage *ps, socklen_t ps_len,
|
|
|
|
bool is_listen, bool is_telnet)
|
|
|
|
{
|
|
|
|
char shost[NI_MAXHOST], sserv[NI_MAXSERV];
|
|
|
|
char phost[NI_MAXHOST], pserv[NI_MAXSERV];
|
|
|
|
const char *left = "", *right = "";
|
|
|
|
|
|
|
|
switch (ss->ss_family) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
case AF_UNIX:
|
|
|
|
return g_strdup_printf("unix:%s%s",
|
|
|
|
((struct sockaddr_un *)(ss))->sun_path,
|
|
|
|
is_listen ? ",server" : "");
|
|
|
|
#endif
|
|
|
|
case AF_INET6:
|
|
|
|
left = "[";
|
|
|
|
right = "]";
|
|
|
|
/* fall through */
|
|
|
|
case AF_INET:
|
|
|
|
getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
|
|
|
|
sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
|
|
|
|
pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
|
|
|
|
is_telnet ? "telnet" : "tcp",
|
|
|
|
left, shost, right, sserv,
|
|
|
|
is_listen ? ",server" : "",
|
|
|
|
left, phost, right, pserv);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return g_strdup_printf("unknown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcp_chr_connect(void *opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(opaque);
|
|
|
|
|
|
|
|
g_free(chr->filename);
|
|
|
|
chr->filename = sockaddr_to_str(
|
|
|
|
&s->sioc->localAddr, s->sioc->localAddrLen,
|
|
|
|
&s->sioc->remoteAddr, s->sioc->remoteAddrLen,
|
|
|
|
s->is_listen, s->is_telnet);
|
|
|
|
|
|
|
|
s->connected = 1;
|
2018-03-20 23:18:43 +08:00
|
|
|
chr->gsource = io_add_watch_poll(chr, s->ioc,
|
|
|
|
tcp_chr_read_poll,
|
|
|
|
tcp_chr_read,
|
|
|
|
chr, chr->gcontext);
|
2018-01-25 21:51:29 +08:00
|
|
|
|
|
|
|
s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
|
|
|
|
g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
|
|
|
|
chr, NULL);
|
|
|
|
g_source_attach(s->hup_source, chr->gcontext);
|
|
|
|
|
2016-12-14 19:23:02 +08:00
|
|
|
qemu_chr_be_event(chr, CHR_EVENT_OPENED);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
static void tcp_chr_telnet_destroy(SocketChardev *s)
|
|
|
|
{
|
|
|
|
if (s->telnet_source) {
|
|
|
|
g_source_destroy(s->telnet_source);
|
|
|
|
g_source_unref(s->telnet_source);
|
|
|
|
s->telnet_source = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 14:35:54 +08:00
|
|
|
static void tcp_chr_update_read_handler(Chardev *chr)
|
2016-12-12 23:41:00 +08:00
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
2018-03-06 13:33:14 +08:00
|
|
|
if (s->listener) {
|
|
|
|
/*
|
|
|
|
* It's possible that chardev context is changed in
|
|
|
|
* qemu_chr_be_update_read_handlers(). Reset it for QIO net
|
|
|
|
* listener if there is.
|
|
|
|
*/
|
|
|
|
qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
|
|
|
|
chr, NULL, chr->gcontext);
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
if (s->telnet_source) {
|
|
|
|
tcp_chr_telnet_init(CHARDEV(s));
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
if (!s->connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 09:15:32 +08:00
|
|
|
remove_fd_in_watch(chr);
|
2016-12-12 23:41:00 +08:00
|
|
|
if (s->ioc) {
|
2017-04-19 09:15:32 +08:00
|
|
|
chr->gsource = io_add_watch_poll(chr, s->ioc,
|
2016-12-12 23:41:00 +08:00
|
|
|
tcp_chr_read_poll,
|
|
|
|
tcp_chr_read, chr,
|
2017-09-21 14:35:53 +08:00
|
|
|
chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
|
|
|
|
GIOCondition cond G_GNUC_UNUSED,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2018-03-06 13:33:15 +08:00
|
|
|
SocketChardev *s = user_data;
|
|
|
|
Chardev *chr = CHARDEV(s);
|
|
|
|
TCPChardevTelnetInit *init = s->telnet_init;
|
2016-12-12 23:41:00 +08:00
|
|
|
ssize_t ret;
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
assert(init);
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == QIO_CHANNEL_ERR_BLOCK) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2018-03-06 13:33:15 +08:00
|
|
|
tcp_chr_disconnect(chr);
|
2018-03-01 16:44:24 +08:00
|
|
|
goto end;
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
init->buflen -= ret;
|
|
|
|
|
|
|
|
if (init->buflen == 0) {
|
2018-03-06 13:33:15 +08:00
|
|
|
tcp_chr_connect(chr);
|
2018-03-01 16:44:24 +08:00
|
|
|
goto end;
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memmove(init->buf, init->buf + ret, init->buflen);
|
|
|
|
|
2018-03-01 16:44:24 +08:00
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
|
|
|
end:
|
2018-03-06 13:33:15 +08:00
|
|
|
g_free(s->telnet_init);
|
|
|
|
s->telnet_init = NULL;
|
|
|
|
g_source_unref(s->telnet_source);
|
|
|
|
s->telnet_source = NULL;
|
2018-03-01 16:44:24 +08:00
|
|
|
return G_SOURCE_REMOVE;
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tcp_chr_telnet_init(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
2018-03-06 13:33:15 +08:00
|
|
|
TCPChardevTelnetInit *init;
|
2016-12-12 23:41:00 +08:00
|
|
|
size_t n = 0;
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
/* Destroy existing task */
|
|
|
|
tcp_chr_telnet_destroy(s);
|
|
|
|
|
|
|
|
if (s->telnet_init) {
|
|
|
|
/* We are possibly during a handshake already */
|
|
|
|
goto cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
|
|
|
|
init = s->telnet_init;
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
#define IACSET(x, a, b, c) \
|
|
|
|
do { \
|
|
|
|
x[n++] = a; \
|
|
|
|
x[n++] = b; \
|
|
|
|
x[n++] = c; \
|
|
|
|
} while (0)
|
|
|
|
|
2016-09-23 14:06:11 +08:00
|
|
|
if (!s->is_tn3270) {
|
|
|
|
init->buflen = 12;
|
|
|
|
/* Prep the telnet negotion to put telnet in binary,
|
|
|
|
* no echo, single char mode */
|
|
|
|
IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
|
|
|
|
IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
|
|
|
|
IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
|
|
|
|
IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
|
|
|
|
} else {
|
|
|
|
init->buflen = 21;
|
|
|
|
/* Prep the TN3270 negotion based on RFC1576 */
|
|
|
|
IACSET(init->buf, 0xff, 0xfd, 0x19); /* IAC DO EOR */
|
|
|
|
IACSET(init->buf, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
|
|
|
|
IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
|
|
|
|
IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
|
|
|
|
IACSET(init->buf, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
|
|
|
|
IACSET(init->buf, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
|
|
|
|
IACSET(init->buf, 0x01, 0xff, 0xf0); /* SEND IAC SE */
|
|
|
|
}
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
#undef IACSET
|
|
|
|
|
2018-03-06 13:33:15 +08:00
|
|
|
cont:
|
|
|
|
s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
|
|
|
|
tcp_chr_telnet_init_io,
|
|
|
|
s, NULL,
|
|
|
|
chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tcp_chr_tls_handshake(QIOTask *task,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
Chardev *chr = user_data;
|
|
|
|
SocketChardev *s = user_data;
|
|
|
|
|
|
|
|
if (qio_task_propagate_error(task, NULL)) {
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
} else {
|
2017-12-21 18:52:37 +08:00
|
|
|
if (s->do_telnetopt) {
|
2016-12-12 23:41:00 +08:00
|
|
|
tcp_chr_telnet_init(chr);
|
|
|
|
} else {
|
|
|
|
tcp_chr_connect(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tcp_chr_tls_init(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
QIOChannelTLS *tioc;
|
|
|
|
Error *err = NULL;
|
|
|
|
gchar *name;
|
|
|
|
|
2018-03-08 22:07:14 +08:00
|
|
|
if (!machine_init_done) {
|
|
|
|
/* This will be postponed to machine_done notifier */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
if (s->is_listen) {
|
|
|
|
tioc = qio_channel_tls_new_server(
|
|
|
|
s->ioc, s->tls_creds,
|
|
|
|
NULL, /* XXX Use an ACL */
|
|
|
|
&err);
|
|
|
|
} else {
|
|
|
|
tioc = qio_channel_tls_new_client(
|
|
|
|
s->ioc, s->tls_creds,
|
2017-04-26 15:36:41 +08:00
|
|
|
s->addr->u.inet.host,
|
2016-12-12 23:41:00 +08:00
|
|
|
&err);
|
|
|
|
}
|
|
|
|
if (tioc == NULL) {
|
|
|
|
error_free(err);
|
|
|
|
tcp_chr_disconnect(chr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
name = g_strdup_printf("chardev-tls-%s-%s",
|
|
|
|
s->is_listen ? "server" : "client",
|
|
|
|
chr->label);
|
|
|
|
qio_channel_set_name(QIO_CHANNEL(tioc), name);
|
|
|
|
g_free(name);
|
|
|
|
object_unref(OBJECT(s->ioc));
|
|
|
|
s->ioc = QIO_CHANNEL(tioc);
|
|
|
|
|
|
|
|
qio_channel_tls_handshake(tioc,
|
|
|
|
tcp_chr_tls_handshake,
|
|
|
|
chr,
|
2018-03-05 14:43:24 +08:00
|
|
|
NULL,
|
2018-03-06 13:33:19 +08:00
|
|
|
chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tcp_chr_set_client_ioc_name(Chardev *chr,
|
|
|
|
QIOChannelSocket *sioc)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
char *name;
|
|
|
|
name = g_strdup_printf("chardev-tcp-%s-%s",
|
|
|
|
s->is_listen ? "server" : "client",
|
|
|
|
chr->label);
|
|
|
|
qio_channel_set_name(QIO_CHANNEL(sioc), name);
|
|
|
|
g_free(name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
if (s->ioc != NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->ioc = QIO_CHANNEL(sioc);
|
|
|
|
object_ref(OBJECT(sioc));
|
|
|
|
s->sioc = sioc;
|
|
|
|
object_ref(OBJECT(sioc));
|
|
|
|
|
|
|
|
qio_channel_set_blocking(s->ioc, false, NULL);
|
|
|
|
|
|
|
|
if (s->do_nodelay) {
|
|
|
|
qio_channel_set_delay(s->ioc, false);
|
|
|
|
}
|
2017-12-18 21:54:17 +08:00
|
|
|
if (s->listener) {
|
2018-03-06 13:33:14 +08:00
|
|
|
qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
|
|
|
|
NULL, chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->tls_creds) {
|
|
|
|
tcp_chr_tls_init(chr);
|
|
|
|
} else {
|
|
|
|
if (s->do_telnetopt) {
|
|
|
|
tcp_chr_telnet_init(chr);
|
|
|
|
} else {
|
|
|
|
tcp_chr_connect(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int tcp_chr_add_client(Chardev *chr, int fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
QIOChannelSocket *sioc;
|
|
|
|
|
|
|
|
sioc = qio_channel_socket_new_fd(fd, NULL);
|
|
|
|
if (!sioc) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tcp_chr_set_client_ioc_name(chr, sioc);
|
|
|
|
ret = tcp_chr_new_client(chr, sioc);
|
|
|
|
object_unref(OBJECT(sioc));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:54:17 +08:00
|
|
|
static void tcp_chr_accept(QIONetListener *listener,
|
|
|
|
QIOChannelSocket *cioc,
|
|
|
|
void *opaque)
|
2016-12-12 23:41:00 +08:00
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
|
2017-12-18 21:54:17 +08:00
|
|
|
tcp_chr_set_client_ioc_name(chr, cioc);
|
|
|
|
tcp_chr_new_client(chr, cioc);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
QIOChannelSocket *sioc;
|
|
|
|
|
|
|
|
/* It can't wait on s->connected, since it is set asynchronously
|
|
|
|
* in TLS and telnet cases, only wait for an accepted socket */
|
|
|
|
while (!s->ioc) {
|
|
|
|
if (s->is_listen) {
|
2017-07-12 21:57:48 +08:00
|
|
|
info_report("QEMU waiting for connection on: %s",
|
|
|
|
chr->filename);
|
2017-12-18 21:54:17 +08:00
|
|
|
sioc = qio_net_listener_wait_client(s->listener);
|
|
|
|
tcp_chr_set_client_ioc_name(chr, sioc);
|
|
|
|
tcp_chr_new_client(chr, sioc);
|
|
|
|
object_unref(OBJECT(sioc));
|
2016-12-12 23:41:00 +08:00
|
|
|
} else {
|
|
|
|
sioc = qio_channel_socket_new();
|
|
|
|
tcp_chr_set_client_ioc_name(chr, sioc);
|
|
|
|
if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
|
|
|
|
object_unref(OBJECT(sioc));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tcp_chr_new_client(chr, sioc);
|
|
|
|
object_unref(OBJECT(sioc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void char_socket_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(obj);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(obj);
|
|
|
|
|
|
|
|
tcp_chr_free_connection(chr);
|
2018-01-04 22:18:35 +08:00
|
|
|
tcp_chr_reconn_timer_cancel(s);
|
2016-12-12 23:41:00 +08:00
|
|
|
qapi_free_SocketAddress(s->addr);
|
2018-03-06 13:33:15 +08:00
|
|
|
tcp_chr_telnet_destroy(s);
|
|
|
|
g_free(s->telnet_init);
|
2017-12-18 21:54:17 +08:00
|
|
|
if (s->listener) {
|
2018-03-06 13:33:14 +08:00
|
|
|
qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
|
|
|
|
NULL, chr->gcontext);
|
2017-12-18 21:54:17 +08:00
|
|
|
object_unref(OBJECT(s->listener));
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
if (s->tls_creds) {
|
|
|
|
object_unref(OBJECT(s->tls_creds));
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
|
|
|
|
{
|
|
|
|
QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (qio_task_propagate_error(task, &err)) {
|
|
|
|
check_report_connect_error(chr, err);
|
|
|
|
error_free(err);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->connect_err_reported = false;
|
|
|
|
tcp_chr_new_client(chr, sioc);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
object_unref(OBJECT(sioc));
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:17 +08:00
|
|
|
static void tcp_chr_connect_async(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
QIOChannelSocket *sioc;
|
|
|
|
|
|
|
|
sioc = qio_channel_socket_new();
|
|
|
|
tcp_chr_set_client_ioc_name(chr, sioc);
|
|
|
|
qio_channel_socket_connect_async(sioc, s->addr,
|
|
|
|
qemu_chr_socket_connected,
|
|
|
|
chr, NULL, chr->gcontext);
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
static gboolean socket_reconnect_timeout(gpointer opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(opaque);
|
|
|
|
|
2018-01-04 22:18:35 +08:00
|
|
|
g_source_unref(s->reconnect_timer);
|
|
|
|
s->reconnect_timer = NULL;
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
if (chr->be_open) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:17 +08:00
|
|
|
tcp_chr_connect_async(chr);
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qmp_chardev_open_socket(Chardev *chr,
|
|
|
|
ChardevBackend *backend,
|
|
|
|
bool *be_opened,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
ChardevSocket *sock = backend->u.socket.data;
|
|
|
|
bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
|
|
|
|
bool is_listen = sock->has_server ? sock->server : true;
|
|
|
|
bool is_telnet = sock->has_telnet ? sock->telnet : false;
|
2016-09-23 14:06:11 +08:00
|
|
|
bool is_tn3270 = sock->has_tn3270 ? sock->tn3270 : false;
|
2016-12-12 23:41:00 +08:00
|
|
|
bool is_waitconnect = sock->has_wait ? sock->wait : false;
|
|
|
|
int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
|
|
|
|
QIOChannelSocket *sioc = NULL;
|
2017-04-26 15:36:41 +08:00
|
|
|
SocketAddress *addr;
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
s->is_listen = is_listen;
|
|
|
|
s->is_telnet = is_telnet;
|
2016-09-23 14:06:11 +08:00
|
|
|
s->is_tn3270 = is_tn3270;
|
2016-12-12 23:41:00 +08:00
|
|
|
s->do_nodelay = do_nodelay;
|
|
|
|
if (sock->tls_creds) {
|
|
|
|
Object *creds;
|
|
|
|
creds = object_resolve_path_component(
|
|
|
|
object_get_objects_root(), sock->tls_creds);
|
|
|
|
if (!creds) {
|
|
|
|
error_setg(errp, "No TLS credentials with id '%s'",
|
|
|
|
sock->tls_creds);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
s->tls_creds = (QCryptoTLSCreds *)
|
|
|
|
object_dynamic_cast(creds,
|
|
|
|
TYPE_QCRYPTO_TLS_CREDS);
|
|
|
|
if (!s->tls_creds) {
|
|
|
|
error_setg(errp, "Object with id '%s' is not TLS credentials",
|
|
|
|
sock->tls_creds);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
object_ref(OBJECT(s->tls_creds));
|
|
|
|
if (is_listen) {
|
|
|
|
if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
|
|
|
|
error_setg(errp, "%s",
|
|
|
|
"Expected TLS credentials for server endpoint");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
|
|
|
|
error_setg(errp, "%s",
|
|
|
|
"Expected TLS credentials for client endpoint");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:36:41 +08:00
|
|
|
s->addr = addr = socket_address_flatten(sock->addr);
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
|
2017-03-31 01:43:09 +08:00
|
|
|
/* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
|
2017-04-26 15:36:41 +08:00
|
|
|
if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
|
2016-12-12 23:41:00 +08:00
|
|
|
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* be isn't opened until we get a connection */
|
|
|
|
*be_opened = false;
|
|
|
|
|
2016-12-20 22:32:31 +08:00
|
|
|
update_disconnected_filename(s);
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
if (is_listen) {
|
2016-09-23 14:06:11 +08:00
|
|
|
if (is_telnet || is_tn3270) {
|
2016-12-12 23:41:00 +08:00
|
|
|
s->do_telnetopt = 1;
|
|
|
|
}
|
|
|
|
} else if (reconnect > 0) {
|
|
|
|
s->reconnect_time = reconnect;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:18 +08:00
|
|
|
/* If reconnect_time is set, will do that in chr_machine_done. */
|
|
|
|
if (!s->reconnect_time) {
|
2016-12-12 23:41:00 +08:00
|
|
|
if (s->is_listen) {
|
|
|
|
char *name;
|
2017-12-18 21:54:17 +08:00
|
|
|
s->listener = qio_net_listener_new();
|
2016-12-12 23:41:00 +08:00
|
|
|
|
|
|
|
name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
|
2017-12-18 21:54:17 +08:00
|
|
|
qio_net_listener_set_name(s->listener, name);
|
2016-12-12 23:41:00 +08:00
|
|
|
g_free(name);
|
|
|
|
|
2017-12-18 21:54:17 +08:00
|
|
|
if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) {
|
|
|
|
object_unref(OBJECT(s->listener));
|
|
|
|
s->listener = NULL;
|
2016-12-12 23:41:00 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2016-12-20 22:36:57 +08:00
|
|
|
|
|
|
|
qapi_free_SocketAddress(s->addr);
|
2017-12-18 21:54:17 +08:00
|
|
|
s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
|
2016-12-20 22:36:57 +08:00
|
|
|
update_disconnected_filename(s);
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
if (is_waitconnect &&
|
|
|
|
qemu_chr_wait_connected(chr, errp) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!s->ioc) {
|
2018-03-06 13:33:14 +08:00
|
|
|
qio_net_listener_set_client_func_full(s->listener,
|
|
|
|
tcp_chr_accept,
|
|
|
|
chr, NULL,
|
|
|
|
chr->gcontext);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
} else if (qemu_chr_wait_connected(chr, errp) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (sioc) {
|
|
|
|
object_unref(OBJECT(sioc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
bool is_listen = qemu_opt_get_bool(opts, "server", false);
|
|
|
|
bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
|
|
|
|
bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
|
2016-09-23 14:06:11 +08:00
|
|
|
bool is_tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
|
2016-12-12 23:41:00 +08:00
|
|
|
bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
|
|
|
|
int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
|
|
|
|
const char *path = qemu_opt_get(opts, "path");
|
|
|
|
const char *host = qemu_opt_get(opts, "host");
|
|
|
|
const char *port = qemu_opt_get(opts, "port");
|
char: allow passing pre-opened socket file descriptor at startup
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-21 20:57:54 +08:00
|
|
|
const char *fd = qemu_opt_get(opts, "fd");
|
2016-12-12 23:41:00 +08:00
|
|
|
const char *tls_creds = qemu_opt_get(opts, "tls-creds");
|
2017-04-26 15:36:39 +08:00
|
|
|
SocketAddressLegacy *addr;
|
2016-12-12 23:41:00 +08:00
|
|
|
ChardevSocket *sock;
|
|
|
|
|
char: allow passing pre-opened socket file descriptor at startup
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-21 20:57:54 +08:00
|
|
|
if ((!!path + !!fd + !!host) != 1) {
|
2017-12-22 21:20:40 +08:00
|
|
|
error_setg(errp,
|
char: allow passing pre-opened socket file descriptor at startup
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-21 20:57:54 +08:00
|
|
|
"Exactly one of 'path', 'fd' or 'host' required");
|
2017-12-22 21:20:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
|
2017-12-22 21:20:40 +08:00
|
|
|
if (path) {
|
|
|
|
if (tls_creds) {
|
|
|
|
error_setg(errp, "TLS can only be used over TCP socket");
|
2016-12-12 23:41:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-12-22 21:20:40 +08:00
|
|
|
} else if (host) {
|
2016-12-12 23:41:00 +08:00
|
|
|
if (!port) {
|
|
|
|
error_setg(errp, "chardev: socket: no port given");
|
|
|
|
return;
|
|
|
|
}
|
char: allow passing pre-opened socket file descriptor at startup
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-21 20:57:54 +08:00
|
|
|
} else if (fd) {
|
|
|
|
/* We don't know what host to validate against when in client mode */
|
|
|
|
if (tls_creds && !is_listen) {
|
|
|
|
error_setg(errp, "TLS can not be used with pre-opened client FD");
|
|
|
|
return;
|
|
|
|
}
|
2016-12-12 23:41:00 +08:00
|
|
|
} else {
|
2017-12-22 21:20:40 +08:00
|
|
|
g_assert_not_reached();
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
|
|
|
|
qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
|
|
|
|
|
|
|
|
sock->has_nodelay = true;
|
|
|
|
sock->nodelay = do_nodelay;
|
|
|
|
sock->has_server = true;
|
|
|
|
sock->server = is_listen;
|
|
|
|
sock->has_telnet = true;
|
|
|
|
sock->telnet = is_telnet;
|
2016-09-23 14:06:11 +08:00
|
|
|
sock->has_tn3270 = true;
|
|
|
|
sock->tn3270 = is_tn3270;
|
2016-12-12 23:41:00 +08:00
|
|
|
sock->has_wait = true;
|
|
|
|
sock->wait = is_waitconnect;
|
|
|
|
sock->has_reconnect = true;
|
|
|
|
sock->reconnect = reconnect;
|
|
|
|
sock->tls_creds = g_strdup(tls_creds);
|
|
|
|
|
2017-04-26 15:36:39 +08:00
|
|
|
addr = g_new0(SocketAddressLegacy, 1);
|
2016-12-12 23:41:00 +08:00
|
|
|
if (path) {
|
|
|
|
UnixSocketAddress *q_unix;
|
2017-04-26 15:36:39 +08:00
|
|
|
addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
|
2016-12-12 23:41:00 +08:00
|
|
|
q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
|
|
|
|
q_unix->path = g_strdup(path);
|
2017-12-22 21:20:40 +08:00
|
|
|
} else if (host) {
|
2017-04-26 15:36:39 +08:00
|
|
|
addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
|
2016-12-12 23:41:00 +08:00
|
|
|
addr->u.inet.data = g_new(InetSocketAddress, 1);
|
|
|
|
*addr->u.inet.data = (InetSocketAddress) {
|
|
|
|
.host = g_strdup(host),
|
|
|
|
.port = g_strdup(port),
|
|
|
|
.has_to = qemu_opt_get(opts, "to"),
|
|
|
|
.to = qemu_opt_get_number(opts, "to", 0),
|
|
|
|
.has_ipv4 = qemu_opt_get(opts, "ipv4"),
|
|
|
|
.ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
|
|
|
|
.has_ipv6 = qemu_opt_get(opts, "ipv6"),
|
|
|
|
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
|
|
|
|
};
|
char: allow passing pre-opened socket file descriptor at startup
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-21 20:57:54 +08:00
|
|
|
} else if (fd) {
|
|
|
|
addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
|
|
|
|
addr->u.fd.data = g_new(String, 1);
|
|
|
|
addr->u.fd.data->str = g_strdup(fd);
|
2017-12-22 21:20:40 +08:00
|
|
|
} else {
|
|
|
|
g_assert_not_reached();
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
sock->addr = addr;
|
|
|
|
}
|
|
|
|
|
2016-12-21 19:26:38 +08:00
|
|
|
static void
|
|
|
|
char_socket_get_addr(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(obj);
|
|
|
|
|
|
|
|
visit_type_SocketAddress(v, name, &s->addr, errp);
|
|
|
|
}
|
|
|
|
|
2016-12-21 22:43:46 +08:00
|
|
|
static bool
|
|
|
|
char_socket_get_connected(Object *obj, Error **errp)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(obj);
|
|
|
|
|
|
|
|
return s->connected;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:18 +08:00
|
|
|
static int tcp_chr_machine_done_hook(Chardev *chr)
|
|
|
|
{
|
|
|
|
SocketChardev *s = SOCKET_CHARDEV(chr);
|
|
|
|
|
|
|
|
if (s->reconnect_time) {
|
|
|
|
tcp_chr_connect_async(chr);
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:07:14 +08:00
|
|
|
if (s->ioc && s->tls_creds) {
|
|
|
|
tcp_chr_tls_init(chr);
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:33:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:41:00 +08:00
|
|
|
static void char_socket_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
ChardevClass *cc = CHARDEV_CLASS(oc);
|
|
|
|
|
|
|
|
cc->parse = qemu_chr_parse_socket;
|
|
|
|
cc->open = qmp_chardev_open_socket;
|
|
|
|
cc->chr_wait_connected = tcp_chr_wait_connected;
|
|
|
|
cc->chr_write = tcp_chr_write;
|
|
|
|
cc->chr_sync_read = tcp_chr_sync_read;
|
|
|
|
cc->chr_disconnect = tcp_chr_disconnect;
|
|
|
|
cc->get_msgfds = tcp_get_msgfds;
|
|
|
|
cc->set_msgfds = tcp_set_msgfds;
|
|
|
|
cc->chr_add_client = tcp_chr_add_client;
|
|
|
|
cc->chr_add_watch = tcp_chr_add_watch;
|
|
|
|
cc->chr_update_read_handler = tcp_chr_update_read_handler;
|
2018-03-06 13:33:18 +08:00
|
|
|
cc->chr_machine_done = tcp_chr_machine_done_hook;
|
2016-12-21 19:26:38 +08:00
|
|
|
|
|
|
|
object_class_property_add(oc, "addr", "SocketAddress",
|
|
|
|
char_socket_get_addr, NULL,
|
|
|
|
NULL, NULL, &error_abort);
|
2016-12-21 22:43:46 +08:00
|
|
|
|
|
|
|
object_class_property_add_bool(oc, "connected", char_socket_get_connected,
|
|
|
|
NULL, &error_abort);
|
2016-12-12 23:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo char_socket_type_info = {
|
|
|
|
.name = TYPE_CHARDEV_SOCKET,
|
|
|
|
.parent = TYPE_CHARDEV,
|
|
|
|
.instance_size = sizeof(SocketChardev),
|
|
|
|
.instance_finalize = char_socket_finalize,
|
|
|
|
.class_init = char_socket_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&char_socket_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|