Skip to contentSkip to conversation

chardev cleanups

PATCH In review [email protected] 6 participants 19 messages Jun 16, 2016 – Jun 29, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover chardev cleanups

From: Marc-André Lureau <[email protected]>

Hi

A small series to do some chardev cleanup when removing them and leaving qemu.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

v1->v2:
- use atexit() for qemu_chr_cleanup()
- add missing braces

Marc-André Lureau (3):
char: clean up remaining chardevs when leaving
socket: add listen feature
socket: unlink unix socket on remove

 include/io/channel.h           |  1 +
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 17 +++++++++++++++++
 qemu-char.c                    | 11 +++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 6 files changed, 49 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/3 char: clean up remaining chardevs when leaving

From: Marc-André Lureau <[email protected]>

This helps to remove various chardev resources leaks when leaving qemu.

---
 qemu-char.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
qemu-char.c +11 -0
index c926e9a..98dcd49 100644
@@ -4541,6 +4541,15 @@ void qmp_chardev_remove(const char *id, Error **errp)
     qemu_chr_delete(chr);
 }
 
+static void qemu_chr_cleanup(void)
+{
+    CharDriverState *chr;
+
+    QTAILQ_FOREACH(chr, &chardevs, next) {
+        qemu_chr_delete(chr);
+    }
+}
+
 static void register_types(void)
 {
     register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
@@ -4587,6 +4596,8 @@ static void register_types(void)
      * is specified
      */
     qemu_add_machine_init_done_notifier(&muxes_realize_notify);
+
+    atexit(qemu_chr_cleanup);
 }
 
 type_init(register_types);
Signature · 1 line
2.7.4
Reviewed-by on v2 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] char: clean up remaining chardevs when leaving

Quoted 8 lines · On Thu, Jun 16, 2016 at 09:28:50PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

This helps to remove various chardev resources leaks when leaving qemu.

---
 qemu-char.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] char: clean up remaining chardevs when leaving

In reply to [email protected]
Quoted 15 lines · On 16/06/2016 21:28, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

This helps to remove various chardev resources leaks when leaving qemu.

---
 qemu-char.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
qemu-char.c +0 -0
index c926e9a..98dcd49 100644
@@ -4541,6 +4541,15 @@ void qmp_chardev_remove(const char *id, Error **errp)
     qemu_chr_delete(chr);

qemu-char.c void qmp_chardev_remove(const char *id, Error **errp) · line 4551

Quoted 10 lines
qemu-char.c +8 -0
 }
 
+static void qemu_chr_cleanup(void)
+{
+    CharDriverState *chr;
+
+    QTAILQ_FOREACH(chr, &chardevs, next) {
+        qemu_chr_delete(chr);
+    }
+}

FYI, this patch is necessary on top:

qemu-char.c +2 -2
index 016badb..bc04ced 100644
@@ -4551,9 +4551,9 @@
 
 static void qemu_chr_cleanup(void)
 {
-    CharDriverState *chr;
+    CharDriverState *chr, *tmp;
 
-    QTAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) {
         qemu_chr_delete(chr);
     }
 }

(Reproducer: start QEMU with MALLOC_PERTURB_=42 and type "quit" on the monitor).

Thanks,

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] char: clean up remaining chardevs when leaving

Hi

Quoted 38 lines · Paolo Bonzini
Quoted 15 lines · On 16/06/2016 21:28, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

This helps to remove various chardev resources leaks when leaving qemu.

---
 qemu-char.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
qemu-char.c +0 -0
index c926e9a..98dcd49 100644
@@ -4541,6 +4541,15 @@ void qmp_chardev_remove(const char *id, Error **errp)
     qemu_chr_delete(chr);

qemu-char.c void qmp_chardev_remove(const char *id, Error **errp) · line 4551

Quoted 10 lines
qemu-char.c +8 -0
 }
 
