nbd: Use same retry count per connection
[nbdkit/ericb.git] / wrapper.c
blobd7bb75870d103c0d8b84a786b3a911b2122ec06d
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];
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
143 * development.
145 s = getenv ("NBDKIT_VALGRIND");
146 if (s && strcmp (s, "1") == 0) {
147 passthru (VALGRIND);
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");
159 else {
160 s = getenv ("NBDKIT_GDB");
161 if (s && strcmp (s, "1") == 0) {
162 passthru ("gdb");
163 passthru ("--args");
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
172 * rewriting.
174 for (;;) {
175 int c;
176 int long_index = -1;
177 bool is_long_option;
179 c = getopt_long (argc, argv, short_options, long_options, &long_index);
180 if (c == -1)
181 break;
183 if (c == '?') /* getopt prints an error */
184 exit (EXIT_FAILURE);
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. */
190 if (c == 'v') {
191 verbose = true;
192 if (is_long_option)
193 passthru ("--verbose");
194 else
195 passthru ("-v");
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);
202 else
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. */
213 else {
214 passthru_format ("-%c", c);
215 if (optarg)
216 passthru (optarg);
220 /* Are there any non-option arguments? */
221 if (optind < argc) {
222 /* Ensure any further parameters can never be parsed as options by
223 * real nbdkit.
225 passthru ("--");
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",
234 builddir);
235 passthru_format ("%s/plugins/%s/nbdkit-%s-plugin",
236 builddir, argv[optind], argv[optind]);
238 else {
239 passthru_format ("%s/plugins/%s/.libs/nbdkit-%s-plugin.so",
240 builddir, argv[optind], argv[optind]);
242 ++optind;
245 /* Everything else is passed through without rewriting. */
246 while (optind < argc) {
247 passthru (argv[optind]);
248 ++optind;
252 end_passthru ();
253 if (verbose)
254 print_command ();
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);
262 time (&t);
263 tu = t;
264 tu %= 255;
265 tu++;
266 snprintf (ts, sizeof ts, "%u", tu);
267 setenv ("MALLOC_PERTURB_", ts, 0);
269 /* Run the final command. */
270 execvp (cmd[0], (char **) cmd);
271 perror (cmd[0]);
272 exit (EXIT_FAILURE);