Patchwork [v2,6/9] qemu-sockets: add socket_listen, socket_connect, socket_parse

login
register
mail settings
Submitter Paolo Bonzini
Date Oct. 1, 2012, 2:52 p.m.
Message ID <1349103144-6827-7-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/188313/
State New
Headers show

Comments

Paolo Bonzini - Oct. 1, 2012, 2:52 p.m.
These are QAPI-friendly versions of the qemu-sockets functions.  They
support IP sockets, Unix sockets, and named file descriptors, using a
QAPI union to dispatch to the correct function.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-sockets.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-tool.c    |  6 ++++
 qemu_socket.h  |  4 +++
 3 file modificati, 105 inserzioni(+)
Luiz Capitulino - Oct. 2, 2012, 12:37 p.m.
On Mon,  1 Oct 2012 16:52:21 +0200
Paolo Bonzini <pbonzini@redhat.com> wrote:

> These are QAPI-friendly versions of the qemu-sockets functions.  They
> support IP sockets, Unix sockets, and named file descriptors, using a
> QAPI union to dispatch to the correct function.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>

> ---
>  qemu-sockets.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  qemu-tool.c    |  6 ++++
>  qemu_socket.h  |  4 +++
>  3 file modificati, 105 inserzioni(+)
> 
> diff --git a/qemu-sockets.c b/qemu-sockets.c
> index f67a113..ed2d2be 100644
> --- a/qemu-sockets.c
> +++ b/qemu-sockets.c
> @@ -22,6 +22,7 @@
>  #include <errno.h>
>  #include <unistd.h>
>  
> +#include "monitor.h"
>  #include "qemu_socket.h"
>  #include "qemu-common.h" /* for qemu_isdigit */
>  #include "main-loop.h"
> @@ -843,6 +844,100 @@ int unix_connect(const char *path, Error **errp)
>  
>  #endif
>  
> +SocketAddress *socket_parse(const char *str, Error **errp)
> +{
> +    SocketAddress *addr = NULL;
> +
> +    addr = g_new(SocketAddress, 1);
> +    if (strstart(str, "unix:", NULL)) {
> +        if (str[5] == '\0') {
> +            error_setg(errp, "invalid Unix socket address\n");
> +            goto fail;
> +        } else {
> +            addr->kind = SOCKET_ADDRESS_KIND_UNIX;
> +            addr->q_unix = g_new(UnixSocketAddress, 1);
> +            addr->q_unix->path = g_strdup(str + 5);
> +        }
> +    } else if (strstart(str, "fd:", NULL)) {
> +        if (str[3] == '\0') {
> +            error_setg(errp, "invalid file descriptor address\n");
> +            goto fail;
> +        } else {
> +            addr->kind = SOCKET_ADDRESS_KIND_FD;
> +            addr->fd = g_new(String, 1);
> +            addr->fd->str = g_strdup(str + 3);
> +        }
> +    } else {
> +        addr->kind = SOCKET_ADDRESS_KIND_INET;
> +        addr->inet = g_new(IPSocketAddress, 1);
> +        addr->inet = inet_parse(str, errp);
> +        if (addr->inet == NULL) {
> +            goto fail;
> +        }
> +    }
> +    return addr;
> +
> +fail:
> +    qapi_free_SocketAddress(addr);
> +    return NULL;
> +}
> +
> +int socket_connect(SocketAddress *addr, Error **errp)
> +{
> +    QemuOpts *opts;
> +    int fd;
> +
> +    opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
> +    switch (addr->kind) {
> +    case SOCKET_ADDRESS_KIND_INET:
> +        inet_addr_to_opts(opts, addr->inet);
> +        fd = inet_connect_opts(opts, true, NULL, errp, NULL);
> +        break;
> +
> +    case SOCKET_ADDRESS_KIND_UNIX:
> +        qemu_opt_set(opts, "path", addr->q_unix->path);
> +        fd = unix_connect_opts(opts, errp);
> +        break;
> +
> +    case SOCKET_ADDRESS_KIND_FD:
> +        fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
> +        break;
> +
> +    default:
> +        abort();
> +    }
> +    qemu_opts_del(opts);
> +    return fd;
> +}
> +
> +int socket_listen(SocketAddress *addr, Error **errp)
> +{
> +    QemuOpts *opts;
> +    int fd;
> +
> +    opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
> +    switch (addr->kind) {
> +    case SOCKET_ADDRESS_KIND_INET:
> +        inet_addr_to_opts(opts, addr->inet);
> +        fd = inet_listen_opts(opts, 0, errp);
> +        break;
> +
> +    case SOCKET_ADDRESS_KIND_UNIX:
> +        qemu_opt_set(opts, "path", addr->q_unix->path);
> +        fd = unix_listen_opts(opts, errp);
> +        break;
> +
> +    case SOCKET_ADDRESS_KIND_FD:
> +        fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
> +        break;
> +
> +    default:
> +        abort();
> +    }
> +    qemu_opts_del(opts);
> +    return fd;
> +}
> +
>  #ifdef _WIN32
>  static void socket_cleanup(void)
>  {
> diff --git a/qemu-tool.c b/qemu-tool.c
> index 18205ba..7658fde 100644
> --- a/qemu-tool.c
> +++ b/qemu-tool.c
> @@ -42,6 +42,12 @@ int monitor_cur_is_qmp(void)
>      return 0;
>  }
>  
> +int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
> +{
> +    error_setg(errp, "only QEMU supports file descriptor passing");
> +    return -1;
> +}
> +
>  void monitor_set_error(Monitor *mon, QError *qerror)
>  {
>  }
> diff --git a/qemu_socket.h b/qemu_socket.h
> index afe8689..cd083d0 100644
> --- a/qemu_socket.h
> +++ b/qemu_socket.h
> @@ -61,6 +61,10 @@ int unix_listen(const char *path, char *ostr, int olen, Error **errp);
>  int unix_connect_opts(QemuOpts *opts, Error **errp);
>  int unix_connect(const char *path, Error **errp);
>  
> +SocketAddress *socket_parse(const char *str, Error **errp);
> +int socket_connect(SocketAddress *addr, Error **errp);
> +int socket_listen(SocketAddress *addr, Error **errp);
> +
>  /* Old, ipv4 only bits.  Don't use for new code. */
>  int parse_host_port(struct sockaddr_in *saddr, const char *str);
>  int socket_init(void);

