nbd_create3
Create libnbd handles and fetch errors
Usage
#include <libnbd.h>
struct nbd_handle *nbd;
struct nbd_handle *nbd_create (void);
void nbd_close (struct nbd_handle *nbd);
const char *nbd_get_error (void);
int nbd_get_errno (void);
Overview
struct nbd_handle is an opaque structure which describes an NBD client handle and the connection to an NBD server.
Creating a libnbd handle
nbd_create creates a new handle. It returns a pointer to the opaque handle structure.
On error this returns "NULL". See "ERROR HANDLING" in libnbd(3) for how to get further details of the error.
Closing a handle
nbd_close closes the handle, closing and freeing any associated resources.
The final status of any command that has not been retired (whether by nbd_aio_command_completed(3) or by a low-level completion callback returning 1) is lost.
This function is not safe to call while any other thread is still using any "nbd_*" API on the same handle. This function can block in the case where we wait for a subprocess (eg. one created with nbd_connect_command(3)).
Getting the latest error message in the thread
nbd_get_error returns the most recent error message in the current thread. The error message is only valid if called immediately after the failing call, from the same thread. The error string returned will be freed up next time any libnbd API is called from the same thread, so if you need to keep it you must make a copy.
This should never return "NULL" provided there was an error returned from the immediately preceding libnbd call in the current thread.
nbd_get_errno returns the most recent "errno" in the current thread. Not all errors have corresponding errnos, so even if there has been an error this may return 0. Error codes are the standard ones from "<errno.h>".
See "ERROR HANDLING" in libnbd(3) for more discussion of how error handling works in libnbd.
Examples
#include <libnbd.h>main (){struct nbd_handle *nbd;nbd = nbd_create ();if (nbd == NULL) {fprintf (stderr, "%s\n", nbd_get_error ());nbd_close (nbd);exit (EXIT_FAILURE);}nbd_close (nbd);exit (EXIT_SUCCESS);}
Version
These functions were all present in libnbd 1.0.
Releases
4 releases since Debian 11 (Bullseye); the text changed 3 times.