@@ -509,7 +509,8 @@ static int net_socket_listen_init(NetClientState *peer,
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
- perror("socket");
+ error_setg_errno(errp, errno, "Create socket(PF_INET,"
+ " SOCK_STREAM) failed");
return -1;
}
qemu_set_nonblock(fd);
@@ -518,13 +519,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, "Bind ip=%s to fd=%d failed",
+ inet_ntoa(saddr.sin_addr), fd);
closesocket(fd);
return -1;
}
ret = listen(fd, 0);
if (ret < 0) {
- perror("listen");
+ error_setg_errno(errp, errno, "Listen socket fd=%d failed", fd);
closesocket(fd);
return -1;
}
@@ -556,7 +558,8 @@ static int net_socket_connect_init(NetClientState *peer,
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
- perror("socket");
+ error_setg_errno(errp, errno, "Create socket(PF_INET,"
+ " SOCK_STREAM) failed");
return -1;
}
qemu_set_nonblock(fd);
@@ -572,7 +575,7 @@ static int net_socket_connect_init(NetClientState *peer,
errno == EINVAL) {
break;
} else {
- perror("connect");
+ error_setg_errno(errp, errno, "Connection failed");
closesocket(fd);
return -1;
}
@@ -606,8 +609,11 @@ static int net_socket_mcast_init(NetClientState *peer,
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;
@@ -651,18 +657,22 @@ static int net_socket_udp_init(NetClientState *peer,
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
- perror("socket(PF_INET, SOCK_DGRAM)");
+ error_setg_errno(errp, errno, "Create socket(PF_INET,"
+ " SOCK_DGRAM) failed");
return -1;
}
ret = socket_set_fast_reuse(fd);
if (ret < 0) {
+ error_setg_errno(errp, errno, "Set the socket fd=%d attribute"
+ " SO_REUSEADDR failed", fd);
closesocket(fd);
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
if (ret < 0) {
- perror("bind");
+ error_setg_errno(errp, errno, "Bind ip=%s to fd=%d failed",
+ inet_ntoa(laddr.sin_addr), fd);
closesocket(fd);
return -1;
}
@@ -684,8 +694,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);
@@ -693,22 +701,21 @@ int net_init_socket(const Netdev *netdev, const char *name,
if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
sock->has_udp != 1) {
- error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
+ error_setg(errp, "exactly one of fd=, 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);
@@ -746,7 +753,7 @@ 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,
When -net socket fails, it first reports a specific error, then a generic one, like this: $ qemu-system-x86_64 -net socket, qemu-system-x86_64: -net socket: exactly one of fd=, listen=, connect=, mcast= or udp= is required qemu-system-x86_64: -net socket: Device 'socket' could not be initialized Convert net_init_socket() to Error to get rid of the superfluous second error message. After the patch, the effect like this: $ qemu-system-x86_64 -net socket, qemu-system-x86_64: -net socket: exactly one of fd=, listen=, connect=, mcast= or udp= is required At the same time, add many explicit error handling message when it fails. Cc: jasowang@redhat.com Cc: armbru@redhat.com Signed-off-by: Mao Zhongyi <maozy.fnst@cn.fujitsu.com> --- net/socket.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-)