1nbd_aio_connect_unix(3)             LIBNBD             nbd_aio_connect_unix(3)
2
3
4

NAME

6       nbd_aio_connect_unix - connect to the NBD server over a Unix domain
7       socket
8

SYNOPSIS

10        #include <libnbd.h>
11
12        int nbd_aio_connect_unix (struct nbd_handle *h,
13                                  const char *unixsocket);
14

DESCRIPTION

16       Begin connecting to the NBD server over Unix domain socket
17       ("unixsocket").  Parameters behave as documented in
18       nbd_connect_unix(3).
19
20       You can check if the connection is still connecting by calling
21       nbd_aio_is_connecting(3), or if it has connected to the server and
22       completed the NBD handshake by calling nbd_aio_is_ready(3), on the
23       connection.
24

RETURN VALUE

26       If the call is successful the function returns 0.
27

ERRORS

29       On error "-1" is returned.
30
31       Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
32       of the error.
33

HANDLE STATE

35       The handle must be newly created, otherwise this call will return an
36       error.
37

VERSION

39       This function first appeared in libnbd 1.0.
40
41       If you need to test if this function is available at compile time check
42       if the following macro is defined:
43
44        #define LIBNBD_HAVE_NBD_AIO_CONNECT_UNIX 1
45

EXAMPLE

47       This example is also available as examples/aio-connect-read.c in the
48       libnbd source code.
49
50        /* This example shows how to use the AIO (asynchronous) low
51         * level API to connect to a server and read the disk.
52         *
53         * Here are a few ways to try this example:
54         *
55         * nbdkit -U - linuxdisk . \
56         *   --run './aio-connect-read $unixsocket'
57         *
58         * nbdkit -U - floppy . \
59         *   --run './aio-connect-read $unixsocket'
60         *
61         * nbdkit -U - pattern size=1M \
62         *   --run './aio-connect-read $unixsocket'
63         */
64
65        #include <stdio.h>
66        #include <stdlib.h>
67        #include <stdint.h>
68        #include <inttypes.h>
69        #include <assert.h>
70
71        #include <libnbd.h>
72
73        #define NR_SECTORS 32
74        #define SECTOR_SIZE 512
75
76        struct data {
77          uint64_t offset;
78          char sector[SECTOR_SIZE];
79        };
80
81        static int
82        hexdump (void *user_data, int *error)
83        {
84          struct data *data = user_data;
85          FILE *pp;
86
87          printf ("sector at offset 0x%" PRIx64 ":\n",
88                  data->offset);
89          pp = popen ("hexdump -C", "w");
90          if (pp == NULL) {
91            perror ("popen: hexdump");
92            exit (EXIT_FAILURE);
93          }
94          fwrite (data->sector, SECTOR_SIZE, 1, pp);
95          pclose (pp);
96          printf ("\n");
97
98          /* Returning 1 from the callback automatically retires
99           * the command.
100           */
101          return 1;
102        }
103
104        static struct data data[NR_SECTORS];
105
106        int
107        main (int argc, char *argv[])
108        {
109          struct nbd_handle *nbd;
110          size_t i;
111
112          if (argc != 2) {
113            fprintf (stderr, "%s socket\n", argv[0]);
114            exit (EXIT_FAILURE);
115          }
116
117          /* Create the libnbd handle. */
118          nbd = nbd_create ();
119          if (nbd == NULL) {
120            fprintf (stderr, "%s\n", nbd_get_error ());
121            exit (EXIT_FAILURE);
122          }
123
124          /* Connect to the NBD server over a Unix domain socket.
125           * This only starts the connection.
126           */
127          if (nbd_aio_connect_unix (nbd, argv[1]) == -1) {
128            fprintf (stderr, "%s\n", nbd_get_error ());
129            exit (EXIT_FAILURE);
130          }
131
132          /* Wait for the connection to complete.  The use of
133           * nbd_poll here is only as an example.  You could also
134           * integrate this with poll(2), glib or another main
135           * loop.  Read libnbd(3) and the source file lib/poll.c.
136           */
137          while (!nbd_aio_is_ready (nbd)) {
138            if (nbd_poll (nbd, -1) == -1) {
139              fprintf (stderr, "%s\n", nbd_get_error ());
140              exit (EXIT_FAILURE);
141            }
142          }
143
144          assert (nbd_get_size (nbd) >= NR_SECTORS * SECTOR_SIZE);
145
146          /* Issue read commands for the first NR sectors. */
147          for (i = 0; i < NR_SECTORS; ++i) {
148            data[i].offset = i * SECTOR_SIZE;
149
150            /* The callback (hexdump) is called when the command
151             * completes.  The buffer must continue to exist while
152             * the command is running.
153             */
154            if (nbd_aio_pread (nbd, data[i].sector, SECTOR_SIZE,
155                               data[i].offset,
156                               (nbd_completion_callback) {
157                                 .callback = hexdump,
158                                 .user_data = &data[i],
159                               }, 0) == -1) {
160              fprintf (stderr, "%s\n", nbd_get_error ());
161              exit (EXIT_FAILURE);
162            }
163          }
164
165          /* Run the main loop until all the commands have
166           * completed and retired.  Again the use of nbd_poll
167           * here is only as an example.
168           */
169          while (nbd_aio_in_flight (nbd) > 0) {
170            if (nbd_poll (nbd, -1) == -1) {
171              fprintf (stderr, "%s\n", nbd_get_error ());
172              exit (EXIT_FAILURE);
173            }
174          }
175
176          /* Close the libnbd handle. */
177          nbd_close (nbd);
178
179          exit (EXIT_SUCCESS);
180        }
181

SEE ALSO

183       nbd_aio_is_connecting(3), nbd_aio_is_ready(3), nbd_connect_unix(3),
184       nbd_create(3), libnbd(3).
185

AUTHORS

187       Eric Blake
188
189       Richard W.M. Jones
190
192       Copyright (C) 2019-2021 Red Hat Inc.
193

LICENSE

195       This library is free software; you can redistribute it and/or modify it
196       under the terms of the GNU Lesser General Public License as published
197       by the Free Software Foundation; either version 2 of the License, or
198       (at your option) any later version.
199
200       This library is distributed in the hope that it will be useful, but
201       WITHOUT ANY WARRANTY; without even the implied warranty of
202       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
203       Lesser General Public License for more details.
204
205       You should have received a copy of the GNU Lesser General Public
206       License along with this library; if not, write to the Free Software
207       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
208       02110-1301 USA
209
210
211
212libnbd-1.10.1                     2021-10-25           nbd_aio_connect_unix(3)
Impressum