@@ -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 */
@@ -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 */
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(-)