+static void qemu_chr_cleanup(void)
+{
+    CharDriverState *chr;
+
+    QTAILQ_FOREACH(chr, &chardevs, next) {
+        qemu_chr_delete(chr);
+    }
+}

FYI, this patch is necessary on top:

qemu-char.c +0 -0
index 016badb..bc04ced 100644
@@ -4551,9 +4551,9 @@
 

qemu-char.c line 4559

Quoted 10 lines
qemu-char.c +2 -2
 static void qemu_chr_cleanup(void)
 {
-    CharDriverState *chr;
+    CharDriverState *chr, *tmp;
 
-    QTAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) {
         qemu_chr_delete(chr);
     }
 }

ack, I guess you'll have it in your pull request? squash or seperate, anyhow.

thanks

Signature · 1 line
Marc-André Lureau
#5 lore raw

PATCH v2 2/3 socket: add listen feature

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add a flag to tell whether the channel socket is listening.

---
 include/io/channel.h | 1 +
 io/channel-socket.c  | 7 +++++++
 2 files changed, 8 insertions(+)
include/io/channel.h +1 -0
index d37acd2..e52f059 100644
@@ -42,6 +42,7 @@ typedef enum QIOChannelFeature QIOChannelFeature;
 enum QIOChannelFeature {
     QIO_CHANNEL_FEATURE_FD_PASS  = (1 << 0),
     QIO_CHANNEL_FEATURE_SHUTDOWN = (1 << 1),
+    QIO_CHANNEL_FEATURE_LISTEN   = (1 << 2),
 };
 
 
io/channel-socket.c +7 -0
index ca8bc20..1cd5848 100644
@@ -71,6 +71,9 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,
                           int fd,
                           Error **errp)
 {
+    int val;
+    socklen_t len = sizeof(val);
+
     if (sioc->fd != -1) {
         error_setg(errp, "Socket is already open");
         return -1;
@@ -106,6 +109,10 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,
         ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
     }
 #endif /* WIN32 */
+    if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == 0 && val) {
+        QIOChannel *ioc = QIO_CHANNEL(sioc);
+        ioc->features |= (1 << QIO_CHANNEL_FEATURE_LISTEN);
+    }
 
     return 0;
 
Signature · 1 line
2.7.4
Reviewed-by on v2 2/3
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] socket: add listen feature

Quoted 9 lines · On Thu, Jun 16, 2016 at 09:28:51PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Add a flag to tell whether the channel socket is listening.

---
 include/io/channel.h | 1 +
 io/channel-socket.c  | 7 +++++++
 2 files changed, 8 insertions(+)

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#7 lore raw

PATCH v2 3/3 socket: unlink unix socket on remove

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
include/qemu/sockets.h +1 -0
index 1bd9218..5dd2648 100644
@@ -51,6 +51,7 @@ SocketAddress *socket_parse(const char *str, Error **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
io/channel-socket.c +10 -0
index 1cd5848..6ec87f8 100644
@@ -400,7 +400,17 @@ static void qio_channel_socket_init(Object *obj)
 static void qio_channel_socket_finalize(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
+
     if (ioc->fd != -1) {
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
+            Error *err = NULL;
+
+            socket_listen_cleanup(ioc->fd, &err);
+            if (err) {
+                error_report_err(err);
+                err = NULL;
+            }
+        }
 #ifdef WIN32
         WSAEventSelect(ioc->fd, NULL, 0);
 #endif
tests/test-io-channel-socket.c +1 -1
index 855306b..f73e063 100644
@@ -383,7 +383,7 @@ static void test_io_channel_unix(bool async)
 
     qapi_free_SocketAddress(listen_addr);
     qapi_free_SocketAddress(connect_addr);
-    unlink(TEST_SOCKET);
+    g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS) == FALSE);
 }
 
 
