1nbd_connect_systemd_socket_activationL(I3Bn)NbBdD_connect_systemd_socket_activation(3)
2
3
4

NAME

6       nbd_connect_systemd_socket_activation - connect using systemd socket
7       activation
8

SYNOPSIS

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

DESCRIPTION

16       Run the command as a subprocess and connect to it using systemd socket
17       activation.
18
19       This is especially useful for running qemu-nbd(1) as a subprocess of
20       libnbd, for example to use it to open qcow2 files.
21
22       To run nbdkit as a subprocess, this function can be used, or
23       nbd_connect_command(3).
24
25       To run nbd-server(1) as a subprocess, this function cannot be used, you
26       must use nbd_connect_command(3).
27
28   Socket activation
29       Libnbd will fork the "argv" command and pass an NBD socket to it using
30       special "LISTEN_*" environment variables (as defined by the systemd
31       socket activation protocol).
32
33        ┌─────────┬─────────┐    ┌───────────────┐
34        │ program │ libnbd  │    │  qemu-nbd or  │
35        │         │         │    │  other server │
36        │         │ socket ╍╍╍╍╍╍╍╍▶             │
37        └─────────┴─────────┘    └───────────────┘
38
39       When the NBD handle is closed the server subprocess is killed.
40

RETURN VALUE

42       If the call is successful the function returns 0.
43

ERRORS

45       On error "-1" is returned.
46
47       Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
48       of the error.
49

HANDLE STATE

51       The handle must be newly created, otherwise this call will return an
52       error.
53

VERSION

55       This function first appeared in libnbd 1.2.
56
57       If you need to test if this function is available at compile time check
58       if the following macro is defined:
59
60        #define LIBNBD_HAVE_NBD_CONNECT_SYSTEMD_SOCKET_ACTIVATION 1
61

EXAMPLE

63       This example is also available as examples/open-qcow2.c in the libnbd
64       source code.
65
66        /* This example shows how to use qemu-nbd
67         * to open a local qcow2 file.
68         */
69
70        #include <stdio.h>
71        #include <stdlib.h>
72        #include <string.h>
73
74        #include <libnbd.h>
75
76        int
77        main (int argc, char *argv[])
78        {
79          const char *filename;
80          struct nbd_handle *nbd;
81          char buf[512];
82          FILE *fp;
83
84          if (argc != 2) {
85            fprintf (stderr, "open-qcow2 file.qcow2\n");
86            exit (EXIT_FAILURE);
87          }
88          filename = argv[1];
89
90          /* Create the libnbd handle. */
91          nbd = nbd_create ();
92          if (nbd == NULL) {
93            fprintf (stderr, "%s\n", nbd_get_error ());
94            exit (EXIT_FAILURE);
95          }
96
97          /* Run qemu-nbd as a subprocess using
98           * systemd socket activation.
99           */
100          char *args[] = {
101            "qemu-nbd", "-f", "qcow2",
102            (char *) filename,
103            NULL
104          };
105          if (nbd_connect_systemd_socket_activation (nbd,
106                                                     args) == -1) {
107            fprintf (stderr, "%s\n", nbd_get_error ());
108            exit (EXIT_FAILURE);
109          }
110
111          /* Read the first sector and print it. */
112          if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
113            fprintf (stderr, "%s\n", nbd_get_error ());
114            exit (EXIT_FAILURE);
115          }
116
117          fp = popen ("hexdump -C", "w");
118          if (fp == NULL) {
119            perror ("popen: hexdump");
120            exit (EXIT_FAILURE);
121          }
122          fwrite (buf, sizeof buf, 1, fp);
123          pclose (fp);
124
125          /* Close the libnbd handle. */
126          nbd_close (nbd);
127
128          exit (EXIT_SUCCESS);
129        }
130

SEE ALSO

132       nbd_aio_connect_systemd_socket_activation(3), nbd_connect_command(3),
133       nbd_create(3), nbd_kill_subprocess(3), libnbd(3), qemu-nbd(1),
134       http://0pointer.de/blog/projects/socket-activation.html.
135

AUTHORS

137       Eric Blake
138
139       Richard W.M. Jones
140
142       Copyright (C) 2019-2021 Red Hat Inc.
143

LICENSE

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