1nbd_connect_command(3)              LIBNBD              nbd_connect_command(3)
2
3
4

NAME

6       nbd_connect_command - connect to NBD server command
7

SYNOPSIS

9        #include <libnbd.h>
10
11        int nbd_connect_command (struct nbd_handle *h, char **argv);
12

DESCRIPTION

14       Run the command as a subprocess and connect to it over stdin/stdout.
15       This is for use with NBD servers which can behave like inetd clients,
16       such as "nbdkit --single".
17
18   Subprocess
19       Libnbd will fork the "argv" command and pass the NBD socket to it using
20       file descriptors 0 and 1 (stdin/stdout):
21
22        ┌─────────┬─────────┐    ┌────────────────┐
23        │ program │ libnbd  │    │   NBD server   │
24        │         │         │    │       (argv)   │
25        │         │ socket ╍╍╍╍╍╍╍╍▶ stdin/stdout │
26        └─────────┴─────────┘    └────────────────┘
27
28       When the NBD handle is closed the server subprocess is killed.
29

RETURN VALUE

31       If the call is successful the function returns 0.
32

ERRORS

34       On error "-1" is returned.
35
36       Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
37       of the error.
38

HANDLE STATE

40       The handle must be newly created, otherwise this call will return an
41       error.
42

VERSION

44       This function first appeared in libnbd 1.0.
45
46       If you need to test if this function is available at compile time check
47       if the following macro is defined:
48
49        #define LIBNBD_HAVE_NBD_CONNECT_COMMAND 1
50

EXAMPLE

52       This example is also available as examples/connect-command.c in the
53       libnbd source code.
54
55        /* This example shows how to run an NBD server
56         * (nbdkit) as a subprocess of libnbd.
57         */
58
59        #include <stdio.h>
60        #include <stdlib.h>
61        #include <string.h>
62
63        #include <libnbd.h>
64
65        int
66        main (int argc, char *argv[])
67        {
68          struct nbd_handle *nbd;
69          char wbuf[512], rbuf[512];
70          size_t i;
71
72          /* Create the libnbd handle. */
73          nbd = nbd_create ();
74          if (nbd == NULL) {
75            fprintf (stderr, "%s\n", nbd_get_error ());
76            exit (EXIT_FAILURE);
77          }
78
79          /* Run nbdkit as a subprocess. */
80          char *args[] = {
81            "nbdkit",
82
83            /* You must use ‘-s’ (which tells nbdkit to serve
84             * a single connection on stdin/stdout).
85             */
86            "-s",
87
88            /* It is recommended to use ‘--exit-with-parent’
89             * to ensure nbdkit is always cleaned up even
90             * if the main program crashes.
91             */
92            "--exit-with-parent",
93
94            /* Use this to enable nbdkit debugging. */
95            "-v",
96
97            /* The nbdkit plugin name - this is a RAM disk. */
98            "memory", "size=1M",
99
100            /* Use NULL to terminate the arg list. */
101            NULL
102          };
103          if (nbd_connect_command (nbd, args) == -1) {
104            fprintf (stderr, "%s\n", nbd_get_error ());
105            exit (EXIT_FAILURE);
106          }
107
108          /* Write some random data to the first sector. */
109          for (i = 0; i < sizeof wbuf; ++i)
110            wbuf[i] = i % 13;
111          if (nbd_pwrite (nbd, wbuf, sizeof wbuf, 0, 0) == -1) {
112            fprintf (stderr, "%s\n", nbd_get_error ());
113            exit (EXIT_FAILURE);
114          }
115
116          /* Read the first sector back. */
117          if (nbd_pread (nbd, rbuf, sizeof rbuf, 0, 0) == -1) {
118            fprintf (stderr, "%s\n", nbd_get_error ());
119            exit (EXIT_FAILURE);
120          }
121
122          /* Close the libnbd handle. */
123          nbd_close (nbd);
124
125          /* What was read must be exactly the same as what
126           * was written.
127           */
128          if (memcmp (rbuf, wbuf, sizeof rbuf) != 0) {
129            fprintf (stderr, "FAILED: "
130                     "read data did not match written data\n");
131            exit (EXIT_FAILURE);
132          }
133
134          exit (EXIT_SUCCESS);
135        }
136

SEE ALSO

138       nbd_create(3), nbd_kill_subprocess(3), libnbd(3).
139

AUTHORS

141       Eric Blake
142
143       Richard W.M. Jones
144
146       Copyright (C) 2019 Red Hat Inc.
147

LICENSE

149       This library is free software; you can redistribute it and/or modify it
150       under the terms of the GNU Lesser General Public License as published
151       by the Free Software Foundation; either version 2 of the License, or
152       (at your option) any later version.
153
154       This library is distributed in the hope that it will be useful, but
155       WITHOUT ANY WARRANTY; without even the implied warranty of
156       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
157       Lesser General Public License for more details.
158
159       You should have received a copy of the GNU Lesser General Public
160       License along with this library; if not, write to the Free Software
161       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
162       02110-1301 USA
163
164
165
166libnbd-1.3.7                      2020-04-23            nbd_connect_command(3)
Impressum