From patchwork Fri Sep 27 12:10:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Riku Voipio X-Patchwork-Id: 278572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 40A952C0337 for ; Fri, 27 Sep 2013 22:14:46 +1000 (EST) Received: from localhost ([::1]:35968 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPWwW-0003iX-20 for incoming@patchwork.ozlabs.org; Fri, 27 Sep 2013 08:14:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59642) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPWsK-0005sI-GD for qemu-devel@nongnu.org; Fri, 27 Sep 2013 08:10:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VPWsA-0008Af-Ew for qemu-devel@nongnu.org; Fri, 27 Sep 2013 08:10:24 -0400 Received: from afflict.kos.to ([92.243.29.197]:51432) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPWsA-00089t-8D for qemu-devel@nongnu.org; Fri, 27 Sep 2013 08:10:14 -0400 Received: from kos.to (a91-156-63-85.elisa-laajakaista.fi [91.156.63.85]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by afflict.kos.to (Postfix) with ESMTPSA id 33AF326552 for ; Fri, 27 Sep 2013 14:10:12 +0200 (CEST) Received: from voipio (uid 1000) (envelope-from voipio@kos.to) id 5e0a86 by kos.to (DragonFly Mail Agent); Fri, 27 Sep 2013 15:10:08 +0300 From: riku.voipio@linaro.org To: qemu-devel@nongnu.org Date: Fri, 27 Sep 2013 15:10:06 +0300 Message-Id: <53d09b761f032f50c4424e8649396a9041070bae.1380283598.git.riku.voipio@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 92.243.29.197 Cc: "Edgar E. Iglesias" Subject: [Qemu-devel] [PATCH 11/11] linux-user: Handle SOCK_CLOEXEC/NONBLOCK if unavailable on host X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "Edgar E. Iglesias" If the host lacks SOCK_CLOEXEC, bail out with -EINVAL. If the host lacks SOCK_ONONBLOCK, try to emulate it with fcntl() and O_NONBLOCK. Signed-off-by: Edgar E. Iglesias Signed-off-by: Riku Voipio --- linux-user/syscall.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index b3822b3..4a14a43 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1773,7 +1773,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; @@ -1790,22 +1790,56 @@ 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; +#else + 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_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. */