From: Guoyi Tu <t...@chinatelecom.cn> qemu_socketpair() will create a pair of connected sockets with FD_CLOEXEC set
Signed-off-by: Guoyi Tu <t...@chinatelecom.cn> --- include/qemu/sockets.h | 3 +++ util/osdep.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 038faa157f..52cf2855df 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -14,6 +14,9 @@ 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 +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); diff --git a/util/osdep.c b/util/osdep.c index 60fcbbaebe..4b1ab623c7 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -481,6 +481,30 @@ int qemu_socket(int domain, int type, int protocol) return ret; } +#ifndef _WIN32 +/* + * Create a pair of connected sockets with FD_CLOEXEC set + */ +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; +} +#endif + /* * Accept a connection and set FD_CLOEXEC */ -- 2.25.1