Patch

diff --git a/qemu-sockets.c b/qemu-sockets.c
index f67a113..ed2d2be 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -22,6 +22,7 @@ 
 #include <errno.h>
 #include <unistd.h>
 
+#include "monitor.h"
 #include "qemu_socket.h"
 #include "qemu-common.h" /* for qemu_isdigit */
 #include "main-loop.h"
@@ -843,6 +844,100 @@  int unix_connect(const char *path, Error **errp)
 
 #endif
 
+SocketAddress *socket_parse(const char *str, Error **errp)
+{
+    SocketAddress *addr = NULL;
+
+    addr = g_new(SocketAddress, 1);
+    if (strstart(str, "unix:", NULL)) {
+        if (str[5] == '\0') {
+            error_setg(errp, "invalid Unix socket address\n");
+            goto fail;
+        } else {
+            addr->kind = SOCKET_ADDRESS_KIND_UNIX;
+            addr->q_unix = g_new(UnixSocketAddress, 1);
+            addr->q_unix->path = g_strdup(str + 5);
+        }
+    } else if (strstart(str, "fd:", NULL)) {
+        if (str[3] == '\0') {
+            error_setg(errp, "invalid file descriptor address\n");
+            goto fail;
+        } else {
+            addr->kind = SOCKET_ADDRESS_KIND_FD;
+            addr->fd = g_new(String, 1);
+            addr->fd->str = g_strdup(str + 3);
+        }
+    } else {
+        addr->kind = SOCKET_ADDRESS_KIND_INET;
+        addr->inet = g_new(IPSocketAddress, 1);
+        addr->inet = inet_parse(str, errp);
+        if (addr->inet == NULL) {
+            goto fail;
+        }
+    }
+    return addr;
+
+fail:
+    qapi_free_SocketAddress(addr);
+    return NULL;
+}
+
+int socket_connect(SocketAddress *addr, Error **errp)
+{
+    QemuOpts *opts;
+    int fd;
+
+    opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+    switch (addr->kind) {
+    case SOCKET_ADDRESS_KIND_INET:
+        inet_addr_to_opts(opts, addr->inet);
+        fd = inet_connect_opts(opts, true, NULL, errp, NULL);
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        qemu_opt_set(opts, "path", addr->q_unix->path);
+        fd = unix_connect_opts(opts, errp);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
+        break;
+
+    default:
+        abort();
+    }
+    qemu_opts_del(opts);
+    return fd;
+}
+
+int socket_listen(SocketAddress *addr, Error **errp)
+{
+    QemuOpts *opts;
+    int fd;
+
+    opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+    switch (addr->kind) {
+    case SOCKET_ADDRESS_KIND_INET:
+        inet_addr_to_opts(opts, addr->inet);
+        fd = inet_listen_opts(opts, 0, errp);
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        qemu_opt_set(opts, "path", addr->q_unix->path);
+        fd = unix_listen_opts(opts, errp);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
+        break;
+
+    default:
+        abort();
+    }
+    qemu_opts_del(opts);
+    return fd;
+}
+
 #ifdef _WIN32
 static void socket_cleanup(void)
 {
diff --git a/qemu-tool.c b/qemu-tool.c
index 18205ba..7658fde 100644
--- a/qemu-tool.c
+++ b/qemu-tool.c
@@ -42,6 +42,12 @@  int monitor_cur_is_qmp(void)
     return 0;
 }
 
+int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
+{
+    error_setg(errp, "only QEMU supports file descriptor passing");
+    return -1;
+}
+
 void monitor_set_error(Monitor *mon, QError *qerror)
 {
 }
diff --git a/qemu_socket.h b/qemu_socket.h
index afe8689..cd083d0 100644
--- a/qemu_socket.h
+++ b/qemu_socket.h
@@ -61,6 +61,10 @@  int unix_listen(const char *path, char *ostr, int olen, Error **errp);
 int unix_connect_opts(QemuOpts *opts, Error **errp);
 int unix_connect(const char *path, Error **errp);
 
+SocketAddress *socket_parse(const char *str, Error **errp);
+int socket_connect(SocketAddress *addr, Error **errp);
+int socket_listen(SocketAddress *addr, Error **errp);
+
 /* Old, ipv4 only bits.  Don't use for new code. */
 int parse_host_port(struct sockaddr_in *saddr, const char *str);
 int socket_init(void);