Skip to contentSkip to conversation

chardev cleanups

PATCH Superseded [email protected] 3 participants 8 messages Jun 16, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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 +
 include/sysemu/char.h          |  7 +++++++
 io/channel-socket.c            | 17 +++++++++++++++++
 qemu-char.c                    |  9 +++++++++
 tests/test-io-channel-socket.c |  2 +-
 util/qemu-sockets.c            | 18 ++++++++++++++++++
 vl.c                           |  1 +
 8 files changed, 55 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 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.

---
 include/sysemu/char.h | 7 +++++++
 qemu-char.c           | 9 +++++++++
 vl.c                  | 1 +
 3 files changed, 17 insertions(+)
include/sysemu/char.h +7 -0
index 372a6fd..8954be1 100644
@@ -382,6 +382,13 @@ void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len);
  */
 void qemu_chr_be_event(CharDriverState *s, int event);
 
+/**
+ * @qemu_chr_cleanup:
+ *
+ * Delete all chardevs (when leaving qemu)
+ */
+void qemu_chr_cleanup(void);
+
 void qemu_chr_add_handlers(CharDriverState *s,
                            IOCanReadHandler *fd_can_read,
                            IOReadHandler *fd_read,
qemu-char.c +9 -0
index b13ecbb..bf098a1 100644
@@ -4088,6 +4088,15 @@ CharDriverState *qemu_chr_find(const char *name)
     return NULL;
 }
 
+void qemu_chr_cleanup(void)
+{
+    CharDriverState *chr;
+
+    QTAILQ_FOREACH(chr, &chardevs, next) {
+        qemu_chr_delete(chr);
+    }
+}
+
 QemuOptsList qemu_chardev_opts = {
     .name = "chardev",
     .implied_opt_name = "backend",
vl.c +1 -0
index 45eff56..ac92b1d 100644
@@ -4628,6 +4628,7 @@ int main(int argc, char **argv, char **envp)
 #ifdef CONFIG_TPM
     tpm_cleanup();
 #endif
+    qemu_chr_cleanup();
 
     return 0;
 }
Signature · 1 line
2.7.4
#2 lore raw

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

Quoted 54 lines · On 16/06/2016 13:17, [email protected] wrote:

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

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

---
 include/sysemu/char.h | 7 +++++++
 qemu-char.c           | 9 +++++++++
 vl.c                  | 1 +
 3 files changed, 17 insertions(+)
include/sysemu/char.h +7 -0
index 372a6fd..8954be1 100644
@@ -382,6 +382,13 @@ void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len);
  */
 void qemu_chr_be_event(CharDriverState *s, int event);
 
+/**
+ * @qemu_chr_cleanup:
+ *
+ * Delete all chardevs (when leaving qemu)
+ */
+void qemu_chr_cleanup(void);
+
 void qemu_chr_add_handlers(CharDriverState *s,
                            IOCanReadHandler *fd_can_read,
                            IOReadHandler *fd_read,
qemu-char.c +9 -0
index b13ecbb..bf098a1 100644
@@ -4088,6 +4088,15 @@ CharDriverState *qemu_chr_find(const char *name)
     return NULL;
 }
 
+void qemu_chr_cleanup(void)
+{
+    CharDriverState *chr;
+
+    QTAILQ_FOREACH(chr, &chardevs, next) {
+        qemu_chr_delete(chr);
+    }
+}
+
 QemuOptsList qemu_chardev_opts = {
     .name = "chardev",
     .implied_opt_name = "backend",
vl.c +0 -0
index 45eff56..ac92b1d 100644

vl.c int main(int argc, char **argv, char **envp) · line 4631

Quoted 4 lines
vl.c +1 -0
@@ -4628,6 +4628,7 @@ int main(int argc, char **argv, char **envp)
 #ifdef CONFIG_TPM
     tpm_cleanup();
 #endif
+    qemu_chr_cleanup();

Given the amount of exit(1) calls surviving in QEMU, it's probably better to use atexit here.

Paolo

Quoted 3 lines
vl.c +0 -0
     return 0;
 }
 
#3 lore raw

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

Hi

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

Quoted 8 lines
Quoted 8 lines
qemu-char.c +1 -0
+
 QemuOptsList qemu_chardev_opts = {
     .name = "chardev",
     .implied_opt_name = "backend",
vl.c +0 -0
index 45eff56..ac92b1d 100644
Quoted 8 lines

vl.c int main(int argc, char **argv, char **envp) · line 4631

Quoted 5 lines
vl.c +1 -0
@@ -4628,6 +4628,7 @@ int main(int argc, char **argv, char **envp)
 #ifdef CONFIG_TPM
     tpm_cleanup();
 #endif
+    qemu_chr_cleanup();

Given the amount of exit(1) calls surviving in QEMU, it's probably better to use atexit here.

Agreed, fixed

#4 lore raw

PATCH 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
#5 lore raw

PATCH 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..319e1ab 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) {
+        Error *err = NULL;
+
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN)
+            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
#6 lore raw

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

Quoted 33 lines · On 16/06/2016 13:17, [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

---
 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 +0 -0
index 1cd5848..319e1ab 100644

io/channel-socket.c static void qio_channel_socket_init(Object *obj) · line 408

Quoted 9 lines
io/channel-socket.c +5 -0
@@ -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) {
+        Error *err = NULL;
+
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN)
+            socket_listen_cleanup(ioc->fd, &err);

Every time you forget braces, checkpatch.pl kills a kitten! Think of the kittens!

Paolo

Quoted 51 lines
io/channel-socket.c +5 -0
+
+        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;
#7 lore raw

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

Hi

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

Quoted 42 lines · On 16/06/2016 13:17, [email protected] wrote:
Quoted 39 lines

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 +1 -0
index 1cd5848..319e1ab 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);
+
Quoted 8 lines

io/channel-socket.c static void qio_channel_socket_init(Object *obj) · line 408

Quoted 5 lines
io/channel-socket.c +4 -0
     if (ioc->fd != -1) {
+        Error *err = NULL;
+
+        if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN)
+            socket_listen_cleanup(ioc->fd, &err);

Every time you forget braces, checkpatch.pl kills a kitten! Think of the kittens!

Oh no! :) (weird the git hook usually catch this for me), fixed