nbd_add_close_callback(3)

πŸ“¦
version 1.25.8 Β· 2 versions
, in centos-stream 10

πŸ’– nbd_add_close_callback(3) β€” the LIBNBD bestie guide πŸ’–

okay so this is nbd_add_close_callback(3), from the LIBNBD manual, and babes, it does exactly what it says on the tin: it lets you register a callback that fires when your handle gets closed. very "leave a note for future you" energy. let's get into it ✨


πŸŽ€ NAME πŸŽ€

nbd_add_close_callback β€” add a callback which is called on close

✨ 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
    );

like, look at that struct. it's giving organized. it's giving "I have a free function, I'm responsible". we love to see it πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

so the tea is: libnbd handles keep a whole lil list of callbacks, and every single one of them gets called when the handle is closed. this function right here? it adds one to that list. that's it. that's the whole vibe. 🫢

⚠️ bestie, listen: callbacks cannot be removed from the list once added. no takebacks. no undo. it's in there forever, like that one screenshot you forgot to delete. also, callbacks might not necessarily be called in all circumstances (eg. if the program crashes) β€” like, a crash is a crash, nobody's running your callback after that πŸ’€

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

int callback (void *user_data);

where the parameter is the user_data passed to this function, and an int is returned. and the return value? ignored. completely. because nbd_close(3) returns void and cannot be cancelled β€” there's literally nothing to do with your int, so it goes straight in the void. πŸ™ˆ

😭 ok important: the callback should not call nbd_* APIs on the same handle, because it can be called while holding the handle lock and will cause a deadlock. like, do not do that. deadlock is not the aesthetic. periodt.

if there are multiple close callbacks, the order in which they are called is not defined. nope. unknown. mystery. but here's the one thing that is guaranteed: all the callbacks are called first, followed by all the free functions. callbacks first, frees after. that's the rule πŸ”₯

⭐ RETURN VALUE ⭐

if the call is successful the function returns 0. slay. ✨

πŸ’… ERRORS πŸ’…

on error -1 is returned. 😭

refer to "ERROR HANDLING" in libnbd(3) for how to get further details of the error β€” that's your group chat for error lore.

also: the following parameters must not be NULL: h. for more information see "Non-NULL parameters" in libnbd(3). like, h is the main character here, don't NULL it πŸ™

πŸ‘‘ VERSION πŸ‘‘

this function first appeared in libnbd 1.26. she's new. she's fresh. she's iconic. πŸŽ€

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

🌈 SEE ALSO 🌈

(iykyk β€” atexit(3) is literally the same energy but for your whole process 🧚)

🌸 AUTHORS 🌸

Eric Blake

Richard W.M. Jones

Copyright Red Hat

πŸ’œ LICENSE πŸ’œ

this library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

this library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. see the GNU Lesser General Public License for more details.

you should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


πŸ¦‹ ok that's the whole page, bestie β€” go forth and close your handles responsibly. this was libnbd-1.25.8, dated 2026-10-13, and it's nbd_add_close_callback(3) signing off πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from