util/qemu-sockets.c +18 -0
index 0d6cd1f..5d03695 100644
@@ -997,6 +997,24 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+void socket_listen_cleanup(int fd, Error **errp)
+{
+    SocketAddress *addr;
+
+    addr = socket_local_address(fd, errp);
+
+    if (addr->type == SOCKET_ADDRESS_KIND_UNIX
+        && addr->u.q_unix.data->path) {
+        if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno,
+                             "Failed to unlink socket %s",
+                             addr->u.q_unix.data->path);
+        }
+    }
+
+    g_free(addr);
+}
+
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
 {
     int fd;
Signature · 1 line
2.7.4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 9 lines · On Thu, Jun 16, 2016 at 09:28:52PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

We normally only unlink what we created.

unlinking files we didn't create
looks like a silent change that might easily break existing users.

Quoted 86 lines
---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
include/qemu/sockets.h +1 -0
index 1bd9218..5dd2648 100644
@@ -51,6 +51,7 @@ SocketAddress *socket_parse(const char *str, Error **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
io/channel-socket.c +10 -0
index 1cd5848..6ec87f8 100644
@@ -400,7 +400,17 @@ static void qio_channel_socket_init(Object *obj)
 static void qio_channel_socket_finalize(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
+
     if (ioc->fd != -1) {
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
+            Error *err = NULL;
+
+            socket_listen_cleanup(ioc->fd, &err);
+            if (err) {
+                error_report_err(err);
+                err = NULL;
+            }
+        }
 #ifdef WIN32
         WSAEventSelect(ioc->fd, NULL, 0);
 #endif
tests/test-io-channel-socket.c +1 -1
index 855306b..f73e063 100644
@@ -383,7 +383,7 @@ static void test_io_channel_unix(bool async)
 
     qapi_free_SocketAddress(listen_addr);
     qapi_free_SocketAddress(connect_addr);
-    unlink(TEST_SOCKET);
+    g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS) == FALSE);
 }
 
 
util/qemu-sockets.c +18 -0
index 0d6cd1f..5d03695 100644
@@ -997,6 +997,24 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+void socket_listen_cleanup(int fd, Error **errp)
+{
+    SocketAddress *addr;
+
+    addr = socket_local_address(fd, errp);
+
+    if (addr->type == SOCKET_ADDRESS_KIND_UNIX
+        && addr->u.q_unix.data->path) {
+        if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno,
+                             "Failed to unlink socket %s",
+                             addr->u.q_unix.data->path);
+        }
+    }
+
+    g_free(addr);
+}
+
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
 {
     int fd;
Signature · 1 line
2.7.4
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · On Thu, Jun 16, 2016 at 09:28:52PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

Quoted 7 lines
Quoted 1 line

We normally only unlink what we created.

unlinking files we didn't create
looks like a silent change that might easily break existing users.

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Quoted 87 lines
Quoted 87 lines
---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
include/qemu/sockets.h +1 -0
index 1bd9218..5dd2648 100644
@@ -51,6 +51,7 @@ SocketAddress *socket_parse(const char *str, Error **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
io/channel-socket.c +10 -0
index 1cd5848..6ec87f8 100644
@@ -400,7 +400,17 @@ static void qio_channel_socket_init(Object *obj)
 static void qio_channel_socket_finalize(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
+
     if (ioc->fd != -1) {
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
+            Error *err = NULL;
+
+            socket_listen_cleanup(ioc->fd, &err);
+            if (err) {
+                error_report_err(err);
+                err = NULL;
+            }
+        }
 #ifdef WIN32
         WSAEventSelect(ioc->fd, NULL, 0);
 #endif
tests/test-io-channel-socket.c +1 -1
index 855306b..f73e063 100644
@@ -383,7 +383,7 @@ static void test_io_channel_unix(bool async)
 
     qapi_free_SocketAddress(listen_addr);
     qapi_free_SocketAddress(connect_addr);
-    unlink(TEST_SOCKET);
+    g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS) == FALSE);
 }
 
 
