Skip to contentSkip to conversation

introduce qemu_socketpiar()

PATCH In review [email protected] 3 participants 10 messages Aug 23, 2022 – Sep 19, 2022

lore
Share & tools
lore
#0 lore raw

cover introduce qemu_socketpiar()

From: Guoyi Tu <[email protected]>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

 hw/display/vhost-user-gpu.c |  3 ++-
 hw/virtio/vhost-user.c      |  2 +-
 include/qemu/sockets.h      | 18 ++++++++++++++++++
 util/oslib-posix.c          | 19 +++++++++++++++++++
 4 files changed, 40 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 oslib-posix: Introduce qemu_socketpair()

From: Guoyi Tu <[email protected]>

qemu_socketpair() will create a pair of connected sockets with FD_CLOEXEC set

---
 include/qemu/sockets.h | 18 ++++++++++++++++++
 util/oslib-posix.c     | 19 +++++++++++++++++++
 2 files changed, 37 insertions(+)
include/qemu/sockets.h +18 -0
index 038faa157f..036745e586 100644
@@ -14,6 +14,24 @@ int inet_aton(const char *cp, struct in_addr *ia);
 /* misc helpers */
 bool fd_is_socket(int fd);
 int qemu_socket(int domain, int type, int protocol);
+
+#ifndef WIN32
+/**
+ * qemu_socketpair:
+ * @domain: specifies a communication domain, such as PF_UNIX
+ * @type: specifies the socket type.
+ * @protocol: specifies a particular protocol to be used with the  socket
+ * @sv: an array to store the pair of socket created
+ *
+ * Creates an unnamed pair of connected sockets in the specified domain,
+ * of the specified type, and using the optionally specified protocol.
+ * And automatically set the close-on-exec flags on the returned sockets
+ *
+ * Return 0 on success.
+ */
+int qemu_socketpair(int domain, int type, int protocol, int sv[2]);
+#endif
+
 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 int socket_set_cork(int fd, int v);
 int socket_set_nodelay(int fd);
util/oslib-posix.c +19 -0
index 7a34c1657c..e5f5ebe469 100644
@@ -261,6 +261,25 @@ void qemu_set_cloexec(int fd)
     assert(f != -1);
 }
 
+int qemu_socketpair(int domain, int type, int protocol, int sv[2])
+{
+    int ret;
+
+#ifdef SOCK_CLOEXEC
+    ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
+    if (ret != -1 || errno != EINVAL) {
+        return ret;
+    }
+#endif
+    ret = socketpair(domain, type, protocol, sv);;
+    if (ret == 0) {
+        qemu_set_cloexec(sv[0]);
+        qemu_set_cloexec(sv[1]);
+    }
+
+    return ret;
+}
+
 char *
 qemu_get_local_state_dir(void)
 {
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/2 vhost-user: Call qemu_socketpair() instead of socketpair()

In reply to [email protected]

From: Guoyi Tu <[email protected]>

As the close-on-exec flags is not set on the file descriptors returned by socketpair() at default, the fds will survive across exec' function.

In the case that exec' function get invoked, such as the live-update feature which is been developing, it will cause fd leaks.

To address this problem, we should call qemu_socketpair() to create an pair of connected sockets with the close-on-exec flag set.

---
 hw/display/vhost-user-gpu.c | 3 ++-
 hw/virtio/vhost-user.c      | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
hw/display/vhost-user-gpu.c +2 -1
index 3340ef9e5f..19c0e20103 100644
@@ -11,6 +11,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/sockets.h"
 #include "hw/qdev-properties.h"
 #include "hw/virtio/virtio-gpu.h"
 #include "chardev/char-fe.h"
@@ -375,7 +376,7 @@ vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
     Chardev *chr;
     int sv[2];
 
-    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
+    if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
         error_setg_errno(errp, errno, "socketpair() failed");
         return false;
     }
hw/virtio/vhost-user.c +1 -1
index 75b8df21a4..4d2b227028 100644
@@ -1726,7 +1726,7 @@ static int vhost_setup_slave_channel(struct vhost_dev *dev)
         return 0;
     }
 
-    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
+    if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
         int saved_errno = errno;
         error_report("socketpair() failed");
         return -saved_errno;
Signature · 1 line
2.25.1
author
#3 lore raw
In reply to [email protected]

Ping...

Any advises are welcome

Quoted 19 lines · On 8/23/22 15:50, [email protected] wrote:

From: Guoyi Tu <[email protected]>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

  hw/display/vhost-user-gpu.c |  3 ++-
  hw/virtio/vhost-user.c      |  2 +-
  include/qemu/sockets.h      | 18 ++++++++++++++++++
  util/oslib-posix.c          | 19 +++++++++++++++++++
  4 files changed, 40 insertions(+), 2 deletions(-)
Reviewed-by on the v1 series
#4 lore raw
In reply to [email protected]

Hi

Quoted 6 lines · On Tue, Aug 23, 2022 at 12:00 PM <[email protected]> wrote:

From: Guoyi Tu <[email protected]>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

Quoted 7 lines

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

Looks like a good idea to me. We will eventually extend the support for win32 (as discussed in "[PATCH 19/51] tests/qtest: Build test-filter-{mirror, redirector} cases for posix only").

There are other places where you can replace existing socketpair() calls in the code base. Why not do it?

Current patches lgtm

Signature · 1 line
Marc-André Lureau
author
#5 lore raw
Quoted 13 lines · Marc-André Lureau

