From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, jasowang@redhat.com, berrange@redhat.com,
ashijeetacharya@gmail.com, crobinso@redhat.com,
"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PATCH for-2.7 1/2] net: fix socket connect
Date: Tue, 23 Aug 2016 13:45:36 +0400 [thread overview]
Message-ID: <20160823094537.8782-2-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20160823094537.8782-1-marcandre.lureau@redhat.com>
Commit 7e8449594c929 changed net socket code to use socket_*() functions
from include/qemu/sockets.h. However, the change broke the connection
part, because socket_connect() returns a connected socket that is
being ignored. Use socket_connect() return value appropriately and fix
leaks on error path.
Note that after this change, the function is still blocking.
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1368447
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
net/socket.c | 54 +++++++++++++++++++++++-------------------------------
1 file changed, 23 insertions(+), 31 deletions(-)
diff --git a/net/socket.c b/net/socket.c
index 17e635d..645bcb0 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -501,6 +501,7 @@ static int net_socket_listen_init(NetClientState *peer,
ret = socket_listen(saddr, &local_error);
if (ret < 0) {
+ qapi_free_SocketAddress(saddr);
error_report_err(local_error);
return -1;
}
@@ -522,9 +523,9 @@ static int net_socket_connect_init(NetClientState *peer,
const char *host_str)
{
NetSocketState *s;
- int fd, connected, ret;
- char *addr_str;
- SocketAddress *saddr;
+ int fd = -1, ret = -1;
+ char *addr_str = NULL;
+ SocketAddress *saddr = NULL;
Error *local_error = NULL;
saddr = socket_parse(host_str, &local_error);
@@ -533,45 +534,36 @@ static int net_socket_connect_init(NetClientState *peer,
return -1;
}
- fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+ fd = socket_connect(saddr, &local_error, NULL, NULL);
if (fd < 0) {
- perror("socket");
- return -1;
+ error_report_err(local_error);
+ goto end;
}
+
qemu_set_nonblock(fd);
- connected = 0;
- for(;;) {
- ret = socket_connect(saddr, &local_error, NULL, NULL);
- if (ret < 0) {
- if (errno == EINTR || errno == EWOULDBLOCK) {
- /* continue */
- } else if (errno == EINPROGRESS ||
- errno == EALREADY ||
- errno == EINVAL) {
- break;
- } else {
- error_report_err(local_error);
- closesocket(fd);
- return -1;
- }
- } else {
- connected = 1;
- break;
- }
+
+ s = net_socket_fd_init(peer, model, name, fd, true);
+ if (!s) {
+ goto end;
}
- s = net_socket_fd_init(peer, model, name, fd, connected);
- if (!s)
- return -1;
addr_str = socket_address_to_string(saddr, &local_error);
- if (addr_str == NULL)
- return -1;
+ if (addr_str == NULL) {
+ error_report_err(local_error);
+ goto end;
+ }
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"socket: connect to %s", addr_str);
+ ret = 0;
+
+end:
+ if (ret == -1 && fd >= 0) {
+ closesocket(fd);
+ }
qapi_free_SocketAddress(saddr);
g_free(addr_str);
- return 0;
+ return ret;
}
static int net_socket_mcast_init(NetClientState *peer,
--
2.9.0
next prev parent reply other threads:[~2016-08-23 9:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-23 9:45 [Qemu-devel] [PATCH for-2.7 0/2] Fix net socket connect regressions Marc-André Lureau
2016-08-23 9:45 ` Marc-André Lureau [this message]
2016-08-23 9:45 ` [Qemu-devel] [PATCH for-2.7 2/2] net: make socket connect non-blocking again Marc-André Lureau
2016-08-23 10:43 ` Cao jin
2016-08-23 13:13 ` Daniel P. Berrange
2016-08-23 13:33 ` Marc-André Lureau
2016-08-30 12:07 ` [Qemu-devel] [PATCH for-2.7 0/2] Fix net socket connect regressions Paolo Bonzini
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=20160823094537.8782-2-marcandre.lureau@redhat.com \
--to=marcandre.lureau@redhat.com \
--cc=ashijeetacharya@gmail.com \
--cc=berrange@redhat.com \
--cc=crobinso@redhat.com \
--cc=jasowang@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).