Patchwork [RFC,v4,02/18] virtagent: base definitions for host/guest RPC server

login
register
mail settings
Submitter Michael Roth
Date Nov. 16, 2010, 4:01 p.m.
Message ID <1289923320-5638-3-git-send-email-mdroth@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/71415/
State New
Headers show

Comments

Michael Roth - Nov. 16, 2010, 4:01 p.m.
Basic skeleton code for RPC server. This is shared by both the
guest-side RPC server as well as the host-side one (the advertised RPCs
for each by guest/host-specific arrays).

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 virtagent-daemon.c |  209 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 virtagent-daemon.h |   22 ++++++
 2 files changed, 231 insertions(+), 0 deletions(-)
 create mode 100644 virtagent-daemon.c
 create mode 100644 virtagent-daemon.h
Jes Sorensen - Nov. 18, 2010, 1:57 p.m.
On 11/16/10 17:01, Michael Roth wrote:
> +#include <syslog.h>
> +#include "qemu_socket.h"
> +#include "virtagent-daemon.h"
> +#include "virtagent-common.h"
> +#include "virtagent.h"
> +
> +static bool va_enable_syslog = false; /* enable syslog'ing of RPCs */
> +
> +#define SLOG(msg, ...) do { \
> +    char msg_buf[1024]; \
> +    if (!va_enable_syslog) { \
> +        break; \
> +    } \
> +    sprintf(msg_buf, msg, ## __VA_ARGS__); \
> +    syslog(LOG_INFO, "virtagent, %s", msg_buf); \
> +} while(0)

You have a potential buffer overflow here, s/sprintf/snprintf/

> +#include "virtproxy.h"
> +
> +#define GUEST_AGENT_SERVICE_ID "virtagent"
> +#define GUEST_AGENT_PATH "/tmp/virtagent-guest.sock"
> +#define HOST_AGENT_SERVICE_ID "virtagent-host"
> +#define HOST_AGENT_PATH "/tmp/virtagent-host.sock"
> +#define VA_GETFILE_MAX 1 << 30
> +#define VA_FILEBUF_LEN 16384
> +
> +int va_server_init(VPDriver *vp_drv, bool is_host);

More stuff which I think should go into a config file.

Jes

Patch

