1nbd_poll(3)                         LIBNBD                         nbd_poll(3)
2
3
4

NAME

6       nbd_poll - poll the handle once
7

SYNOPSIS

9        #include <libnbd.h>
10
11        int nbd_poll (struct nbd_handle *h, int timeout);
12

DESCRIPTION

14       This is a simple implementation of poll(2) which is used internally by
15       synchronous API calls.  On success, it returns 0 if the "timeout" (in
16       milliseconds) occurs, or 1 if the poll completed and the state machine
17       progressed. Set "timeout" to "-1" to block indefinitely (but be careful
18       that eventual action is actually expected - for example, if the
19       connection is established but there are no commands in flight, using an
20       infinite timeout will permanently block).
21
22       This function is mainly useful as an example of how you might integrate
23       libnbd with your own main loop, rather than being intended as something
24       you would use.
25

RETURN VALUE

27       This call returns an integer ≥ 0.
28

ERRORS

30       On error "-1" is returned.
31
32       Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
33       of the error.
34
35       The following parameters must not be NULL: "h".  For more information
36       see "Non-NULL parameters" in libnbd(3).
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_POLL 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 <errno.h>
70        #include <assert.h>
71
72        #include <libnbd.h>
73
74        #define NR_SECTORS 32
75        #define SECTOR_SIZE 512
76
77        struct data {
78          uint64_t offset;
79          char sector[SECTOR_SIZE];
80        };
81
82        static int
83        hexdump (void *user_data, int *error)
84        {
85          struct data *data = user_data;
86          FILE *pp;
87
88          if (*error) {
89            errno = *error;
90            perror ("failed to read");
91            exit (EXIT_FAILURE);
92          }
93
94          printf ("sector at offset 0x%" PRIx64 ":\n",
95                  data->offset);
96          pp = popen ("hexdump -C", "w");
97          if (pp == NULL) {
98            perror ("popen: hexdump");
99            exit (EXIT_FAILURE);
100          }
101          fwrite (data->sector, SECTOR_SIZE, 1, pp);
102          pclose (pp);
103          printf ("\n");
104
105          /* Returning 1 from the callback automatically retires
106           * the command.
107           */
108          return 1;
109        }
110
111        static struct data data[NR_SECTORS];
112
113        int
114        main (int argc, char *argv[])
115        {
116          struct nbd_handle *nbd;
117          size_t i;
118
119          if (argc != 2) {
120            fprintf (stderr, "%s socket\n", argv[0]);
121            exit (EXIT_FAILURE);
122          }
123
124          /* Create the libnbd handle. */
125          nbd = nbd_create ();
126          if (nbd == NULL) {
127            fprintf (stderr, "%s\n", nbd_get_error ());
128            exit (EXIT_FAILURE);
129          }
130
131          /* Connect to the NBD server over a Unix domain socket.
132           * This only starts the connection.
133           */
134          if (nbd_aio_connect_unix (nbd, argv[1]) == -1) {
135            fprintf (stderr, "%s\n", nbd_get_error ());
136            exit (EXIT_FAILURE);
137          }
138
139          /* Wait for the connection to complete.  The use of
140           * nbd_poll here is only as an example.  You could also
141           * integrate this with poll(2), glib or another main
142           * loop.  Read libnbd(3) and the source file lib/poll.c.
143           */
144          while (!nbd_aio_is_ready (nbd)) {
145            if (nbd_poll (nbd, -1) == -1) {
146              fprintf (stderr, "%s\n", nbd_get_error ());
147              exit (EXIT_FAILURE);
148            }
149          }
150
151          assert (nbd_get_size (nbd) >= NR_SECTORS * SECTOR_SIZE);
152
153          /* Issue read commands for the first NR sectors. */
154          for (i = 0; i < NR_SECTORS; ++i) {
155            data[i].offset = i * SECTOR_SIZE;
156
157            /* The callback (hexdump) is called when the command
158             * completes.  The buffer must continue to exist while
159             * the command is running.
160             */
161            if (nbd_aio_pread (nbd, data[i].sector, SECTOR_SIZE,
162                               data[i].offset,
163                               (nbd_completion_callback) {
164                                 .callback = hexdump,
165                                 .user_data = &data[i],
166                               }, 0) == -1) {
167              fprintf (stderr, "%s\n", nbd_get_error ());
168              exit (EXIT_FAILURE);
169            }
170          }
171
172          /* Run the main loop until all the commands have
173           * completed and retired.  Again the use of nbd_poll
174           * here is only as an example.
175           */
176          while (nbd_aio_in_flight (nbd) > 0) {
177            if (nbd_poll (nbd, -1) == -1) {
178              fprintf (stderr, "%s\n", nbd_get_error ());
179              exit (EXIT_FAILURE);
180            }
181          }
182
183          /* Close the libnbd handle. */
184          nbd_close (nbd);
185
186          exit (EXIT_SUCCESS);
187        }
188

SEE ALSO

190       nbd_create(3), libnbd(3), poll(2).
191

AUTHORS

193       Eric Blake
194
195       Richard W.M. Jones
196
198       Copyright (C) 2019-2021 Red Hat Inc.
199

LICENSE

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