server/public.c: Rearrange functions.
[nbdkit/ericb.git] / server / captive.c
blob90e4205053eda477aeb666bfa07279e840ff5789
1 /* nbdkit
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
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 <sys/wait.h>
42 #include <signal.h>
44 #include "utils.h"
46 #include "internal.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.
51 void
52 run_command (void)
54 FILE *fp;
55 char *cmd = NULL;
56 size_t len = 0;
57 int r;
58 pid_t pid;
60 if (!run)
61 return;
63 fp = open_memstream (&cmd, &len);
64 if (fp == NULL) {
65 perror ("open_memstream");
66 exit (EXIT_FAILURE);
69 /* Construct $uri. */
70 fprintf (fp, "uri=");
71 if (port) {
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);
79 fprintf (fp, "\n");
81 /* Construct older $nbd "URL". Unfortunately guestfish and qemu take
82 * different syntax, so try to guess which one we need.
84 fprintf (fp, "nbd=");
85 if (strstr (run, "guestfish")) {
86 if (port) {
87 fprintf (fp, "nbd://localhost:");
88 shell_quote (port, fp);
90 else if (unixsocket) {
91 fprintf (fp, "nbd://\\?socket=");
92 shell_quote (unixsocket, fp);
94 else
95 abort ();
97 else /* qemu */ {
98 if (port) {
99 fprintf (fp, "nbd:localhost:");
100 shell_quote (port, fp);
102 else if (unixsocket) {
103 fprintf (fp, "nbd:unix:");
104 shell_quote (unixsocket, fp);
106 else
107 abort ();
109 fprintf (fp, "\n");
111 /* Construct $port and $unixsocket. */
112 fprintf (fp, "port=");
113 if (port)
114 shell_quote (port, fp);
115 fprintf (fp, "\n");
116 fprintf (fp, "unixsocket=");
117 if (unixsocket)
118 shell_quote (unixsocket, fp);
119 fprintf (fp, "\n");
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");
126 exit (EXIT_FAILURE);
129 /* Fork. Captive nbdkit runs as the child process. */
130 pid = fork ();
131 if (pid == -1) {
132 perror ("fork");
133 exit (EXIT_FAILURE);
136 if (pid > 0) { /* Parent process is the run command. */
137 r = system (cmd);
138 if (WIFEXITED (r))
139 r = WEXITSTATUS (r);
140 else if (WIFSIGNALED (r)) {
141 fprintf (stderr, "%s: external command was killed by signal %d\n",
142 program_name, WTERMSIG (r));
143 r = 1;
145 else if (WIFSTOPPED (r)) {
146 fprintf (stderr, "%s: external command was stopped by signal %d\n",
147 program_name, WSTOPSIG (r));
148 r = 1;
151 kill (pid, SIGTERM); /* Kill captive nbdkit. */
153 _exit (r);
156 free (cmd);
158 debug ("forked into background (new pid = %d)", getpid ());