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
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
33 /*------------------------------------------------------------
34 * This is not nbdkit. This is a wrapper which lets you run nbdkit
35 * from the source directory without installing nbdkit.
39 * ./nbdkit file [arg=value] [arg=value] ...
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 *------------------------------------------------------------
76 /* Construct an array of parameters passed through to real nbdkit. */
77 static const char **cmd
;
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.
85 is_perl_plugin (const char *name
)
87 return strcmp (name
, "example4") == 0 || strcmp (name
, "tar") == 0;
91 passthru (const char *s
)
93 cmd
= realloc (cmd
, (len
+1) * sizeof (const char *));
100 static void __attribute__((format (printf
, 1, 2)))
101 passthru_format (const char *fs
, ...)
107 if (vasprintf (&str
, fs
, args
) == -1)
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;
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
146 s
= getenv ("NBDKIT_VALGRIND");
147 if (s
&& strcmp (s
, "1") == 0) {
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");
161 s
= getenv ("NBDKIT_GDB");
162 if (s
&& strcmp (s
, "1") == 0) {
168 /* Needed for plugins written in OCaml. */
169 s
= getenv ("LD_LIBRARY_PATH");
171 r
= asprintf (&s
, "%s/plugins/ocaml/.libs:%s", builddir
, s
);
173 r
= asprintf (&s
, "%s/plugins/ocaml/.libs", builddir
);
178 setenv ("LD_LIBRARY_PATH", s
, 1);
180 s
= getenv ("LIBRARY_PATH");
182 r
= asprintf (&s
, "%s/plugins/ocaml/.libs:%s", builddir
, s
);
184 r
= asprintf (&s
, "%s/plugins/ocaml/.libs", builddir
);
189 setenv ("LIBRARY_PATH", s
, 1);
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
204 c
= getopt_long (argc
, argv
, short_options
, long_options
, &long_index
);
208 if (c
== '?') /* getopt prints an error */
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. */
218 passthru ("--verbose");
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
);
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. */
239 passthru_format ("-%c", c
);
245 /* Are there any non-option arguments? */
247 /* Ensure any further parameters can never be parsed as options by
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",
260 passthru_format ("%s/plugins/%s/nbdkit-%s-plugin",
261 builddir
, argv
[optind
], argv
[optind
]);
264 passthru_format ("%s/plugins/%s/.libs/nbdkit-%s-plugin.so",
265 builddir
, argv
[optind
], argv
[optind
]);
270 /* Everything else is passed through without rewriting. */
271 while (optind
< argc
) {
272 passthru (argv
[optind
]);
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);
291 snprintf (ts
, sizeof ts
, "%u", tu
);
292 setenv ("MALLOC_PERTURB_", ts
, 0);
294 /* Run the final command. */
295 execvp (cmd
[0], (char **) cmd
);