1nbd_shutdown(3)                     LIBNBD                     nbd_shutdown(3)
2
3
4

NAME

6       nbd_shutdown - disconnect from the NBD server
7

SYNOPSIS

9        #include <libnbd.h>
10
11        int nbd_shutdown (struct nbd_handle *h, uint32_t flags);
12

DESCRIPTION

14       Issue the disconnect command to the NBD server.  This is a nice way to
15       tell the server we are going away, but from the client's point of view
16       has no advantage over abruptly closing the connection (see
17       nbd_close(3)).
18
19       This function works whether or not the handle is ready for transmission
20       of commands, and as such does not take a "flags" parameter. If more
21       fine-grained control is needed, see nbd_aio_disconnect(3).
22
23       The "flags" parameter must be 0 for now (it exists for future NBD
24       protocol extensions).
25

RETURN VALUE

27       If the call is successful the function returns 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

HANDLE STATE

36       The handle must be connected and finished handshaking with the server,
37       otherwise this call will return an error.
38

VERSION

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

EXAMPLE

48       This example is also available as examples/reads-and-writes.c in the
49       libnbd source code.
50
51        /* This example shows how to do synchronous reads
52         * and writes randomly over the first megabyte of an
53         * NBD server.  Note this will destroy any existing
54         * content on the NBD server.
55         *
56         * To test it with nbdkit and a RAM disk:
57         *
58         * nbdkit -U - memory 1M \
59         *     --run './simple-reads-and-writes $unixsocket'
60         */
61
62        #include <stdio.h>
63        #include <stdlib.h>
64        #include <inttypes.h>
65        #include <assert.h>
66        #include <time.h>
67
68        #include <libnbd.h>
69
70        int
71        main (int argc, char *argv[])
72        {
73          struct nbd_handle *nbd;
74          char buf[512];
75          size_t i;
76          int64_t exportsize;
77          uint64_t offset;
78
79          srand (time (NULL));
80
81          if (argc != 2) {
82            fprintf (stderr, "%s socket\n", argv[0]);
83            exit (EXIT_FAILURE);
84          }
85
86          /* Create the libnbd handle. */
87          nbd = nbd_create ();
88          if (nbd == NULL) {
89            fprintf (stderr, "%s\n", nbd_get_error ());
90            exit (EXIT_FAILURE);
91          }
92
93          /* Connect to the NBD server over a
94           * Unix domain socket.
95           */
96          if (nbd_connect_unix (nbd, argv[1]) == -1) {
97            fprintf (stderr, "%s\n", nbd_get_error ());
98            exit (EXIT_FAILURE);
99          }
100
101          /* Get the size of the disk and check
102           * it's large enough.
103           */
104          exportsize = nbd_get_size (nbd);
105          if (exportsize == -1) {
106            fprintf (stderr, "%s\n", nbd_get_error ());
107            exit (EXIT_FAILURE);
108          }
109          assert (exportsize >= sizeof buf);
110
111          /* Check that the server is writable. */
112          if (nbd_is_read_only (nbd) == 1) {
113            fprintf (stderr, "%s: "
114                     "error: this NBD export is read-only\n",
115                     argv[0]);
116            exit (EXIT_FAILURE);
117          }
118
119          for (i = 0; i < sizeof buf; ++i)
120            buf[i] = rand ();
121
122          /* 1000 writes. */
123          for (i = 0; i < 1000; ++i) {
124            offset = rand () % (exportsize - sizeof buf);
125
126            if (nbd_pwrite (nbd, buf, sizeof buf,
127                            offset, 0) == -1) {
128              fprintf (stderr, "%s\n", nbd_get_error ());
129              exit (EXIT_FAILURE);
130            }
131          }
132
133          /* 1000 reads and writes. */
134          for (i = 0; i < 1000; ++i) {
135            offset = rand () % (exportsize - sizeof buf);
136            if (nbd_pread (nbd, buf, sizeof buf,
137                           offset, 0) == -1) {
138              fprintf (stderr, "%s\n", nbd_get_error ());
139              exit (EXIT_FAILURE);
140            }
141
142            offset = rand () % (exportsize - sizeof buf);
143            if (nbd_pwrite (nbd, buf, sizeof buf,
144                            offset, 0) == -1) {
145              fprintf (stderr, "%s\n", nbd_get_error ());
146              exit (EXIT_FAILURE);
147            }
148          }
149
150          /* Sends a graceful shutdown to the server. */
151          if (nbd_shutdown (nbd, 0) == -1) {
152            fprintf (stderr, "%s\n", nbd_get_error ());
153            exit (EXIT_FAILURE);
154          }
155
156          nbd_close (nbd);
157
158          exit (EXIT_SUCCESS);
159        }
160

SEE ALSO

162       nbd_close(3), nbd_aio_disconnect(3), nbd_create(3), libnbd(3).
163

AUTHORS

165       Eric Blake
166
167       Richard W.M. Jones
168
170       Copyright (C) 2019 Red Hat Inc.
171

LICENSE

173       This library is free software; you can redistribute it and/or modify it
174       under the terms of the GNU Lesser General Public License as published
175       by the Free Software Foundation; either version 2 of the License, or
176       (at your option) any later version.
177
178       This library is distributed in the hope that it will be useful, but
179       WITHOUT ANY WARRANTY; without even the implied warranty of
180       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
181       Lesser General Public License for more details.
182
183       You should have received a copy of the GNU Lesser General Public
184       License along with this library; if not, write to the Free Software
185       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
186       02110-1301 USA
187
188
189
190libnbd-1.2.1                      2019-11-14                   nbd_shutdown(3)
Impressum