Skip to content

Commit

Permalink
char: introduce a blocking version of qemu_chr_fe_write
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
  • Loading branch information
Anthony Liguori committed Mar 26, 2013
1 parent e769bdc commit cd18720
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/char/char.h
Expand Up @@ -169,6 +169,21 @@ int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
*/
int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len);

/**
* @qemu_chr_fe_write_all:
*
* Write data to a character backend from the front end. This function will
* send data from the front end to the back end. Unlike @qemu_chr_fe_write,
* this function will block if the back end cannot consume all of the data
* attempted to be written.
*
* @buf the data
* @len the number of bytes to send
*
* Returns: the number of bytes consumed
*/
int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len);

/**
* @qemu_chr_fe_ioctl:
*
Expand Down
27 changes: 27 additions & 0 deletions qemu-char.c
Expand Up @@ -140,6 +140,33 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
return s->chr_write(s, buf, len);
}

int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
{
int offset = 0;
int res;

while (offset < len) {
do {
res = s->chr_write(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;
}

return offset;
}

int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
{
if (!s->chr_ioctl)
Expand Down

0 comments on commit cd18720

Please sign in to comment.