nbd_connect_unix(3)
π nbd_connect_unix
okay bestie, so this is a page from the libnbd manual β section 3, nbd_connect_unix(3), the one about connecting to an NBD server over a Unix domain socket. yes, that one. sit down, get comfy, let's go π
β¨ NAME β¨
nbd_connect_unix β connect to an NBD server over a Unix domain socket. that's it, that's the whole job, and it does it flawlessly π
πΈ SYNOPSIS πΈ
#include <libnbd.h>
int nbd_connect_unix (
struct nbd_handle *h, const char *unixsocket
);
look at it. gorgeous. iconic. the #include is literally the first thing you do, don't skip it π
π¦ DESCRIPTION π¦
so here's the tea: this little function connects synchronously over the Unix domain socket you name in unixsocket, all the way to an NBD server that's running on the same machine as you. same machine, bestie. no network shenanigans, no travelling, the server is right there π«Ά
β RETURN VALUE β
if it all goes well? it returns 0. slay. that's your green flag right there β
π ERRORS π
if things go sideways it returns -1. not the vibe, but that's how you know π
if you want the actual details of what went wrong, go read "ERROR HANDLING" in libnbd(3) β that's where the lore lives.
also, heads up: these parameters must not be NULL: h, unixsocket. like, not even a little. for the whole explanation see "Non-NULL parameters" in libnbd(3).
π NOTES π
π₯ Blocking connect call
this call blocks β it comes back to you only once the connection has actually been made. no half-measures, no "we'll get back to you", it just does it.
by default it proceeds all the way through to the transmission phase, which is the whole journey. but if you're the kind of girl who needs control, you can use nbd_set_opt_mode(3) and take manual control over option negotiation performed before the transmission phase. main character energy tbh π§
π HANDLE STATE π
real talk: nbd_connect_unix can only be called when the handle is in a very specific state. vibe check time:
- Handle created, before connecting β β allowed
- Connecting β β error
- Connecting & handshaking (opt_mode) β β error
- Connected to the server β β error
- Connection shut down β β error
- Handle dead β β error
so basically: freshly created handle only. one shot. don't come back later expecting a second chance π
π VERSION π
this function first appeared in libnbd 1.0. an og. a legend. we love a debut π
if you need to test at compile time whether it's available, check whether this macro is defined:
#define LIBNBD_HAVE_NBD_CONNECT_UNIX 1
π EXAMPLE π
this example is also available as examples/fetch-first-sector.c in the libnbd source code, iykyk.
/* This example shows how to connect to an NBD server
* and fetch and print the first sector (usually the
* boot sector or partition table or filesystem
* superblock).
*
* You can test it with nbdkit like this:
*
* nbdkit -U - floppy . \
* --run './fetch-first-sector $unixsocket'
*
* The nbdkit floppy plugin creates an MBR disk so the
* first sector is the partition table.
*/
#include <stdio.h>
#include <stdlib.h>
#include <libnbd.h>
int
main (int argc, char *argv[])
{
struct nbd_handle *nbd;
char buf[512];
FILE *pp;
if (argc != 2) {
fprintf (stderr, "%s socket\n", argv[0]);
exit (EXIT_FAILURE);
}
/* Create the libnbd handle. */
nbd = nbd_create ();
if (nbd == NULL) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Connect to the NBD server over a
* Unix domain socket.
*/
if (nbd_connect_unix (nbd, argv[1]) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Read the first sector synchronously. */
if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Close the libnbd handle. */
nbd_close (nbd);
/* Print the first sector. */
pp = popen ("hexdump -C", "w");
if (pp == NULL) {
perror ("popen: hexdump");
exit (EXIT_FAILURE);
}
fwrite (buf, sizeof buf, 1, pp);
pclose (pp);
exit (EXIT_SUCCESS);
}
and yes, that comment block inside the code is doing the most and we respect it π also yes, it pipes to hexdump -C. hexdump girlies rise πΈ
π SEE ALSO π
go read them, they're all part of the same friend group π«Ά
π AUTHORS π
- Eric Blake
- Richard W.M. Jones
legends. both of them. periodt π
π 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
and that's the whole page, bestie π¦β¨ this one's from libnbd-1.25.8, dated 2026-10-13 β so it's fresh, it's current, it's giving documentation excellence ππ mwah, go connect to something π
π¦ 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