From: David Marchand <david.marchand@6wind.com>
To: qemu-devel@nongnu.org
Cc: kvm@vger.kernel.org, stefanha@gmail.com,
claudio.fontana@huawei.com, armbru@redhat.com,
arei.gonglei@huawei.com, pbonzini@redhat.com,
jani.kokkonen@huawei.com, cam@cs.ualberta.ca
Subject: [Qemu-devel] [PATCH v4 11/14] contrib/ivshmem-*: rework error handling
Date: Tue, 2 Sep 2014 17:25:29 +0200 [thread overview]
Message-ID: <1409671532-12706-12-git-send-email-david.marchand@6wind.com> (raw)
In-Reply-To: <1409671532-12706-1-git-send-email-david.marchand@6wind.com>
Following Gonglei comments, rework error handling using goto.
Signed-off-by: David Marchand <david.marchand@6wind.com>
---
contrib/ivshmem-client/ivshmem-client.c | 17 ++++++++---------
contrib/ivshmem-server/ivshmem-server.c | 19 ++++++++++---------
2 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/contrib/ivshmem-client/ivshmem-client.c b/contrib/ivshmem-client/ivshmem-client.c
index a08f4d9..e9a19ff 100644
--- a/contrib/ivshmem-client/ivshmem-client.c
+++ b/contrib/ivshmem-client/ivshmem-client.c
@@ -180,18 +180,14 @@ ivshmem_client_connect(IvshmemClient *client)
if (connect(client->sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
debug_log(client, "cannot connect to %s: %s\n", sun.sun_path,
strerror(errno));
- close(client->sock_fd);
- client->sock_fd = -1;
- return -1;
+ goto err_close;
}
/* first, we expect our index + a fd == -1 */
if (read_one_msg(client, &client->local.id, &fd) < 0 ||
client->local.id < 0 || fd != -1) {
debug_log(client, "cannot read from server\n");
- close(client->sock_fd);
- client->sock_fd = -1;
- return -1;
+ goto err_close;
}
debug_log(client, "our_id=%ld\n", client->local.id);
@@ -200,13 +196,16 @@ ivshmem_client_connect(IvshmemClient *client)
if (read_one_msg(client, &tmp, &fd) < 0 ||
tmp != -1 || fd < 0) {
debug_log(client, "cannot read from server (2)\n");
- close(client->sock_fd);
- client->sock_fd = -1;
- return -1;
+ goto err_close;
}
debug_log(client, "shm_fd=%d\n", fd);
return 0;
+
+err_close:
+ close(client->sock_fd);
+ client->sock_fd = -1;
+ return -1;
}
/* close connection to the server, and free all peer structures */
diff --git a/contrib/ivshmem-server/ivshmem-server.c b/contrib/ivshmem-server/ivshmem-server.c
index 4732dab..f441da7 100644
--- a/contrib/ivshmem-server/ivshmem-server.c
+++ b/contrib/ivshmem-server/ivshmem-server.c
@@ -264,7 +264,7 @@ ivshmem_server_start(IvshmemServer *server)
if (ivshmem_ftruncate(shm_fd, server->shm_size) < 0) {
fprintf(stderr, "ftruncate(%s) failed: %s\n", server->shm_path,
strerror(errno));
- return -1;
+ goto err_close_shm;
}
debug_log(server, "create & bind socket %s\n", server->unix_sock_path);
@@ -273,8 +273,7 @@ ivshmem_server_start(IvshmemServer *server)
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
debug_log(server, "cannot create socket: %s\n", strerror(errno));
- close(shm_fd);
- return -1;
+ goto err_close_shm;
}
sun.sun_family = AF_UNIX;
@@ -283,22 +282,24 @@ ivshmem_server_start(IvshmemServer *server)
if (bind(sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
debug_log(server, "cannot connect to %s: %s\n", sun.sun_path,
strerror(errno));
- close(sock_fd);
- close(shm_fd);
- return -1;
+ goto err_close_sock;
}
if (listen(sock_fd, IVSHMEM_SERVER_LISTEN_BACKLOG) < 0) {
debug_log(server, "listen() failed: %s\n", strerror(errno));
- close(sock_fd);
- close(shm_fd);
- return -1;
+ goto err_close_sock;
}
server->sock_fd = sock_fd;
server->shm_fd = shm_fd;
return 0;
+
+err_close_sock:
+ close(sock_fd);
+err_close_shm:
+ close(shm_fd);
+ return -1;
}
/* close connections to clients, the unix socket and the shm fd */
--
1.7.10.4
next prev parent reply other threads:[~2014-09-02 15:26 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-02 15:25 [Qemu-devel] [PATCH v4 00/14] ivshmem: update documentation, add client/server tools David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 01/14] contrib: add ivshmem client and server David Marchand
2014-09-02 20:20 ` Eric Blake
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 02/14] docs: update ivshmem device spec David Marchand
2014-09-02 20:24 ` Eric Blake
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 03/14] contrib/ivshmem-*: comply with QEMU coding style David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 04/14] contrib/ivshmem-*: reuse qemu/queue.h David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 05/14] contrib/ivshmem-*: switch to QEMU headers David Marchand
2014-09-02 20:28 ` Eric Blake
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 06/14] contrib/ivshmem-server: set client sockets as non blocking David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 07/14] contrib/ivshmem-*: add missing const and static attrs David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 08/14] contrib/ivshmem-*: plug client and server in QEMU top Makefile David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 09/14] contrib/ivshmem-*: switch to g_malloc0/g_free David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 10/14] contrib/ivshmem-server: fix mem leak on error David Marchand
2014-09-02 15:25 ` David Marchand [this message]
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 12/14] contrib/ivshmem-*: various fixes David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 13/14] contrib/ivshmem-server: align server default parameter values David Marchand
2014-09-02 15:25 ` [Qemu-devel] [PATCH v4 14/14] ivshmem: add check on protocol version in QEMU David Marchand
2014-09-02 20:31 ` [Qemu-devel] [PATCH v4 00/14] ivshmem: update documentation, add client/server tools Eric Blake
2014-09-03 13:01 ` David Marchand
2014-09-03 14:47 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1409671532-12706-12-git-send-email-david.marchand@6wind.com \
--to=david.marchand@6wind.com \
--cc=arei.gonglei@huawei.com \
--cc=armbru@redhat.com \
--cc=cam@cs.ualberta.ca \
--cc=claudio.fontana@huawei.com \
--cc=jani.kokkonen@huawei.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).