2 * Copyright (C) 2019-2020 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
44 #ifdef HAVE_SYS_WAIT_H
54 /* Handle the --run option. If run is NULL, does nothing. If run is
55 * not NULL then run nbdkit as a captive subprocess of the command.
72 fp
= open_memstream (&cmd
, &len
);
74 perror ("open_memstream");
80 if (tls
== 2) /* --tls=require */
86 fprintf (fp
, "://localhost:");
87 shell_quote (port
, fp
);
88 if (strcmp (export_name
, "") != 0) {
90 uri_quote (export_name
, fp
);
94 fprintf (fp
, "+vsock://1:"); /* 1 = VMADDR_CID_LOCAL */
95 shell_quote (port
, fp
);
96 if (strcmp (export_name
, "") != 0) {
98 uri_quote (export_name
, fp
);
102 else if (unixsocket
) {
103 fprintf (fp
, "+unix://");
104 if (strcmp (export_name
, "") != 0) {
106 uri_quote (export_name
, fp
);
108 fprintf (fp
, "\\?socket=");
109 uri_quote (unixsocket
, fp
);
113 /* Since nbdkit 1.24, $nbd is a synonym for $uri. */
114 fprintf (fp
, "nbd=\"$uri\"\n");
116 /* Expose $exportname. */
117 fprintf (fp
, "exportname=");
118 shell_quote (export_name
, fp
);
121 /* Construct $tls, $port and $unixsocket. */
123 fprintf (fp
, "tls=%d\n", tls
);
124 fprintf (fp
, "port=");
126 shell_quote (port
, fp
);
128 fprintf (fp
, "unixsocket=");
130 shell_quote (unixsocket
, fp
);
133 /* Add the --run command. Note we don't have to quote this. */
134 fprintf (fp
, "%s", run
);
136 if (fclose (fp
) == EOF
) {
137 perror ("memstream failed");
141 /* Fork. Captive nbdkit runs as the child process. */
148 if (pid
> 0) { /* Parent process is the run command. */
149 /* Restore original stdin/out */
150 if (dup2 (saved_stdin
, STDIN_FILENO
) == -1 ||
151 dup2 (saved_stdout
, STDOUT_FILENO
) == -1) {
157 nbdkit_error ("failure to execute external command: %m");
160 else if (WIFEXITED (r
))
163 assert (WIFSIGNALED (r
));
164 fprintf (stderr
, "%s: external command was killed by signal %d\n",
165 program_name
, WTERMSIG (r
));
166 r
= WTERMSIG (r
) + 128;
169 switch (waitpid (pid
, &status
, WNOHANG
)) {
171 nbdkit_error ("waitpid: %m");
175 /* Captive nbdkit is still running; kill it. We want to wait
176 * for nbdkit to exit since that ensures all cleanup is done in
177 * the plugin before we return. However we don't care if nbdkit
178 * returns an error, the exit code we return always comes from
182 waitpid (pid
, NULL
, 0);
185 /* Captive nbdkit exited unexpectedly; update the exit status. */
186 if (WIFEXITED (status
)) {
188 r
= WEXITSTATUS (status
);
191 assert (WIFSIGNALED (status
));
192 fprintf (stderr
, "%s: nbdkit command was killed by signal %d\n",
193 program_name
, WTERMSIG (status
));
194 r
= WTERMSIG (status
) + 128;
203 debug ("forked into background (new pid = %d)", getpid ());
214 NOT_IMPLEMENTED_ON_WINDOWS ("--run");