From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 09/24] libxl: provide libxl__datacopier_*
Date: Mon, 16 Apr 2012 18:17:51 +0100 [thread overview]
Message-ID: <1334596686-8479-10-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1334596686-8479-1-git-send-email-ian.jackson@eu.citrix.com>
General facility for ao operations to shovel data between fds.
This will be used by the bootloader machinery.
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Changes since v6
* assert that the ao is non-null on _init.
---
tools/libxl/Makefile | 3 +-
tools/libxl/libxl_aoutils.c | 189 ++++++++++++++++++++++++++++++++++++++++++
tools/libxl/libxl_internal.h | 40 +++++++++
3 files changed, 231 insertions(+), 1 deletions(-)
create mode 100644 tools/libxl/libxl_aoutils.c
diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index 5ba144f..6e253b1 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -52,7 +52,8 @@ LIBXL_LIBS += -lyajl
LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
- libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+ libxl_internal.o libxl_utils.o libxl_uuid.o \
+ libxl_json.o libxl_aoutils.o \
libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y)
LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
diff --git a/tools/libxl/libxl_aoutils.c b/tools/libxl/libxl_aoutils.c
new file mode 100644
index 0000000..4c60ad9
--- /dev/null
+++ b/tools/libxl/libxl_aoutils.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2010 Citrix Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * 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 Lesser General Public License for more details.
+ */
+
+#include "libxl_osdeps.h" /* must come before any other headers */
+
+#include "libxl_internal.h"
+
+/*----- data copier -----*/
+
+void libxl__datacopier_init(libxl__datacopier_state *dc)
+{
+ assert(dc->ao);
+ libxl__ev_fd_init(&dc->toread);
+ libxl__ev_fd_init(&dc->towrite);
+ LIBXL_TAILQ_INIT(&dc->bufs);
+}
+
+void libxl__datacopier_kill(libxl__datacopier_state *dc)
+{
+ STATE_AO_GC(dc->ao);
+ libxl__datacopier_buf *buf, *tbuf;
+
+ libxl__ev_fd_deregister(gc, &dc->toread);
+ libxl__ev_fd_deregister(gc, &dc->towrite);
+ LIBXL_TAILQ_FOREACH_SAFE(buf, &dc->bufs, entry, tbuf)
+ free(buf);
+ LIBXL_TAILQ_INIT(&dc->bufs);
+}
+
+static void datacopier_callback(libxl__egc *egc, libxl__datacopier_state *dc,
+ int onwrite, int errnoval)
+{
+ libxl__datacopier_kill(dc);
+ dc->callback(egc, dc, onwrite, errnoval);
+}
+
+static void datacopier_writable(libxl__egc *egc, libxl__ev_fd *ev,
+ int fd, short events, short revents);
+
+static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
+{
+ STATE_AO_GC(dc->ao);
+ int rc;
+
+ if (dc->used) {
+ if (!libxl__ev_fd_isregistered(&dc->towrite)) {
+ rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+ dc->writefd, POLLOUT);
+ if (rc) {
+ LOG(ERROR, "unable to establish write event on %s"
+ " during copy of %s", dc->writewhat, dc->copywhat);
+ datacopier_callback(egc, dc, -1, 0);
+ return;
+ }
+ }
+ } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+ /* we have had eof */
+ datacopier_callback(egc, dc, 0, 0);
+ return;
+ } else {
+ /* nothing buffered, but still reading */
+ libxl__ev_fd_deregister(gc, &dc->towrite);
+ }
+}
+
+static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
+ int fd, short events, short revents) {
+ libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, toread);
+ STATE_AO_GC(dc->ao);
+
+ if (revents & ~POLLIN) {
+ LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN)"
+ " on %s during copy of %s", revents, dc->readwhat, dc->copywhat);
+ datacopier_callback(egc, dc, -1, 0);
+ return;
+ }
+ assert(revents & POLLIN);
+ for (;;) {
+ while (dc->used >= dc->maxsz) {
+ libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+ dc->used -= rm->used;
+ assert(dc->used >= 0);
+ LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+ free(rm);
+ }
+
+ libxl__datacopier_buf *buf =
+ LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+ if (!buf || buf->used >= sizeof(buf->buf)) {
+ buf = malloc(sizeof(*buf));
+ if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+ buf->used = 0;
+ LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+ }
+ int r = read(ev->fd,
+ buf->buf + buf->used,
+ sizeof(buf->buf) - buf->used);
+ if (r < 0) {
+ if (errno == EINTR) continue;
+ if (errno == EWOULDBLOCK) break;
+ LOGE(ERROR, "error reading %s during copy of %s",
+ dc->readwhat, dc->copywhat);
+ datacopier_callback(egc, dc, 0, errno);
+ return;
+ }
+ if (r == 0) {
+ libxl__ev_fd_deregister(gc, &dc->toread);
+ break;
+ }
+ buf->used += r;
+ dc->used += r;
+ assert(buf->used <= sizeof(buf->buf));
+ }
+ datacopier_check_state(egc, dc);
+}
+
+static void datacopier_writable(libxl__egc *egc, libxl__ev_fd *ev,
+ int fd, short events, short revents) {
+ libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, towrite);
+ STATE_AO_GC(dc->ao);
+
+ if (revents & ~POLLOUT) {
+ LOG(ERROR, "unexpected poll event 0x%x (should be POLLOUT)"
+ " on %s during copy of %s", revents, dc->writewhat, dc->copywhat);
+ datacopier_callback(egc, dc, -1, 0);
+ return;
+ }
+ assert(revents & POLLOUT);
+ for (;;) {
+ libxl__datacopier_buf *buf = LIBXL_TAILQ_FIRST(&dc->bufs);
+ if (!buf)
+ break;
+ if (!buf->used) {
+ LIBXL_TAILQ_REMOVE(&dc->bufs, buf, entry);
+ free(buf);
+ continue;
+ }
+ int r = write(ev->fd, buf->buf, buf->used);
+ if (r < 0) {
+ if (errno == EINTR) continue;
+ if (errno == EWOULDBLOCK) break;
+ LOGE(ERROR, "error writing to %s during copy of %s",
+ dc->writewhat, dc->copywhat);
+ datacopier_callback(egc, dc, 1, errno);
+ return;
+ }
+ assert(r > 0);
+ assert(r <= buf->used);
+ buf->used -= r;
+ dc->used -= r;
+ assert(dc->used >= 0);
+ memmove(buf->buf, buf->buf+r, buf->used);
+ }
+ datacopier_check_state(egc, dc);
+}
+
+int libxl__datacopier_start(libxl__datacopier_state *dc)
+{
+ int rc;
+ STATE_AO_GC(dc->ao);
+
+ libxl__datacopier_init(dc);
+
+ rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
+ dc->readfd, POLLIN);
+ if (rc) goto out;
+
+ rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+ dc->writefd, POLLOUT);
+ if (rc) goto out;
+
+ return 0;
+
+ out:
+ libxl__datacopier_kill(dc);
+ return rc;
+}
+
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 6ceb362..20c95db 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -833,6 +833,7 @@ _hidden int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,
*/
_hidden int libxl__try_phy_backend(mode_t st_mode);
+
/* from libxl_pci */
_hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, int starting);
@@ -1458,6 +1459,45 @@ int libxl__carefd_close(libxl__carefd*);
int libxl__carefd_fd(const libxl__carefd*);
+/*----- datacopier: copies data from one fd to another -----*/
+
+typedef struct libxl__datacopier_state libxl__datacopier_state;
+typedef struct libxl__datacopier_buf libxl__datacopier_buf;
+
+/* onwrite==1 means failure happened when writing, logged, errnoval is valid
+ * onwrite==0 means failure happened when reading
+ * errnoval==0 means we got eof and all data was written
+ * errnoval!=0 means we had a read error, logged
+ * onwrite==-1 means some other internal failure, errnoval not valid, logged
+ * in all cases copier is killed before calling this callback */
+typedef void libxl__datacopier_callback(libxl__egc *egc,
+ libxl__datacopier_state *dc, int onwrite, int errnoval);
+
+struct libxl__datacopier_buf {
+ /* private to datacopier */
+ LIBXL_TAILQ_ENTRY(libxl__datacopier_buf) entry;
+ int used;
+ char buf[1000];
+};
+
+struct libxl__datacopier_state {
+ /* caller must fill these in, and they must all remain valid */
+ libxl__ao *ao;
+ int readfd, writefd;
+ ssize_t maxsz;
+ const char *copywhat, *readwhat, *writewhat; /* for error msgs */
+ libxl__datacopier_callback *callback;
+ /* remaining fields are private to datacopier */
+ libxl__ev_fd toread, towrite;
+ ssize_t used;
+ LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
+};
+
+_hidden void libxl__datacopier_init(libxl__datacopier_state *dc);
+_hidden void libxl__datacopier_kill(libxl__datacopier_state *dc);
+_hidden int libxl__datacopier_start(libxl__datacopier_state *dc);
+
+
/*
* Convenience macros.
*/
--
1.7.2.5
next prev parent reply other threads:[~2012-04-16 17:17 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-16 17:17 [PATCH v7 00/24] libxl: subprocess handling Ian Jackson
2012-04-16 17:17 ` [PATCH 01/24] libxl: handle POLLERR, POLLHUP, POLLNVAL properly Ian Jackson
2012-04-16 17:17 ` [PATCH 02/24] libxl: support multiple libxl__ev_fds for the same fd Ian Jackson
2012-04-16 17:17 ` [PATCH 03/24] libxl: event API: new facilities for waiting for subprocesses Ian Jackson
2012-04-16 17:17 ` [PATCH 04/24] autoconf: trim the configure script; use autoheader Ian Jackson
2012-04-17 9:18 ` Ian Campbell
2012-04-17 13:18 ` Ian Jackson
2012-04-17 13:22 ` Ian Campbell
2012-04-17 14:07 ` Roger Pau Monne
2012-04-16 17:17 ` [PATCH 05/24] autoconf: New test for openpty et al Ian Jackson
2012-04-17 9:20 ` Ian Campbell
2012-04-16 17:17 ` [PATCH 06/24] libxl: provide libxl__remove_file " Ian Jackson
2012-04-16 17:17 ` [PATCH 07/24] libxl: Introduce libxl__sendmsg_fds and libxl__recvmsg_fds Ian Jackson
2012-04-16 17:17 ` [PATCH 08/24] libxl: Clean up setdefault in do_domain_create Ian Jackson
2012-04-16 17:17 ` Ian Jackson [this message]
2012-04-17 9:37 ` [PATCH 09/24] libxl: provide libxl__datacopier_* Ian Campbell
2012-04-16 17:17 ` [PATCH 10/24] libxl: provide libxl__openpty_* Ian Jackson
2012-04-16 17:17 ` [PATCH 11/24] libxl: ao: Convert libxl_run_bootloader Ian Jackson
2012-04-16 17:17 ` [PATCH 12/24] libxl: make libxl_create_logfile const-correct Ian Jackson
2012-04-16 17:17 ` [PATCH 13/24] libxl: log bootloader output Ian Jackson
2012-04-16 17:17 ` [PATCH 14/24] libxl: Allow AO_GC and EGC_GC even if not used Ian Jackson
2012-04-16 17:17 ` [PATCH 15/24] libxl: remove ctx->waitpid_instead Ian Jackson
2012-04-17 9:40 ` Ian Campbell
2012-04-17 13:24 ` Ian Jackson
2012-04-16 17:17 ` [PATCH 16/24] libxl: change some structures to unit arrays Ian Jackson
2012-04-17 9:41 ` Ian Campbell
2012-04-16 17:17 ` [PATCH 17/24] libxl: ao: convert libxl__spawn_* Ian Jackson
2012-04-17 15:17 ` Ian Campbell
2012-04-17 17:03 ` Ian Jackson
2012-04-18 10:12 ` Ian Campbell
2012-04-18 10:52 ` Ian Jackson
2012-04-18 11:07 ` Ian Campbell
2012-04-16 17:18 ` [PATCH 18/24] libxl: make libxl_create run bootloader via callback Ian Jackson
2012-04-24 13:46 ` Ian Campbell
2012-04-24 13:54 ` Ian Jackson
2012-04-16 17:18 ` [PATCH 19/24] libxl: provide progress reporting for long-running operations Ian Jackson
2012-04-24 13:59 ` Ian Campbell
2012-04-25 13:38 ` Ian Jackson
2012-04-16 17:18 ` [PATCH 20/24] libxl: remove malloc failure handling from NEW_EVENT Ian Jackson
2012-04-24 14:01 ` Ian Campbell
2012-04-16 17:18 ` [PATCH 21/24] libxl: convert console callback to libxl_asyncprogress_how Ian Jackson
2012-04-24 14:05 ` Ian Campbell
2012-04-16 17:18 ` [PATCH 22/24] libxl: clarify definition of "slow" operation Ian Jackson
2012-04-24 14:06 ` Ian Campbell
2012-04-16 17:18 ` [PATCH 23/24] libxl: child processes cleanups Ian Jackson
2012-04-24 14:12 ` Ian Campbell
2012-04-24 14:27 ` Ian Jackson
2012-04-24 15:05 ` Ian Campbell
2012-04-24 15:07 ` Ian Campbell
2012-04-24 15:22 ` Ian Jackson
2012-04-24 15:32 ` Ian Campbell
2012-04-16 17:18 ` [PATCH 24/24] libxl: aborting bootloader invocation when domain dioes Ian Jackson
2012-04-24 14:18 ` Ian Campbell
2012-04-24 14:38 ` Ian Jackson
2012-04-24 15:08 ` Ian Campbell
2012-04-24 15:26 ` Ian Jackson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1334596686-8479-10-git-send-email-ian.jackson@eu.citrix.com \
--to=ian.jackson@eu.citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).