diff mbox series

[1/2] osdeps: Introduce qemu_socketpair()

Message ID 211833676831b86d70af12df9912aa971d46092b.1660303075.git.tugy@chinatelecom.cn (mailing list archive)
State New, archived
Headers show
Series introduce qemu_socketpiar() | expand

Commit Message

Guoyi Tu Aug. 12, 2022, 11:44 a.m. UTC
From: Guoyi Tu <tugy@chinatelecom.cn>

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

Signed-off-by: Guoyi Tu <tugy@chinatelecom.cn>
---
 include/qemu/sockets.h |  3 +++
 util/osdep.c           | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

Comments

Peter Maydell Aug. 12, 2022, 11:49 a.m. UTC | #1
On Fri, 12 Aug 2022 at 12:44, <tugy@chinatelecom.cn> wrote:
>
> From: Guoyi Tu <tugy@chinatelecom.cn>
>
> qemu_socketpair() will create a pair of connected sockets
> with FD_CLOEXEC set
>
> Signed-off-by: Guoyi Tu <tugy@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]);

Any new function declaration in a header file needs a
doc-comment documenting what it does, please.

> +#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

If this function only exists and is usable on posix
hosts, put it in util/oslib-posix.c rather than having
it here with a win32 ifdef.

thanks
-- PMM
Guoyi Tu Aug. 18, 2022, 11:50 a.m. UTC | #2
On 8/12/22 19:49, Peter Maydell wrote:
> On Fri, 12 Aug 2022 at 12:44, <tugy@chinatelecom.cn> wrote:
>>
>> From: Guoyi Tu <tugy@chinatelecom.cn>
>>
>> qemu_socketpair() will create a pair of connected sockets
>> with FD_CLOEXEC set
>>
>> Signed-off-by: Guoyi Tu <tugy@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]);
> 
> Any new function declaration in a header file needs a
> doc-comment documenting what it does, please.

OK, I'll add some comments

>> +#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
> 
> If this function only exists and is usable on posix
> hosts, put it in util/oslib-posix.c rather than having
> it here with a win32 ifdef.
> 
will do

Thanks.

> thanks
> -- PMM
>
diff mbox series

Patch

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
  */