--- lib/Makefile.am | 1 + server/internal.h | 7 +++++ server/main.c | 5 +++ server/nbdkit.syms | 5 --- server/plugins.c | 2 +- server/public.c | 21 +++---------- server/quit.c | 2 +- lib/lib.h | 15 +++++++++ lib/init.c | 15 +++++++++ lib/slow.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ lib/libnbdkit.syms | 5 +++ 11 files changed, 132 insertions(+), 24 deletions(-) diff --git a/lib/Makefile.am b/lib/Makefile.am index 6826269b..4fca7c9d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -48,6 +48,7 @@ libnbdkit_la_SOURCES = \ parse.c \ password.c \ path.c \ + slow.c \ vfprintf.c \ $(NULL) diff --git a/server/internal.h b/server/internal.h index a3f4d1f1..9ef0b066 100644 --- a/server/internal.h +++ b/server/internal.h @@ -153,6 +153,7 @@ extern int quit_fd; extern void set_up_quit_pipe (void); extern void close_quit_pipe (void); extern void handle_quit (int sig); +extern void do_nbdkit_shutdown (void); /* signals.c */ extern void set_up_signals (void); @@ -473,6 +474,7 @@ extern int backend_cache (struct backend *b, extern struct backend *plugin_register (size_t index, const char *filename, void *dl, struct nbdkit_plugin *(*plugin_init) (void)) __attribute__((__nonnull__ (2, 3, 4))); +extern void do_nbdkit_set_error (int err); /* filters.c */ extern struct backend *filter_register (struct backend *next, size_t index, @@ -515,6 +517,11 @@ extern void *threadlocal_buffer (size_t size); extern void threadlocal_set_conn (struct connection *conn); extern struct connection *threadlocal_get_conn (void); +/* public.c */ +extern int do_nbdkit_nanosleep (unsigned sec, unsigned nsec); +extern const char *do_nbdkit_export_name (void); +extern int do_nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen); + /* Macro which sets local variable struct connection *conn from * thread-local storage, asserting that it is non-NULL. If you want * to check if conn could be NULL (eg. outside a connection context) diff --git a/server/main.c b/server/main.c index 9fcb8e13..156d85bd 100644 --- a/server/main.c +++ b/server/main.c @@ -171,6 +171,11 @@ main (int argc, char *argv[]) /* Initialize libnbdkit.so. This must be done very early. */ libnbdkit_private_init (PACKAGE_VERSION, &verbose, + do_nbdkit_export_name, + do_nbdkit_nanosleep, + do_nbdkit_peer_name, + do_nbdkit_set_error, + do_nbdkit_shutdown, do_nbdkit_verror, threadlocal_get_name, threadlocal_get_instance_num); diff --git a/server/nbdkit.syms b/server/nbdkit.syms index ea46ac3e..5b665f85 100644 --- a/server/nbdkit.syms +++ b/server/nbdkit.syms @@ -34,11 +34,6 @@ { global: - nbdkit_export_name; - nbdkit_nanosleep; - nbdkit_peer_name; - nbdkit_set_error; - nbdkit_shutdown; # -D server.* flags must be visible to nbdkit itself. nbdkit_debug_*; diff --git a/server/plugins.c b/server/plugins.c index fa572a6a..444ba63e 100644 --- a/server/plugins.c +++ b/server/plugins.c @@ -460,7 +460,7 @@ plugin_can_cache (struct backend *b, void *handle) * where !errno_is_preserved. */ void -nbdkit_set_error (int err) +do_nbdkit_set_error (int err) { threadlocal_set_error (err); } diff --git a/server/public.c b/server/public.c index 33d40688..56302bb4 100644 --- a/server/public.c +++ b/server/public.c @@ -30,34 +30,21 @@ * SUCH DAMAGE. */ -/* This file contains the public utility APIs to be exported by nbdkit - * for use by filters and plugins, declared in nbdkit-common.h. - */ - #include <config.h> #include <stdio.h> #include <stdlib.h> -#include <stdbool.h> -#include <stdint.h> -#include <inttypes.h> -#include <string.h> #include <unistd.h> #include <fcntl.h> #include <limits.h> -#include <ctype.h> -#include <termios.h> -#include <errno.h> -#include <poll.h> +#include <time.h> #include <signal.h> #include <sys/socket.h> -#include "get-current-dir-name.h" - #include "internal.h" int -nbdkit_nanosleep (unsigned sec, unsigned nsec) +do_nbdkit_nanosleep (unsigned sec, unsigned nsec) { struct timespec ts; @@ -136,7 +123,7 @@ nbdkit_nanosleep (unsigned sec, unsigned nsec) } const char * -nbdkit_export_name (void) +do_nbdkit_export_name (void) { struct connection *conn = threadlocal_get_conn (); @@ -149,7 +136,7 @@ nbdkit_export_name (void) } int -nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen) +do_nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen) { struct connection *conn = threadlocal_get_conn (); int s; diff --git a/server/quit.c b/server/quit.c index 13fef437..9b96d33a 100644 --- a/server/quit.c +++ b/server/quit.c @@ -106,7 +106,7 @@ handle_quit (int sig) } void -nbdkit_shutdown (void) +do_nbdkit_shutdown (void) { set_quit (); } diff --git a/lib/lib.h b/lib/lib.h index 077bcdc6..317602ef 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -38,6 +38,11 @@ #include <sys/types.h> #include <sys/socket.h> +typedef const char *(*do_nbdkit_export_name_t) (void); +typedef int (*do_nbdkit_nanosleep_t) (unsigned sec, unsigned nsec); +typedef int (*do_nbdkit_peer_name_t) (struct sockaddr *addr, socklen_t *addrlen); +typedef void (*do_nbdkit_set_error_t) (int err); +typedef void (*do_nbdkit_shutdown_t) (void); typedef void (*do_nbdkit_verror_t) (const char *fs, va_list args); typedef const char *(*threadlocal_get_name_t) (void); typedef size_t (*threadlocal_get_instance_num_t) (void); @@ -48,6 +53,11 @@ typedef size_t (*threadlocal_get_instance_num_t) (void); * are set up to point back to the corresponding functions in the * server. */ +extern do_nbdkit_export_name_t do_nbdkit_export_name; +extern do_nbdkit_nanosleep_t do_nbdkit_nanosleep; +extern do_nbdkit_peer_name_t do_nbdkit_peer_name; +extern do_nbdkit_set_error_t do_nbdkit_set_error; +extern do_nbdkit_shutdown_t do_nbdkit_shutdown; extern do_nbdkit_verror_t do_nbdkit_verror; extern threadlocal_get_name_t threadlocal_get_name; extern threadlocal_get_instance_num_t threadlocal_get_instance_num; @@ -80,6 +90,11 @@ extern int replace_vfprintf (FILE *f, const char *fmt, va_list args) */ extern void libnbdkit_private_init (const char *expected_version, bool *verbose, + do_nbdkit_export_name_t do_nbdkit_export_name, + do_nbdkit_nanosleep_t do_nbdkit_nanosleep, + do_nbdkit_peer_name_t do_nbdkit_peer_name, + do_nbdkit_set_error_t do_nbdkit_set_error, + do_nbdkit_shutdown_t do_nbdkit_shutdown, do_nbdkit_verror_t do_nbdkit_verror, threadlocal_get_name_t threadlocal_get_name, threadlocal_get_instance_num_t threadlocal_get_instance_num); diff --git a/lib/init.c b/lib/init.c index 172c265e..ee70607e 100644 --- a/lib/init.c +++ b/lib/init.c @@ -41,6 +41,11 @@ bool *verbose; +do_nbdkit_export_name_t do_nbdkit_export_name; +do_nbdkit_nanosleep_t do_nbdkit_nanosleep; +do_nbdkit_peer_name_t do_nbdkit_peer_name; +do_nbdkit_set_error_t do_nbdkit_set_error; +do_nbdkit_shutdown_t do_nbdkit_shutdown; do_nbdkit_verror_t do_nbdkit_verror; threadlocal_get_name_t threadlocal_get_name; threadlocal_get_instance_num_t threadlocal_get_instance_num; @@ -48,6 +53,11 @@ threadlocal_get_instance_num_t threadlocal_get_instance_num; void libnbdkit_private_init (const char *expected_version, bool *_verbose, + do_nbdkit_export_name_t _do_nbdkit_export_name, + do_nbdkit_nanosleep_t _do_nbdkit_nanosleep, + do_nbdkit_peer_name_t _do_nbdkit_peer_name, + do_nbdkit_set_error_t _do_nbdkit_set_error, + do_nbdkit_shutdown_t _do_nbdkit_shutdown, do_nbdkit_verror_t _do_nbdkit_verror, threadlocal_get_name_t _threadlocal_get_name, threadlocal_get_instance_num_t _threadlocal_get_instance_num) @@ -61,6 +71,11 @@ libnbdkit_private_init (const char *expected_version, verbose = _verbose; + do_nbdkit_export_name = _do_nbdkit_export_name; + do_nbdkit_nanosleep = _do_nbdkit_nanosleep; + do_nbdkit_peer_name = _do_nbdkit_peer_name; + do_nbdkit_set_error = _do_nbdkit_set_error; + do_nbdkit_shutdown = _do_nbdkit_shutdown; do_nbdkit_verror = _do_nbdkit_verror; threadlocal_get_name = _threadlocal_get_name; diff --git a/lib/slow.c b/lib/slow.c new file mode 100644 index 00000000..af9ce3ec --- /dev/null +++ b/lib/slow.c @@ -0,0 +1,78 @@ +/* nbdkit + * Copyright (C) 2013-2019 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 <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> + +#include "nbdkit-plugin.h" + +#include "lib.h" + +/* Slow functions whose implementation depends on server internals are + * simply bounced into the server. + */ + +const char * +nbdkit_export_name (void) +{ + return do_nbdkit_export_name (); +} + +int +nbdkit_nanosleep (unsigned sec, unsigned nsec) +{ + return do_nbdkit_nanosleep (sec, nsec); +} + +int +nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen) +{ + return do_nbdkit_peer_name (addr, addrlen); +} + +void +nbdkit_set_error (int err) +{ + do_nbdkit_set_error (err); +} + +void +nbdkit_shutdown (void) +{ + do_nbdkit_shutdown (); +} diff --git a/lib/libnbdkit.syms b/lib/libnbdkit.syms index 96a6aa68..a8a0cb74 100644 --- a/lib/libnbdkit.syms +++ b/lib/libnbdkit.syms @@ -42,10 +42,12 @@ nbdkit_add_extent; nbdkit_debug; nbdkit_error; + nbdkit_export_name; nbdkit_extents_count; nbdkit_extents_free; nbdkit_extents_new; nbdkit_get_extent; + nbdkit_nanosleep; nbdkit_parse_bool; nbdkit_parse_int16_t; nbdkit_parse_int32_t; @@ -58,8 +60,11 @@ nbdkit_parse_uint64_t; nbdkit_parse_uint8_t; nbdkit_parse_unsigned; + nbdkit_peer_name; nbdkit_read_password; nbdkit_realpath; + nbdkit_set_error; + nbdkit_shutdown; nbdkit_vdebug; nbdkit_verror; -- 2.25.0