docs, tests: Improved method to probe for plugins and filters.
[nbdkit/ericb.git] / server / internal.h
bloba1fa73099cd4a191ae6c31bdfb9754c7ddd27be1
1 /* nbdkit
2 * Copyright (C) 2013-2019 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 #ifndef NBDKIT_INTERNAL_H
34 #define NBDKIT_INTERNAL_H
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdarg.h>
39 #include <sys/socket.h>
40 #include <pthread.h>
42 #define NBDKIT_API_VERSION 2
43 #include "nbdkit-plugin.h"
44 #include "nbdkit-filter.h"
45 #include "cleanup.h"
46 #include "nbd-protocol.h"
48 /* Define unlikely macro, but only for GCC. These are used to move
49 * debug and error handling code out of hot paths.
51 #if defined(__GNUC__)
52 #define unlikely(x) __builtin_expect (!!(x), 0)
53 #define if_verbose if (unlikely (verbose))
54 #else
55 #define unlikely(x) (x)
56 #define if_verbose if (verbose)
57 #endif
59 #ifdef __APPLE__
60 #define UNIX_PATH_MAX 104
61 #else
62 #define UNIX_PATH_MAX 108
63 #endif
65 #if HAVE_VALGRIND
66 # include <valgrind.h>
67 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
68 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
69 #elif defined(__SANITIZE_ADDRESS__)
70 # define DO_DLCLOSE 0
71 #elif ENABLE_LIBFUZZER
72 /* XXX This causes dlopen in the server to leak during fuzzing.
73 * However it is necessary because of
74 * https://bugs.llvm.org/show_bug.cgi?id=43917
76 # define DO_DLCLOSE 0
77 #else
78 # define DO_DLCLOSE 1
79 #endif
81 #define container_of(ptr, type, member) ({ \
82 const typeof (((type *) 0)->member) *__mptr = (ptr); \
83 (type *) ((char *) __mptr - offsetof(type, member)); \
86 /* Maximum read or write request that we will handle. */
87 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
89 /* main.c */
90 enum log_to {
91 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
92 we forked into the background in which
93 case log to syslog */
94 LOG_TO_STDERR, /* --log=stderr forced on the command line */
95 LOG_TO_SYSLOG, /* --log=syslog forced on the command line */
96 LOG_TO_NULL, /* --log=null forced on the command line */
99 extern struct debug_flag *debug_flags;
100 extern const char *exportname;
101 extern bool foreground;
102 extern const char *ipaddr;
103 extern enum log_to log_to;
104 extern unsigned mask_handshake;
105 extern bool newstyle;
106 extern bool no_sr;
107 extern const char *port;
108 extern bool read_only;
109 extern const char *run;
110 extern bool listen_stdin;
111 extern const char *selinux_label;
112 extern unsigned threads;
113 extern int tls;
114 extern const char *tls_certificates_dir;
115 extern const char *tls_psk;
116 extern bool tls_verify_peer;
117 extern char *unixsocket;
118 extern const char *user, *group;
119 extern bool verbose;
121 extern struct backend *backend;
122 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
124 /* quit.c */
125 extern volatile int quit;
126 extern int quit_fd;
127 extern void set_up_quit_pipe (void);
128 extern void close_quit_pipe (void);
129 extern void handle_quit (int sig);
130 extern void set_quit (void);
132 /* signals.c */
133 extern void set_up_signals (void);
135 /* background.c */
136 extern bool forked_into_background;
137 extern void fork_into_background (void);
139 /* captive.c */
140 extern void run_command (void);
142 /* socket-activation.c */
143 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
144 extern unsigned int get_socket_activation (void);
146 /* usergroup.c */
147 extern void change_user (void);
149 /* connections.c */
150 struct connection;
152 /* Flags for connection_send_function */
153 enum {
154 SEND_MORE = 1, /* Hint to use MSG_MORE/corking to group send()s */
157 typedef int (*connection_recv_function) (struct connection *,
158 void *buf, size_t len)
159 __attribute__((__nonnull__ (1, 2)));
160 typedef int (*connection_send_function) (struct connection *,
161 const void *buf, size_t len,
162 int flags)
163 __attribute__((__nonnull__ (1, 2)));
164 typedef void (*connection_close_function) (struct connection *)
165 __attribute__((__nonnull__ (1)));
167 enum {
168 HANDLE_OPEN = 1, /* Set if .open passed, so .close is needed */
169 HANDLE_CONNECTED = 2, /* Set if .prepare passed, so .finalize is needed */
170 HANDLE_FAILED = 4, /* Set if .finalize failed */
173 struct b_conn_handle {
174 void *handle;
176 unsigned char state; /* Bitmask of HANDLE_* values */
178 uint64_t exportsize;
179 int can_write;
180 int can_flush;
181 int is_rotational;
182 int can_trim;
183 int can_zero;
184 int can_fast_zero;
185 int can_fua;
186 int can_multi_conn;
187 int can_extents;
188 int can_cache;
191 static inline void
192 reset_b_conn_handle (struct b_conn_handle *h)
194 h->handle = NULL;
195 h->state = 0;
196 h->exportsize = -1;
197 h->can_write = -1;
198 h->can_flush = -1;
199 h->is_rotational = -1;
200 h->can_trim = -1;
201 h->can_zero = -1;
202 h->can_fast_zero = -1;
203 h->can_fua = -1;
204 h->can_multi_conn = -1;
205 h->can_extents = -1;
206 h->can_cache = -1;
209 struct connection {
210 pthread_mutex_t request_lock;
211 pthread_mutex_t read_lock;
212 pthread_mutex_t write_lock;
213 pthread_mutex_t status_lock;
214 int status; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
215 int status_pipe[2]; /* track status changes via poll when nworkers > 1 */
216 void *crypto_session;
217 int nworkers;
219 struct b_conn_handle *handles;
220 size_t nr_handles;
222 char exportname[NBD_MAX_STRING + 1];
223 uint32_t exportnamelen;
224 uint32_t cflags;
225 uint16_t eflags;
226 bool using_tls;
227 bool structured_replies;
228 bool meta_context_base_allocation;
230 int sockin, sockout;
231 connection_recv_function recv;
232 connection_send_function send;
233 connection_close_function close;
236 extern void handle_single_connection (int sockin, int sockout);
237 extern int connection_get_status (struct connection *conn)
238 __attribute__((__nonnull__ (1)));
239 extern int connection_set_status (struct connection *conn, int value)
240 __attribute__((__nonnull__ (1)));
242 /* protocol-handshake.c */
243 extern int protocol_handshake (struct connection *conn)
244 __attribute__((__nonnull__ (1)));
245 extern int protocol_common_open (struct connection *conn,
246 uint64_t *exportsize, uint16_t *flags)
247 __attribute__((__nonnull__ (1, 2, 3)));
249 /* protocol-handshake-oldstyle.c */
250 extern int protocol_handshake_oldstyle (struct connection *conn)
251 __attribute__((__nonnull__ (1)));
253 /* protocol-handshake-newstyle.c */
254 extern int protocol_handshake_newstyle (struct connection *conn)
255 __attribute__((__nonnull__ (1)));
257 /* protocol.c */
258 extern int protocol_recv_request_send_reply (struct connection *conn)
259 __attribute__((__nonnull__ (1)));
261 /* The context ID of base:allocation. As far as I can tell it doesn't
262 * matter what this is as long as nbdkit always returns the same
263 * number.
265 #define base_allocation_id 1
267 /* crypto.c */
268 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
269 extern void crypto_init (bool tls_set_on_cli);
270 extern void crypto_free (void);
271 extern int crypto_negotiate_tls (struct connection *conn,
272 int sockin, int sockout)
273 __attribute__((__nonnull__ (1)));
275 /* debug.c */
276 #define debug(fs, ...) \
277 do { \
278 if_verbose \
279 nbdkit_debug ((fs), ##__VA_ARGS__); \
280 } while (0)
282 /* debug-flags.c */
283 extern void add_debug_flag (const char *arg);
284 extern void apply_debug_flags (void *dl, const char *name);
285 extern void free_debug_flags (void);
287 /* log-*.c */
288 #if !HAVE_VFPRINTF_PERCENT_M
289 #include <stdio.h>
290 #define vfprintf nbdkit_vfprintf
291 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
292 __attribute__((__format__ (printf, 2, 0)));
293 #endif
294 extern void log_stderr_verror (const char *fs, va_list args)
295 __attribute__((__format__ (printf, 1, 0)));
296 extern void log_syslog_verror (const char *fs, va_list args)
297 __attribute__((__format__ (printf, 1, 0)));
299 /* backend.c */
300 struct backend {
301 /* Next filter or plugin in the chain. This is always NULL for
302 * plugins and never NULL for filters.
304 struct backend *next;
306 /* A unique index used to fetch the handle from the connections
307 * object. The plugin (last in the chain) has index 0, and the
308 * filters have index 1, 2, ... depending how "far" they are from
309 * the plugin.
311 size_t i;
313 /* The type of backend: filter or plugin. */
314 const char *type;
316 /* A copy of the backend name that survives a dlclose. */
317 char *name;
319 /* The file the backend was loaded from. */
320 char *filename;
322 /* The dlopen handle for the backend. */
323 void *dl;
325 /* Backend callbacks. All are required. */
326 void (*free) (struct backend *);
327 int (*thread_model) (struct backend *);
328 const char *(*plugin_name) (struct backend *);
329 void (*usage) (struct backend *);
330 const char *(*version) (struct backend *);
331 void (*dump_fields) (struct backend *);
332 void (*config) (struct backend *, const char *key, const char *value);
333 void (*config_complete) (struct backend *);
334 const char *(*magic_config_key) (struct backend *);
335 int (*preconnect) (struct backend *, struct connection *conn, int readonly);
336 void *(*open) (struct backend *, struct connection *conn, int readonly);
337 int (*prepare) (struct backend *, struct connection *conn, void *handle,
338 int readonly);
339 int (*finalize) (struct backend *, struct connection *conn, void *handle);
340 void (*close) (struct backend *, struct connection *conn, void *handle);
342 int64_t (*get_size) (struct backend *, struct connection *conn, void *handle);
343 int (*can_write) (struct backend *, struct connection *conn, void *handle);
344 int (*can_flush) (struct backend *, struct connection *conn, void *handle);
345 int (*is_rotational) (struct backend *, struct connection *conn,
346 void *handle);
347 int (*can_trim) (struct backend *, struct connection *conn, void *handle);
348 int (*can_zero) (struct backend *, struct connection *conn, void *handle);
349 int (*can_fast_zero) (struct backend *, struct connection *conn,
350 void *handle);
351 int (*can_extents) (struct backend *, struct connection *conn, void *handle);
352 int (*can_fua) (struct backend *, struct connection *conn, void *handle);
353 int (*can_multi_conn) (struct backend *, struct connection *conn,
354 void *handle);
355 int (*can_cache) (struct backend *, struct connection *conn, void *handle);
357 int (*pread) (struct backend *, struct connection *conn, void *handle,
358 void *buf, uint32_t count, uint64_t offset,
359 uint32_t flags, int *err);
360 int (*pwrite) (struct backend *, struct connection *conn, void *handle,
361 const void *buf, uint32_t count, uint64_t offset,
362 uint32_t flags, int *err);
363 int (*flush) (struct backend *, struct connection *conn, void *handle,
364 uint32_t flags, int *err);
365 int (*trim) (struct backend *, struct connection *conn, void *handle,
366 uint32_t count, uint64_t offset, uint32_t flags, int *err);
367 int (*zero) (struct backend *, struct connection *conn, void *handle,
368 uint32_t count, uint64_t offset, uint32_t flags, int *err);
369 int (*extents) (struct backend *, struct connection *conn, void *handle,
370 uint32_t count, uint64_t offset, uint32_t flags,
371 struct nbdkit_extents *extents, int *err);
372 int (*cache) (struct backend *, struct connection *conn, void *handle,
373 uint32_t count, uint64_t offset, uint32_t flags, int *err);
376 extern void backend_init (struct backend *b, struct backend *next, size_t index,
377 const char *filename, void *dl, const char *type)
378 __attribute__((__nonnull__ (1, 4, 5, 6)));
379 extern void backend_load (struct backend *b, const char *name,
380 void (*load) (void))
381 __attribute__((__nonnull__ (1 /* not 2 */)));
382 extern void backend_unload (struct backend *b, void (*unload) (void))
383 __attribute__((__nonnull__ (1)));
385 extern int backend_open (struct backend *b, struct connection *conn,
386 int readonly)
387 __attribute__((__nonnull__ (1, 2)));
388 extern int backend_prepare (struct backend *b, struct connection *conn)
389 __attribute__((__nonnull__ (1, 2)));
390 extern int backend_finalize (struct backend *b, struct connection *conn)
391 __attribute__((__nonnull__ (1, 2)));
392 extern void backend_close (struct backend *b, struct connection *conn)
393 __attribute__((__nonnull__ (1, 2)));
394 extern bool backend_valid_range (struct backend *b, struct connection *conn,
395 uint64_t offset, uint32_t count)
396 __attribute__((__nonnull__ (1, 2)));
398 extern int backend_reopen (struct backend *b, struct connection *conn,
399 int readonly)
400 __attribute__((__nonnull__ (1, 2)));
401 extern int64_t backend_get_size (struct backend *b, struct connection *conn)
402 __attribute__((__nonnull__ (1, 2)));
403 extern int backend_can_write (struct backend *b, struct connection *conn)
404 __attribute__((__nonnull__ (1, 2)));
405 extern int backend_can_flush (struct backend *b, struct connection *conn)
406 __attribute__((__nonnull__ (1, 2)));
407 extern int backend_is_rotational (struct backend *b, struct connection *conn)
408 __attribute__((__nonnull__ (1, 2)));
409 extern int backend_can_trim (struct backend *b, struct connection *conn)
410 __attribute__((__nonnull__ (1, 2)));
411 extern int backend_can_zero (struct backend *b, struct connection *conn)
412 __attribute__((__nonnull__ (1, 2)));
413 extern int backend_can_fast_zero (struct backend *b, struct connection *conn)
414 __attribute__((__nonnull__ (1, 2)));
415 extern int backend_can_extents (struct backend *b, struct connection *conn)
416 __attribute__((__nonnull__ (1, 2)));
417 extern int backend_can_fua (struct backend *b, struct connection *conn)
418 __attribute__((__nonnull__ (1, 2)));
419 extern int backend_can_multi_conn (struct backend *b, struct connection *conn)
420 __attribute__((__nonnull__ (1, 2)));
421 extern int backend_can_cache (struct backend *b, struct connection *conn)
422 __attribute__((__nonnull__ (1, 2)));
424 extern int backend_pread (struct backend *b, struct connection *conn,
425 void *buf, uint32_t count, uint64_t offset,
426 uint32_t flags, int *err)
427 __attribute__((__nonnull__ (1, 2, 3, 7)));
428 extern int backend_pwrite (struct backend *b, struct connection *conn,
429 const void *buf, uint32_t count, uint64_t offset,
430 uint32_t flags, int *err)
431 __attribute__((__nonnull__ (1, 2, 3, 7)));
432 extern int backend_flush (struct backend *b, struct connection *conn,
433 uint32_t flags, int *err)
434 __attribute__((__nonnull__ (1, 2, 4)));
435 extern int backend_trim (struct backend *b, struct connection *conn,
436 uint32_t count, uint64_t offset, uint32_t flags,
437 int *err)
438 __attribute__((__nonnull__ (1, 2, 6)));
439 extern int backend_zero (struct backend *b, struct connection *conn,
440 uint32_t count, uint64_t offset, uint32_t flags,
441 int *err)
442 __attribute__((__nonnull__ (1, 2, 6)));
443 extern int backend_extents (struct backend *b, struct connection *conn,
444 uint32_t count, uint64_t offset, uint32_t flags,
445 struct nbdkit_extents *extents, int *err)
446 __attribute__((__nonnull__ (1, 2, 6, 7)));
447 extern int backend_cache (struct backend *b, struct connection *conn,
448 uint32_t count, uint64_t offset,
449 uint32_t flags, int *err)
450 __attribute__((__nonnull__ (1, 2, 6)));
452 /* plugins.c */
453 extern struct backend *plugin_register (size_t index, const char *filename,
454 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
455 __attribute__((__nonnull__ (2, 3, 4)));
457 /* filters.c */
458 extern struct backend *filter_register (struct backend *next, size_t index,
459 const char *filename, void *dl,
460 struct nbdkit_filter *(*filter_init) (void))
461 __attribute__((__nonnull__ (1, 3, 4, 5)));
463 /* locks.c */
464 extern void lock_init_thread_model (void);
465 extern const char *name_of_thread_model (int model);
466 extern void lock_connection (void);
467 extern void unlock_connection (void);
468 extern void lock_request (struct connection *conn);
469 extern void unlock_request (struct connection *conn);
470 extern void lock_unload (void);
471 extern void unlock_unload (void);
473 /* sockets.c */
474 extern int *bind_unix_socket (size_t *)
475 __attribute__((__nonnull__ (1)));
476 extern int *bind_tcpip_socket (size_t *)
477 __attribute__((__nonnull__ (1)));
478 extern int *bind_vsock (size_t *)
479 __attribute__((__nonnull__ (1)));
480 extern void accept_incoming_connections (int *socks, size_t nr_socks)
481 __attribute__((__nonnull__ (1)));
483 /* threadlocal.c */
484 extern void threadlocal_init (void);
485 extern void threadlocal_new_server_thread (void);
486 extern void threadlocal_set_name (const char *name)
487 __attribute__((__nonnull__ (1)));
488 extern const char *threadlocal_get_name (void);
489 extern void threadlocal_set_instance_num (size_t instance_num);
490 extern size_t threadlocal_get_instance_num (void);
491 extern void threadlocal_set_error (int err);
492 extern int threadlocal_get_error (void);
493 extern void *threadlocal_buffer (size_t size);
494 extern void threadlocal_set_conn (struct connection *conn);
495 extern struct connection *threadlocal_get_conn (void);
497 /* Declare program_name. */
498 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
499 #include <errno.h>
500 #define program_name program_invocation_short_name
501 #else
502 #define program_name "nbdkit"
503 #endif
505 #endif /* NBDKIT_INTERNAL_H */