From patchwork Mon Jan 14 22:52:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 10763547 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5D3731390 for ; Mon, 14 Jan 2019 23:16:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4D2E52AE5A for ; Mon, 14 Jan 2019 23:16:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 417702BD60; Mon, 14 Jan 2019 23:16:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id ADD372AE5A for ; Mon, 14 Jan 2019 23:16:52 +0000 (UTC) Received: from localhost ([127.0.0.1]:35569 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjBTP-00071x-Ux for patchwork-qemu-devel@patchwork.kernel.org; Mon, 14 Jan 2019 18:16:52 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34750) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjB6k-0004Bi-Us for qemu-devel@nongnu.org; Mon, 14 Jan 2019 17:53:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjB6h-0001Cl-Up for qemu-devel@nongnu.org; Mon, 14 Jan 2019 17:53:25 -0500 Received: from hera.aquilenet.fr ([185.233.100.1]:40300) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjB6h-0001AM-HM for qemu-devel@nongnu.org; Mon, 14 Jan 2019 17:53:23 -0500 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id E74B61BC1; Mon, 14 Jan 2019 23:53:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WgJrttHf7kFH; Mon, 14 Jan 2019 23:53:19 +0100 (CET) Received: from function.home (lfbn-1-11161-124.w86-213.abo.wanadoo.fr [86.213.235.124]) by hera.aquilenet.fr (Postfix) with ESMTPSA id 719AF1BC3; Mon, 14 Jan 2019 23:53:10 +0100 (CET) Received: from samy by function.home with local (Exim 4.92-RC4) (envelope-from ) id 1gjB6T-0005e8-0J; Mon, 14 Jan 2019 23:53:09 +0100 From: Samuel Thibault To: qemu-devel@nongnu.org Date: Mon, 14 Jan 2019 23:52:14 +0100 Message-Id: <20190114225306.21569-14-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190114225306.21569-1-samuel.thibault@ens-lyon.org> References: <20190114225306.21569-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 185.233.100.1 Subject: [Qemu-devel] [PULL 13/65] slirp: move socket pair creation in helper function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Samuel Thibault , stefanha@redhat.com, jan.kiszka@siemens.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Originally, the patch was fixing a bunch of issues, but Peter beat me to it with earlier commit "slirp: fork_exec(): create and connect child socket before fork()". Factor out socket pair creation, to simplify the fork_exec() code. Use the name socketpair_with_oob() since the code is actually similar to what socketpair() would do, except that it uses TCP sockets, for SLIRP to be able to call send with MSG_OOB (since SO_OOBINLINE is set, this could probably be faked instead on regular unix sockets though). Signed-off-by: Marc-André Lureau Signed-off-by: Samuel Thibault --- slirp/misc.c | 142 +++++++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/slirp/misc.c b/slirp/misc.c index 4840187750..7362e14339 100644 --- a/slirp/misc.c +++ b/slirp/misc.c @@ -73,85 +73,92 @@ fork_exec(struct socket *so, const char *ex) #else -/* - * XXX This is ugly - * We create and bind a socket, then fork off to another - * process, which connects to this socket, after which we - * exec the wanted program. If something (strange) happens, - * the accept() call could block us forever. - */ +static int +slirp_socketpair_with_oob(int sv[2]) +{ + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = 0, + .sin_addr.s_addr = INADDR_ANY, + }; + socklen_t addrlen = sizeof(addr); + int ret, s; + + sv[1] = -1; + s = qemu_socket(AF_INET, SOCK_STREAM, 0); + if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 || + listen(s, 1) < 0 || + getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) { + goto err; + } + + sv[1] = qemu_socket(AF_INET, SOCK_STREAM, 0); + if (sv[1] < 0) { + goto err; + } + /* + * This connect won't block because we've already listen()ed on + * the server end (even though we won't accept() the connection + * until later on). + */ + do { + ret = connect(sv[1], (struct sockaddr *)&addr, addrlen); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + goto err; + } + + do { + sv[0] = accept(s, (struct sockaddr *)&addr, &addrlen); + } while (sv[0] < 0 && errno == EINTR); + if (sv[0] < 0) { + goto err; + } + + closesocket(s); + return 0; + +err: + error_report("Error: slirp_socketpair(): %s", strerror(errno)); + if (s >= 0) { + closesocket(s); + } + if (sv[1] >= 0) { + closesocket(sv[1]); + } + return -1; +} + int fork_exec(struct socket *so, const char *ex) { - int s, cs; - struct sockaddr_in addr, csaddr; - socklen_t addrlen = sizeof(addr); - socklen_t csaddrlen = sizeof(csaddr); - int opt; char **argv; - int ret; + int opt, c, sp[2]; pid_t pid; DEBUG_CALL("fork_exec"); DEBUG_ARG("so = %p", so); DEBUG_ARG("ex = %p", ex); - addr.sin_family = AF_INET; - addr.sin_port = 0; - addr.sin_addr.s_addr = INADDR_ANY; - - s = qemu_socket(AF_INET, SOCK_STREAM, 0); - if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 || - listen(s, 1) < 0) { - error_report("Error: inet socket: %s", strerror(errno)); - if (s >= 0) { - closesocket(s); - } + if (slirp_socketpair_with_oob(sp) < 0) { return 0; } - if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) { - closesocket(s); - return 0; - } - cs = qemu_socket(AF_INET, SOCK_STREAM, 0); - if (cs < 0) { - closesocket(s); - return 0; - } - csaddr.sin_addr = loopback_addr; - /* - * This connect won't block because we've already listen()ed on - * the server end (even though we won't accept() the connection - * until later on). - */ - do { - ret = connect(cs, (struct sockaddr *)&csaddr, csaddrlen); - } while (ret < 0 && errno == EINTR); - if (ret < 0) { - closesocket(s); - closesocket(cs); - return 0; - } - pid = fork(); switch(pid) { case -1: error_report("Error: fork failed: %s", strerror(errno)); - closesocket(cs); - close(s); + closesocket(sp[0]); + closesocket(sp[1]); return 0; case 0: - setsid(); - - /* Set the DISPLAY */ - close(s); - dup2(cs, 0); - dup2(cs, 1); - dup2(cs, 2); - for (s = getdtablesize() - 1; s >= 3; s--) - close(s); + setsid(); + dup2(sp[1], 0); + dup2(sp[1], 1); + dup2(sp[1], 2); + for (c = getdtablesize() - 1; c >= 3; c--) + close(c); argv = g_strsplit(ex, " ", -1); execvp(argv[0], (char **)argv); @@ -163,19 +170,12 @@ fork_exec(struct socket *so, const char *ex) exit(1); default: + so->s = sp[0]; + closesocket(sp[1]); qemu_add_child_watch(pid); - closesocket(cs); - /* - * This should never block, because we already connect()ed - * on the child end before we forked. - */ - do { - so->s = accept(s, (struct sockaddr *)&addr, &addrlen); - } while (so->s < 0 && errno == EINTR); - closesocket(s); - socket_set_fast_reuse(so->s); - opt = 1; - qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int)); + socket_set_fast_reuse(so->s); + opt = 1; + qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int)); qemu_set_nonblock(so->s); return 1; }