Patchwork [21/21] qom-chrdrv: add UnixServer

login
register
mail settings
Submitter Anthony Liguori
Date July 25, 2011, 1:44 a.m.
Message ID <1311558293-5855-22-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/106586/
State New
Headers show

Comments

Anthony Liguori - July 25, 2011, 1:44 a.m.
This is roughly equivalent to -chardev socket,path=PATH,server=on

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 chrdrv/Makefile        |    1 +
 chrdrv/Qconfig         |    7 +++
 chrdrv/unixsrv.c       |  104 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/unixsrv.h |   24 +++++++++++
 4 files changed, 136 insertions(+), 0 deletions(-)
 create mode 100644 chrdrv/unixsrv.c
 create mode 100644 include/qemu/unixsrv.h

Patch

diff --git a/chrdrv/Makefile b/chrdrv/Makefile
index 50a3d50..43e516f 100644
--- a/chrdrv/Makefile
+++ b/chrdrv/Makefile
@@ -2,3 +2,4 @@  chrdrv-obj-$(CONFIG_CHRDRV) := chrdrv.o
 chrdrv-obj-$(CONFIG_CHRDRV_MEM) += memchr.o
 chrdrv-obj-$(CONFIG_CHRDRV_SOCKET) += socketchr.o
 chrdrv-obj-$(CONFIG_CHRDRV_TCP_SERVER) += tcpsrv.o
+chrdrv-obj-$(CONFIG_CHRDRV_UNIX_SERVER) += unixsrv.o
diff --git a/chrdrv/Qconfig b/chrdrv/Qconfig
index ea22b50..ae2c20e 100644
--- a/chrdrv/Qconfig
+++ b/chrdrv/Qconfig
@@ -24,3 +24,10 @@  config CHRDRV_TCP_SERVER
        depends on CHRDRV_SOCKET
        help
          Character driver that implements a TCP server transport.
+
+config CHRDRV_UNIX_SERVER
+       bool "UNIX server character driver"
+       default y
+       depends on CHRDRV_SOCKET && UNIX
+       help
+         Character driver that implements a domain socket server transport.
diff --git a/chrdrv/unixsrv.c b/chrdrv/unixsrv.c
new file mode 100644
index 0000000..c4a0621
--- /dev/null
+++ b/chrdrv/unixsrv.c
@@ -0,0 +1,104 @@ 
+#include "qemu/unixsrv.h"
+
+void unix_server_initialize(UnixServer *obj, const char *id)
+{
+    type_initialize(obj, TYPE_UNIX_SERVER, id);
+}
+
+void unix_server_finalize(UnixServer *obj)
+{
+    type_finalize(obj);
+}
+
+const char *unix_server_get_path(UnixServer *obj, Error **errp)
+{
+    return obj->path;
+}
+
+void unix_server_set_path(UnixServer *obj, const char *path, Error **errp)
+{
+    qemu_free(obj->path);
+    obj->path = qemu_strdup(path);
+
+    socket_server_rebind(SOCKET_SERVER(obj));
+}
+
+static int unix_server_make_listen_socket(SocketServer *ss)
+{
+    UnixServer *s = UNIX_SERVER(ss);
+    struct sockaddr_un addr;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
+    if (fd == -1) {
+        return -1;
+    }
+
+    addr.sun_family = AF_UNIX;
+    snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", s->path);
+
+    ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
+    if (ret == -1) {
+        closesocket(fd);
+        return -1;
+    }
+
+    listen(fd, 1);
+
+    return fd;
+}
+
+static int unix_server_accept(SocketServer *ss)
+{
+    int fd;
+
+    do {
+        struct sockaddr_un addr;
+        socklen_t addrlen = sizeof(addr);
+        fd = qemu_accept(ss->listen_fd, (struct sockaddr *)&addr, &addrlen);
+    } while (fd == -1 && errno == EINTR);
+
+    return fd;
+}
+
+static void unix_server_init(TypeInstance *inst)
+{
+    UnixServer *s = UNIX_SERVER(inst);
+
+    plug_add_property_str(PLUG(s), "path",
+                          (PlugPropertyGetterStr *)unix_server_get_path,
+                          (PlugPropertySetterStr *)unix_server_set_path,
+                          PROP_F_READWRITE);
+}
+
+static void unix_server_fini(TypeInstance *inst)
+{
+    UnixServer *s = UNIX_SERVER(inst);
+
+    qemu_free(s->path);
+}
+
+static void unix_server_class_init(TypeClass *class)
+{
+    SocketServerClass *ssc = SOCKET_SERVER_CLASS(class);
+
+    ssc->accept = unix_server_accept;
+    ssc->make_listen_socket = unix_server_make_listen_socket;
+}
+
+static TypeInfo unix_server_type_info = {
+    .name = TYPE_UNIX_SERVER,
+    .parent = TYPE_SOCKET_SERVER,
+    .instance_size = sizeof(UnixServer),
+    .instance_init = unix_server_init,
+    .instance_finalize = unix_server_fini,
+    .class_init = unix_server_class_init,
+};
+
+static void register_backends(void)
+{
+    type_register_static(&unix_server_type_info);
+}
+
+device_init(register_backends);
diff --git a/include/qemu/unixsrv.h b/include/qemu/unixsrv.h
new file mode 100644
index 0000000..71182bc
--- /dev/null
+++ b/include/qemu/unixsrv.h
@@ -0,0 +1,24 @@ 
+#ifndef CHAR_DRIVER_UNIX_H
+#define CHAR_DRIVER_UNIX_H
+
+#include "qemu/socketchr.h"
+#include "qemu_socket.h"
+
+typedef struct UnixServer
+{
+    SocketServer parent;
+
+    /* private */
+    char *path;
+} UnixServer;
+
+#define TYPE_UNIX_SERVER "unix-server"
+#define UNIX_SERVER(obj) TYPE_CHECK(UnixServer, obj, TYPE_UNIX_SERVER)
+
+void unix_server_initialize(UnixServer *obj, const char *id);
+void unix_server_finalize(UnixServer *obj);
+
+const char *unix_server_get_path(UnixServer *obj, Error **errp);
+void unix_server_set_path(UnixServer *obj, const char *value, Error **errp);
+
+#endif