Hi

On Tue, Aug 23, 2022 at 12:00 PM <[email protected] <mailto:[email protected]>> wrote:

From: Guoyi Tu <[email protected] <mailto:[email protected]>>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Quoted 9 lines

Guoyi Tu (2):
  oslib-posix: Introduce qemu_socketpair()
  vhost-user: Call qemu_socketpair() instead of socketpair()

Looks like a good idea to me. We will eventually extend the support for win32 (as discussed in "[PATCH 19/51] tests/qtest: Build test-filter-{mirror, redirector} cases for posix only").

There are other places where you can replace existing socketpair() calls in the code base. Why not do it?

yeah, Thanks for reminding me, Maybe i can replace all the existing socketpair() calls in a separate patch if this patch can be merged to upstream.

--
Guoyi

Quoted 5 lines

Current patches lgtm

Reviewed-by Marc-André Lureau <[email protected] <mailto:[email protected]>>
Signature · 1 line
Marc-André Lureau
#6 lore raw

Hi

Quoted 26 lines · Guoyi Tu
Quoted 23 lines · Marc-André Lureau

Hi

On Tue, Aug 23, 2022 at 12:00 PM <[email protected] <mailto:[email protected]>> wrote:

From: Guoyi Tu <[email protected] <mailto:[email protected]>>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

Looks like a good idea to me. We will eventually extend the support for win32 (as discussed in "[PATCH 19/51] tests/qtest: Build test-filter-{mirror, redirector} cases for posix only").

Quoted 7 lines
Quoted 3 lines

There are other places where you can replace existing socketpair() calls in the code base. Why not do it?

yeah, Thanks for reminding me, Maybe i can replace all the existing socketpair() calls in a separate patch if this patch can be merged to upstream.

It needs to be reviewed though, all may not want to set CLOEXEC, but most should. Else, we should probably consider switching to GSpawn which does dup or unset CLOEXEC for explicitely passed fds.

Quoted 10 lines

--
Guoyi

Quoted 6 lines

Current patches lgtm

<mailto:[email protected]>>

--
Marc-André Lureau

Signature · 1 line
Marc-André Lureau
author
#7 lore raw
Quoted 43 lines · Marc-André Lureau

Hi

On Mon, Sep 5, 2022 at 4:28 PM Guoyi Tu <[email protected] <mailto:[email protected]>> wrote:

On 9/5/22 19:19, Marc-André Lureau wrote:
> Hi
>
> On Tue, Aug 23, 2022 at 12:00 PM <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>> wrote:
>
>     From: Guoyi Tu <[email protected]
<mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>
>
>     Introduce qemu_socketpair() to create socket pair fd, and >     set the close-on-exec flag at default as with the other type >     of socket does.
>
>     besides, the live update feature is developing, so it's necessary >     to do that.
>
>     Guoyi Tu (2):
>        oslib-posix: Introduce qemu_socketpair()
>        vhost-user: Call qemu_socketpair() instead of socketpair() >
>
> Looks like a good idea to me. We will eventually extend the
support for
> win32 (as discussed in "[PATCH 19/51] tests/qtest: Build > test-filter-{mirror, redirector} cases for posix only"). >
> There are other places where you can replace existing
socketpair() calls
> in the code base. Why not do it?
>
yeah, Thanks for reminding me, Maybe i can replace all the existing socketpair() calls in a separate patch if this patch can be merged to upstream.

Quoted 1 line

It needs to be reviewed though, all may not want to set CLOEXEC, but most should. Else, we should probably consider switching to GSpawn which does dup or unset CLOEXEC for explicitely passed fds.

Got it. Special care will be taken when replace those functions.

Thanks again.

--
Guoyi

Quoted 16 lines

--
Guoyi

> Current patches lgtm
> Reviewed-by: Marc-André Lureau <[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>
>
> --
> Marc-André Lureau

Signature · 1 line
Marc-André Lureau
Reviewed-by on the v1 series
#8 lore raw
In reply to [email protected]
Quoted 13 lines · On Dienstag, 23. August 2022 09:50:38 CEST [email protected] wrote:

From: Guoyi Tu <[email protected]>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

Quoted 5 lines
 hw/display/vhost-user-gpu.c |  3 ++-
 hw/virtio/vhost-user.c      |  2 +-
 include/qemu/sockets.h      | 18 ++++++++++++++++++
 util/oslib-posix.c          | 19 +++++++++++++++++++
 4 files changed, 40 insertions(+), 2 deletions(-)
author
#9 lore raw
In reply to [email protected]

Hi Peter,

what do you think about this patches? If this could be merged in upstream, i will start to update the test codes

Quoted 19 lines · On 8/23/22 15:50, [email protected] wrote:

From: Guoyi Tu <[email protected]>

Introduce qemu_socketpair() to create socket pair fd, and set the close-on-exec flag at default as with the other type of socket does.

besides, the live update feature is developing, so it's necessary to do that.

Guoyi Tu (2):
oslib-posix: Introduce qemu_socketpair()
vhost-user: Call qemu_socketpair() instead of socketpair()

  hw/display/vhost-user-gpu.c |  3 ++-
  hw/virtio/vhost-user.c      |  2 +-
  include/qemu/sockets.h      | 18 ++++++++++++++++++
  util/oslib-posix.c          | 19 +++++++++++++++++++
  4 files changed, 40 insertions(+), 2 deletions(-)