osdep: remove use of socket_error() from all code

Now that QEMU wraps the Win32 sockets methods to automatically
set errno upon failure, there is no reason for callers to use
the socket_error() method. They can rely on accessing errno
even on Win32. Remove all use of socket_error() from general
code, leaving it as a static method in oslib-win32.c only.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-03-07 20:36:03 +00:00
parent a2d96af4bb
commit b16a44e13e
11 changed files with 46 additions and 63 deletions

View File

@ -615,14 +615,13 @@ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
ret = qemu_co_send(sockfd, hdr, sizeof(*hdr)); ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
if (ret != sizeof(*hdr)) { if (ret != sizeof(*hdr)) {
error_report("failed to send a req, %s", strerror(errno)); error_report("failed to send a req, %s", strerror(errno));
ret = -socket_error(); return -errno;
return ret;
} }
ret = qemu_co_send(sockfd, data, *wlen); ret = qemu_co_send(sockfd, data, *wlen);
if (ret != *wlen) { if (ret != *wlen) {
ret = -socket_error();
error_report("failed to send a req, %s", strerror(errno)); error_report("failed to send a req, %s", strerror(errno));
return -errno;
} }
return ret; return ret;

View File

@ -40,8 +40,6 @@ void os_daemonize(void);
void os_setup_post(void); void os_setup_post(void);
int os_mlock(void); int os_mlock(void);
#define socket_error() errno
#define closesocket(s) close(s) #define closesocket(s) close(s)
#define ioctlsocket(s, r, v) ioctl(s, r, v) #define ioctlsocket(s, r, v) ioctl(s, r, v)

View File

@ -55,8 +55,6 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime_r(const time_t *timep, struct tm *result); struct tm *localtime_r(const time_t *timep, struct tm *result);
#endif /* CONFIG_LOCALTIME_R */ #endif /* CONFIG_LOCALTIME_R */
int socket_error(void);
static inline void os_setup_signal_handling(void) {} static inline void os_setup_signal_handling(void) {}
static inline void os_daemonize(void) {} static inline void os_daemonize(void) {}
static inline void os_setup_post(void) {} static inline void os_setup_post(void) {}

View File

