Currently it does nothing. --- configure.ac | 1 + Makefile.am | 1 + lib/Makefile.am | 71 ++++++++++++++++++++++++++++++++++++++++++++++ server/Makefile.am | 3 ++ server/internal.h | 1 + lib/lib.h | 48 +++++++++++++++++++++++++++++++ lib/init.c | 53 ++++++++++++++++++++++++++++++++++ wrapper.c | 18 ++++++++---- lib/libnbdkit.syms | 47 ++++++++++++++++++++++++++++++ 9 files changed, 237 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 92e0d4e3..bf720cec 100644 --- a/configure.ac +++ b/configure.ac @@ -1057,6 +1057,7 @@ AC_CONFIG_FILES([Makefile filters/truncate/Makefile filters/xz/Makefile fuzzing/Makefile + lib/Makefile server/Makefile server/nbdkit.pc tests/functions.sh diff --git a/Makefile.am b/Makefile.am index ec8ae05d..3e6091f7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -75,6 +75,7 @@ SUBDIRS = \ common/include \ common/protocol \ common/utils \ + lib \ server \ $(NULL) diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 00000000..4896dc95 --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,71 @@ +# nbdkit +# Copyright (C) 2013-2020 Red Hat Inc. +# +# 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 = libnbdkit.syms + +# Note this library always has soname “libnbdkit.so.0” because plugins +# which may link to this library must forever have a stable soname. +# However the library only works with the corresponding nbdkit server +# binary compiled at the same time. The two must be shipped together. + +lib_LTLIBRARIES = libnbdkit.la +libnbdkit_la_SOURCES = \ + init.c \ + lib.h \ + $(NULL) + +libnbdkit_la_CPPFLAGS = \ + -I$(top_srcdir)/include \ + -I$(top_srcdir)/common/include \ + -I$(top_srcdir)/common/utils \ + -DIN_NBDKIT_LIB=1 \ + $(NULL) +libnbdkit_la_CFLAGS = \ + $(PTHREAD_CFLAGS) \ + $(WARNINGS_CFLAGS) \ + $(NULL) +libnbdkit_la_LDFLAGS = \ + $(PTHREAD_LIBS) \ + $(NULL) +libnbdkit_la_LIBADD = \ + $(top_builddir)/common/utils/libutils.la \ + $(NULL) + +if USE_LINKER_SCRIPT_FOR_SERVER +# We have to disable the linker script for libFuzzer because Clang +# adds loads of fuzzer and ASAN-related symbols that are required by +# the plugins but which our linker script tries to hide. +if !ENABLE_LIBFUZZER +libnbdkit_la_LDFLAGS += -Wl,--version-script=$(srcdir)/libnbdkit.syms +endif +endif diff --git a/server/Makefile.am b/server/Makefile.am index 4c789934..ad0de9b1 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -80,6 +80,7 @@ nbdkit_CPPFLAGS = \ -Dfilterdir=\"$(filterdir)\" \ -Dsbindir=\"$(sbindir)\" \ -Dsysconfdir=\"$(sysconfdir)\" \ + -I$(top_srcdir)/lib \ -I$(top_srcdir)/include \ -I$(top_srcdir)/common/include \ -I$(top_srcdir)/common/protocol \ @@ -93,6 +94,7 @@ nbdkit_CFLAGS = \ $(VALGRIND_CFLAGS) \ $(NULL) nbdkit_LDADD = \ + ../lib/libnbdkit.la \ $(GNUTLS_LIBS) \ $(LIBSELINUX_LIBS) \ $(DL_LIBS) \ @@ -142,6 +144,7 @@ test_public_SOURCES = \ extents.c \ $(NULL) test_public_CPPFLAGS = \ + -I$(top_srcdir)/lib \ -I$(top_srcdir)/include \ -I$(top_srcdir)/common/include \ -I$(top_srcdir)/common/protocol \ diff --git a/server/internal.h b/server/internal.h index b43798ff..a1db231e 100644 --- a/server/internal.h +++ b/server/internal.h @@ -43,6 +43,7 @@ #define NBDKIT_INTERNAL #include "nbdkit-plugin.h" #include "nbdkit-filter.h" +#include "lib.h" #include "cleanup.h" #include "nbd-protocol.h" diff --git a/lib/lib.h b/lib/lib.h new file mode 100644 index 00000000..2d7ca930 --- /dev/null +++ b/lib/lib.h @@ -0,0 +1,48 @@ +/* nbdkit + * Copyright (C) 2013-2020 Red Hat Inc. + * + * 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. + */ + +#ifndef NBDKIT_LIB_H +#define NBDKIT_LIB_H + +#include <stdarg.h> +#include <stdbool.h> +#include <sys/types.h> +#include <sys/socket.h> + +/* Defines the private function which is used by the server to + * initialize libnbdkit.so at runtime. This ABI may change at any + * time, which is why nbdkit and the corresponding libnbdkit.so must + * always be shipped together. + */ +extern void libnbdkit_private_init (const char *expected_version); + +#endif /* NBDKIT_LIB_H */ diff --git a/lib/init.c b/lib/init.c new file mode 100644 index 00000000..ef7eeb3d --- /dev/null +++ b/lib/init.c @@ -0,0 +1,53 @@ +/* nbdkit + * Copyright (C) 2013-2020 Red Hat Inc. + * + * 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 <stdbool.h> +#include <string.h> + +#include "lib.h" + +bool *verbose; + +void +libnbdkit_private_init (const char *expected_version) +{ + if (strcmp (expected_version, PACKAGE_VERSION) != 0) { + fprintf (stderr, + "packaging error: " + "nbdkit and libnbdkit.so versions do not match\n"); + abort (); + } +} diff --git a/wrapper.c b/wrapper.c index 6aef81a1..eb0ba8ba 100644 --- a/wrapper.c +++ b/wrapper.c @@ -168,12 +168,16 @@ main (int argc, char *argv[]) } } - /* Needed for plugins written in OCaml. */ + /* Use libnbdkit.so from lib/. The plugins/ocaml/.libs path is + * needed for plugins written in OCaml. + */ s = getenv ("LD_LIBRARY_PATH"); if (s) - r = asprintf (&s, "%s/plugins/ocaml/.libs:%s", builddir, s); + r = asprintf (&s, "%s/lib/.libs:%s/plugins/ocaml/.libs:%s", + builddir, builddir, s); else - r = asprintf (&s, "%s/plugins/ocaml/.libs", builddir); + r = asprintf (&s, "%s/lib/.libs:%s/plugins/ocaml/.libs", + builddir, builddir); if (r < 0) { perror ("asprintf"); exit (EXIT_FAILURE); @@ -182,9 +186,11 @@ main (int argc, char *argv[]) free (s); s = getenv ("LIBRARY_PATH"); if (s) - r = asprintf (&s, "%s/plugins/ocaml/.libs:%s", builddir, s); + r = asprintf (&s, "%s/lib/.libs:%s/plugins/ocaml/.libs:%s", + builddir, builddir, s); else - r = asprintf (&s, "%s/plugins/ocaml/.libs", builddir); + r = asprintf (&s, "%s/lib/.libs:%s/plugins/ocaml/.libs", + builddir, builddir); if (r < 0) { perror ("asprintf"); exit (EXIT_FAILURE); @@ -193,7 +199,7 @@ main (int argc, char *argv[]) free (s); /* Absolute path of the real nbdkit command. */ - passthru_format ("%s/server/nbdkit", builddir); + passthru_format ("%s/server/.libs/nbdkit", builddir); /* Option parsing. We don't really parse options here. We are only * interested in which options have arguments and which need diff --git a/lib/libnbdkit.syms b/lib/libnbdkit.syms new file mode 100644 index 00000000..a70b35fc --- /dev/null +++ b/lib/libnbdkit.syms @@ -0,0 +1,47 @@ +# nbdkit +# Copyright (C) 2018-2020 Red Hat Inc. +# +# 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. + +# This linker script controls the visibility of symbols in the +# libnbdkit.so library. We want to export some symbols to plugins, +# but at the same time we don't want plugins to be able to call +# arbitrary functions from nbdkit, so this script lists only the +# symbols we want to export. + +{ + # The functions we want plugins and filters to call. + global: + + # Private function that must only be called by the server. + libnbdkit_private_init; + + # Everything else is hidden. + local: *; +}; -- 2.25.0