nbd: Always call "close_fn" in nbd_client_new

Rename the parameter "close" to "close_fn" to disambiguous with
close(2).

This unifies error handling paths of NBDClient allocation:
nbd_client_new will shutdown the socket and call the "close_fn" callback
if negotiation failed, so the caller don't need a different path than
the normal close.

The returned pointer is never used, make it void in preparation for the
next patch.

Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <1452760863-25350-2-git-send-email-famz@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Fam Zheng 2016-01-14 16:41:01 +08:00 committed by Paolo Bonzini
parent e1dc68155c
commit ee7d7aabda
4 changed files with 11 additions and 18 deletions

View File

@ -27,9 +27,8 @@ static void nbd_accept(void *opaque)
socklen_t addr_len = sizeof(addr); socklen_t addr_len = sizeof(addr);
int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) { if (fd >= 0) {
shutdown(fd, 2); nbd_client_new(NULL, fd, nbd_client_put);
close(fd);
} }
} }

View File

@ -98,8 +98,7 @@ NBDExport *nbd_export_find(const char *name);
void nbd_export_set_name(NBDExport *exp, const char *name); void nbd_export_set_name(NBDExport *exp, const char *name);
void nbd_export_close_all(void); void nbd_export_close_all(void);
NBDClient *nbd_client_new(NBDExport *exp, int csock, void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *));
void (*close)(NBDClient *));
void nbd_client_get(NBDClient *client); void nbd_client_get(NBDClient *client);
void nbd_client_put(NBDClient *client); void nbd_client_put(NBDClient *client);

11
nbd.c
View File

@ -1475,8 +1475,7 @@ static void nbd_update_can_read(NBDClient *client)
} }
} }
NBDClient *nbd_client_new(NBDExport *exp, int csock, void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *))
void (*close)(NBDClient *))
{ {
NBDClient *client; NBDClient *client;
client = g_malloc0(sizeof(NBDClient)); client = g_malloc0(sizeof(NBDClient));
@ -1485,10 +1484,11 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,
client->sock = csock; client->sock = csock;
client->can_read = true; client->can_read = true;
if (nbd_send_negotiate(client)) { if (nbd_send_negotiate(client)) {
g_free(client); shutdown(client->sock, 2);
return NULL; close_fn(client);
return;
} }
client->close = close; client->close = close_fn;
qemu_co_mutex_init(&client->send_lock); qemu_co_mutex_init(&client->send_lock);
nbd_set_handlers(client); nbd_set_handlers(client);
@ -1496,5 +1496,4 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,
QTAILQ_INSERT_TAIL(&exp->clients, client, next); QTAILQ_INSERT_TAIL(&exp->clients, client, next);
nbd_export_get(exp); nbd_export_get(exp);
} }
return client;
} }

View File

@ -333,13 +333,9 @@ static void nbd_accept(void *opaque)
return; return;
} }
if (nbd_client_new(exp, fd, nbd_client_closed)) { nb_fds++;
nb_fds++; nbd_update_server_fd_handler(server_fd);
nbd_update_server_fd_handler(server_fd); nbd_client_new(exp, fd, nbd_client_closed);
} else {
shutdown(fd, 2);
close(fd);
}
} }
static void nbd_update_server_fd_handler(int fd) static void nbd_update_server_fd_handler(int fd)