@ -82,11 +82,11 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,
if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr, if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
&sioc->remoteAddrLen) < 0) { &sioc->remoteAddrLen) < 0) {
if (socket_error() == ENOTCONN) { if (errno == ENOTCONN) {
memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr)); memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
sioc->remoteAddrLen = sizeof(sioc->remoteAddr); sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
} else { } else {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to query remote socket address"); "Unable to query remote socket address");
goto error; goto error;
} }
@ -94,7 +94,7 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,
if (getsockname(fd, (struct sockaddr *)&sioc->localAddr, if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
&sioc->localAddrLen) < 0) { &sioc->localAddrLen) < 0) {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to query local socket address"); "Unable to query local socket address");
goto error; goto error;
} }
@ -356,7 +356,7 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
&cioc->remoteAddrLen); &cioc->remoteAddrLen);
if (cioc->fd < 0) { if (cioc->fd < 0) {
trace_qio_channel_socket_accept_fail(ioc); trace_qio_channel_socket_accept_fail(ioc);
if (socket_error() == EINTR) { if (errno == EINTR) {
goto retry; goto retry;
} }
goto error; goto error;
@ -364,7 +364,7 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr, if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
&cioc->localAddrLen) < 0) { &cioc->localAddrLen) < 0) {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to query local socket address"); "Unable to query local socket address");
goto error; goto error;
} }
@ -478,14 +478,14 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
retry: retry:
ret = recvmsg(sioc->fd, &msg, sflags); ret = recvmsg(sioc->fd, &msg, sflags);
if (ret < 0) { if (ret < 0) {
if (socket_error() == EAGAIN) { if (errno == EAGAIN) {
return QIO_CHANNEL_ERR_BLOCK; return QIO_CHANNEL_ERR_BLOCK;
} }
if (socket_error() == EINTR) { if (errno == EINTR) {
goto retry; goto retry;
} }
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to read from socket"); "Unable to read from socket");
return -1; return -1;
} }
@ -537,13 +537,13 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
retry: retry:
ret = sendmsg(sioc->fd, &msg, 0); ret = sendmsg(sioc->fd, &msg, 0);
if (ret <= 0) { if (ret <= 0) {
if (socket_error() == EAGAIN) { if (errno == EAGAIN) {
return QIO_CHANNEL_ERR_BLOCK; return QIO_CHANNEL_ERR_BLOCK;
} }
if (socket_error() == EINTR) { if (errno == EINTR) {
goto retry; goto retry;
} }
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to write to socket"); "Unable to write to socket");
return -1; return -1;
} }
@ -569,16 +569,16 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
iov[i].iov_len, iov[i].iov_len,
0); 0);
if (ret < 0) { if (ret < 0) {
if (socket_error() == EAGAIN) { if (errno == EAGAIN) {
if (done) { if (done) {
return done; return done;
} else { } else {
return QIO_CHANNEL_ERR_BLOCK; return QIO_CHANNEL_ERR_BLOCK;
} }
} else if (socket_error() == EINTR) { } else if (errno == EINTR) {
goto retry; goto retry;
} else { } else {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to read from socket"); "Unable to read from socket");
return -1; return -1;
} }
@ -611,16 +611,16 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
iov[i].iov_len, iov[i].iov_len,
0); 0);
if (ret < 0) { if (ret < 0) {
if (socket_error() == EAGAIN) { if (errno == EAGAIN) {
if (done) { if (done) {
return done; return done;
} else { } else {
return QIO_CHANNEL_ERR_BLOCK; return QIO_CHANNEL_ERR_BLOCK;
} }
} else if (socket_error() == EINTR) { } else if (errno == EINTR) {
goto retry; goto retry;
} else { } else {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to write to socket"); "Unable to write to socket");
return -1; return -1;
} }
@ -692,7 +692,7 @@ qio_channel_socket_close(QIOChannel *ioc,
#endif #endif
if (closesocket(sioc->fd) < 0) { if (closesocket(sioc->fd) < 0) {
sioc->fd = -1; sioc->fd = -1;
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to close socket"); "Unable to close socket");
return -1; return -1;
} }
@ -723,7 +723,7 @@ qio_channel_socket_shutdown(QIOChannel *ioc,
} }
if (shutdown(sioc->fd, sockhow) < 0) { if (shutdown(sioc->fd, sockhow) < 0) {
error_setg_errno(errp, socket_error(), error_setg_errno(errp, errno,
"Unable to shutdown socket"); "Unable to shutdown socket");
return -1; return -1;
} }

View File

@ -53,18 +53,16 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
} }
if (size > 0) { if (size > 0) {
err = socket_error(); if (errno != EAGAIN && errno != EWOULDBLOCK) {
if (err != EAGAIN && err != EWOULDBLOCK) {
error_report("socket_writev_buffer: Got err=%d for (%zu/%zu)", error_report("socket_writev_buffer: Got err=%d for (%zu/%zu)",
err, (size_t)size, (size_t)len); errno, (size_t)size, (size_t)len);
/* /*
* If I've already sent some but only just got the error, I * If I've already sent some but only just got the error, I
* could return the amount validly sent so far and wait for the * could return the amount validly sent so far and wait for the
* next call to report the error, but I'd rather flag the error * next call to report the error, but I'd rather flag the error
* immediately. * immediately.
*/ */
return -err; return -errno;
} }
/* Emulate blocking */ /* Emulate blocking */
@ -99,15 +97,15 @@ static ssize_t socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
if (len != -1) { if (len != -1) {
break; break;
} }
if (socket_error() == EAGAIN) { if (errno == EAGAIN) {
yield_until_fd_readable(s->fd); yield_until_fd_readable(s->fd);
} else if (socket_error() != EINTR) { } else if (errno != EINTR) {
break; break;
} }
} }
if (len == -1) { if (len == -1) {
len = -socket_error(); len = -errno;
} }
return len; return len;
} }

View File

@ -59,12 +59,11 @@ static void tcp_accept_incoming_migration(void *opaque)
socklen_t addrlen = sizeof(addr); socklen_t addrlen = sizeof(addr);
int s = (intptr_t)opaque; int s = (intptr_t)opaque;
QEMUFile *f; QEMUFile *f;
int c, err; int c;
do { do {
c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
err = socket_error(); } while (c < 0 && errno == EINTR);
} while (c < 0 && err == EINTR);
qemu_set_fd_handler(s, NULL, NULL, NULL); qemu_set_fd_handler(s, NULL, NULL, NULL);
closesocket(s); closesocket(s);
@ -72,7 +71,7 @@ static void tcp_accept_incoming_migration(void *opaque)
if (c < 0) { if (c < 0) {
error_report("could not accept migration connection (%s)", error_report("could not accept migration connection (%s)",
strerror(err)); strerror(errno));
return; return;
} }

View File

