[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 7/8] oslib-posix: Introduce qemu_socketpair()
From: |
marcandre . lureau |
Subject: |
[PULL 7/8] oslib-posix: Introduce qemu_socketpair() |
Date: |
Thu, 29 Sep 2022 15:05:07 +0400 |
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>
Reviewed-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id:
<17fa1eff729eeabd9a001f4639abccb127ceec81.1661240709.git.tugy@chinatelecom.cn>
---
include/qemu/sockets.h | 18 ++++++++++++++++++
util/oslib-posix.c | 19 +++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index 038faa157f..036745e586 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -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);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index d55af69c11..827a7aadba 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -253,6 +253,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)
{
--
2.37.3
- [PULL 0/8] chardev patches, marcandre . lureau, 2022/09/29
- [PULL 1/8] msmouse: Handle mouse reset, marcandre . lureau, 2022/09/29
- [PULL 2/8] chardev: src buffer const for write functions, marcandre . lureau, 2022/09/29
- [PULL 3/8] msmouse: Use fifo8 instead of array, marcandre . lureau, 2022/09/29
- [PULL 4/8] msmouse: Add pnp data, marcandre . lureau, 2022/09/29
- [PULL 6/8] chardev: fix segfault in finalize, marcandre . lureau, 2022/09/29
- [PULL 5/8] serial: Allow unaligned i/o access, marcandre . lureau, 2022/09/29
- [PULL 8/8] vhost-user: Call qemu_socketpair() instead of socketpair(), marcandre . lureau, 2022/09/29
- [PULL 7/8] oslib-posix: Introduce qemu_socketpair(),
marcandre . lureau <=