=head1 NAME

nbd_add_close_callback - add a callback which is called on close

=head1 SYNOPSIS

 #include <libnbd.h>

 typedef struct {
   int (*callback) (void *user_data);
   void *user_data;
   void (*free) (void *user_data);
 } nbd_close_callback;

 int nbd_add_close_callback (
       struct nbd_handle *h,
       nbd_close_callback close_callback
     );

=head1 DESCRIPTION

libnbd handles maintain a list of callbacks
which are called when the handle is closed.  This adds a
callback to this list.

Callbacks cannot be removed from the list once added.
Callbacks might not necessarily be called in all circumstances
(eg. if the program crashes).

The callback function is declared like this (in the C bindings):

 int callback (void *user_data);

where the parameter is the C<user_data> passed to this function,
and an C<int> is returned.  The return value is ignored because
L<nbd_close(3)> returns C<void> and cannot be cancelled.

The callback should not call C<nbd_*> APIs on the same handle since it can
be called while holding the handle lock and will cause a deadlock.

If there are multiple close callbacks the order in which they
are called is not defined.  But all the callbacks are called
first followed by all the free functions.

=head1 RETURN VALUE

If the call is successful the function returns C<0>.

=head1 ERRORS

On error C<-1> is returned.

Refer to L<libnbd(3)/ERROR HANDLING>
for how to get further details of the error.

The following parameters must not be NULL: C<h>.
For more information see L<libnbd(3)/Non-NULL parameters>.

=head1 VERSION

This function first appeared in libnbd 1.26.

If you need to test if this function is available at compile time
check if the following macro is defined:

 #define LIBNBD_HAVE_NBD_ADD_CLOSE_CALLBACK 1

=head1 SEE ALSO

L<nbd_close(3)>,
L<nbd_create(3)>,
L<nbd_set_private_data(3)>,
L<libnbd(3)>,
L<atexit(3)>.

=head1 AUTHORS

Eric Blake

Richard W.M. Jones

=head1 COPYRIGHT

Copyright Red Hat
