Update Red Hat Copyright Notices
[nbdkit.git] / server / captive.c
blob77b8b208392c540b10c013539df3ef05ff3878a6
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <signal.h>
42 #include <assert.h>
44 #ifdef HAVE_SYS_WAIT_H
45 #include <sys/wait.h>
46 #endif
48 #include "utils.h"
50 #include "internal.h"
52 #ifndef WIN32
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.
57 void
58 run_command (void)
60 FILE *fp;
61 char *cmd = NULL;
62 size_t len = 0;
63 int r, status;
64 pid_t pid;
66 if (!run)
67 return;
69 if (!export_name)
70 export_name = "";
72 fp = open_memstream (&cmd, &len);
73 if (fp == NULL) {
74 perror ("open_memstream");
75 exit (EXIT_FAILURE);
78 /* Construct $uri. */
79 fprintf (fp, "uri=");
80 if (tls == 2) /* --tls=require */
81 fprintf (fp, "nbds");
82 else
83 fprintf (fp, "nbd");
84 if (port) {
85 if (!vsock) {
86 fprintf (fp, "://localhost:");
87 shell_quote (port, fp);
88 if (strcmp (export_name, "") != 0) {
89 putc ('/', fp);
90 uri_quote (export_name, fp);
93 else {
94 fprintf (fp, "+vsock://1:"); /* 1 = VMADDR_CID_LOCAL */
95 shell_quote (port, fp);
96 if (strcmp (export_name, "") != 0) {
97 putc ('/', fp);
98 uri_quote (export_name, fp);
102 else if (unixsocket) {
103 fprintf (fp, "+unix://");
104 if (strcmp (export_name, "") != 0) {
105 putc ('/', fp);
106 uri_quote (export_name, fp);
108 fprintf (fp, "\\?socket=");
109 uri_quote (unixsocket, fp);
111 putc ('\n', 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);
119 putc ('\n', fp);
121 /* Construct $tls, $port and $unixsocket. */
122 if (tls > 0)
123 fprintf (fp, "tls=%d\n", tls);
124 fprintf (fp, "port=");
125 if (port)
126 shell_quote (port, fp);
127 putc ('\n', fp);
128 fprintf (fp, "unixsocket=");
129 if (unixsocket)
130 shell_quote (unixsocket, fp);
131 fprintf (fp, "\n");
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");
138 exit (EXIT_FAILURE);
141 /* Fork. Captive nbdkit runs as the child process. */
142 pid = fork ();
143 if (pid == -1) {
144 perror ("fork");
145 exit (EXIT_FAILURE);
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) {
152 r = -1;
154 else
155 r = system (cmd);
156 if (r == -1) {
157 nbdkit_error ("failure to execute external command: %m");
158 r = EXIT_FAILURE;
160 else if (WIFEXITED (r))
161 r = WEXITSTATUS (r);
162 else {
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)) {
170 case -1:
171 nbdkit_error ("waitpid: %m");
172 r = EXIT_FAILURE;
173 break;
174 case 0:
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
179 * the --run command.
181 kill (pid, SIGTERM);
182 waitpid (pid, NULL, 0);
183 break;
184 default:
185 /* Captive nbdkit exited unexpectedly; update the exit status. */
186 if (WIFEXITED (status)) {
187 if (r == 0)
188 r = WEXITSTATUS (status);
190 else {
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;
198 exit (r);
201 free (cmd);
203 debug ("forked into background (new pid = %d)", getpid ());
206 #else /* WIN32 */
208 void
209 run_command (void)
211 if (!run)
212 return;
214 NOT_IMPLEMENTED_ON_WINDOWS ("--run");
217 #endif /* WIN32 */