2 * Copyright (C) 2019 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>
48 /* Handle the --run option. If run is NULL, does nothing. If run is
49 * not NULL then run nbdkit as a captive subprocess of the command.
63 fp
= open_memstream (&cmd
, &len
);
65 perror ("open_memstream");
72 fprintf (fp
, "nbd://localhost:");
73 shell_quote (port
, fp
);
75 else if (unixsocket
) {
76 fprintf (fp
, "nbd+unix://\\?socket=");
77 uri_quote (unixsocket
, fp
);
81 /* Construct older $nbd "URL". Unfortunately guestfish and qemu take
82 * different syntax, so try to guess which one we need.
85 if (strstr (run
, "guestfish")) {
87 fprintf (fp
, "nbd://localhost:");
88 shell_quote (port
, fp
);
90 else if (unixsocket
) {
91 fprintf (fp
, "nbd://\\?socket=");
92 shell_quote (unixsocket
, fp
);
99 fprintf (fp
, "nbd:localhost:");
100 shell_quote (port
, fp
);
102 else if (unixsocket
) {
103 fprintf (fp
, "nbd:unix:");
104 shell_quote (unixsocket
, fp
);
111 /* Construct $port and $unixsocket. */
112 fprintf (fp
, "port=");
114 shell_quote (port
, fp
);
116 fprintf (fp
, "unixsocket=");
118 shell_quote (unixsocket
, fp
);
121 /* Add the --run command. Note we don't have to quote this. */
122 fprintf (fp
, "%s", run
);
124 if (fclose (fp
) == EOF
) {
125 perror ("memstream failed");
129 /* Fork. Captive nbdkit runs as the child process. */
136 if (pid
> 0) { /* Parent process is the run command. */
140 else if (WIFSIGNALED (r
)) {
141 fprintf (stderr
, "%s: external command was killed by signal %d\n",
142 program_name
, WTERMSIG (r
));
145 else if (WIFSTOPPED (r
)) {
146 fprintf (stderr
, "%s: external command was stopped by signal %d\n",
147 program_name
, WSTOPSIG (r
));
151 kill (pid
, SIGTERM
); /* Kill captive nbdkit. */
158 debug ("forked into background (new pid = %d)", getpid ());