This lets you write simple plugins in Tcl. All the basic features of
nbdkit are supported, but serious Tcl users will probably want to
enhance the plugin further.
Unfortunately Tcl as a language is not very well suited to handling
binary data. It prefers to store binary data in UCS-2 strings,
meaning that every second byte is wasted. Also there appears to be no
way to replace part of such a string/array in-place.
Strictly speaking Tcl requires that:
"only the thread that created a Tcl interpreter can use that
interpreter. In other words, multiple threads can not access the
same Tcl interpreter."
Apparently this applies even if nbdkit uses mutexes to ensure that
multiple threads are not calling into the interpreter at the same
time. We do not have such a threading model in nbdkit (see also VDDK)
but at the same time I was not able to get Tcl to crash.
The Tcl interpreter leaks quite a lot of memory from the tcl_load and
tcl_open calls. This may be connected with the previous point. It
makes valgrind fairly useless so it is disabled for Tcl.
---
.gitignore | 1 +
README | 6 +-
common-rules.mk | 1 +
configure.ac | 17 +
docs/nbdkit-plugin.pod.in | 8 +-
docs/nbdkit.pod.in | 3 +-
plugins/tcl/Makefile.am | 68 ++++
plugins/tcl/example.tcl | 90 +++++
plugins/tcl/nbdkit-tcl-plugin.pod | 333 ++++++++++++++++++
plugins/tcl/tcl.c | 553 ++++++++++++++++++++++++++++++
tests/Makefile.am | 13 +
tests/test-dump-plugin.sh | 2 +-
tests/test-lang-plugins.c | 3 +-
tests/test.tcl | 28 ++
14 files changed, 1119 insertions(+), 7 deletions(-)
diff --git a/.gitignore b/.gitignore
index c986d76..de091f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -81,5 +81,6 @@ Makefile.in
/tests/test-socket-activation
/tests/test-split
/tests/test-streaming
+/tests/test-tcl
/tests/test-xz
/test-driver
diff --git a/README b/README
index ab71a5d..e51b564 100644
--- a/README
+++ b/README
@@ -15,7 +15,7 @@ The key features are:
* Well-documented, simple plugin API with a stable ABI guarantee.
Lets you export "unconventional" block devices easily.
- * You can write plugins in C, Perl, Python, OCaml or Ruby.
+ * You can write plugins in C, Perl, Python, OCaml, Ruby or Tcl.
For documentation, see the docs/ directory.
@@ -91,6 +91,10 @@ For the OCaml plugin:
- OCaml >= 4.02.2 which has support for shared libraries, see:
http://caml.inria.fr/mantis/view.php?id=6693
+For the Tcl plugin:
+
+ - Tcl development library and headers
+
For bash tab completion:
- bash-completion >= 1.99
diff --git a/common-rules.mk b/common-rules.mk
index feb12c0..038f495 100644
--- a/common-rules.mk
+++ b/common-rules.mk
@@ -52,6 +52,7 @@ plugins = \
split \
streaming \
tar \
+ tcl \
vddk \
xz \
zero
diff --git a/configure.ac b/configure.ac
index be05d86..16e3250 100644
--- a/configure.ac
+++ b/configure.ac
@@ -364,6 +364,22 @@ AS_IF([test "x$RUBY" != "xno" && test
"x$enable_ruby" != "xno"],[
AM_CONDITIONAL([HAVE_RUBY],[test "x$RUBY" != "xno" &&
test "x$enable_ruby" = "xyes"])
+dnl Check for Tcl, for embedding in the Tcl plugin.
+AC_ARG_ENABLE([tcl],
+ AS_HELP_STRING([--disable-tcl], [disable Tcl plugin]),
+ [],
+ [enable_tcl=yes])
+AS_IF([test "x$enable_tcl" != "xno"],[
+ PKG_CHECK_MODULES([TCL], [tcl], [
+ AC_SUBST([TCL_CFLAGS])
+ AC_SUBST([TCL_LIBS])
+ ],[
+ AC_MSG_WARN([Tcl not found])
+ enable_tcl=no
+ ])
+])
+AM_CONDITIONAL([HAVE_TCL],[test "x$enable_tcl" = "xyes"])
+
dnl Check for curl (only if you want to compile the curl plugin).
AC_ARG_WITH([curl],[
AS_HELP_STRING([--without-curl],
@@ -584,6 +600,7 @@ AC_CONFIG_FILES([Makefile
plugins/split/Makefile
plugins/streaming/Makefile
plugins/tar/Makefile
+ plugins/tcl/Makefile
plugins/vddk/Makefile
plugins/xz/Makefile
plugins/zero/Makefile
diff --git a/docs/nbdkit-plugin.pod.in b/docs/nbdkit-plugin.pod.in
index 22ca40e..d75ef3b 100644
--- a/docs/nbdkit-plugin.pod.in
+++ b/docs/nbdkit-plugin.pod.in
@@ -63,7 +63,8 @@ To write plugins in other languages, see:
L<nbdkit-ocaml-plugin(3)>,
L<nbdkit-perl-plugin(3)>,
L<nbdkit-python-plugin(3)>,
-L<nbdkit-ruby-plugin(3)>.
+L<nbdkit-ruby-plugin(3)>,
+L<nbdkit-tcl-plugin(3)>.
=head1 C<#define NBDKIT_API_VERSION 2>
@@ -849,14 +850,15 @@ which defines C<$(NBDKIT_PLUGINDIR)> in automake-generated
Makefiles.
=head1 WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES
-You can also write nbdkit plugins in OCaml, Perl, Python or Ruby.
+You can also write nbdkit plugins in OCaml, Perl, Python, Ruby or Tcl.
Other programming languages may be offered in future.
For more information see:
L<nbdkit-ocaml-plugin(3)>,
L<nbdkit-perl-plugin(3)>,
L<nbdkit-python-plugin(3)>,
-L<nbdkit-ruby-plugin(3)>.
+L<nbdkit-ruby-plugin(3)>,
+L<nbdkit-tcl-plugin(3)>.
Plugins written in scripting languages may also be installed in
C<$plugindir>. These must be called C<nbdkit-I<name>-plugin> without
diff --git a/docs/nbdkit.pod.in b/docs/nbdkit.pod.in
index 115db3c..15196d0 100644
--- a/docs/nbdkit.pod.in
+++ b/docs/nbdkit.pod.in
@@ -978,7 +978,8 @@ L<nbdkit-filter(3)>.
L<nbdkit-ocaml-plugin(3)>,
L<nbdkit-perl-plugin(3)>,
L<nbdkit-python-plugin(3)>,
-L<nbdkit-ruby-plugin(3)>.
+L<nbdkit-ruby-plugin(3)>,
+L<nbdkit-tcl-plugin(3)>.
=head2 NBD clients
diff --git a/plugins/tcl/Makefile.am b/plugins/tcl/Makefile.am
new file mode 100644
index 0000000..618c21c
--- /dev/null
+++ b/plugins/tcl/Makefile.am
@@ -0,0 +1,68 @@
+# nbdkit
+# Copyright (C) 2018 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.
+
+include $(top_srcdir)/common-rules.mk
+
+EXTRA_DIST = \
+ nbdkit-tcl-plugin.pod \
+ example.pl
+
+if HAVE_TCL
+
+plugin_LTLIBRARIES = nbdkit-tcl-plugin.la
+
+nbdkit_tcl_plugin_la_SOURCES = \
+ tcl.c \
+ $(top_srcdir)/include/nbdkit-plugin.h
+
+nbdkit_tcl_plugin_la_CPPFLAGS = \
+ -I$(top_srcdir)/include
+nbdkit_tcl_plugin_la_CFLAGS = \
+ $(WARNINGS_CFLAGS) \
+ $(TCL_CFLAGS)
+nbdkit_tcl_plugin_la_LDFLAGS = \
+ -module -avoid-version -shared \
+ $(TCL_LIBS)
+
+if HAVE_POD2MAN
+
+man_MANS = nbdkit-tcl-plugin.3
+CLEANFILES += $(man_MANS)
+
+nbdkit-tcl-plugin.3: nbdkit-tcl-plugin.pod
+ $(POD2MAN) $(POD2MAN_ARGS) --section=3 --name=`basename $@ .3` $< $@.t && \
+ if grep 'POD ERROR' $@.t; then rm $@.t; exit 1; fi && \
+ mv $@.t $@
+
+endif
+
+endif
diff --git a/plugins/tcl/example.tcl b/plugins/tcl/example.tcl
new file mode 100644
index 0000000..306632b
--- /dev/null
+++ b/plugins/tcl/example.tcl
@@ -0,0 +1,90 @@
+# Example Tcl plugin.
+#
+# This example can be freely used for any purpose.
+
+# Run it from the build directory like this:
+#
+# ./nbdkit -f -v tcl ./plugins/tcl/example.tcl file=disk.img
+#
+# Or run it after installing nbdkit like this:
+#
+# nbdkit -f -v tcl ./plugins/tcl/example.tcl file=disk.img
+#
+# The -f -v arguments are optional. They cause the server to stay in
+# the foreground and print debugging, which is useful when testing.
+#
+# You can connect to the server using guestfish or qemu, eg:
+#
+# guestfish --format=raw -a nbd://localhost
+# ><fs> run
+# ><fs> part-disk /dev/sda mbr
+# ><fs> mkfs ext2 /dev/sda1
+# ><fs> list-filesystems
+# ><fs> mount /dev/sda1 /
+# ><fs> [etc]
+
+# This is called from: nbdkit tcl example.tcl --dump-plugin
+proc dump_plugin {} {
+ puts "example_tcl=1"
+}
+
+# We expect a file=... parameter pointing to the file to serve.
+proc config {key value} {
+ global file
+
+ if { $key == "file" } {
+ set file $value
+ } else {
+ error "unknown parameter $key=$value"
+ }
+}
+
+# Check the file parameter was passed.
+proc config_complete {} {
+ global file
+
+ if { ![info exists file] } {
+ error "file parameter missing"
+ }
+}
+
+# Open a new client connection.
+proc plugin_open {readonly} {
+ global file
+
+ # Open the file.
+ if { $readonly } {
+ set flags "r"
+ } else {
+ set flags "r+"
+ }
+ set fh [open $file $flags]
+
+ # Stop Tcl from trying to convert to and from UTF-8.
+ fconfigure $fh -translation binary
+
+ # We can return any Tcl object as the handle. In this
+ # plugin it's convenient to return the file handle.
+ return $fh
+}
+
+# Close a client connection.
+proc plugin_close {fh} {
+ close $fh
+}
+
+proc get_size {fh} {
+ global file
+
+ return [file size $file]
+}
+
+proc pread {fh count offset} {
+ seek $fh $offset
+ return [read $fh $count]
+}
+
+proc pwrite {fh buf offset} {
+ seek $fh $offset
+ puts -nonewline $fh $buf
+}
diff --git a/plugins/tcl/nbdkit-tcl-plugin.pod b/plugins/tcl/nbdkit-tcl-plugin.pod
new file mode 100644
index 0000000..d21ab5b
--- /dev/null
+++ b/plugins/tcl/nbdkit-tcl-plugin.pod
@@ -0,0 +1,333 @@
+=encoding utf8
+
+=head1 NAME
+
+nbdkit-tcl-plugin - nbdkit Tcl plugin
+
+=head1 SYNOPSIS
+
+ nbdkit tcl /path/to/plugin.tcl [arguments...]
+
+=head1 DESCRIPTION
+
+C<nbdkit-tcl-plugin> is an embedded Tcl interpreter for
+L<nbdkit(1)>, allowing you to write nbdkit plugins in Tcl.
+
+Broadly speaking, Tcl nbdkit plugins work like C ones, so you should
+read L<nbdkit-plugin(3)> first.
+
+=head2 USING A TCL NBDKIT PLUGIN
+
+Assuming you have a Tcl script which is an nbdkit plugin, you run it
+like this:
+
+ nbdkit tcl /path/to/plugin.tcl
+
+You may have to add further C<key=value> arguments to the command
+line. Read the Tcl script to see if it requires any.
+
+=head1 WRITING A TCL NBDKIT PLUGIN
+
+For an example plugin written in Tcl, see:
+L<https://github.com/libguestfs/nbdkit/blob/master/plugins/tcl/example.tcl>
+
+To write a Tcl nbdkit plugin, you create a Tcl file which contains
+at least the following required subroutines:
+
+ proc plugin_open {readonly} {
+ # see below
+ return $h
+ }
+ proc get_size {h} {
+ # see below
+ return $size
+ }
+ proc pread {h count offset} {
+ # see below
+ return $buf
+ }
+
+Note that the subroutines must have those literal names (like
+C<plugin_open>), because the C part looks up and calls those functions
+directly. You may want to include documentation and globals (eg. for
+storing global state). Also any top-level statements are run when
+nbdkit starts up.
+
+=head2 EXECUTABLE SCRIPT
+
+If you want you can make the script executable and include a "shebang"
+at the top:
+
+ #!/usr/sbin/nbdkit tcl
+
+See also L<nbdkit(1)/Shebang scripts>.
+
+These scripts can also be installed in the C<$plugindir>. See
+L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.
+
+=head2 EXCEPTIONS
+
+Tcl plugin methods can indicate an error by calling C<error>.
+
+=head2 BINARY DATA
+
+When writing your Tcl script, be careful to ensure that it is
+processing binary data (not Unicode). If reading and writing from
+local disk files, you should use:
+
+ fconfigure $fp -translation binary
+
+Note also that the value returned from C<pread> should convertible to
+a byte array, and the buffer passed to C<pwrite> is also a byte array.
+
+See also: L<https://wiki.tcl.tk/1180>
+
+=head2 TCL CALLBACKS
+
+This just documents the arguments to the callbacks in Tcl, and any
+way that they differ from the C callbacks. In all other respects they
+work the same way as the C callbacks, so you should go and read
+L<nbdkit-plugin(3)>.
+
+=over 4
+
+=item C<dump_plugin>
+
+(Optional)
+
+There are no arguments or return value.
+
+=item C<config>
+
+(Optional)
+
+ proc config {key value} {
+ # No return value.
+ }
+
+=item C<config_complete>
+
+(Optional)
+
+There are no arguments or return value.
+
+=item C<plugin_open>
+
+(Required)
+
+ proc plugin_open {readonly} {
+ set handle ...
+ return $handle
+ }
+
+The C<readonly> flag is a boolean.
+
+You can return any Tcl string or object as the handle. It is passed
+back to subsequent calls.
+
+=item C<plugin_close>
+
+(Optional)
+
+ proc plugin_close {h} {
+ # No return value
+ }
+
+After C<plugin_close> returns, the reference count of the handle is
+decremented in the C part, which usually means that the handle and its
+contents will be garbage collected.
+
+=item C<get_size>
+
+(Required)
+
+ proc get_size {h} {
+ set size .. the size of the disk ..
+ return $size
+ }
+
+This returns the size of the disk.
+
+=item C<can_write>
+
+(Optional)
+
+ proc can_write {h} {
+ return $bool
+ }
+
+Return a boolean indicating whether the disk is writable.
+
+=item C<can_flush>
+
+(Optional)
+
+ proc can_flush {h} {
+ return $bool
+ }
+
+Return a boolean indicating whether flush can be performed.
+
+=item C<is_rotational>
+
+(Optional)
+
+ proc is_rotational {h} {
+ return $bool
+ }
+
+Return a boolean indicating whether the disk is rotational.
+
+=item C<can_trim>
+
+(Optional)
+
+ proc can_trim {h} {
+ return $bool
+ }
+
+Return a boolean indicating whether trim/discard can be performed.
+
+=item C<pread>
+
+(Required)
+
+ proc pread {h count offset} {
+ # Construct a buffer of length $count bytes and return it.
+ return $buf
+ }
+
+The body of your C<pread> function should construct a buffer of length
+(at least) C<$count> bytes. You should read C<$count> bytes from the
+disk starting at C<$offset>.
+
+NBD only supports whole reads, so your function should try to read the
+whole region (perhaps requiring a loop). If the read fails or is
+partial, your function should call C<error>.
+
+=item C<pwrite>
+
+(Optional)
+
+ proc pwrite {h buf offset} {
+ # No return value
+ }
+
+The body of your C<pwrite> function should write the C<$buf> string to
+the disk. You should write C<$count> bytes to the disk starting at
+C<$offset>.
+
+NBD only supports whole writes, so your function should try to write
+the whole region (perhaps requiring a loop). If the write fails or is
+partial, your function should call C<error>.
+
+=item C<plugin_flush>
+
+(Optional)
+
+ proc plugin_flush {h} {
+ # No return value
+ }
+
+The body of your C<plugin_flush> function should do a L<sync(2)> or
+L<fdatasync(2)> or equivalent on the backing store.
+
+=item C<trim>
+
+(Optional)
+
+ proc trim {h count offset} {
+ # No return value
+ }
+
+The body of your C<trim> function should "punch a hole" in the backing
+store.
+
+=item C<zero>
+
+(Optional)
+
+ proc zero {h count offset may_trim} {
+ # No return value
+ }
+
+The body of your C<zero> function should ensure that C<$count> bytes
+of the disk, starting at C<$offset>, will read back as zero. If
+C<$may_trim> is true, the operation may be optimized as a trim as long
+as subsequent reads see zeroes.
+
+NBD only supports whole writes, so your function should try to write
+the whole region (perhaps requiring a loop). If the write fails or is
+partial, your function should call C<error>.
+
+=back
+
+=head2 MISSING CALLBACKS
+
+=over 4
+
+=item Missing: C<load>, C<unload>, C<name>, C<version>,
C<longname>, C<description>, C<config_help>
+
+These are not yet supported.
+
+=back
+
+=head2 THREADS
+
+The thread model for Tcl callbacks currently cannot be set from Tcl.
+It is hard-coded in the C part to
+C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
+settable in future.
+
+=head1 SEE ALSO
+
+L<nbdkit(1)>,
+L<nbdkit-plugin(3)>.
+
+=head1 AUTHORS
+
+Richard W.M. Jones
+
+=head1 COPYRIGHT
+
+Copyright (C) 2018 Red Hat Inc.
+
+=head1 LICENSE
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+=over 4
+
+=item *
+
+Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+
+=item *
+
+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.
+
+=item *
+
+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.
+
+=back
+
+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.
diff --git a/plugins/tcl/tcl.c b/plugins/tcl/tcl.c
new file mode 100644
index 0000000..8b93a9b
--- /dev/null
+++ b/plugins/tcl/tcl.c
@@ -0,0 +1,553 @@
+/* nbdkit
+ * Copyright (C) 2018 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.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <tcl.h>
+
+#include <nbdkit-plugin.h>
+
+static Tcl_Interp *interp;
+static const char *script;
+
+static void
+tcl_load (void)
+{
+ //Tcl_FindExecutable ("nbdkit");
+ interp = Tcl_CreateInterp ();
+ if (Tcl_Init (interp) != TCL_OK) {
+ nbdkit_error ("cannot initialize Tcl interpreter: %s",
+ Tcl_GetStringResult (interp));
+ exit (EXIT_FAILURE);
+ }
+}
+
+static void
+tcl_unload (void)
+{
+ if (interp)
+ Tcl_DeleteInterp (interp);
+ Tcl_Finalize ();
+}
+
+/* Test if proc was defined by the Tcl code. */
+static int
+proc_defined (const char *name)
+{
+ int r;
+ Tcl_Obj *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("info", -1));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("procs", -1));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj (name, -1));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("info procs: %s", Tcl_GetStringResult (interp));
+ return 0; /* We can't return an error here, just return false. */
+ }
+
+ /* 'info procs name' returns the proc name if it exists, else empty
+ * string, so we can just check if the result is not empty.
+ */
+ return strcmp (Tcl_GetStringResult (interp), "") != 0;
+}
+
+static void
+tcl_dump_plugin (void)
+{
+ if (script && proc_defined ("dump_plugin")) {
+ int r;
+ Tcl_Obj *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("dump_plugin", -1));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK)
+ nbdkit_error ("dump_plugin: %s", Tcl_GetStringResult (interp));
+ }
+}
+
+static int
+tcl_config (const char *key, const char *value)
+{
+ int r;
+
+ if (!script) {
+ /* The first parameter MUST be "script". */
+ if (strcmp (key, "script") != 0) {
+ nbdkit_error ("the first parameter must be script=/path/to/script.tcl");
+ return -1;
+ }
+ script = value;
+
+ assert (interp);
+
+ /* Load the Tcl file. */
+ r = Tcl_EvalFile (interp, script);
+ if (r != TCL_OK) {
+ if (r == TCL_ERROR)
+ nbdkit_error ("could not load Tcl script: %s: line %d: %s",
+ script, Tcl_GetErrorLine (interp),
+ Tcl_GetStringResult (interp));
+ else
+ nbdkit_error ("could not load Tcl script: %s: %s",
+ script, Tcl_GetStringResult (interp));
+ return -1;
+ }
+
+ /* Minimal set of callbacks which are required (by nbdkit itself). */
+ if (!proc_defined ("plugin_open") ||
+ !proc_defined ("get_size") ||
+ !proc_defined ("pread")) {
+ nbdkit_error ("%s: one of the required callbacks 'plugin_open',
'get_size' or 'pread' is not defined by this Tcl script. nbdkit requires
these callbacks.", script);
+ return -1;
+ }
+ }
+ else if (proc_defined ("config")) {
+ int r;
+ Tcl_Obj *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("config", -1));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj (key, -1));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj (value, -1));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("config: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ }
+ else {
+ /* Emulate what core nbdkit does if a config callback is NULL. */
+ nbdkit_error ("%s: this plugin does not need command line configuration",
+ script);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+tcl_config_complete (void)
+{
+ int r;
+ Tcl_Obj *cmd;
+
+ if (proc_defined ("config_complete")) {
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("config_complete",
-1));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("config_complete: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static void *
+tcl_open (int readonly)
+{
+ int r;
+ Tcl_Obj *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("plugin_open", -1));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewBooleanObj (readonly));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("plugin_open: %s", Tcl_GetStringResult (interp));
+ return NULL;
+ }
+
+ res = Tcl_GetObjResult (interp);
+ Tcl_IncrRefCount (res);
+ return res;
+}
+
+static void
+tcl_close (void *handle)
+{
+ int r;
+ Tcl_Obj *h = handle, *cmd;
+
+ if (proc_defined ("plugin_close")) {
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("plugin_close", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK)
+ nbdkit_error ("plugin_close: %s", Tcl_GetStringResult (interp));
+ }
+
+ /* Ensure that the handle is freed. */
+ Tcl_DecrRefCount (h);
+}
+
+static int64_t
+tcl_get_size (void *handle)
+{
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+ Tcl_WideInt size;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("get_size", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("get_size: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+
+ res = Tcl_GetObjResult (interp);
+ if (Tcl_GetWideIntFromObj (interp, res, &size) != TCL_OK) {
+ nbdkit_error ("get_size: Tcl_GetWideIntFromObj: %s",
+ Tcl_GetStringResult (interp));
+ return -1;
+ }
+ return size;
+}
+
+static int
+tcl_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
+{
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+ unsigned char *res_bin;
+ int res_len;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("pread", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewIntObj (count));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewWideIntObj (offset));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("pread: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+
+ res = Tcl_GetObjResult (interp);
+ res_bin = Tcl_GetByteArrayFromObj (res, &res_len);
+ if (res_len < count) {
+ nbdkit_error ("pread: buffer returned from pread is too small");
+ return -1;
+ }
+
+ memcpy (buf, res_bin, count);
+ return 0;
+}
+
+static int
+tcl_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset)
+{
+ if (proc_defined ("pwrite")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("pwrite", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewByteArrayObj (buf, count));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewWideIntObj (offset));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("pwrite: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ return 0;
+ }
+
+ nbdkit_error ("pwrite not implemented");
+ return -1;
+}
+
+static int
+tcl_can_write (void *handle)
+{
+ if (proc_defined ("can_write")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("can_write", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("can_write: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ res = Tcl_GetObjResult (interp);
+ Tcl_GetBooleanFromObj (interp, res, &r);
+ return r;
+ }
+ /* No can_write callback, but there's a pwrite callback defined, so
+ * return 1. (In C modules, nbdkit would do this).
+ */
+ else if (proc_defined ("pwrite"))
+ return 1;
+ else
+ return 0;
+}
+
+static int
+tcl_can_flush (void *handle)
+{
+ if (proc_defined ("can_flush")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("can_flush", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("can_flush: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ res = Tcl_GetObjResult (interp);
+ Tcl_GetBooleanFromObj (interp, res, &r);
+ return r;
+ }
+ /* No can_flush callback, but there's a plugin_flush callback
+ * defined, so return 1. (In C modules, nbdkit would do this).
+ */
+ else if (proc_defined ("plugin_flush"))
+ return 1;
+ else
+ return 0;
+}
+
+static int
+tcl_can_trim (void *handle)
+{
+ if (proc_defined ("can_trim")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("can_trim", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("can_trim: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ res = Tcl_GetObjResult (interp);
+ Tcl_GetBooleanFromObj (interp, res, &r);
+ return r;
+ }
+ /* No can_trim callback, but there's a trim callback defined, so
+ * return 1. (In C modules, nbdkit would do this).
+ */
+ else if (proc_defined ("trim"))
+ return 1;
+ else
+ return 0;
+}
+
+static int
+tcl_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
+{
+ if (proc_defined ("zero")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("can_zero", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("can_zero: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ res = Tcl_GetObjResult (interp);
+ Tcl_GetBooleanFromObj (interp, res, &r);
+ return r;
+ }
+
+ nbdkit_debug ("zero falling back to pwrite");
+ nbdkit_set_error (EOPNOTSUPP);
+ return -1;
+}
+
+static int
+tcl_is_rotational (void *handle)
+{
+ if (proc_defined ("is_rotational")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd, *res;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("is_rotational", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("is_rotational: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ res = Tcl_GetObjResult (interp);
+ Tcl_GetBooleanFromObj (interp, res, &r);
+ return r;
+ }
+ else
+ return 0;
+}
+
+static int
+tcl_flush (void *handle)
+{
+ if (proc_defined ("plugin_flush")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("plugin_flush", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("plugin_flush: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ return 0;
+ }
+
+ /* Ignore lack of flush callback, although probably nbdkit will
+ * never call this since .can_flush returns false.
+ */
+ return 0;
+}
+
+static int
+tcl_trim (void *handle, uint32_t count, uint64_t offset)
+{
+ if (proc_defined ("trim")) {
+ int r;
+ Tcl_Obj *h = handle, *cmd;
+
+ cmd = Tcl_NewObj ();
+ Tcl_IncrRefCount (cmd);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewStringObj ("trim", -1));
+ Tcl_ListObjAppendElement (0, cmd, h);
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewIntObj (count));
+ Tcl_ListObjAppendElement (0, cmd, Tcl_NewWideIntObj (offset));
+ r = Tcl_EvalObjEx (interp, cmd, TCL_EVAL_DIRECT);
+ Tcl_DecrRefCount (cmd);
+ if (r != TCL_OK) {
+ nbdkit_error ("trim: %s", Tcl_GetStringResult (interp));
+ return -1;
+ }
+ return 0;
+ }
+
+ /* Ignore lack of trim callback, although probably nbdkit will never
+ * call this since .can_trim returns false.
+ */
+ return 0;
+}
+
+#define tcl_config_help \
+ "script=<FILENAME> (required) The Tcl script to run.\n" \
+ "[other arguments may be used by the plugin that you load]"
+
+#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
+
+static struct nbdkit_plugin plugin = {
+ .name = "tcl",
+ .version = PACKAGE_VERSION,
+
+ .load = tcl_load,
+ .unload = tcl_unload,
+ .dump_plugin = tcl_dump_plugin,
+
+ .config = tcl_config,
+ .config_complete = tcl_config_complete,
+ .config_help = tcl_config_help,
+
+ .open = tcl_open,
+ .close = tcl_close,
+
+ .get_size = tcl_get_size,
+ .can_write = tcl_can_write,
+ .can_flush = tcl_can_flush,
+ .is_rotational = tcl_is_rotational,
+ .can_trim = tcl_can_trim,
+
+ .pread = tcl_pread,
+ .pwrite = tcl_pwrite,
+ .flush = tcl_flush,
+ .trim = tcl_trim,
+ .zero = tcl_zero,
+};
+
+NBDKIT_REGISTER_PLUGIN(plugin)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a7a31b9..d42daeb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -461,6 +461,19 @@ test_ruby_LDADD = libtest.la $(LIBGUESTFS_LIBS)
endif HAVE_RUBY
+# Tcl plugin test.
+if HAVE_TCL
+
+LIBGUESTFS_TESTS += test-tcl
+
+test_tcl_SOURCES = test-lang-plugins.c test.h
+test_tcl_CFLAGS = \
+ -DLANG='"tcl"' -DSCRIPT='"test.tcl"' \
+ $(WARNINGS_CFLAGS) $(LIBGUESTFS_CFLAGS)
+test_tcl_LDADD = libtest.la $(LIBGUESTFS_LIBS)
+
+endif HAVE_TCL
+
#----------------------------------------------------------------------
# Tests of filters.
diff --git a/tests/test-dump-plugin.sh b/tests/test-dump-plugin.sh
index 1104727..6f94eab 100755
--- a/tests/test-dump-plugin.sh
+++ b/tests/test-dump-plugin.sh
@@ -64,7 +64,7 @@ for p in $plugins; do
# VDDK won't run without special environment variables
# being set, so ignore it.
;;
- perl-valgrind | python-valgrind | ruby-valgrind | \
+ perl-valgrind | python-valgrind | ruby-valgrind | tcl-valgrind | \
example4-valgrind | tar-valgrind)
# Plugins written in scripting languages can't run under valgrind.
;;
diff --git a/tests/test-lang-plugins.c b/tests/test-lang-plugins.c
index fc3cbef..92a7ec2 100644
--- a/tests/test-lang-plugins.c
+++ b/tests/test-lang-plugins.c
@@ -56,7 +56,8 @@ main (int argc, char *argv[])
*/
if (getenv ("NBDKIT_VALGRIND") != NULL &&
(strcmp (LANG, "python") == 0 ||
- strcmp (LANG, "ruby") == 0)) {
+ strcmp (LANG, "ruby") == 0 ||
+ strcmp (LANG, "tcl") == 0)) {
fprintf (stderr, "%s test skipped under valgrind.\n", LANG);
exit (77); /* Tells automake to skip the test. */
}
diff --git a/tests/test.tcl b/tests/test.tcl
new file mode 100644
index 0000000..57e8d15
--- /dev/null
+++ b/tests/test.tcl
@@ -0,0 +1,28 @@
+# XXX This actually creates a Unicode (UCS-2) array. It's also rather
+# slow to run. It's possible we should be using a list instead.
+set disk [string repeat "\u0000" [expr 1024*1024]]
+
+proc plugin_open {readonly} {
+ return 1
+}
+
+proc get_size {h} {
+ global disk
+
+ return [string length $disk]
+}
+
+proc pread {h count offset} {
+ global disk
+
+ set last [expr $offset+$count-1]
+ return [string range $disk $offset $last]
+}
+
+proc pwrite {h buf offset} {
+ global disk
+
+ set count [string length $buf]
+ set last [expr $offset+$count-1]
+ set disk [string replace $disk $offset $last $buf]
+}
--
2.17.1