1nbd_connect_systemd_socket_activationL(I3Bn)NbBdD_connect_systemd_socket_activation(3)
2
3
4
6 nbd_connect_systemd_socket_activation - connect using systemd socket
7 activation
8
10 #include <libnbd.h>
11
12 int nbd_connect_systemd_socket_activation (struct nbd_handle *h,
13 char **argv);
14
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. To run nbdkit as a
21 subprocess it is usually better to use nbd_connect_command(3).
22
23 Socket activation
24 Libnbd will fork the "argv" command and pass an NBD socket to it using
25 special "LISTEN_*" environment variables (as defined by the systemd
26 socket activation protocol).
27
28 ┌─────────┬─────────┐ ┌───────────────┐
29 │ program │ libnbd │ │ qemu-nbd or │
30 │ │ │ │ other server │
31 │ │ socket ╍╍╍╍╍╍╍╍▶ │
32 └─────────┴─────────┘ └───────────────┘
33
34 When the NBD handle is closed the server subprocess is killed.
35
37 If the call is successful the function returns 0.
38
40 On error "-1" is returned.
41
42 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
43 of the error.
44
46 The handle must be newly created, otherwise this call will return an
47 error.
48
50 This function first appeared in libnbd 1.2.
51
52 If you need to test if this function is available at compile time check
53 if the following macro is defined:
54
55 #define LIBNBD_HAVE_NBD_CONNECT_SYSTEMD_SOCKET_ACTIVATION 1
56
58 This example is also available as examples/open-qcow2.c in the libnbd
59 source code.
60
61 /* This example shows how to use qemu-nbd
62 * to open a local qcow2 file.
63 */
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 #include <libnbd.h>
70
71 int
72 main (int argc, char *argv[])
73 {
74 const char *filename;
75 struct nbd_handle *nbd;
76 char buf[512];
77 FILE *fp;
78
79 if (argc != 2) {
80 fprintf (stderr, "open-qcow2 file.qcow2\n");
81 exit (EXIT_FAILURE);
82 }
83 filename = argv[1];
84
85 /* Create the libnbd handle. */
86 nbd = nbd_create ();
87 if (nbd == NULL) {
88 fprintf (stderr, "%s\n", nbd_get_error ());
89 exit (EXIT_FAILURE);
90 }
91
92 /* Run qemu-nbd as a subprocess using
93 * systemd socket activation.
94 */
95 char *args[] = {
96 "qemu-nbd", "-f", "qcow2",
97 (char *) filename,
98 NULL
99 };
100 if (nbd_connect_systemd_socket_activation (nbd,
101 args) == -1) {
102 fprintf (stderr, "%s\n", nbd_get_error ());
103 exit (EXIT_FAILURE);
104 }
105
106 /* Read the first sector and print it. */
107 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
108 fprintf (stderr, "%s\n", nbd_get_error ());
109 exit (EXIT_FAILURE);
110 }
111
112 fp = popen ("hexdump -C", "w");
113 if (fp == NULL) {
114 perror ("popen: hexdump");
115 exit (EXIT_FAILURE);
116 }
117 fwrite (buf, sizeof buf, 1, fp);
118 pclose (fp);
119
120 /* Close the libnbd handle. */
121 nbd_close (nbd);
122
123 exit (EXIT_SUCCESS);
124 }
125
127 nbd_connect_command(3), nbd_create(3), nbd_kill_subprocess(3),
128 libnbd(3), qemu-nbd(1),
129 http://0pointer.de/blog/projects/socket-activation.html.
130
132 Eric Blake
133
134 Richard W.M. Jones
135
137 Copyright (C) 2019 Red Hat Inc.
138
140 This library is free software; you can redistribute it and/or modify it
141 under the terms of the GNU Lesser General Public License as published
142 by the Free Software Foundation; either version 2 of the License, or
143 (at your option) any later version.
144
145 This library is distributed in the hope that it will be useful, but
146 WITHOUT ANY WARRANTY; without even the implied warranty of
147 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
148 Lesser General Public License for more details.
149
150 You should have received a copy of the GNU Lesser General Public
151 License along with this library; if not, write to the Free Software
152 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
153 02110-1301 USA
154
155
156
157libnbd-1.3.7 2020-n0b4d-_2c3onnect_systemd_socket_activation(3)