From nobody Thu Nov 6 12:20:23 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541517639988319.6763330417914; Tue, 6 Nov 2018 07:20:39 -0800 (PST) Received: from localhost ([::1]:41638 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK39i-0001ys-Be for importer@patchew.org; Tue, 06 Nov 2018 10:20:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35615) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK33L-0004DP-PK for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:14:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gK33E-0004mJ-7U for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:14:03 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:52316) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gK33D-0004AK-Sy for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:13:56 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gK32k-0007xE-Uc; Tue, 06 Nov 2018 15:13:26 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 6 Nov 2018 15:13:20 +0000 Message-Id: <20181106151323.16154-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106151323.16154-1-peter.maydell@linaro.org> References: <20181106151323.16154-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH for-3.1 1/4] slirp: Don't pass possibly -1 fd to send() 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: Samuel Thibault , Jan Kiszka , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Coverity complains (CID 1005726) that we might pass -1 as the fd argument to send() in slirp_send(), because we previously checked for "so->s =3D=3D -1 && so->extra". The case of "so->s =3D=3D -1 but so->extra NULL" should not in theory happen, but it is hard to guarantee because various places in the code do so->s =3D qemu_socket(...) and so will end up with so->s =3D=3D -1 on failure, and not all the paths which call that always throw away the socket in that case (eg tcp_fconnect()). So just check specifically for the condition and fail slirp_send(). Signed-off-by: Peter Maydell --- This is to some extent just placating Coverity. --- slirp/slirp.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/slirp/slirp.c b/slirp/slirp.c index 51de41fc021..3c3c03b22f7 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -1091,6 +1091,17 @@ ssize_t slirp_send(struct socket *so, const void *bu= f, size_t len, int flags) return len; } =20 + if (so->s =3D=3D -1) { + /* + * This should in theory not happen but it is hard to be + * sure because some code paths will end up with so->s =3D=3D -1 + * on a failure but don't dispose of the struct socket. + * Check specifically, so we don't pass -1 to send(). + */ + errno =3D EBADF; + return -1; + } + return send(so->s, buf, len, flags); } =20 --=20 2.19.1 From nobody Thu Nov 6 12:20:23 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541518408013278.3546935708615; Tue, 6 Nov 2018 07:33:28 -0800 (PST) Received: from localhost ([::1]:41710 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3M6-00055I-Q7 for importer@patchew.org; Tue, 06 Nov 2018 10:33:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39769) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3HZ-0002CL-6C for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gK3HV-00073n-FB for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:43 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:52368) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gK3HM-0006vv-7C for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:35 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gK32m-0007xW-1m; Tue, 06 Nov 2018 15:13:28 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 6 Nov 2018 15:13:21 +0000 Message-Id: <20181106151323.16154-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106151323.16154-1-peter.maydell@linaro.org> References: <20181106151323.16154-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH for-3.1 2/4] slirp: Use g_new() to allocate sockets in socreate() 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: Samuel Thibault , Jan Kiszka , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The slirp socreate() function can only fail if the attempt to malloc() the struct socket fails. Switch to using g_new() instead, which will allow us to remove the error-handling code from its callers. Signed-off-by: Peter Maydell --- We already use g_new/g_malloc in slirp, including for mbuf buffers which are larger than these socket structs. The motivation here is that we can render moot a Coverity complaint about an issue in an error-handling path. As usual, indenting in slirp code is a bit of a mess; I've opted for "keep checkpatch happy". --- slirp/socket.c | 14 ++++++-------- slirp/tcp_input.c | 4 ++-- slirp/tcp_subr.c | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/slirp/socket.c b/slirp/socket.c index 322383a1f9f..35a9a145659 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -46,17 +46,15 @@ struct socket *solookup(struct socket **last, struct so= cket *head, struct socket * socreate(Slirp *slirp) { - struct socket *so; + struct socket *so =3D g_new(struct socket, 1); =20 - so =3D (struct socket *)malloc(sizeof(struct socket)); - if(so) { memset(so, 0, sizeof(struct socket)); so->so_state =3D SS_NOFDREF; so->s =3D -1; so->slirp =3D slirp; so->pollfds_idx =3D -1; - } - return(so); + + return so; } =20 /* @@ -110,7 +108,7 @@ sofree(struct socket *so) if (so->so_tcpcb) { free(so->so_tcpcb); } - free(so); + g_free(so); } =20 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np) @@ -721,8 +719,8 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, u= int32_t laddr, =20 /* Don't tcp_attach... we don't need so_snd nor so_rcv */ if ((so->so_tcpcb =3D tcp_newtcpcb(so)) =3D=3D NULL) { - free(so); - return NULL; + g_free(so); + return NULL; } insque(so, &slirp->tcb); =20 diff --git a/slirp/tcp_input.c b/slirp/tcp_input.c index 07bcbdb2ddd..4f79c95fdb4 100644 --- a/slirp/tcp_input.c +++ b/slirp/tcp_input.c @@ -432,8 +432,8 @@ findso: if ((so =3D socreate(slirp)) =3D=3D NULL) goto dropwithreset; if (tcp_attach(so) < 0) { - free(so); /* Not sofree (if it failed, it's not insqued) */ - goto dropwithreset; + g_free(so); /* Not sofree (if it failed, it's not insqued) */ + goto dropwithreset; } =20 sbreserve(&so->so_snd, TCP_SNDSPACE); diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c index 8d0f94b75fc..0270c89ae3a 100644 --- a/slirp/tcp_subr.c +++ b/slirp/tcp_subr.c @@ -475,7 +475,7 @@ void tcp_connect(struct socket *inso) return; } if (tcp_attach(so) < 0) { - free(so); /* NOT sofree */ + g_free(so); /* NOT sofree */ return; } so->lhost =3D inso->lhost; --=20 2.19.1 From nobody Thu Nov 6 12:20:23 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541518222595782.0964859340964; Tue, 6 Nov 2018 07:30:22 -0800 (PST) Received: from localhost ([::1]:41692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3J6-00033k-Rz for importer@patchew.org; Tue, 06 Nov 2018 10:30:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39732) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3HV-0002AT-Cw for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gK3HL-0006xy-EZ for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:35 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:52368) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gK3HL-0006vv-4e for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:31 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gK32m-0007xp-Vg; Tue, 06 Nov 2018 15:13:28 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 6 Nov 2018 15:13:22 +0000 Message-Id: <20181106151323.16154-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106151323.16154-1-peter.maydell@linaro.org> References: <20181106151323.16154-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH for-3.1 3/4] slirp: Remove code that handles socreate() failure 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: Samuel Thibault , Jan Kiszka , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Now that socreate() can never fail, we can remove the code that was trying to handle that situation. In particular this removes code in tcp_connect() that provoked Coverity to complain (CID 1005724): in closesocket(accept(inso->s, (struct sockaddr *)&addr, &addrlen)); if the accept() call fails then we pass closesocket() -1 instead of a valid file descriptor. Signed-off-by: Peter Maydell --- slirp/ip_icmp.c | 2 +- slirp/slirp.c | 3 --- slirp/socket.c | 3 --- slirp/tcp_input.c | 3 +-- slirp/tcp_subr.c | 5 ----- slirp/udp.c | 6 ------ slirp/udp6.c | 3 --- 7 files changed, 2 insertions(+), 23 deletions(-) diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c index da100d1f55f..9210eef3f35 100644 --- a/slirp/ip_icmp.c +++ b/slirp/ip_icmp.c @@ -160,7 +160,7 @@ icmp_input(struct mbuf *m, int hlen) } else { struct socket *so; struct sockaddr_storage addr; - if ((so =3D socreate(slirp)) =3D=3D NULL) goto freeit; + so =3D socreate(slirp); if (icmp_send(so, m, hlen) =3D=3D 0) { return; } diff --git a/slirp/slirp.c b/slirp/slirp.c index 3c3c03b22f7..322edf51eb8 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -1477,9 +1477,6 @@ static int slirp_state_load(QEMUFile *f, void *opaque= , int version_id) int ret; struct socket *so =3D socreate(slirp); =20 - if (!so) - return -ENOMEM; - ret =3D vmstate_load_state(f, &vmstate_slirp_socket, so, version_i= d); =20 if (ret < 0) diff --git a/slirp/socket.c b/slirp/socket.c index 35a9a145659..c01d8696afb 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -713,9 +713,6 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, u= int32_t laddr, DEBUG_ARG("flags =3D %x", flags); =20 so =3D socreate(slirp); - if (!so) { - return NULL; - } =20 /* Don't tcp_attach... we don't need so_snd nor so_rcv */ if ((so->so_tcpcb =3D tcp_newtcpcb(so)) =3D=3D NULL) { diff --git a/slirp/tcp_input.c b/slirp/tcp_input.c index 4f79c95fdb4..d073ef95256 100644 --- a/slirp/tcp_input.c +++ b/slirp/tcp_input.c @@ -429,8 +429,7 @@ findso: if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) !=3D TH_SYN) goto dropwithreset; =20 - if ((so =3D socreate(slirp)) =3D=3D NULL) - goto dropwithreset; + so =3D socreate(slirp); if (tcp_attach(so) < 0) { g_free(so); /* Not sofree (if it failed, it's not insqued) */ goto dropwithreset; diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c index 0270c89ae3a..fa61349cbb0 100644 --- a/slirp/tcp_subr.c +++ b/slirp/tcp_subr.c @@ -469,11 +469,6 @@ void tcp_connect(struct socket *inso) so =3D inso; } else { so =3D socreate(slirp); - if (so =3D=3D NULL) { - /* If it failed, get rid of the pending connection */ - closesocket(accept(inso->s, (struct sockaddr *)&addr, &addrlen= )); - return; - } if (tcp_attach(so) < 0) { g_free(so); /* NOT sofree */ return; diff --git a/slirp/udp.c b/slirp/udp.c index e5bf065bf23..c47870a61b4 100644 --- a/slirp/udp.c +++ b/slirp/udp.c @@ -171,9 +171,6 @@ udp_input(register struct mbuf *m, int iphlen) * create one */ so =3D socreate(slirp); - if (!so) { - goto bad; - } if (udp_attach(so, AF_INET) =3D=3D -1) { DEBUG_MISC((dfd," udp_attach errno =3D %d-%s\n", errno,strerror(errno))); @@ -331,9 +328,6 @@ udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, u= int32_t laddr, socklen_t addrlen =3D sizeof(struct sockaddr_in); =20 so =3D socreate(slirp); - if (!so) { - return NULL; - } so->s =3D qemu_socket(AF_INET,SOCK_DGRAM,0); if (so->s < 0) { sofree(so); diff --git a/slirp/udp6.c b/slirp/udp6.c index 7c4a6b003a8..986010f0d3a 100644 --- a/slirp/udp6.c +++ b/slirp/udp6.c @@ -91,9 +91,6 @@ void udp6_input(struct mbuf *m) if (so =3D=3D NULL) { /* If there's no socket for this packet, create one. */ so =3D socreate(slirp); - if (!so) { - goto bad; - } if (udp_attach(so, AF_INET6) =3D=3D -1) { DEBUG_MISC((dfd, " udp6_attach errno =3D %d-%s\n", errno, strerror(errno))); --=20 2.19.1 From nobody Thu Nov 6 12:20:23 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541518284257876.2564496187683; Tue, 6 Nov 2018 07:31:24 -0800 (PST) Received: from localhost ([::1]:41699 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3K6-0003uz-Ho for importer@patchew.org; Tue, 06 Nov 2018 10:31:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39708) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gK3HL-00022q-B8 for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gK3HK-0006xI-CH for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:31 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:52368) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gK3HK-0006vv-40 for qemu-devel@nongnu.org; Tue, 06 Nov 2018 10:28:30 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gK32n-0007y7-US; Tue, 06 Nov 2018 15:13:29 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 6 Nov 2018 15:13:23 +0000 Message-Id: <20181106151323.16154-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106151323.16154-1-peter.maydell@linaro.org> References: <20181106151323.16154-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH for-3.1 4/4] slirp: fork_exec(): create and connect child socket before fork() 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: Samuel Thibault , Jan Kiszka , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Currently fork_exec() fork()s, and then creates and connects the child socket which it uses for communication with the parent in the child process. This is awkward because the child has no mechanism to report failure back to the parent, which might end up blocked forever in accept(). The child code also has an issue pointed out by Coverity (CID 1005727), where if the qemu_socket() call fails it will pass -1 as a file descriptor to connect(). Fix these issues by moving the creation of the child's end of the socket to before the fork(), where we are in a position to handle a possible failure. Signed-off-by: Peter Maydell --- slirp/misc.c | 55 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/slirp/misc.c b/slirp/misc.c index 260187b6b62..57bdd808e2d 100644 --- a/slirp/misc.c +++ b/slirp/misc.c @@ -85,9 +85,10 @@ fork_exec(struct socket *so, const char *ex, int do_pty) int fork_exec(struct socket *so, const char *ex, int do_pty) { - int s; - struct sockaddr_in addr; + int s, cs; + struct sockaddr_in addr, csaddr; socklen_t addrlen =3D sizeof(addr); + socklen_t csaddrlen =3D sizeof(csaddr); int opt; const char *argv[256]; /* don't want to clobber the original */ @@ -120,10 +121,35 @@ fork_exec(struct socket *so, const char *ex, int do_p= ty) } } =20 + if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) { + closesocket(s); + return 0; + } + cs =3D qemu_socket(AF_INET, SOCK_STREAM, 0); + if (cs < 0) { + closesocket(s); + return 0; + } + csaddr.sin_addr =3D 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 =3D connect(cs, (struct sockaddr *)&csaddr, csaddrlen); + } while (ret < 0 && errno =3D=3D EINTR); + if (ret < 0) { + closesocket(s); + closesocket(cs); + return 0; + } + pid =3D fork(); switch(pid) { case -1: error_report("Error: fork failed: %s", strerror(errno)); + closesocket(cs); close(s); return 0; =20 @@ -131,21 +157,10 @@ fork_exec(struct socket *so, const char *ex, int do_p= ty) setsid(); =20 /* Set the DISPLAY */ - getsockname(s, (struct sockaddr *)&addr, &addrlen); close(s); - /* - * Connect to the socket - * XXX If any of these fail, we're in trouble! - */ - s =3D qemu_socket(AF_INET, SOCK_STREAM, 0); - addr.sin_addr =3D loopback_addr; - do { - ret =3D connect(s, (struct sockaddr *)&addr, addrlen); - } while (ret < 0 && errno =3D=3D EINTR); - - dup2(s, 0); - dup2(s, 1); - dup2(s, 2); + dup2(cs, 0); + dup2(cs, 1); + dup2(cs, 2); for (s =3D getdtablesize() - 1; s >=3D 3; s--) close(s); =20 @@ -178,12 +193,10 @@ fork_exec(struct socket *so, const char *ex, int do_p= ty) =20 default: qemu_add_child_watch(pid); + closesocket(cs); /* - * XXX this could block us... - * XXX Should set a timer here, and if accept() doesn't - * return after X seconds, declare it a failure - * The only reason this will block forever is if socket() - * of connect() fail in the child process + * This should never block, because we already connect()ed + * on the child end before we forked. */ do { so->s =3D accept(s, (struct sockaddr *)&addr, &addrlen= ); --=20 2.19.1