util/qemu-sockets.c +18 -0
index 0d6cd1f..5d03695 100644
@@ -997,6 +997,24 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+void socket_listen_cleanup(int fd, Error **errp)
+{
+    SocketAddress *addr;
+
+    addr = socket_local_address(fd, errp);
+
+    if (addr->type == SOCKET_ADDRESS_KIND_UNIX
+        && addr->u.q_unix.data->path) {
+        if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno,
+                             "Failed to unlink socket %s",
+                             addr->u.q_unix.data->path);
+        }
+    }
+
+    g_free(addr);
+}
+
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
 {
     int fd;
Signature · 1 line
2.7.4
#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Hi

----- Original Message -----

Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines
Quoted 5 lines · On Thu, Jun 16, 2016 at 09:28:52PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

We normally only unlink what we created.

What do you mean by "we", qemu currently unlink before creating the unix socket. No further cleanup.

Quoted 4 lines
Quoted 4 lines

unlinking files we didn't create
looks like a silent change that might easily break existing users.

That shouldn't remove files we didn't create. But a bad user could recreate the unix socket after qemu did, and that would remove it. I don't think that's a valid use case though, so it should be fine.

Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

thanks

Quoted 93 lines
Quoted 92 lines
Quoted 92 lines

---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
include/qemu/sockets.h +1 -0
index 1bd9218..5dd2648 100644
@@ -51,6 +51,7 @@ SocketAddress *socket_parse(const char *str, Error
 **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error
 **errp);

/* Old, ipv4 only bits. Don't use for new code. */

io/channel-socket.c +10 -0
index 1cd5848..6ec87f8 100644
@@ -400,7 +400,17 @@ static void qio_channel_socket_init(Object *obj)
 static void qio_channel_socket_finalize(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
+
     if (ioc->fd != -1) {
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
+            Error *err = NULL;
+
+            socket_listen_cleanup(ioc->fd, &err);
+            if (err) {
+                error_report_err(err);
+                err = NULL;
+            }
+        }
 #ifdef WIN32
         WSAEventSelect(ioc->fd, NULL, 0);
 #endif
tests/test-io-channel-socket.c +1 -1
index 855306b..f73e063 100644
@@ -383,7 +383,7 @@ static void test_io_channel_unix(bool async)
 
     qapi_free_SocketAddress(listen_addr);
     qapi_free_SocketAddress(connect_addr);
-    unlink(TEST_SOCKET);
+    g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS) == FALSE);
 }
 
 
util/qemu-sockets.c +18 -0
index 0d6cd1f..5d03695 100644
@@ -997,6 +997,24 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+void socket_listen_cleanup(int fd, Error **errp)
+{
+    SocketAddress *addr;
+
+    addr = socket_local_address(fd, errp);
+
+    if (addr->type == SOCKET_ADDRESS_KIND_UNIX
+        && addr->u.q_unix.data->path) {
+        if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno,
+                             "Failed to unlink socket %s",
+                             addr->u.q_unix.data->path);
+        }
+    }
+
+    g_free(addr);
+}
+
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error
 **errp)
 {
     int fd;

--
2.7.4

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 25 lines · Marc-André Lureau

Hi

----- Original Message -----

Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines
Quoted 5 lines · On Thu, Jun 16, 2016 at 09:28:52PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

We normally only unlink what we created.

What do you mean by "we", qemu currently unlink before creating the unix socket. No further cleanup.

Quoted 4 lines
Quoted 4 lines

unlinking files we didn't create
looks like a silent change that might easily break existing users.

Quoted 8 lines

That shouldn't remove files we didn't create. But a bad user could recreate the unix socket after qemu did, and that would remove it. I don't think that's a valid use case though, so it should be fine.

Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

thanks

I'd like it better contained - that's all. So let's set a flag that says "must unlink" as opposed to "it's listening".

Quoted 93 lines
Quoted 93 lines
Quoted 92 lines
Quoted 92 lines

---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
include/qemu/sockets.h +1 -0
index 1bd9218..5dd2648 100644
@@ -51,6 +51,7 @@ SocketAddress *socket_parse(const char *str, Error
 **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error
 **errp);

/* Old, ipv4 only bits. Don't use for new code. */

io/channel-socket.c +10 -0
index 1cd5848..6ec87f8 100644
@@ -400,7 +400,17 @@ static void qio_channel_socket_init(Object *obj)
 static void qio_channel_socket_finalize(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
+
     if (ioc->fd != -1) {
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
+            Error *err = NULL;
+
+            socket_listen_cleanup(ioc->fd, &err);
+            if (err) {
+                error_report_err(err);
+                err = NULL;
+            }
+        }
 #ifdef WIN32
         WSAEventSelect(ioc->fd, NULL, 0);
 #endif
tests/test-io-channel-socket.c +1 -1
index 855306b..f73e063 100644
@@ -383,7 +383,7 @@ static void test_io_channel_unix(bool async)
 
     qapi_free_SocketAddress(listen_addr);
     qapi_free_SocketAddress(connect_addr);
-    unlink(TEST_SOCKET);
+    g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS) == FALSE);
 }
 
 
