[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL v2 044/106] Add chardev API qemu_chr_fe_read_all
From: |
Michael S. Tsirkin |
Subject: |
[Qemu-devel] [PULL v2 044/106] Add chardev API qemu_chr_fe_read_all |
Date: |
Wed, 18 Jun 2014 19:18:18 +0300 |
From: Nikolay Nikolaev <address@hidden>
This function will attempt to read data from the chardev trying
to fill the buffer up to the given length.
Add tcp_chr_disconnect to reuse disconnection code where needed.
Signed-off-by: Antonios Motakis <address@hidden>
Signed-off-by: Nikolay Nikolaev <address@hidden>
Reviewed-by: Michael S. Tsirkin <address@hidden>
Signed-off-by: Michael S. Tsirkin <address@hidden>
---
include/sysemu/char.h | 14 +++++++++
qemu-char.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 87 insertions(+), 10 deletions(-)
diff --git a/include/sysemu/char.h b/include/sysemu/char.h
index 7f5eeb3..5281123 100644
--- a/include/sysemu/char.h
+++ b/include/sysemu/char.h
@@ -56,6 +56,8 @@ typedef void IOEventHandler(void *opaque, int event);
struct CharDriverState {
void (*init)(struct CharDriverState *s);
int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
+ int (*chr_sync_read)(struct CharDriverState *s,
+ const uint8_t *buf, int len);
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
void (*chr_update_read_handler)(struct CharDriverState *s);
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
@@ -189,6 +191,18 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t
*buf, int len);
int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len);
/**
+ * @qemu_chr_fe_read_all:
+ *
+ * Read data to a buffer from the back end.
+ *
+ * @buf the data buffer
+ * @len the number of bytes to read
+ *
+ * Returns: the number of bytes read
+ */
+int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len);
+
+/**
* @qemu_chr_fe_ioctl:
*
* Issue a device specific ioctl to a backend.
diff --git a/qemu-char.c b/qemu-char.c
index f918f90..8fb11c8 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -84,6 +84,7 @@
#include "ui/qemu-spice.h"
#define READ_BUF_LEN 4096
+#define READ_RETRIES 10
/***********************************************************/
/* character device */
@@ -145,6 +146,41 @@ int qemu_chr_fe_write_all(CharDriverState *s, const
uint8_t *buf, int len)
return offset;
}
+int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
+{
+ int offset = 0, counter = 10;
+ int res;
+
+ if (!s->chr_sync_read) {
+ return 0;
+ }
+
+ while (offset < len) {
+ do {
+ res = s->chr_sync_read(s, buf + offset, len - offset);
+ if (res == -1 && errno == EAGAIN) {
+ g_usleep(100);
+ }
+ } while (res == -1 && errno == EAGAIN);
+
+ if (res == 0) {
+ break;
+ }
+
+ if (res < 0) {
+ return res;
+ }
+
+ offset += res;
+
+ if (!counter--) {
+ break;
+ }
+ }
+
+ return offset;
+}
+
int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
{
if (!s->chr_ioctl)
@@ -2454,6 +2490,23 @@ static GSource *tcp_chr_add_watch(CharDriverState *chr,
GIOCondition cond)
return g_io_create_watch(s->chan, cond);
}
+static void tcp_chr_disconnect(CharDriverState *chr)
+{
+ TCPCharDriver *s = chr->opaque;
+
+ s->connected = 0;
+ if (s->listen_chan) {
+ s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
+ tcp_chr_accept, chr);
+ }
+ remove_fd_in_watch(chr);
+ g_io_channel_unref(s->chan);
+ s->chan = NULL;
+ closesocket(s->fd);
+ s->fd = -1;
+ qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
+}
+
static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
{
CharDriverState *chr = opaque;
@@ -2470,16 +2523,7 @@ static gboolean tcp_chr_read(GIOChannel *chan,
GIOCondition cond, void *opaque)
size = tcp_chr_recv(chr, (void *)buf, len);
if (size == 0) {
/* connection closed */
- s->connected = 0;
- if (s->listen_chan) {
- s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
tcp_chr_accept, chr);
- }
- remove_fd_in_watch(chr);
- g_io_channel_unref(s->chan);
- s->chan = NULL;
- closesocket(s->fd);
- s->fd = -1;
- qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
+ tcp_chr_disconnect(chr);
} else if (size > 0) {
if (s->do_telnetopt)
tcp_chr_process_IAC_bytes(chr, s, buf, &size);
@@ -2490,6 +2534,24 @@ static gboolean tcp_chr_read(GIOChannel *chan,
GIOCondition cond, void *opaque)
return TRUE;
}
+static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
+{
+ TCPCharDriver *s = chr->opaque;
+ int size;
+
+ if (!s->connected) {
+ return 0;
+ }
+
+ size = tcp_chr_recv(chr, (void *) buf, len);
+ if (size == 0) {
+ /* connection closed */
+ tcp_chr_disconnect(chr);
+ }
+
+ return size;
+}
+
#ifndef _WIN32
CharDriverState *qemu_chr_open_eventfd(int eventfd)
{
@@ -2678,6 +2740,7 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd,
bool do_nodelay,
chr->opaque = s;
chr->chr_write = tcp_chr_write;
+ chr->chr_sync_read = tcp_chr_sync_read;
chr->chr_close = tcp_chr_close;
chr->get_msgfd = tcp_get_msgfd;
chr->chr_add_client = tcp_chr_add_client;
--
MST
- [Qemu-devel] [PULL v2 021/106] trace: add acpi memory hotplug IO region events, (continued)
- [Qemu-devel] [PULL v2 021/106] trace: add acpi memory hotplug IO region events, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 022/106] trace: pc: add PC_DIMM slot & address allocation, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 023/106] acpi:piix4: allow plug/unlug callbacks handle not only PCI devices, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 025/106] pc: ich9 lpc: make it work with global/compat properties, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 024/106] acpi:piix4: add memory hotplug handling, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 026/106] acpi:ich9: add memory hotplug handling, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 027/106] pc: migrate piix4 & ich9 MemHotplugState, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 033/106] pc: ACPI BIOS: make GPE.3 handle memory hotplug event on PIIX and Q35 machines, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 042/106] virtio-net: announce self by guest, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 043/106] Add kvm_eventfds_enabled function, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 044/106] Add chardev API qemu_chr_fe_read_all,
Michael S. Tsirkin <=
- [Qemu-devel] [PULL v2 045/106] Add chardev API qemu_chr_fe_set_msgfds, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 046/106] Add chardev API qemu_chr_fe_get_msgfds, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 052/106] Add vhost_ops to vhost_dev struct and replace all relevant ioctls, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 053/106] Add vhost-backend and VhostBackendType, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 054/106] Add vhost-user as a vhost backend., Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 055/106] vhost-net: vhost-user feature bits support, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 056/106] Add new vhost-user netdev backend, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 057/106] Add the vhost-user netdev backend to the command line, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 058/106] Add vhost-user protocol documentation, Michael S. Tsirkin, 2014/06/18
- [Qemu-devel] [PULL v2 059/106] libqemustub: add stubs to be able to use qemu-char.c, Michael S. Tsirkin, 2014/06/18