nbdkit/0003-Implement-dump-plugin-...

325 lines
9.4 KiB
Diff

From 04ea431bd119dedd4f9ba063049b1f30102ac8a3 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Sat, 9 Jul 2016 15:18:06 +0100
Subject: [PATCH 03/11] Implement --dump-plugin option.
For example:
$ nbdkit example1 --dump-plugin
path=/usr/lib64/nbdkit/plugins/nbdkit-example1-plugin.so
name=example1
version=1.1.12
api_version=1
struct_size=176
thread_model=serialize_all_requests
has_load=1
has_open=1
has_get_size=1
has_pread=1
---
docs/nbdkit.pod | 7 +++++-
src/internal.h | 3 ++-
src/main.c | 53 ++++++++++++++++++++++++++++------------
src/plugins.c | 61 ++++++++++++++++++++++++++++++++++++++++++++---
tests/Makefile.am | 2 ++
tests/test-dump-plugin.sh | 44 ++++++++++++++++++++++++++++++++++
6 files changed, 150 insertions(+), 20 deletions(-)
create mode 100755 tests/test-dump-plugin.sh
diff --git a/docs/nbdkit.pod b/docs/nbdkit.pod
index 728aad3..12575fa 100644
--- a/docs/nbdkit.pod
+++ b/docs/nbdkit.pod
@@ -6,7 +6,8 @@ nbdkit - A toolkit for creating NBD servers
=head1 SYNOPSIS
- nbdkit [--dump-config] [-e EXPORTNAME] [-f] [-g GROUP] [-i IPADDR]
+ nbdkit [--dump-config] [--dump-plugin]
+ [-e EXPORTNAME] [-f] [-g GROUP] [-i IPADDR]
[--newstyle] [--oldstyle] [-P PIDFILE] [-p PORT] [-r]
[--run CMD] [-s] [-U SOCKET] [-u USER] [-v] [-V]
PLUGIN [key=value [key=value [...]]]
@@ -74,6 +75,10 @@ Display brief command line usage information and exit.
Dump out the compile-time configuration values and exit.
+=item B<--dump-plugin>
+
+Dump out information about the plugin and exit.
+
=item B<-e> EXPORTNAME
=item B<--export> EXPORTNAME
diff --git a/src/internal.h b/src/internal.h
index f58086a..bc4fa12 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -88,7 +88,8 @@ extern void plugin_register (const char *_filename, void *_dl, struct nbdkit_plu
extern void plugin_cleanup (void);
extern const char *plugin_name (void);
extern void plugin_usage (void);
-extern void plugin_version (void);
+extern const char *plugin_version (void);
+extern void plugin_dump_fields (void);
extern void plugin_config (const char *key, const char *value);
extern void plugin_config_complete (void);
extern void plugin_lock_connection (void);
diff --git a/src/main.c b/src/main.c
index 9529f19..9453cce 100644
--- a/src/main.c
+++ b/src/main.c
@@ -89,6 +89,7 @@ static const char *short_options = "e:fg:i:nop:P:rsu:U:vV";
static const struct option long_options[] = {
{ "help", 0, NULL, HELP_OPTION },
{ "dump-config",0, NULL, 0 },
+ { "dump-plugin",0, NULL, 0 },
{ "export", 1, NULL, 'e' },
{ "export-name",1, NULL, 'e' },
{ "exportname", 1, NULL, 'e' },
@@ -119,7 +120,8 @@ static const struct option long_options[] = {
static void
usage (void)
{
- printf ("nbdkit [--dump-config] [-e EXPORTNAME] [-f] [-g GROUP] [-i IPADDR]\n"
+ printf ("nbdkit [--dump-config] [--dump-plugin]\n"
+ " [-e EXPORTNAME] [-f] [-g GROUP] [-i IPADDR]\n"
" [--newstyle] [--oldstyle] [-P PIDFILE] [-p PORT] [-r]\n"
" [--run CMD] [-s] [-U SOCKET] [-u USER] [-v] [-V]\n"
" PLUGIN [key=value [key=value [...]]]\n"
@@ -151,7 +153,7 @@ main (int argc, char *argv[])
{
int c;
int option_index;
- int help = 0, version = 0;
+ int help = 0, version = 0, dump_plugin = 0;
tls_init ();
@@ -166,6 +168,9 @@ main (int argc, char *argv[])
dump_config ();
exit (EXIT_SUCCESS);
}
+ else if (strcmp (long_options[option_index].name, "dump-plugin") == 0) {
+ dump_plugin = 1;
+ }
else if (strcmp (long_options[option_index].name, "run") == 0) {
run = optarg;
foreground = 1;
@@ -261,6 +266,14 @@ main (int argc, char *argv[])
display_version ();
exit (EXIT_SUCCESS);
}
+ if (dump_plugin) {
+ /* Incorrect use of --dump-plugin. */
+ fprintf (stderr,
+ "%s: use 'nbdkit plugin --dump-plugin' or\n"
+ "'nbdkit /path/to/plugin.so --dump-plugin'\n",
+ program_name);
+ exit (EXIT_FAILURE);
+ }
/* Otherwise this is an error. */
fprintf (stderr,
@@ -292,6 +305,29 @@ main (int argc, char *argv[])
open_plugin_so (filename);
+ if (help) {
+ usage ();
+ printf ("\n%s:\n\n", filename);
+ plugin_usage ();
+ exit (EXIT_SUCCESS);
+ }
+
+ if (version) {
+ const char *v;
+
+ display_version ();
+ printf ("%s", plugin_name ());
+ if ((v = plugin_version ()) != NULL)
+ printf (" %s", v);
+ printf ("\n");
+ exit (EXIT_SUCCESS);
+ }
+
+ if (dump_plugin) {
+ plugin_dump_fields ();
+ exit (EXIT_SUCCESS);
+ }
+
/* Find key=value configuration parameters for this plugin. */
++optind;
while (optind < argc && (p = strchr (argv[optind], '=')) != NULL) {
@@ -304,19 +340,6 @@ main (int argc, char *argv[])
++optind;
}
- if (help) {
- usage ();
- printf ("\n%s:\n\n", filename);
- plugin_usage ();
- exit (EXIT_SUCCESS);
- }
-
- if (version) {
- display_version ();
- plugin_version ();
- exit (EXIT_SUCCESS);
- }
-
plugin_config_complete ();
/* If we supported export names, then we'd continue in the loop
diff --git a/src/plugins.c b/src/plugins.c
index 0834874..8f38761 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -189,15 +189,70 @@ plugin_usage (void)
}
}
-void
+const char *
plugin_version (void)
{
assert (dl);
- printf ("%s", plugin.name);
+ return plugin.version;
+}
+
+/* This implements the --dump-plugin option. */
+void
+plugin_dump_fields (void)
+{
+ char *path;
+
+ path = nbdkit_absolute_path (filename);
+ printf ("path=%s\n", path);
+ free (path);
+
+ printf ("name=%s\n", plugin.name);
if (plugin.version)
- printf (" %s", plugin.version);
+ printf ("version=%s\n", plugin.version);
+
+ printf ("api_version=%d\n", plugin._api_version);
+ printf ("struct_size=%" PRIu64 "\n", plugin._struct_size);
+ printf ("thread_model=");
+ switch (plugin._thread_model) {
+ case NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS:
+ printf ("serialize_connections");
+ break;
+ case NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS:
+ printf ("serialize_all_requests");
+ break;
+ case NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS:
+ printf ("serialize_requests");
+ break;
+ case NBDKIT_THREAD_MODEL_PARALLEL:
+ printf ("parallel");
+ break;
+ default:
+ printf ("%d # unknown thread model!", plugin._thread_model);
+ break;
+ }
printf ("\n");
+
+#define HAS(field) if (plugin.field) printf ("has_%s=1\n", #field)
+ HAS (longname);
+ HAS (description);
+ HAS (load);
+ HAS (unload);
+ HAS (config);
+ HAS (config_complete);
+ HAS (config_help);
+ HAS (open);
+ HAS (close);
+ HAS (get_size);
+ HAS (can_write);
+ HAS (can_flush);
+ HAS (is_rotational);
+ HAS (can_trim);
+ HAS (pread);
+ HAS (pwrite);
+ HAS (flush);
+ HAS (trim);
+#undef HAS
}
void
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 04bc0bc..2c936bf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -37,6 +37,7 @@ EXTRA_DIST = \
README.tests \
test-captive.sh \
test-dump-config.sh \
+ test-dump-plugin.sh \
test-foreground.sh \
test-help.sh \
test-help-plugin.sh \
@@ -55,6 +56,7 @@ TESTS = \
test-help.sh \
test-help-plugin.sh \
test-dump-config.sh \
+ test-dump-plugin.sh \
test-start.sh \
test-foreground.sh \
test-single.sh \
diff --git a/tests/test-dump-plugin.sh b/tests/test-dump-plugin.sh
new file mode 100755
index 0000000..5cde25b
--- /dev/null
+++ b/tests/test-dump-plugin.sh
@@ -0,0 +1,44 @@
+#!/bin/bash -
+# nbdkit
+# Copyright (C) 2014 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+set -e
+
+output="$(
+ ../src/nbdkit \
+ ../plugins/example1/.libs/nbdkit-example1-plugin.so --dump-plugin
+ )"
+if [[ ! ( "$output" =~ ^path= ) ]]; then
+ echo "$0: unexpected output from nbdkit --dump-plugin"
+ echo "$output"
+ exit 1
+fi
--
2.7.4