server: Switch to fixed-length conn->exportname
[nbdkit/ericb.git] / wrapper.c
blob88b806295391b33be86c17c88c9e00575003b7c6
1 /* nbdkit
2 * Copyright (C) 2017-2018 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 /*------------------------------------------------------------
34 * This is not nbdkit. This is a wrapper which lets you run nbdkit
35 * from the source directory without installing nbdkit.
37 * You can use either:
39 * ./nbdkit file [arg=value] [arg=value] ...
41 * or:
43 * /path/to/nbdkit file [arg=value] [arg=value] ...
45 * or you can set $PATH to include the nbdkit source directory and run
46 * the bare "nbdkit" command without supplying the full path.
48 * The wrapper modifies the bare plugin name (eg. "file") to be the
49 * full path to the locally compiled plugin. If you don't use this
50 * program and run server/nbdkit directly then it will pick up the
51 * globally installed plugins which is usually not what you want.
53 * This program is also used to run the tests (make check).
55 * You can enable valgrind by setting NBDKIT_VALGRIND=1 (this
56 * is mainly used by the internal tests).
58 * You can enable debugging by setting NBDKIT_GDB=1
59 *------------------------------------------------------------
62 #include <config.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <stdbool.h>
67 #include <stdarg.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <getopt.h>
71 #include <limits.h>
72 #include <time.h>
74 #include "options.h"
76 /* Construct an array of parameters passed through to real nbdkit. */
77 static const char **cmd;
78 static size_t len;
80 /* Plugins written in scripting languages (only Perl right now) need
81 * to be rewritten on the command line in a different way from plugins
82 * written in C. So we have to list those here.
84 static bool
85 is_perl_plugin (const char *name)
87 return strcmp (name, "example4") == 0 || strcmp (name, "tar") == 0;
90 static void
91 passthru (const char *s)
93 cmd = realloc (cmd, (len+1) * sizeof (const char *));
94 if (cmd == NULL)
95 abort ();
96 cmd[len] = s;
97 ++len;
100 static void __attribute__((format (printf, 1, 2)))
101 passthru_format (const char *fs, ...)
103 va_list args;
104 char *str;
106 va_start (args, fs);
107 if (vasprintf (&str, fs, args) == -1)
108 abort ();
109 va_end (args);
110 passthru (str);
113 static void
114 end_passthru (void)
116 passthru (NULL);
119 static void
120 print_command (void)
122 size_t i;
124 if (len > 0)
125 fprintf (stderr, "%s", cmd[0]);
126 for (i = 1; i < len && cmd[i] != NULL; ++i)
127 fprintf (stderr, " %s", cmd[i]);
128 fprintf (stderr, "\n");
132 main (int argc, char *argv[])
134 bool verbose = false;
135 char *s;
136 time_t t;
137 unsigned tu;
138 char ts[32];
139 int r;
141 /* If NBDKIT_VALGRIND=1 is set in the environment, then we run the
142 * program under valgrind. This is used by the tests. Similarly if
143 * NBDKIT_GDB=1 is set, we run the program under GDB, useful during
144 * development.
146 s = getenv ("NBDKIT_VALGRIND");
147 if (s && strcmp (s, "1") == 0) {
148 passthru (VALGRIND);
149 passthru ("--vgdb=no");
150 passthru ("--leak-check=full");
151 passthru ("--show-leak-kinds=all");
152 passthru ("--error-exitcode=119");
153 passthru_format ("--suppressions=%s/valgrind/suppressions", builddir);
154 passthru ("--trace-children=no");
155 passthru ("--run-libc-freeres=no");
156 passthru ("--num-callers=100");
157 /* This is a temporary workaround until RHBZ#1662656 is fixed: */
158 passthru ("--read-inline-info=no");
160 else {
161 s = getenv ("NBDKIT_GDB");
162 if (s && strcmp (s, "1") == 0) {
163 passthru ("gdb");
164 passthru ("--args");
168 /* Needed for plugins written in OCaml. */
169 s = getenv ("LD_LIBRARY_PATH");
170 if (s)
171 r = asprintf (&s, "%s/plugins/ocaml/.libs:%s", builddir, s);
172 else
173 r = asprintf (&s, "%s/plugins/ocaml/.libs", builddir);
174 if (r < 0) {
175 perror ("asprintf");
176 exit (EXIT_FAILURE);
178 setenv ("LD_LIBRARY_PATH", s, 1);
179 free (s);
180 s = getenv ("LIBRARY_PATH");
181 if (s)
182 r = asprintf (&s, "%s/plugins/ocaml/.libs:%s", builddir, s);
183 else
184 r = asprintf (&s, "%s/plugins/ocaml/.libs", builddir);
185 if (r < 0) {
186 perror ("asprintf");
187 exit (EXIT_FAILURE);
189 setenv ("LIBRARY_PATH", s, 1);
190 free (s);
192 /* Absolute path of the real nbdkit command. */
193 passthru_format ("%s/server/nbdkit", builddir);
195 /* Option parsing. We don't really parse options here. We are only
196 * interested in which options have arguments and which need
197 * rewriting.
199 for (;;) {
200 int c;
201 int long_index = -1;
202 bool is_long_option;
204 c = getopt_long (argc, argv, short_options, long_options, &long_index);
205 if (c == -1)
206 break;
208 if (c == '?') /* getopt prints an error */
209 exit (EXIT_FAILURE);
211 /* long_index is only set if it's an actual long option. */
212 is_long_option = long_index >= 0;
214 /* Verbose is special because we will print the final command. */
215 if (c == 'v') {
216 verbose = true;
217 if (is_long_option)
218 passthru ("--verbose");
219 else
220 passthru ("-v");
222 /* Filters can be rewritten if they are a short name. */
223 else if (c == FILTER_OPTION) {
224 if (is_short_name (optarg))
225 passthru_format ("--filter=%s/filters/%s/.libs/nbdkit-%s-filter.so",
226 builddir, optarg, optarg);
227 else
228 passthru_format ("--filter=%s", optarg);
230 /* Any long option. */
231 else if (is_long_option) {
232 if (optarg) /* Long option which takes an argument. */
233 passthru_format ("--%s=%s", long_options[long_index].name, optarg);
234 else /* Long option which takes no argument. */
235 passthru_format ("--%s", long_options[long_index].name);
237 /* Any short option. */
238 else {
239 passthru_format ("-%c", c);
240 if (optarg)
241 passthru (optarg);
245 /* Are there any non-option arguments? */
246 if (optind < argc) {
247 /* Ensure any further parameters can never be parsed as options by
248 * real nbdkit.
250 passthru ("--");
252 /* The first non-option argument is the plugin name. If it is a
253 * short name then rewrite it.
255 if (is_short_name (argv[optind])) {
256 /* Special plugins written in Perl. */
257 if (is_perl_plugin (argv[optind])) {
258 passthru_format ("%s/plugins/perl/.libs/nbdkit-perl-plugin.so",
259 builddir);
260 passthru_format ("%s/plugins/%s/nbdkit-%s-plugin",
261 builddir, argv[optind], argv[optind]);
263 else {
264 passthru_format ("%s/plugins/%s/.libs/nbdkit-%s-plugin.so",
265 builddir, argv[optind], argv[optind]);
267 ++optind;
270 /* Everything else is passed through without rewriting. */
271 while (optind < argc) {
272 passthru (argv[optind]);
273 ++optind;
277 end_passthru ();
278 if (verbose)
279 print_command ();
281 /* This is a cheap way to find some use-after-free and uninitialized
282 * read problems when using glibc, and doesn't affect normal
283 * operation or other libc. We don't overwrite existing values so
284 * this can be disabled or overridden at runtime.
286 setenv ("MALLOC_CHECK_", "1", 0);
287 time (&t);
288 tu = t;
289 tu %= 255;
290 tu++;
291 snprintf (ts, sizeof ts, "%u", tu);
292 setenv ("MALLOC_PERTURB_", ts, 0);
294 /* Run the final command. */
295 execvp (cmd[0], (char **) cmd);
296 perror (cmd[0]);
297 exit (EXIT_FAILURE);