diff --git a/virtagent-daemon.c b/virtagent-daemon.c
new file mode 100644
index 0000000..78d550f
--- /dev/null
+++ b/virtagent-daemon.c
@@ -0,0 +1,209 @@ 
+/*
+ * virt-agent - host/guest RPC daemon functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Adam Litke        <aglitke@linux.vnet.ibm.com>
+ *  Michael Roth      <mdroth@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include <syslog.h>
+#include "qemu_socket.h"
+#include "virtagent-daemon.h"
+#include "virtagent-common.h"
+#include "virtagent.h"
+
+static bool va_enable_syslog = false; /* enable syslog'ing of RPCs */
+
+#define SLOG(msg, ...) do { \
+    char msg_buf[1024]; \
+    if (!va_enable_syslog) { \
+        break; \
+    } \
+    sprintf(msg_buf, msg, ## __VA_ARGS__); \
+    syslog(LOG_INFO, "virtagent, %s", msg_buf); \
+} while(0)
+
+static int va_accept(int listen_fd) {
+    struct sockaddr_in saddr;
+    struct sockaddr *addr;
+    socklen_t len;
+    int fd;
+
+    while (1) {
+        len = sizeof(saddr);
+        addr = (struct sockaddr *)&saddr;
+        fd = qemu_accept(listen_fd, addr, &len);
+        if (fd < 0 && errno != EINTR) {
+            LOG("accept() failed");
+            break;
+        } else if (fd >= 0) {
+            TRACE("accepted connection");
+            break;
+        }
+    }
+    return fd;
+}
+
+typedef struct RPCFunction {
+    xmlrpc_value *(*func)(xmlrpc_env *env, xmlrpc_value *param, void *unused);
+    const char *func_name;
+} RPCFunction;
+
+static RPCFunction guest_functions[] = {
+    { NULL, NULL }
+};
+static RPCFunction host_functions[] = {
+    { NULL, NULL }
+};
+
+static void va_register_functions(xmlrpc_env *env, xmlrpc_registry *registry,
+                                  RPCFunction *list)
+{
+    int i;
+    for (i = 0; list[i].func != NULL; ++i) {
+        TRACE("adding func: %s", list[i].func_name);
+        xmlrpc_registry_add_method(env, registry, NULL, list[i].func_name,
+                                   list[i].func, NULL);
+    }
+}
+
+typedef struct VARPCServerState {
+    VPDriver *vp;
+    int listen_fd;
+    xmlrpc_env env;
+    xmlrpc_registry *registry;
+} VARPCServerState;
+
+/* only one virtagent server instance can exist at a time */
+static VARPCServerState *server_state = NULL;
+
+static void va_accept_handler(void *opaque);
+
+static void va_rpc_send_cb(void *opaque)
+{
+    VARPCData *rpc_data = opaque;
+    VARPCServerState *s = server_state;
+
+    TRACE("called");
+    if (rpc_data->status != VA_RPC_STATUS_OK) {
+        LOG("error sending RPC response");
+    } else {
+        TRACE("RPC completed");
+    }
+
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(s->listen_fd, va_accept_handler, NULL, s);
+}
+
+static void va_rpc_read_cb(void *opaque)
+{
+    VARPCData *rpc_data = opaque;
+    VARPCServerState *s = server_state;
+
+    TRACE("called");
+    if (rpc_data->status != VA_RPC_STATUS_OK) {
+        LOG("error reading RPC request");
+        goto out_bad;
+    }
+
+    rpc_data->send_resp_xml =
+        xmlrpc_registry_process_call(&s->env, s->registry, NULL,
+                                     rpc_data->req_xml, rpc_data->req_xml_len);
+    if (rpc_data->send_resp_xml == NULL) {
+        LOG("error handling RPC request");
+        goto out_bad;
+    }
+
+    rpc_data->cb = va_rpc_send_cb;
+    return;
+
+out_bad:
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(s->listen_fd, va_accept_handler, NULL, s);
+}
+
+static void va_accept_handler(void *opaque)
+{
+    VARPCData *rpc_data;
+    int ret, fd;
+
+    TRACE("called");
+    fd = va_accept(server_state->listen_fd);
+    if (fd < 0) {
+        TRACE("connection error: %s", strerror(errno));
+        return;
+    }
+    ret = fcntl(fd, F_GETFL);
+    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+
+    TRACE("RPC client connected, reading RPC request...");
+    rpc_data = qemu_mallocz(sizeof(VARPCData));
+    rpc_data->cb = va_rpc_read_cb;
+    ret = va_rpc_read_request(rpc_data, fd);
+    if (ret != 0) {
+        LOG("error setting up read handler");
+        qemu_free(rpc_data);
+        return;
+    }
+    vp_set_fd_handler(server_state->listen_fd, NULL, NULL, NULL);
+}
+
+int va_server_init(VPDriver *vp_drv, bool is_host)
+{
+    RPCFunction *func_list = is_host ? host_functions : guest_functions;
+    QemuOpts *opts;
+    int ret, fd;
+    const char *path, *service_id;
+
+    if (server_state) {
+        LOG("virtagent server already initialized");
+        return -1;
+    }
+    va_enable_syslog = !is_host; /* enable logging for guest agent */
+
+    server_state = qemu_mallocz(sizeof(VARPCServerState));
+    service_id = is_host ? HOST_AGENT_SERVICE_ID : GUEST_AGENT_SERVICE_ID;
+    /* TODO: host agent path needs to be made unique amongst multiple
+     * qemu instances
+     */
+    path = is_host ? HOST_AGENT_PATH : GUEST_AGENT_PATH;
+
+    /* setup listening socket for server */
+    opts = qemu_opts_create(qemu_find_opts("net"), "va_server_opts", 0);
+    qemu_opt_set(opts, "path", path);
+    fd = unix_listen_opts(opts);
+    qemu_opts_del(opts);
+    if (fd < 0) {
+        LOG("error setting up listening socket");
+        goto out_bad;
+    }
+
+    /* tell virtproxy to forward incoming virtagent connections to the socket */
+    ret = vp_set_iforward(vp_drv, service_id, path, NULL, false);
+    if (ret < 0) {
+        LOG("error setting up virtproxy iforward");
+        goto out_bad;
+    }
+
+    server_state->vp = vp_drv;
+    server_state->listen_fd = fd;
+    xmlrpc_env_init(&server_state->env);
+    server_state->registry = xmlrpc_registry_new(&server_state->env);
+    va_register_functions(&server_state->env, server_state->registry, func_list);
+
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(server_state->listen_fd, va_accept_handler, NULL,
+                      server_state);
+
+    return 0;
+
+out_bad:
+    qemu_free(server_state);
+    server_state = NULL;
+    return -1;
+}
diff --git a/virtagent-daemon.h b/virtagent-daemon.h
new file mode 100644
index 0000000..6c3436a
--- /dev/null
+++ b/virtagent-daemon.h
@@ -0,0 +1,22 @@ 
+/*
+ * virt-agent - host/guest RPC daemon functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Michael Roth      <mdroth@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "virtproxy.h"
+
+#define GUEST_AGENT_SERVICE_ID "virtagent"
+#define GUEST_AGENT_PATH "/tmp/virtagent-guest.sock"
+#define HOST_AGENT_SERVICE_ID "virtagent-host"
+#define HOST_AGENT_PATH "/tmp/virtagent-host.sock"
+#define VA_GETFILE_MAX 1 << 30
+#define VA_FILEBUF_LEN 16384
+
+int va_server_init(VPDriver *vp_drv, bool is_host);