util/qemu-sockets.c +18 -0
index 0d6cd1f..5d03695 100644
@@ -997,6 +997,24 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+void socket_listen_cleanup(int fd, Error **errp)
+{
+    SocketAddress *addr;
+
+    addr = socket_local_address(fd, errp);
+
+    if (addr->type == SOCKET_ADDRESS_KIND_UNIX
+        && addr->u.q_unix.data->path) {
+        if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno,
+                             "Failed to unlink socket %s",
+                             addr->u.q_unix.data->path);
+        }
+    }
+
+    g_free(addr);
+}
+
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error
 **errp)
 {
     int fd;

--
2.7.4

#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 4 lines · Michael S. Tsirkin
Quoted 4 lines
Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

Actually it's not possible to pass a listening fd to a socket chardev today (the path argument doesn't understand /dev/fdset), so only path created by qemu will be cleaned up.

Quoted 3 lines

I'd like it better contained - that's all. So let's set a flag that says "must unlink" as opposed to "it's listening".

You suggest to rename QIO_CHANNEL_FEATURE_LISTEN to QIO_CHANNEL_FEATURE_LISTEN_MUST_UNLINK ? Or to add another feature flag? I don't think that brings anything useful here.

What would you think Daniel?

Signature · 1 line
Marc-André Lureau
#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 11 lines · Marc-André Lureau
Quoted 4 lines · Michael S. Tsirkin
Quoted 4 lines
Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

Actually it's not possible to pass a listening fd to a socket chardev today (the path argument doesn't understand /dev/fdset), so only path created by qemu will be cleaned up.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

I'd like it better contained - that's all. So let's set a flag that says "must unlink" as opposed to "it's listening".

You suggest to rename QIO_CHANNEL_FEATURE_LISTEN to QIO_CHANNEL_FEATURE_LISTEN_MUST_UNLINK ? Or to add another feature flag? I don't think that brings anything useful here.

IMHO the existing QIO_CHANNEL_FEATURE_LISTEN makes more sense than a QIO_CHANNEL_FEATURE_MUST_UNLINK, so I'd like to see your current patches go in as is.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#14 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

In reply to Marc-André Lureau
Quoted 11 lines · Marc-André Lureau
Quoted 4 lines · Michael S. Tsirkin
Quoted 4 lines
Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

Actually it's not possible to pass a listening fd to a socket chardev today (the path argument doesn't understand /dev/fdset), so only path created by qemu will be cleaned up.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

I'd like it better contained - that's all. So let's set a flag that says "must unlink" as opposed to "it's listening".

You suggest to rename QIO_CHANNEL_FEATURE_LISTEN to QIO_CHANNEL_FEATURE_LISTEN_MUST_UNLINK ?

