nbd_add_close_callback(3)
π 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 π
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
- centos-stream 10: libnbd-devel 1.25.8-1.el10 Β· LGPL-2.0-or-later AND BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms