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;
140 /* If NBDKIT_VALGRIND=1 is set in the environment, then we run the
141 * program under valgrind. This is used by the tests. Similarly if
142 * NBDKIT_GDB=1 is set, we run the program under GDB, useful during
145 s
= getenv ("NBDKIT_VALGRIND");
146 if (s
&& strcmp (s
, "1") == 0) {
148 passthru ("--vgdb=no");
149 passthru ("--leak-check=full");
150 passthru ("--show-leak-kinds=all");
151 passthru ("--error-exitcode=119");
152 passthru_format ("--suppressions=%s/valgrind/suppressions", builddir
);
153 passthru ("--trace-children=no");
154 passthru ("--run-libc-freeres=no");
155 passthru ("--num-callers=100");
156 /* This is a temporary workaround until RHBZ#1662656 is fixed: */
157 passthru ("--read-inline-info=no");
160 s
= getenv ("NBDKIT_GDB");
161 if (s
&& strcmp (s
, "1") == 0) {
167 /* Absolute path of the real nbdkit command. */
168 passthru_format ("%s/server/nbdkit", builddir
);
170 /* Option parsing. We don't really parse options here. We are only
171 * interested in which options have arguments and which need
179 c
= getopt_long (argc
, argv
, short_options
, long_options
, &long_index
);
183 if (c
== '?') /* getopt prints an error */
186 /* long_index is only set if it's an actual long option. */
187 is_long_option
= long_index
>= 0;
189 /* Verbose is special because we will print the final command. */
193 passthru ("--verbose");
197 /* Filters can be rewritten if they are a short name. */
198 else if (c
== FILTER_OPTION
) {
199 if (is_short_name (optarg
))
200 passthru_format ("--filter=%s/filters/%s/.libs/nbdkit-%s-filter.so",
201 builddir
, optarg
, optarg
);
203 passthru_format ("--filter=%s", optarg
);
205 /* Any long option. */
206 else if (is_long_option
) {
207 if (optarg
) /* Long option which takes an argument. */
208 passthru_format ("--%s=%s", long_options
[long_index
].name
, optarg
);
209 else /* Long option which takes no argument. */
210 passthru_format ("--%s", long_options
[long_index
].name
);
212 /* Any short option. */
214 passthru_format ("-%c", c
);
220 /* Are there any non-option arguments? */
222 /* Ensure any further parameters can never be parsed as options by
227 /* The first non-option argument is the plugin name. If it is a
228 * short name then rewrite it.
230 if (is_short_name (argv
[optind
])) {
231 /* Special plugins written in Perl. */
232 if (is_perl_plugin (argv
[optind
])) {
233 passthru_format ("%s/plugins/perl/.libs/nbdkit-perl-plugin.so",
235 passthru_format ("%s/plugins/%s/nbdkit-%s-plugin",
236 builddir
, argv
[optind
], argv
[optind
]);
239 passthru_format ("%s/plugins/%s/.libs/nbdkit-%s-plugin.so",
240 builddir
, argv
[optind
], argv
[optind
]);
245 /* Everything else is passed through without rewriting. */
246 while (optind
< argc
) {
247 passthru (argv
[optind
]);
256 /* This is a cheap way to find some use-after-free and uninitialized
257 * read problems when using glibc, and doesn't affect normal
258 * operation or other libc. We don't overwrite existing values so
259 * this can be disabled or overridden at runtime.
261 setenv ("MALLOC_CHECK_", "1", 0);
266 snprintf (ts
, sizeof ts
, "%u", tu
);
267 setenv ("MALLOC_PERTURB_", ts
, 0);
269 /* Run the final command. */
270 execvp (cmd
[0], (char **) cmd
);