From: edgar.iglesias@gmail.com
To: qemu-devel@nongnu.org
Cc: Riku Voipio <riku.voipio@iki.fi>
Subject: [Qemu-devel] [PATCH] linux-user: Emulate SOCK_CLOEXEC/NONBLOCK if unavailable
Date: Mon, 16 Sep 2013 15:08:06 +0200 [thread overview]
Message-ID: <1379336886-14715-1-git-send-email-edgar.iglesias@gmail.com> (raw)
From: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>
If the host lacks support for SOCK_CLOEXEC or SOCK_NONBLOCK,
try to emulate them with fcntl() FD_CLOEXEC and O_NONBLOCK.
Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
---
linux-user/syscall.c | 48 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 45 insertions(+), 3 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index c62d875..6aa8cd7 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1701,7 +1701,7 @@ static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
free(vec);
}
-static inline void target_to_host_sock_type(int *type)
+static inline int target_to_host_sock_type(int *type)
{
int host_type = 0;
int target_type = *type;
@@ -1718,22 +1718,64 @@ static inline void target_to_host_sock_type(int *type)
break;
}
if (target_type & TARGET_SOCK_CLOEXEC) {
+#if defined(SOCK_CLOEXEC)
host_type |= SOCK_CLOEXEC;
+#elif !defined(FD_CLOEXEC)
+ return -TARGET_EINVAL;
+#endif
}
if (target_type & TARGET_SOCK_NONBLOCK) {
+#if defined(SOCK_NONBLOCK)
host_type |= SOCK_NONBLOCK;
+#elif !defined(O_NONBLOCK)
+ return -TARGET_EINVAL;
+#endif
}
*type = host_type;
+ return 0;
+}
+
+/* Try to emulate socket type flags after socket creation. */
+static int sock_flags_fixup(int fd, int target_type)
+{
+#if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC)
+ if (target_type & TARGET_SOCK_CLOEXEC) {
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
+ close(fd);
+ return -TARGET_EINVAL;
+ }
+ }
+#endif
+#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
+ if (target_type & TARGET_SOCK_NONBLOCK) {
+ int flags = fcntl(fd, F_GETFL);
+ if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
+ close(fd);
+ return -TARGET_EINVAL;
+ }
+ }
+#endif
+ return fd;
}
/* do_socket() Must return target values and target errnos. */
static abi_long do_socket(int domain, int type, int protocol)
{
- target_to_host_sock_type(&type);
+ int target_type = type;
+ int ret;
+
+ ret = target_to_host_sock_type(&type);
+ if (ret) {
+ return ret;
+ }
if (domain == PF_NETLINK)
return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
- return get_errno(socket(domain, type, protocol));
+ ret = get_errno(socket(domain, type, protocol));
+ if (ret >= 0) {
+ ret = sock_flags_fixup(ret, target_type);
+ }
+ return ret;
}
/* do_bind() Must return target values and target errnos. */
--
1.7.10.4
next reply other threads:[~2013-09-16 13:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-16 13:08 edgar.iglesias [this message]
2013-09-23 9:37 ` [Qemu-devel] [PATCH] linux-user: Emulate SOCK_CLOEXEC/NONBLOCK if unavailable Riku Voipio
2013-09-23 11:28 ` Edgar E. Iglesias
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1379336886-14715-1-git-send-email-edgar.iglesias@gmail.com \
--to=edgar.iglesias@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=riku.voipio@iki.fi \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).