Skip to content

Commit

Permalink
osdep: remove use of socket_error() from all code
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
berrange committed Mar 10, 2016
1 parent a2d96af commit b16a44e
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 63 deletions.
5 changes: 2 additions & 3 deletions block/sheepdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
if (ret != sizeof(*hdr)) {
error_report("failed to send a req, %s", strerror(errno));
ret = -socket_error();
return ret;
return -errno;
}

ret = qemu_co_send(sockfd, data, *wlen);
if (ret != *wlen) {
ret = -socket_error();
error_report("failed to send a req, %s", strerror(errno));
return -errno;
}

return ret;
Expand Down
2 changes: 0 additions & 2 deletions include/sysemu/os-posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ void os_daemonize(void);
void os_setup_post(void);
int os_mlock(void);

#define socket_error() errno

#define closesocket(s) close(s)
#define ioctlsocket(s, r, v) ioctl(s, r, v)

Expand Down
2 changes: 0 additions & 2 deletions include/sysemu/os-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
#endif /* CONFIG_LOCALTIME_R */

int socket_error(void);

static inline void os_setup_signal_handling(void) {}
static inline void os_daemonize(void) {}
static inline void os_setup_post(void) {}
Expand Down
38 changes: 19 additions & 19 deletions io/channel-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,

