python: Make robust against traceback.format_exception returning error.
[nbdkit/ericb.git] / server / internal.h
blob2a7c6eed2caba82101fb82cfb8777202c7b9af9f
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 struct debug_flag {
91 struct debug_flag *next;
92 char *name; /* plugin or filter name */
93 char *flag; /* flag name */
94 int value; /* value of flag */
95 bool used; /* if flag was successfully set */
98 enum log_to {
99 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
100 we forked into the background in which
101 case log to syslog */
102 LOG_TO_STDERR, /* --log=stderr forced on the command line */
103 LOG_TO_SYSLOG, /* --log=syslog forced on the command line */
104 LOG_TO_NULL, /* --log=null forced on the command line */
107 extern struct debug_flag *debug_flags;
108 extern const char *exportname;
109 extern bool foreground;
110 extern const char *ipaddr;
111 extern enum log_to log_to;
112 extern unsigned mask_handshake;
113 extern bool newstyle;
114 extern bool no_sr;
115 extern const char *port;
116 extern bool read_only;
117 extern const char *run;
118 extern bool listen_stdin;
119 extern const char *selinux_label;
120 extern unsigned threads;
121 extern int tls;
122 extern const char *tls_certificates_dir;
123 extern const char *tls_psk;
124 extern bool tls_verify_peer;
125 extern char *unixsocket;
126 extern const char *user, *group;
127 extern bool verbose;
129 extern struct backend *backend;
130 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
132 /* quit.c */
133 extern volatile int quit;
134 extern int quit_fd;
135 extern void set_up_quit_pipe (void);
136 extern void close_quit_pipe (void);
137 extern void handle_quit (int sig);
138 extern void set_quit (void);
140 /* signals.c */
141 extern void set_up_signals (void);
143 /* background.c */
144 extern bool forked_into_background;
145 extern void fork_into_background (void);
147 /* captive.c */
148 extern void run_command (void);
150 /* socket-activation.c */
151 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
152 extern unsigned int get_socket_activation (void);
154 /* usergroup.c */
155 extern void change_user (void);
157 /* connections.c */
158 struct connection;
160 /* Flags for connection_send_function */
161 enum {
162 SEND_MORE = 1, /* Hint to use MSG_MORE/corking to group send()s */
165 typedef int (*connection_recv_function) (struct connection *,
166 void *buf, size_t len)
167 __attribute__((__nonnull__ (1, 2)));
168 typedef int (*connection_send_function) (struct connection *,
169 const void *buf, size_t len,
170 int flags)
171 __attribute__((__nonnull__ (1, 2)));
172 typedef void (*connection_close_function) (struct connection *)
173 __attribute__((__nonnull__ (1)));
175 enum {
176 HANDLE_OPEN = 1, /* Set if .open passed, so .close is needed */
177 HANDLE_CONNECTED = 2, /* Set if .prepare passed, so .finalize is needed */
178 HANDLE_FAILED = 4, /* Set if .finalize failed */
181 struct b_conn_handle {
182 void *handle;
184 unsigned char state; /* Bitmask of HANDLE_* values */
186 uint64_t exportsize;
187 int can_write;
188 int can_flush;
189 int is_rotational;
190 int can_trim;
191 int can_zero;
192 int can_fast_zero;
193 int can_fua;
194 int can_multi_conn;
195 int can_extents;
196 int can_cache;
199 static inline void
200 reset_b_conn_handle (struct b_conn_handle *h)
202 h->handle = NULL;
203 h->state = 0;
204 h->exportsize = -1;
205 h->can_write = -1;
206 h->can_flush = -1;
207 h->is_rotational = -1;
208 h->can_trim = -1;
209 h->can_zero = -1;
210 h->can_fast_zero = -1;
211 h->can_fua = -1;
212 h->can_multi_conn = -1;
213 h->can_extents = -1;
214 h->can_cache = -1;
217 struct connection {
218 pthread_mutex_t request_lock;
219 pthread_mutex_t read_lock;
220 pthread_mutex_t write_lock;
221 pthread_mutex_t status_lock;
222 int status; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
223 int status_pipe[2]; /* track status changes via poll when nworkers > 1 */
224 void *crypto_session;
225 int nworkers;
227 struct b_conn_handle *handles;
228 size_t nr_handles;
230 char exportname[NBD_MAX_STRING + 1];
231 uint32_t exportnamelen;
232 uint32_t cflags;
233 uint16_t eflags;
234 bool using_tls;
235 bool structured_replies;
236 bool meta_context_base_allocation;
238 int sockin, sockout;
239 connection_recv_function recv;
240 connection_send_function send;
241 connection_close_function close;
244 extern void handle_single_connection (int sockin, int sockout);
245 extern int connection_get_status (struct connection *conn)
246 __attribute__((__nonnull__ (1)));
247 extern int connection_set_status (struct connection *conn, int value)
248 __attribute__((__nonnull__ (1)));
250 /* protocol-handshake.c */
251 extern int protocol_handshake (struct connection *conn)
252 __attribute__((__nonnull__ (1)));
253 extern int protocol_common_open (struct connection *conn,
254 uint64_t *exportsize, uint16_t *flags)
255 __attribute__((__nonnull__ (1, 2, 3)));
257 /* protocol-handshake-oldstyle.c */
258 extern int protocol_handshake_oldstyle (struct connection *conn)
259 __attribute__((__nonnull__ (1)));
261 /* protocol-handshake-newstyle.c */
262 extern int protocol_handshake_newstyle (struct connection *conn)
263 __attribute__((__nonnull__ (1)));
265 /* protocol.c */
266 extern int protocol_recv_request_send_reply (struct connection *conn)
267 __attribute__((__nonnull__ (1)));
269 /* The context ID of base:allocation. As far as I can tell it doesn't
270 * matter what this is as long as nbdkit always returns the same
271 * number.
273 #define base_allocation_id 1
275 /* crypto.c */
276 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
277 extern void crypto_init (bool tls_set_on_cli);
278 extern void crypto_free (void);
279 extern int crypto_negotiate_tls (struct connection *conn,
280 int sockin, int sockout)
281 __attribute__((__nonnull__ (1)));
283 /* debug.c */
284 #define debug(fs, ...) \
285 do { \
286 if_verbose \
287 nbdkit_debug ((fs), ##__VA_ARGS__); \
288 } while (0)
290 /* log-*.c */
291 #if !HAVE_VFPRINTF_PERCENT_M
292 #include <stdio.h>
293 #define vfprintf nbdkit_vfprintf
294 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
295 __attribute__((__format__ (printf, 2, 0)));
296 #endif
297 extern void log_stderr_verror (const char *fs, va_list args)
298 __attribute__((__format__ (printf, 1, 0)));
299 extern void log_syslog_verror (const char *fs, va_list args)
300 __attribute__((__format__ (printf, 1, 0)));
302 /* backend.c */
303 struct backend {
304 /* Next filter or plugin in the chain. This is always NULL for
305 * plugins and never NULL for filters.
307 struct backend *next;
309 /* A unique index used to fetch the handle from the connections
310 * object. The plugin (last in the chain) has index 0, and the
311 * filters have index 1, 2, ... depending how "far" they are from
312 * the plugin.
314 size_t i;
316 /* The type of backend: filter or plugin. */
317 const char *type;
319 /* A copy of the backend name that survives a dlclose. */
320 char *name;
322 /* The file the backend was loaded from. */
323 char *filename;
325 /* The dlopen handle for the backend. */
326 void *dl;
328 /* Backend callbacks. All are required. */
329 void (*free) (struct backend *);
330 int (*thread_model) (struct backend *);
331 const char *(*plugin_name) (struct backend *);
332 void (*usage) (struct backend *);
333 const char *(*version) (struct backend *);
334 void (*dump_fields) (struct backend *);
335 void (*config) (struct backend *, const char *key, const char *value);
336 void (*config_complete) (struct backend *);
337 const char *(*magic_config_key) (struct backend *);
338 void *(*open) (struct backend *, struct connection *conn, int readonly);
339 int (*prepare) (struct backend *, struct connection *conn, void *handle,
340 int readonly);
341 int (*finalize) (struct backend *, struct connection *conn, void *handle);
342 void (*close) (struct backend *, struct connection *conn, void *handle);
344 int64_t (*get_size) (struct backend *, struct connection *conn, void *handle);
345 int (*can_write) (struct backend *, struct connection *conn, void *handle);
346 int (*can_flush) (struct backend *, struct connection *conn, void *handle);
347 int (*is_rotational) (struct backend *, struct connection *conn,
348 void *handle);
349 int (*can_trim) (struct backend *, struct connection *conn, void *handle);
350 int (*can_zero) (struct backend *, struct connection *conn, void *handle);
351 int (*can_fast_zero) (struct backend *, struct connection *conn,
352 void *handle);
353 int (*can_extents) (struct backend *, struct connection *conn, void *handle);
354 int (*can_fua) (struct backend *, struct connection *conn, void *handle);
355 int (*can_multi_conn) (struct backend *, struct connection *conn,
356 void *handle);
357 int (*can_cache) (struct backend *, struct connection *conn, void *handle);
359 int (*pread) (struct backend *, struct connection *conn, void *handle,
360 void *buf, uint32_t count, uint64_t offset,
361 uint32_t flags, int *err);
362 int (*pwrite) (struct backend *, struct connection *conn, void *handle,
363 const void *buf, uint32_t count, uint64_t offset,
364 uint32_t flags, int *err);
365 int (*flush) (struct backend *, struct connection *conn, void *handle,
366 uint32_t flags, int *err);
367 int (*trim) (struct backend *, struct connection *conn, void *handle,
368 uint32_t count, uint64_t offset, uint32_t flags, int *err);
369 int (*zero) (struct backend *, struct connection *conn, void *handle,
370 uint32_t count, uint64_t offset, uint32_t flags, int *err);
371 int (*extents) (struct backend *, struct connection *conn, void *handle,
372 uint32_t count, uint64_t offset, uint32_t flags,
373 struct nbdkit_extents *extents, int *err);
374 int (*cache) (struct backend *, struct connection *conn, void *handle,
375 uint32_t count, uint64_t offset, uint32_t flags, int *err);
378 extern void backend_init (struct backend *b, struct backend *next, size_t index,
379 const char *filename, void *dl, const char *type)
380 __attribute__((__nonnull__ (1, 4, 5, 6)));
381 extern void backend_load (struct backend *b, const char *name,
382 void (*load) (void))
383 __attribute__((__nonnull__ (1 /* not 2 */)));
384 extern void backend_unload (struct backend *b, void (*unload) (void))
385 __attribute__((__nonnull__ (1)));
387 extern int backend_open (struct backend *b, struct connection *conn,
388 int readonly)
389 __attribute__((__nonnull__ (1, 2)));
390 extern int backend_prepare (struct backend *b, struct connection *conn)
391 __attribute__((__nonnull__ (1, 2)));
392 extern int backend_finalize (struct backend *b, struct connection *conn)
393 __attribute__((__nonnull__ (1, 2)));
394 extern void backend_close (struct backend *b, struct connection *conn)
395 __attribute__((__nonnull__ (1, 2)));
396 extern bool backend_valid_range (struct backend *b, struct connection *conn,
397 uint64_t offset, uint32_t count)
398 __attribute__((__nonnull__ (1, 2)));
400 extern int backend_reopen (struct backend *b, struct connection *conn,
401 int readonly)
402 __attribute__((__nonnull__ (1, 2)));
403 extern int64_t backend_get_size (struct backend *b, struct connection *conn)
404 __attribute__((__nonnull__ (1, 2)));
405 extern int backend_can_write (struct backend *b, struct connection *conn)
406 __attribute__((__nonnull__ (1, 2)));
407 extern int backend_can_flush (struct backend *b, struct connection *conn)
408 __attribute__((__nonnull__ (1, 2)));
409 extern int backend_is_rotational (struct backend *b, struct connection *conn)
410 __attribute__((__nonnull__ (1, 2)));
411 extern int backend_can_trim (struct backend *b, struct connection *conn)
412 __attribute__((__nonnull__ (1, 2)));
413 extern int backend_can_zero (struct backend *b, struct connection *conn)
414 __attribute__((__nonnull__ (1, 2)));
415 extern int backend_can_fast_zero (struct backend *b, struct connection *conn)
416 __attribute__((__nonnull__ (1, 2)));
417 extern int backend_can_extents (struct backend *b, struct connection *conn)
418 __attribute__((__nonnull__ (1, 2)));
419 extern int backend_can_fua (struct backend *b, struct connection *conn)
420 __attribute__((__nonnull__ (1, 2)));
421 extern int backend_can_multi_conn (struct backend *b, struct connection *conn)
422 __attribute__((__nonnull__ (1, 2)));
423 extern int backend_can_cache (struct backend *b, struct connection *conn)
424 __attribute__((__nonnull__ (1, 2)));
426 extern int backend_pread (struct backend *b, struct connection *conn,
427 void *buf, uint32_t count, uint64_t offset,
428 uint32_t flags, int *err)
429 __attribute__((__nonnull__ (1, 2, 3, 7)));
430 extern int backend_pwrite (struct backend *b, struct connection *conn,
431 const void *buf, uint32_t count, uint64_t offset,
432 uint32_t flags, int *err)
433 __attribute__((__nonnull__ (1, 2, 3, 7)));
434 extern int backend_flush (struct backend *b, struct connection *conn,
435 uint32_t flags, int *err)
436 __attribute__((__nonnull__ (1, 2, 4)));
437 extern int backend_trim (struct backend *b, struct connection *conn,
438 uint32_t count, uint64_t offset, uint32_t flags,
439 int *err)
440 __attribute__((__nonnull__ (1, 2, 6)));
441 extern int backend_zero (struct backend *b, struct connection *conn,
442 uint32_t count, uint64_t offset, uint32_t flags,
443 int *err)
444 __attribute__((__nonnull__ (1, 2, 6)));
445 extern int backend_extents (struct backend *b, struct connection *conn,
446 uint32_t count, uint64_t offset, uint32_t flags,
447 struct nbdkit_extents *extents, int *err)
448 __attribute__((__nonnull__ (1, 2, 6, 7)));
449 extern int backend_cache (struct backend *b, struct connection *conn,
450 uint32_t count, uint64_t offset,
451 uint32_t flags, int *err)
452 __attribute__((__nonnull__ (1, 2, 6)));
454 /* plugins.c */
455 extern struct backend *plugin_register (size_t index, const char *filename,
456 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
457 __attribute__((__nonnull__ (2, 3, 4)));
459 /* filters.c */
460 extern struct backend *filter_register (struct backend *next, size_t index,
461 const char *filename, void *dl,
462 struct nbdkit_filter *(*filter_init) (void))
463 __attribute__((__nonnull__ (1, 3, 4, 5)));
465 /* locks.c */
466 extern void lock_init_thread_model (void);
467 extern const char *name_of_thread_model (int model);
468 extern void lock_connection (void);
469 extern void unlock_connection (void);
470 extern void lock_request (struct connection *conn);
471 extern void unlock_request (struct connection *conn);
472 extern void lock_unload (void);
473 extern void unlock_unload (void);
475 /* sockets.c */
476 extern int *bind_unix_socket (size_t *)
477 __attribute__((__nonnull__ (1)));
478 extern int *bind_tcpip_socket (size_t *)
479 __attribute__((__nonnull__ (1)));
480 extern int *bind_vsock (size_t *)
481 __attribute__((__nonnull__ (1)));
482 extern void accept_incoming_connections (int *socks, size_t nr_socks)
483 __attribute__((__nonnull__ (1)));
485 /* threadlocal.c */
486 extern void threadlocal_init (void);
487 extern void threadlocal_new_server_thread (void);
488 extern void threadlocal_set_name (const char *name)
489 __attribute__((__nonnull__ (1)));
490 extern const char *threadlocal_get_name (void);
491 extern void threadlocal_set_instance_num (size_t instance_num);
492 extern size_t threadlocal_get_instance_num (void);
493 extern void threadlocal_set_error (int err);
494 extern int threadlocal_get_error (void);
495 extern void *threadlocal_buffer (size_t size);
496 extern void threadlocal_set_conn (struct connection *conn);
497 extern struct connection *threadlocal_get_conn (void);
499 /* Declare program_name. */
500 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
501 #include <errno.h>
502 #define program_name program_invocation_short_name
503 #else
504 #define program_name "nbdkit"
505 #endif
507 #endif /* NBDKIT_INTERNAL_H */