introduce qemu_socketpiar()
PATCH 1/2 oslib-posix: Introduce qemu_socketpair()
In reply to [email protected]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
@@ -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
@@ -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
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
@@ -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
@@ -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
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
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()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(-)
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()
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
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()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
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()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
--
GuoyiQuoted 6 lines
Current patches lgtm
Reviewed-by Marc-André Lureau <[email protected]<mailto:[email protected]>>
--
Marc-André Lureau
Signature · 1 line
Marc-André Lureau
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()
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(-)
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]>>>
>
> Introduceqemu_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: Introduceqemu_socketpair()
> vhost-user: Callqemu_socketpair()instead ofsocketpair()>
>
> 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 existingsocketpair()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é LureauSignature · 1 line
Marc-André Lureau
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: Introduceqemu_socketpair()
vhost-user: Callqemu_socketpair()instead ofsocketpair()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(-)