From 6981b79516d96092571f09dc2a204f8c391cbd2a Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 29 Dec 2010 08:52:58 +0000 Subject: [PATCH] Check SO_ERROR when async connect completes When non-blocking connect completes the error code can be read using getsockopt(SO_ERROR). Doing this is important for identifying failure to connect, especially if POLLERR and POLLHUP were not employed by the user. The QEMU iscsi block driver does not use POLLERR/POLLHUP and depends on SO_ERROR to detect connection failure. Signed-off-by: Stefan Hajnoczi --- lib/socket.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/socket.c b/lib/socket.c index 1ad1dd9..116e991 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -317,6 +317,22 @@ iscsi_service(struct iscsi_context *iscsi, int revents) } if (iscsi->is_connected == 0 && iscsi->fd != -1 && revents&POLLOUT) { + int err = 0; + socklen_t err_size = sizeof(err); + + if (getsockopt(iscsi->fd, SOL_SOCKET, SO_ERROR, + &err, &err_size) != 0 || err != 0) { + if (err == 0) { + err = errno; + } + iscsi_set_error(iscsi, "iscsi_service: socket error " + "%s(%d) while connecting.", + strerror(err), err); + iscsi->socket_status_cb(iscsi, SCSI_STATUS_ERROR, + NULL, iscsi->connect_data); + return -1; + } + iscsi->is_connected = 1; iscsi->socket_status_cb(iscsi, SCSI_STATUS_GOOD, NULL, iscsi->connect_data);