aboutsummaryrefslogtreecommitdiff
path: root/net/socket.c
diff options
context:
space:
mode:
authorMao Zhongyi <maozy.fnst@cn.fujitsu.com>2017-09-04 22:35:40 +0800
committerJason Wang <jasowang@redhat.com>2017-09-08 08:17:37 +0800
commit0522a959aec29768610900636f6234ab40530f82 (patch)
tree86513fab9d0c58d8806211b89a160819782e62b2 /net/socket.c
parentbcd4dfd6852361361d6622de7e67f94e2b475d27 (diff)
net/socket: Improve -net socket error reporting
When -net socket fails, it first reports a specific error, then a generic one, like this: $ ./x86_64-softmmu/qemu-system-x86_64 -net socket,mcast=230.0.0.1:1234,listen qemu-system-x86_64: -net socket,mcast=230.0.0.1:1234,listen: exactly one of listen=, connect=, mcast= or udp= is required qemu-system-x86_64: -net socket,mcast=230.0.0.1:1234,listen: Device 'socket' could not be initialized Convert net_socket_*_init() to Error to get rid of the superfluous second error message. After the patch, the effect like this: $ ./x86_64-softmmu/qemu-system-x86_64 -net socket,mcast=230.0.0.1:1234,listen qemu-system-x86_64: -net socket,mcast=230.0.0.1:1234,listen: exactly one of listen=, connect=, mcast= or udp= is requireda This also fixes a few silent failures to report an error. Cc: jasowang@redhat.com Cc: armbru@redhat.com Cc: berrange@redhat.com Signed-off-by: Mao Zhongyi <maozy.fnst@cn.fujitsu.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Jason Wang <jasowang@redhat.com>
Diffstat (limited to 'net/socket.c')
-rw-r--r--net/socket.c92
1 files changed, 44 insertions, 48 deletions
diff --git a/net/socket.c b/net/socket.c
index 12495d1ce8..e6b471c63d 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -489,22 +489,21 @@ static void net_socket_accept(void *opaque)
static int net_socket_listen_init(NetClientState *peer,
const char *model,
const char *name,
- const char *host_str)
+ const char *host_str,
+ Error **errp)
{
NetClientState *nc;
NetSocketState *s;
struct sockaddr_in saddr;
int fd, ret;
- Error *err = NULL;
- if (parse_host_port(&saddr, host_str, &err) < 0) {
- error_report_err(err);
+ if (parse_host_port(&saddr, host_str, errp) < 0) {
return -1;
}
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
- perror("socket");
+ error_setg_errno(errp, errno, "can't create stream socket");
return -1;
}
qemu_set_nonblock(fd);
@@ -513,13 +512,14 @@ static int net_socket_listen_init(NetClientState *peer,
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret < 0) {
- perror("bind");
+ error_setg_errno(errp, errno, "can't bind ip=%s to socket",
+ inet_ntoa(saddr.sin_addr));
closesocket(fd);
return -1;
}
ret = listen(fd, 0);
if (ret < 0) {
- perror("listen");
+ error_setg_errno(errp, errno, "can't listen on socket");
closesocket(fd);
return -1;
}
@@ -538,21 +538,20 @@ static int net_socket_listen_init(NetClientState *peer,
static int net_socket_connect_init(NetClientState *peer,
const char *model,
const char *name,
- const char *host_str)
+ const char *host_str,
+ Error **errp)
{
NetSocketState *s;
int fd, connected, ret;
struct sockaddr_in saddr;
- Error *err = NULL;
- if (parse_host_port(&saddr, host_str, &err) < 0) {
- error_report_err(err);
+ if (parse_host_port(&saddr, host_str, errp) < 0) {
return -1;
}
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
- perror("socket");
+ error_setg_errno(errp, errno, "can't create stream socket");
return -1;
}
qemu_set_nonblock(fd);
@@ -568,7 +567,7 @@ static int net_socket_connect_init(NetClientState *peer,
errno == EINVAL) {
break;
} else {
- perror("connect");
+ error_setg_errno(errp, errno, "can't connect socket");
closesocket(fd);
return -1;
}
@@ -577,9 +576,8 @@ static int net_socket_connect_init(NetClientState *peer,
break;
}
}
- s = net_socket_fd_init(peer, model, name, fd, connected, NULL, &err);
+ s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp);
if (!s) {
- error_report_err(err);
return -1;
}
@@ -593,36 +591,36 @@ static int net_socket_mcast_init(NetClientState *peer,
const char *model,
const char *name,
const char *host_str,
- const char *localaddr_str)
+ const char *localaddr_str,
+ Error **errp)
{
NetSocketState *s;
int fd;
struct sockaddr_in saddr;
struct in_addr localaddr, *param_localaddr;
- Error *err = NULL;
- if (parse_host_port(&saddr, host_str, &err) < 0) {
- error_report_err(err);
+ if (parse_host_port(&saddr, host_str, errp) < 0) {
return -1;
}
if (localaddr_str != NULL) {
- if (inet_aton(localaddr_str, &localaddr) == 0)
+ if (inet_aton(localaddr_str, &localaddr) == 0) {
+ error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
+ localaddr_str);
return -1;
+ }
param_localaddr = &localaddr;
} else {
param_localaddr = NULL;
}
- fd = net_socket_mcast_create(&saddr, param_localaddr, &err);
+ fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
if (fd < 0) {
- error_report_err(err);
return -1;
}
- s = net_socket_fd_init(peer, model, name, fd, 0, NULL, &err);
+ s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
if (!s) {
- error_report_err(err);
return -1;
}
@@ -639,45 +637,45 @@ static int net_socket_udp_init(NetClientState *peer,
const char *model,
const char *name,
const char *rhost,
- const char *lhost)
+ const char *lhost,
+ Error **errp)
{
NetSocketState *s;
int fd, ret;
struct sockaddr_in laddr, raddr;
- Error *err = NULL;
- if (parse_host_port(&laddr, lhost, &err) < 0) {
- error_report_err(err);
+ if (parse_host_port(&laddr, lhost, errp) < 0) {
return -1;
}
- if (parse_host_port(&raddr, rhost, &err) < 0) {
- error_report_err(err);
+ if (parse_host_port(&raddr, rhost, errp) < 0) {
return -1;
}
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
- perror("socket(PF_INET, SOCK_DGRAM)");
+ error_setg_errno(errp, errno, "can't create datagram socket");
return -1;
}
ret = socket_set_fast_reuse(fd);
if (ret < 0) {
+ error_setg_errno(errp, errno,
+ "can't set socket option SO_REUSEADDR");
closesocket(fd);
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
if (ret < 0) {
- perror("bind");
+ error_setg_errno(errp, errno, "can't bind ip=%s to socket",
+ inet_ntoa(laddr.sin_addr));
closesocket(fd);
return -1;
}
qemu_set_nonblock(fd);
- s = net_socket_fd_init(peer, model, name, fd, 0, NULL, &err);
+ s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
if (!s) {
- error_report_err(err);
return -1;
}
@@ -692,8 +690,6 @@ static int net_socket_udp_init(NetClientState *peer,
int net_init_socket(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp)
{
- /* FIXME error_setg(errp, ...) on failure */
- Error *err = NULL;
const NetdevSocketOptions *sock;
assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
@@ -701,22 +697,21 @@ int net_init_socket(const Netdev *netdev, const char *name,
if (sock->has_listen + sock->has_connect + sock->has_mcast +
sock->has_udp > 1) {
- error_report("exactly one of listen=, connect=, mcast= or udp="
- " is required");
+ error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
+ " is required");
return -1;
}
if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
- error_report("localaddr= is only valid with mcast= or udp=");
+ error_setg(errp, "localaddr= is only valid with mcast= or udp=");
return -1;
}
if (sock->has_fd) {
int fd;
- fd = monitor_fd_param(cur_mon, sock->fd, &err);
+ fd = monitor_fd_param(cur_mon, sock->fd, errp);
if (fd == -1) {
- error_report_err(err);
return -1;
}
qemu_set_nonblock(fd);
@@ -728,15 +723,16 @@ int net_init_socket(const Netdev *netdev, const char *name,
}
if (sock->has_listen) {
- if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
+ if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
+ < 0) {
return -1;
}
return 0;
}
if (sock->has_connect) {
- if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
- -1) {
+ if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
+ < 0) {
return -1;
}
return 0;
@@ -746,7 +742,7 @@ int net_init_socket(const Netdev *netdev, const char *name,
/* if sock->localaddr is missing, it has been initialized to "all bits
* zero" */
if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
- sock->localaddr) == -1) {
+ sock->localaddr, errp) < 0) {
return -1;
}
return 0;
@@ -754,11 +750,11 @@ int net_init_socket(const Netdev *netdev, const char *name,
assert(sock->has_udp);
if (!sock->has_localaddr) {
- error_report("localaddr= is mandatory with udp=");
+ error_setg(errp, "localaddr= is mandatory with udp=");
return -1;
}
- if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
- -1) {
+ if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr,
+ errp) < 0) {
return -1;
}
return 0;