xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Thanos Makatos <thanos.makatos@citrix.com>
To: xen-devel@lists.xensource.com
Cc: thanos.makatos@citrix.com
Subject: [PATCH 2 of 7 v2] blktap3/tapback: Introduces functionality required to access XenStore
Date: Fri, 4 Jan 2013 12:14:02 +0000	[thread overview]
Message-ID: <efd3f343f7c7bb24194d.1357301642@makatos-desktop> (raw)
In-Reply-To: <patchbomb.1357301640@makatos-desktop>

This patch introduces convenience functions that read/write values from/to
XenStore.

diff -r 0777319f0a6e -r efd3f343f7c7 tools/blktap3/tapback/xenstore.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/blktap3/tapback/xenstore.c	Fri Jan 04 11:54:51 2013 +0000
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2012      Citrix Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+ * USA.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <xenstore.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "blktap3.h"
+#include "tapback.h"
+#include "xenstore.h"
+
+/**
+ * Prints the supplied arguments to a buffer and returns it. The buffer must
+ * be deallocated by the user.
+ */
+static char *
+vmprintf(const char * const fmt, va_list ap)
+{
+    char *s = NULL;
+
+    if (vasprintf(&s, fmt, ap) < 0)
+        s = NULL;
+
+    return s;
+}
+
+__printf(1, 2)
+char *
+mprintf(const char * const fmt, ...)
+{
+    va_list ap;
+    char *s = NULL;
+
+    va_start(ap, fmt);
+    s = vmprintf(fmt, ap);
+    va_end(ap);
+
+    return s;
+}
+
+char *
+tapback_xs_vread(struct xs_handle * const xs, xs_transaction_t xst,
+        const char * const fmt, va_list ap)
+{
+    char *path = NULL, *data = NULL;
+    unsigned int len = 0;
+
+    assert(xs);
+
+    path = vmprintf(fmt, ap);
+    data = xs_read(xs, xst, path, &len);
+    free(path);
+
+    return data;
+}
+
+__printf(3, 4)
+char *
+tapback_xs_read(struct xs_handle * const xs, xs_transaction_t xst,
+        const char * const fmt, ...)
+{
+    va_list ap;
+    char *s = NULL;
+
+    assert(xs);
+
+    va_start(ap, fmt);
+    s = tapback_xs_vread(xs, xst, fmt, ap);
+    va_end(ap);
+
+    return s;
+}
+
+char *
+tapback_device_read(const vbd_t * const device, const char * const path)
+{
+    assert(device);
+    assert(path);
+
+    return tapback_xs_read(blktap3_daemon.xs, blktap3_daemon.xst, "%s/%d/%s/%s",
+            BLKTAP3_BACKEND_PATH, device->domid, device->name, path);
+}
+
+char *
+tapback_device_read_otherend(vbd_t * const device,
+        const char * const path)
+{
+    assert(device);
+    assert(path);
+
+    return tapback_xs_read(blktap3_daemon.xs, blktap3_daemon.xst, "%s/%s",
+            device->frontend_path, path);
+}
+
+__scanf(3, 4)
+int
+tapback_device_scanf_otherend(vbd_t * const device,
+        const char * const path, const char * const fmt, ...)
+{
+    va_list ap;
+    int n = 0;
+    char *s = NULL;
+
+    assert(device);
+    assert(path);
+
+    if (!(s = tapback_device_read_otherend(device, path)))
+        return -1;
+    va_start(ap, fmt);
+    n = vsscanf(s, fmt, ap);
+    free(s);
+    va_end(ap);
+
+    return n;
+}
+
+__printf(4, 5)
+int
+tapback_device_printf(vbd_t * const device, const char * const key,
+        const bool mkread, const char * const fmt, ...)
+{
+    va_list ap;
+    int err = 0;
+    char *path = NULL, *val = NULL;
+    bool nerr = false;
+
+    assert(device);
+    assert(key);
+
+    if (!(path = mprintf("%s/%d/%s/%s", BLKTAP3_BACKEND_PATH, device->domid,
+                    device->name, key))) {
+        err = -errno;
+        goto fail;
+    }
+
+    va_start(ap, fmt);
+    val = vmprintf(fmt, ap);
+    va_end(ap);
+
+    if (!val) {
+        err = -errno;
+        goto fail;
+    }
+
+    if (!(nerr = xs_write(blktap3_daemon.xs, blktap3_daemon.xst, path, val,
+                    strlen(val)))) {
+        err = -errno;
+        goto fail;
+    }
+
+    if (mkread) {
+        struct xs_permissions perms = {
+            device->domid,
+            XS_PERM_READ
+        };
+
+        if (!(nerr = xs_set_permissions(blktap3_daemon.xs, blktap3_daemon.xst,
+                        path, &perms, 1))) {
+            err = -errno;
+            goto fail;
+        }
+    }
+
+fail:
+    free(path);
+    free(val);
+
+    return err;
+}
diff -r 0777319f0a6e -r efd3f343f7c7 tools/blktap3/tapback/xenstore.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/blktap3/tapback/xenstore.h	Fri Jan 04 11:54:51 2013 +0000
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2012      Citrix Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+ * USA.
+ */
+
+/**
+ * Prints the supplied arguments to a buffer and returns it. The buffer must
+ * be deallocated by the user.
+ */
+__printf(1, 2)
+char *
+mprintf(const char * const fmt, ...);
+
+/**
+ * Retrieves the XenStore value of the specified key of the VBD's front-end.
+ * The caller must free the returned buffer.
+ *
+ * @param device the VBD
+ * @param path key under the front-end directory
+ * @returns a buffer containing the value, or NULL on error
+ */
+char *
+tapback_device_read_otherend(vbd_t * const device,
+        const char * const path);
+
+/**
+ * Writes to XenStore backened/tapback/<domid>/<devname>/@key = @fmt.
+ *
+ * @param device the VBD
+ * @param key the key to write to
+ * @param mkread TODO
+ * @param fmt format
+ * @returns 0 on success, an error code otherwise
+ */
+__printf(4, 5)
+int
+tapback_device_printf(vbd_t * const device, const char * const key,
+        const bool mkread, const char * const fmt, ...);
+
+/**
+ * Reads the specified XenStore path under the front-end directory in a
+ * scanf-like manner.
+ *
+ * @param device the VBD
+ * @param path XenStore path to read
+ * @param fmt format
+ */
+__scanf(3, 4)
+int
+tapback_device_scanf_otherend(vbd_t * const device,
+        const char * const path, const char * const fmt, ...);
+
+/**
+ * Retrieves the value of the specified of the device from XenStore,
+ * i.e. backend/tapback/<domid>/<devname>/@path
+ * The caller must free the returned buffer.
+ *
+ * @param device the VBD 
+ * @param path the XenStore key
+ * @returns a buffer containing the value, or NULL on error
+ */
+char *
+tapback_device_read(const vbd_t * const device, const char * const path);
+
+/**
+ * Reads the specified XenStore path. The caller must free the returned buffer.
+ *
+ * @param xs handle to XenStore
+ * @param xst XenStore transaction 
+ * @param fmt format
+ * @param ap arguments
+ * @returns a buffer containing the value, or NULL on error
+ */
+char *
+tapback_xs_vread(struct xs_handle * const xs, xs_transaction_t xst,
+        const char * const fmt, va_list ap);
+
+/**
+ * Reads the specified XenStore path. The caller must free the returned buffer.
+ *
+ * @param xs handle to XenStore
+ * @param xst XenStore transaction
+ * @param fmt format
+ * @returns a buffer containing the value, or NULL on error
+ */
+__printf(3, 4)
+char *
+tapback_xs_read(struct xs_handle * const xs, xs_transaction_t xst,
+        const char * const fmt, ...);

  parent reply	other threads:[~2013-01-04 12:14 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-04 12:14 [PATCH 0 of 7 v2] blktap3: Introduce the tapback daemon (most of blkback in user-space) Thanos Makatos
2013-01-04 12:14 ` [PATCH 1 of 7 v2] blktap3/tapback: Introduce core defines and structure definitions Thanos Makatos
2013-01-18 15:28   ` Ian Campbell
2013-01-25 13:25     ` Thanos Makatos
2013-01-04 12:14 ` Thanos Makatos [this message]
2013-01-18 16:08   ` [PATCH 2 of 7 v2] blktap3/tapback: Introduces functionality required to access XenStore Ian Campbell
2013-01-25 14:03     ` Thanos Makatos
2013-01-25 14:13       ` Ian Campbell
2013-01-04 12:14 ` [PATCH 3 of 7 v2] blktap3/tapback: Logging for the tapback daemon and libxenio Thanos Makatos
2013-01-04 12:14 ` [PATCH 4 of 7 v2] blktap3/tapback: Introduce back-end XenStore path handler Thanos Makatos
2013-01-18 17:07   ` Ian Campbell
2013-01-25 14:31     ` Thanos Makatos
2013-01-25 14:38       ` Ian Campbell
2013-01-04 12:14 ` [PATCH 5 of 7 v2] blktap3/tapback: Introduce front-end " Thanos Makatos
2013-01-18 17:17   ` Ian Campbell
2013-01-25 15:09     ` Thanos Makatos
2013-01-25 16:15       ` Ian Campbell
2013-01-04 12:14 ` [PATCH 6 of 7 v2] blktap3/tapback: Introduce the tapback daemon Thanos Makatos
2013-01-04 12:14 ` [PATCH 7 of 7 v2] blktap3/tapback: Introduce tapback daemon Makefile Thanos Makatos
2013-01-18 17:21   ` Ian Campbell

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=efd3f343f7c7bb24194d.1357301642@makatos-desktop \
    --to=thanos.makatos@citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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).