On 29/04/2015 12:37, Fam Zheng wrote: > Achieved by: > > - Remembering the server fd with a global variable, in order to access > it from nbd_client_closed. > > - Checking nbd_can_accept() and updating server_fd handler whenever > client connects or disconnects. > > Signed-off-by: Fam Zheng <f...@redhat.com> > --- > qemu-nbd.c | 21 +++++++++++++++++---- > 1 file changed, 17 insertions(+), 4 deletions(-) > > diff --git a/qemu-nbd.c b/qemu-nbd.c > index 7e690ff..5af6d11 100644 > --- a/qemu-nbd.c > +++ b/qemu-nbd.c > @@ -53,6 +53,7 @@ static int persistent = 0; > static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state; > static int shared = 1; > static int nb_fds; > +static int server_fd; > > static void usage(const char *name) > { > @@ -340,7 +341,7 @@ out: > return (void *) EXIT_FAILURE; > } > > -static int nbd_can_accept(void *opaque) > +static int nbd_can_accept(void) > { > return nb_fds < shared; > } > @@ -351,19 +352,21 @@ static void nbd_export_closed(NBDExport *exp) > state = TERMINATED; > } > > +static void nbd_update_server_fd_handler(int fd); > + > static void nbd_client_closed(NBDClient *client) > { > nb_fds--; > if (nb_fds == 0 && !persistent && state == RUNNING) { > state = TERMINATE; > } > + nbd_update_server_fd_handler(server_fd); > qemu_notify_event(); > nbd_client_put(client); > } > > static void nbd_accept(void *opaque) > { > - int server_fd = (uintptr_t) opaque; > struct sockaddr_in addr; > socklen_t addr_len = sizeof(addr); > > @@ -380,12 +383,22 @@ static void nbd_accept(void *opaque) > > if (nbd_client_new(exp, fd, nbd_client_closed)) { > nb_fds++; > + nbd_update_server_fd_handler(server_fd); > } else { > shutdown(fd, 2); > close(fd); > } > } > > +static void nbd_update_server_fd_handler(int fd) > +{ > + if (nbd_can_accept()) { > + qemu_set_fd_handler(fd, nbd_accept, NULL, (void *)(uintptr_t)fd); > + } else { > + qemu_set_fd_handler(fd, NULL, NULL, NULL); > + } > +} > + > int main(int argc, char **argv) > { > BlockBackend *blk; > @@ -761,8 +774,8 @@ int main(int argc, char **argv) > memset(&client_thread, 0, sizeof(client_thread)); > } > > - qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL, > - (void *)(uintptr_t)fd); > + server_fd = fd; > + nbd_update_server_fd_handler(fd); > > /* now when the initialization is (almost) complete, chdir("/") > * to free any busy filesystems */ >
Reviewed-by: Paolo Bonzini <pbonz...@redhat.com>