qemu-sockets: add error propagation to inet_dgram_opts

Before:

    $ qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
    inet_dgram_opts: bind(ipv4,127.0.0.1,631): OK
    inet_dgram_opts failed
    chardev: opening backend "udp" failed

After:

    $ x86_64-softmmu/qemu-system-x86_64 -monitor udp:localhost:631@localhost:631
    qemu-system-x86_64: -monitor udp:localhost:631@localhost:631: Failed to bind socket: Address already in use
    chardev: opening backend "udp" failed

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2012-10-02 09:25:14 +02:00
parent 11663b553b
commit 4f085c8229
2 changed files with 8 additions and 27 deletions

View file

@ -2105,7 +2105,6 @@ static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
fd = inet_dgram_opts(opts, &local_err);
if (fd < 0) {
fprintf(stderr, "inet_dgram_opts failed\n");
goto return_err;
}

View file

@ -396,8 +396,6 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
struct addrinfo ai, *peer = NULL, *local = NULL;
const char *addr;
const char *port;
char uaddr[INET6_ADDRSTRLEN+1];
char uport[33];
int sock = -1, rc;
/* lookup peer addr */
@ -412,7 +410,7 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
addr = "localhost";
}
if (port == NULL || strlen(port) == 0) {
fprintf(stderr, "inet_dgram: port not specified\n");
error_setg(errp, "remote port not specified");
return -1;
}
@ -422,8 +420,8 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
ai.ai_family = PF_INET6;
if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
gai_strerror(rc));
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
gai_strerror(rc));
return -1;
}
@ -442,44 +440,28 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
port = "0";
if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
gai_strerror(rc));
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
gai_strerror(rc));
goto err;
}
/* create socket */
sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
if (sock < 0) {
fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
inet_strfamily(peer->ai_family), strerror(errno));
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
goto err;
}
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
/* bind socket */
if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
uaddr,INET6_ADDRSTRLEN,uport,32,
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
goto err;
}
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
inet_strfamily(local->ai_family), uaddr, inet_getport(local));
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
goto err;
}
/* connect to peer */
if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
uaddr, INET6_ADDRSTRLEN, uport, 32,
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
goto err;
}
if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
inet_strfamily(peer->ai_family),
peer->ai_canonname, uaddr, uport, strerror(errno));
error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
goto err;
}