@ -145,15 +145,14 @@ static void net_socket_send_completed(NetClientState *nc, ssize_t len)
static void net_socket_send(void *opaque) static void net_socket_send(void *opaque)
{ {
NetSocketState *s = opaque; NetSocketState *s = opaque;
int size, err; int size;
unsigned l; unsigned l;
uint8_t buf1[NET_BUFSIZE]; uint8_t buf1[NET_BUFSIZE];
const uint8_t *buf; const uint8_t *buf;
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0); size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
if (size < 0) { if (size < 0) {
err = socket_error(); if (errno != EWOULDBLOCK)
if (err != EWOULDBLOCK)
goto eoc; goto eoc;
} else if (size == 0) { } else if (size == 0) {
/* end of connection */ /* end of connection */
@ -566,7 +565,7 @@ static int net_socket_connect_init(NetClientState *peer,
const char *host_str) const char *host_str)
{ {
NetSocketState *s; NetSocketState *s;
int fd, connected, ret, err; int fd, connected, ret;
struct sockaddr_in saddr; struct sockaddr_in saddr;
if (parse_host_port(&saddr, host_str) < 0) if (parse_host_port(&saddr, host_str) < 0)
@ -583,14 +582,12 @@ static int net_socket_connect_init(NetClientState *peer,
for(;;) { for(;;) {
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret < 0) { if (ret < 0) {
err = socket_error(); if (errno == EINTR || errno == EWOULDBLOCK) {
if (err == EINTR || err == EWOULDBLOCK) { /* continue */
} else if (err == EINPROGRESS) { } else if (errno == EINPROGRESS ||
errno == EALREADY ||
errno == EINVAL) {
break; break;
#ifdef _WIN32
} else if (err == WSAEALREADY || err == WSAEINVAL) {
break;
#endif
} else { } else {
perror("connect"); perror("connect");
closesocket(fd); closesocket(fd);

View File

@ -586,11 +586,7 @@ findso:
} }
if ((tcp_fconnect(so, so->so_ffamily) == -1) && if ((tcp_fconnect(so, so->so_ffamily) == -1) &&
#if defined(_WIN32)
socket_error() != WSAEWOULDBLOCK
#else
(errno != EINPROGRESS) && (errno != EWOULDBLOCK) (errno != EINPROGRESS) && (errno != EWOULDBLOCK)
#endif
) { ) {
u_char code=ICMP_UNREACH_NET; u_char code=ICMP_UNREACH_NET;
DEBUG_MISC((dfd, " tcp fconnect errno = %d-%s\n", DEBUG_MISC((dfd, " tcp fconnect errno = %d-%s\n",

View File

@ -145,7 +145,7 @@ int socket_set_fast_reuse(int fd)
} }
int socket_error(void) static int socket_error(void)
{ {
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
case 0: case 0:

View File

@ -35,18 +35,16 @@ qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
{ {
size_t done = 0; size_t done = 0;
ssize_t ret; ssize_t ret;
int err;
while (done < bytes) { while (done < bytes) {
ret = iov_send_recv(sockfd, iov, iov_cnt, ret = iov_send_recv(sockfd, iov, iov_cnt,
offset + done, bytes - done, do_send); offset + done, bytes - done, do_send);
if (ret > 0) { if (ret > 0) {
done += ret; done += ret;
} else if (ret < 0) { } else if (ret < 0) {
err = socket_error(); if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (err == EAGAIN || err == EWOULDBLOCK) {
qemu_coroutine_yield(); qemu_coroutine_yield();
} else if (done == 0) { } else if (done == 0) {
return -err; return -errno;
} else { } else {
break; break;
} }

View File

@ -268,7 +268,7 @@ static void wait_for_connect(void *opaque)
do { do {
rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize); rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
} while (rc == -1 && socket_error() == EINTR); } while (rc == -1 && errno == EINTR);
/* update rc to contain error */ /* update rc to contain error */
if (!rc && val) { if (!rc && val) {
@ -330,7 +330,7 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
do { do {
rc = 0; rc = 0;
if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
rc = -socket_error(); rc = -errno;
} }
} while (rc == -EINTR); } while (rc == -EINTR);
@ -787,7 +787,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp,
do { do {
rc = 0; rc = 0;
if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
rc = -socket_error(); rc = -errno;
} }
} while (rc == -EINTR); } while (rc == -EINTR);
@ -1082,7 +1082,7 @@ SocketAddress *socket_local_address(int fd, Error **errp)
socklen_t sslen = sizeof(ss); socklen_t sslen = sizeof(ss);
if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) { if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
error_setg_errno(errp, socket_error(), "%s", error_setg_errno(errp, errno, "%s",
"Unable to query local socket address"); "Unable to query local socket address");
return NULL; return NULL;
} }
@ -1097,7 +1097,7 @@ SocketAddress *socket_remote_address(int fd, Error **errp)
socklen_t sslen = sizeof(ss); socklen_t sslen = sizeof(ss);
if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) { if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
error_setg_errno(errp, socket_error(), "%s", error_setg_errno(errp, errno, "%s",
"Unable to query remote socket address"); "Unable to query remote socket address");
return NULL; return NULL;
} }