if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
&sioc->remoteAddrLen) < 0) {
if (socket_error() == ENOTCONN) {
if (errno == ENOTCONN) {
memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
} else {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to query remote socket address");
goto error;
}
}

if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
&sioc->localAddrLen) < 0) {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to query local socket address");
goto error;
}
Expand Down Expand Up @@ -356,15 +356,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
&cioc->remoteAddrLen);
if (cioc->fd < 0) {
trace_qio_channel_socket_accept_fail(ioc);
if (socket_error() == EINTR) {
if (errno == EINTR) {
goto retry;
}
goto error;
}

if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
&cioc->localAddrLen) < 0) {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to query local socket address");
goto error;
}
Expand Down Expand Up @@ -478,14 +478,14 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
retry:
ret = recvmsg(sioc->fd, &msg, sflags);
if (ret < 0) {
if (socket_error() == EAGAIN) {
if (errno == EAGAIN) {
return QIO_CHANNEL_ERR_BLOCK;
}
if (socket_error() == EINTR) {
if (errno == EINTR) {
goto retry;
}

error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to read from socket");
return -1;
}
Expand Down Expand Up @@ -537,13 +537,13 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
retry:
ret = sendmsg(sioc->fd, &msg, 0);
if (ret <= 0) {
if (socket_error() == EAGAIN) {
if (errno == EAGAIN) {
return QIO_CHANNEL_ERR_BLOCK;
}
if (socket_error() == EINTR) {
if (errno == EINTR) {
goto retry;
}
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to write to socket");
return -1;
}
Expand All @@ -569,16 +569,16 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
iov[i].iov_len,
0);
if (ret < 0) {
if (socket_error() == EAGAIN) {
if (errno == EAGAIN) {
if (done) {
return done;
} else {
return QIO_CHANNEL_ERR_BLOCK;
}
} else if (socket_error() == EINTR) {
} else if (errno == EINTR) {
goto retry;
} else {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to read from socket");
return -1;
}
Expand Down Expand Up @@ -611,16 +611,16 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
iov[i].iov_len,
0);
if (ret < 0) {
if (socket_error() == EAGAIN) {
if (errno == EAGAIN) {
if (done) {
return done;
} else {
return QIO_CHANNEL_ERR_BLOCK;
}
} else if (socket_error() == EINTR) {
} else if (errno == EINTR) {
goto retry;
} else {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to write to socket");
return -1;
}
Expand Down Expand Up @@ -692,7 +692,7 @@ qio_channel_socket_close(QIOChannel *ioc,
#endif
if (closesocket(sioc->fd) < 0) {
sioc->fd = -1;
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to close socket");
return -1;
}
Expand Down Expand Up @@ -723,7 +723,7 @@ qio_channel_socket_shutdown(QIOChannel *ioc,
}

if (shutdown(sioc->fd, sockhow) < 0) {
error_setg_errno(errp, socket_error(),
error_setg_errno(errp, errno,
"Unable to shutdown socket");
return -1;
}
Expand Down
14 changes: 6 additions & 8 deletions migration/qemu-file-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
}

if (size > 0) {
err = socket_error();

if (err != EAGAIN && err != EWOULDBLOCK) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
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
* 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
* immediately.
*/
return -err;
return -errno;
}

/* Emulate blocking */
Expand Down Expand Up @@ -99,15 +97,15 @@ static ssize_t socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
if (len != -1) {
break;
}
if (socket_error() == EAGAIN) {
if (errno == EAGAIN) {
yield_until_fd_readable(s->fd);
} else if (socket_error() != EINTR) {
} else if (errno != EINTR) {
break;
}
}

if (len == -1) {
len = -socket_error();
len = -errno;
}
return len;
}
Expand Down
7 changes: 3 additions & 4 deletions migration/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ static void tcp_accept_incoming_migration(void *opaque)
socklen_t addrlen = sizeof(addr);
int s = (intptr_t)opaque;
QEMUFile *f;
int c, err;
int c;

do {
c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
err = socket_error();
} while (c < 0 && err == EINTR);
} while (c < 0 && errno == EINTR);
qemu_set_fd_handler(s, NULL, NULL, NULL);
closesocket(s);

DPRINTF("accepted migration\n");

if (c < 0) {
error_report("could not accept migration connection (%s)",
strerror(err));
strerror(errno));
return;
}

Expand Down
19 changes: 8 additions & 11 deletions net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ static void net_socket_send_completed(NetClientState *nc, ssize_t len)
static void net_socket_send(void *opaque)
{
NetSocketState *s = opaque;
int size, err;
int size;
unsigned l;
uint8_t buf1[NET_BUFSIZE];
const uint8_t *buf;

size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
if (size < 0) {
err = socket_error();
if (err != EWOULDBLOCK)
if (errno != EWOULDBLOCK)
goto eoc;
} else if (size == 0) {
/* end of connection */
Expand Down Expand Up @@ -566,7 +565,7 @@ static int net_socket_connect_init(NetClientState *peer,
const char *host_str)
{
NetSocketState *s;
int fd, connected, ret, err;
int fd, connected, ret;
struct sockaddr_in saddr;

if (parse_host_port(&saddr, host_str) < 0)
Expand All @@ -583,14 +582,12 @@ static int net_socket_connect_init(NetClientState *peer,
for(;;) {
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret < 0) {
err = socket_error();
if (err == EINTR || err == EWOULDBLOCK) {
} else if (err == EINPROGRESS) {
break;
#ifdef _WIN32
} else if (err == WSAEALREADY || err == WSAEINVAL) {
if (errno == EINTR || errno == EWOULDBLOCK) {
/* continue */
} else if (errno == EINPROGRESS ||
errno == EALREADY ||
errno == EINVAL) {
break;
#endif
} else {
perror("connect");
closesocket(fd);
Expand Down
4 changes: 0 additions & 4 deletions slirp/tcp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
}

if ((tcp_fconnect(so, so->so_ffamily) == -1) &&
#if defined(_WIN32)
socket_error() != WSAEWOULDBLOCK
#else
(errno != EINPROGRESS) && (errno != EWOULDBLOCK)
#endif
) {
u_char code=ICMP_UNREACH_NET;
DEBUG_MISC((dfd, " tcp fconnect errno = %d-%s\n",
Expand Down
2 changes: 1 addition & 1 deletion util/oslib-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int socket_set_fast_reuse(int fd)
}


int socket_error(void)
static int socket_error(void)
{
switch (WSAGetLastError()) {
case 0:
Expand Down
6 changes: 2 additions & 4 deletions util/qemu-coroutine-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
{
size_t done = 0;
ssize_t ret;
int err;
while (done < bytes) {
ret = iov_send_recv(sockfd, iov, iov_cnt,
offset + done, bytes - done, do_send);
if (ret > 0) {
done += ret;
} else if (ret < 0) {
err = socket_error();
if (err == EAGAIN || err == EWOULDBLOCK) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
qemu_coroutine_yield();
} else if (done == 0) {
return -err;
return -errno;
} else {
break;
}
Expand Down
10 changes: 5 additions & 5 deletions util/qemu-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static void wait_for_connect(void *opaque)

do {
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 */
if (!rc && val) {
Expand Down Expand Up @@ -330,7 +330,7 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
do {
rc = 0;
if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
rc = -socket_error();
rc = -errno;
}
} while (rc == -EINTR);

Expand Down Expand Up @@ -787,7 +787,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp,
do {
rc = 0;
if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
rc = -socket_error();
rc = -errno;
}
} while (rc == -EINTR);

Expand Down Expand Up @@ -1082,7 +1082,7 @@ SocketAddress *socket_local_address(int fd, Error **errp)
socklen_t sslen = sizeof(ss);

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");
return NULL;
}
Expand All @@ -1097,7 +1097,7 @@ SocketAddress *socket_remote_address(int fd, Error **errp)
socklen_t sslen = sizeof(ss);

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");
return NULL;
}
Expand Down

0 comments on commit b16a44e

Please sign in to comment.