1nbd_connect_unix(3) LIBNBD nbd_connect_unix(3)
2
3
4
6 nbd_connect_unix - connect to NBD server over a Unix domain socket
7
9 #include <libnbd.h>
10
11 int nbd_connect_unix (struct nbd_handle *h,
12 const char *unixsocket);
13
15 Connect (synchronously) over the named Unix domain socket
16 ("unixsocket") to an NBD server running on the same machine. This call
17 returns when the connection has been made.
18
20 If the call is successful the function returns 0.
21
23 On error "-1" is returned.
24
25 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
26 of the error.
27
28 The following parameters must not be NULL: "h", "unixsocket". For more
29 information see "Non-NULL parameters" in libnbd(3).
30
32 The handle must be newly created, otherwise this call will return an
33 error.
34
36 This function first appeared in libnbd 1.0.
37
38 If you need to test if this function is available at compile time check
39 if the following macro is defined:
40
41 #define LIBNBD_HAVE_NBD_CONNECT_UNIX 1
42
44 This example is also available as examples/fetch-first-sector.c in the
45 libnbd source code.
46
47 /* This example shows how to connect to an NBD server
48 * and fetch and print the first sector (usually the
49 * boot sector or partition table or filesystem
50 * superblock).
51 *
52 * You can test it with nbdkit like this:
53 *
54 * nbdkit -U - floppy . \
55 * --run './fetch-first-sector $unixsocket'
56 *
57 * The nbdkit floppy plugin creates an MBR disk so the
58 * first sector is the partition table.
59 */
60
61 #include <stdio.h>
62 #include <stdlib.h>
63
64 #include <libnbd.h>
65
66 int
67 main (int argc, char *argv[])
68 {
69 struct nbd_handle *nbd;
70 char buf[512];
71 FILE *pp;
72
73 if (argc != 2) {
74 fprintf (stderr, "%s socket\n", argv[0]);
75 exit (EXIT_FAILURE);
76 }
77
78 /* Create the libnbd handle. */
79 nbd = nbd_create ();
80 if (nbd == NULL) {
81 fprintf (stderr, "%s\n", nbd_get_error ());
82 exit (EXIT_FAILURE);
83 }
84
85 /* Connect to the NBD server over a
86 * Unix domain socket.
87 */
88 if (nbd_connect_unix (nbd, argv[1]) == -1) {
89 fprintf (stderr, "%s\n", nbd_get_error ());
90 exit (EXIT_FAILURE);
91 }
92
93 /* Read the first sector synchronously. */
94 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
95 fprintf (stderr, "%s\n", nbd_get_error ());
96 exit (EXIT_FAILURE);
97 }
98
99 /* Close the libnbd handle. */
100 nbd_close (nbd);
101
102 /* Print the first sector. */
103 pp = popen ("hexdump -C", "w");
104 if (pp == NULL) {
105 perror ("popen: hexdump");
106 exit (EXIT_FAILURE);
107 }
108 fwrite (buf, sizeof buf, 1, pp);
109 pclose (pp);
110
111 exit (EXIT_SUCCESS);
112 }
113
115 nbd_aio_connect_unix(3), nbd_create(3), libnbd(3).
116
118 Eric Blake
119
120 Richard W.M. Jones
121
123 Copyright (C) 2019-2021 Red Hat Inc.
124
126 This library is free software; you can redistribute it and/or modify it
127 under the terms of the GNU Lesser General Public License as published
128 by the Free Software Foundation; either version 2 of the License, or
129 (at your option) any later version.
130
131 This library is distributed in the hope that it will be useful, but
132 WITHOUT ANY WARRANTY; without even the implied warranty of
133 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
134 Lesser General Public License for more details.
135
136 You should have received a copy of the GNU Lesser General Public
137 License along with this library; if not, write to the Free Software
138 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
139 02110-1301 USA
140
141
142
143libnbd-1.14.2 2023-01-03 nbd_connect_unix(3)