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 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