QIO_CHANNEL_FEATURE_UNLINK_ON_CLOSE

or something like this.

Or maybe QIO_CHANNEL_FEATURE_BOUND

Quoted 2 lines

Or to add another feature
flag? I don't think that brings anything useful here.

The point is that in the future we might be listening on sockets where we did not bind it. I would think that in that case, we do not want to unlink it. So name should reflect this somehow.

Quoted 4 lines

What would you think Daniel?

Signature · 1 line
Marc-André Lureau
#15 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

Quoted 22 lines · Michael S. Tsirkin
Quoted 11 lines · Marc-André Lureau
Quoted 4 lines · Michael S. Tsirkin
Quoted 4 lines
Quoted 2 lines

Maybe what you want is a need_unlink feature. Set it for unix sockets only, that would make some sense.

Oh perhaps what you mean is that if the fd was passed, we should cleanup the unix socket? Yes, I think we should do that then. I'll update the series.

Actually it's not possible to pass a listening fd to a socket chardev today (the path argument doesn't understand /dev/fdset), so only path created by qemu will be cleaned up.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

I'd like it better contained - that's all. So let's set a flag that says "must unlink" as opposed to "it's listening".

You suggest to rename QIO_CHANNEL_FEATURE_LISTEN to QIO_CHANNEL_FEATURE_LISTEN_MUST_UNLINK ?

QIO_CHANNEL_FEATURE_UNLINK_ON_CLOSE

or something like this.

Quoted 8 lines

Or maybe QIO_CHANNEL_FEATURE_BOUND

Quoted 2 lines

Or to add another feature
flag? I don't think that brings anything useful here.

The point is that in the future we might be listening on sockets where we did not bind it. I would think that in that case, we do not want to unlink it. So name should reflect this somehow.

There is no possibility of listening on sockets without binding to them with the current design of the code. If & when we want such support we can adapt as needed, but I don't see any real point in trying to support something that is impossible right now.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
Reviewed-by on v2 3/3
#16 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] socket: unlink unix socket on remove

In reply to [email protected]
Quoted 8 lines · On Thu, Jun 16, 2016 at 09:28:52PM +0200, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu leaves unix socket files behind when removing a listening chardev or leaving. qemu could clean that up, even if doing so isn't race-free.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

Quoted 7 lines
---
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 10 ++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#17 lore raw
In reply to [email protected]
Quoted 19 lines · On 16/06/2016 21:28, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Hi

A small series to do some chardev cleanup when removing them and leaving qemu.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

v1->v2:
- use atexit() for qemu_chr_cleanup()
- add missing braces

Marc-André Lureau (3):
char: clean up remaining chardevs when leaving
socket: add listen feature
socket: unlink unix socket on remove

Quoted 8 lines
 include/io/channel.h           |  1 +
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 17 +++++++++++++++++
 qemu-char.c                    | 11 +++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 6 files changed, 49 insertions(+), 1 deletion(-)

Looks good, I'll queue them if Dan acks patch 2.

Paolo

#18 lore raw
Quoted 31 lines · Paolo Bonzini
Quoted 19 lines · On 16/06/2016 21:28, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Hi

A small series to do some chardev cleanup when removing them and leaving qemu.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1347077

v1->v2:
- use atexit() for qemu_chr_cleanup()
- add missing braces

Marc-André Lureau (3):
char: clean up remaining chardevs when leaving
socket: add listen feature
socket: unlink unix socket on remove

Quoted 8 lines
 include/io/channel.h           |  1 +
 include/qemu/sockets.h         |  1 +
 io/channel-socket.c            | 17 +++++++++++++++++
 qemu-char.c                    | 11 +++++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 6 files changed, 49 insertions(+), 1 deletion(-)
Quoted 1 line

Looks good, I'll queue them if Dan acks patch 2.

Go ahead, its all fine with me.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|