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
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
33 #ifndef NBDKIT_INTERNAL_H
34 #define NBDKIT_INTERNAL_H
39 #include <sys/socket.h>
42 #define NBDKIT_API_VERSION 2
43 #include "nbdkit-plugin.h"
44 #include "nbdkit-filter.h"
48 #define UNIX_PATH_MAX 104
50 #define UNIX_PATH_MAX 108
54 # include <valgrind.h>
55 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
56 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
57 #elif defined(__SANITIZE_ADDRESS__)
63 #define container_of(ptr, type, member) ({ \
64 const typeof (((type *) 0)->member) *__mptr = (ptr); \
65 (type *) ((char *) __mptr - offsetof(type, member)); \
68 /* Maximum read or write request that we will handle. */
69 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
73 struct debug_flag
*next
;
74 char *name
; /* plugin or filter name */
75 char *flag
; /* flag name */
76 int value
; /* value of flag */
77 bool used
; /* if flag was successfully set */
81 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
82 we forked into the background in which
84 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
85 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
88 extern struct debug_flag
*debug_flags
;
89 extern const char *exportname
;
90 extern bool foreground
;
91 extern const char *ipaddr
;
92 extern enum log_to log_to
;
95 extern const char *port
;
97 extern const char *run
;
98 extern bool listen_stdin
;
99 extern const char *selinux_label
;
102 extern const char *tls_certificates_dir
;
103 extern const char *tls_psk
;
104 extern bool tls_verify_peer
;
105 extern char *unixsocket
;
106 extern const char *user
, *group
;
109 extern struct backend
*backend
;
110 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
113 extern volatile int quit
;
115 extern void set_up_quit_pipe (void);
116 extern void handle_quit (int sig
);
117 extern void set_quit (void);
120 extern void set_up_signals (void);
123 extern bool forked_into_background
;
124 extern void fork_into_background (void);
127 extern void run_command (void);
129 /* socket-activation.c */
130 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
131 extern unsigned int get_socket_activation (void);
134 extern void change_user (void);
139 /* Flags for connection_send_function */
141 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
144 typedef int (*connection_recv_function
) (struct connection
*,
145 void *buf
, size_t len
)
146 __attribute__((__nonnull__ (1, 2)));
147 typedef int (*connection_send_function
) (struct connection
*,
148 const void *buf
, size_t len
,
150 __attribute__((__nonnull__ (1, 2)));
151 typedef void (*connection_close_function
) (struct connection
*)
152 __attribute__((__nonnull__ (1)));
154 struct b_conn_handle
{
162 pthread_mutex_t request_lock
;
163 pthread_mutex_t read_lock
;
164 pthread_mutex_t write_lock
;
165 pthread_mutex_t status_lock
;
166 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
167 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
168 void *crypto_session
;
171 struct b_conn_handle
*handles
;
186 bool structured_replies
;
187 bool meta_context_base_allocation
;
190 connection_recv_function recv
;
191 connection_send_function send
;
192 connection_close_function close
;
195 extern int handle_single_connection (int sockin
, int sockout
);
196 extern void *connection_get_handle (struct connection
*conn
, size_t i
)
197 __attribute__((__nonnull__ (1)));
198 extern int connection_get_status (struct connection
*conn
)
199 __attribute__((__nonnull__ (1)));
200 extern int connection_set_status (struct connection
*conn
, int value
)
201 __attribute__((__nonnull__ (1)));
203 /* protocol-handshake.c */
204 extern int protocol_handshake (struct connection
*conn
)
205 __attribute__((__nonnull__ (1)));
206 extern int protocol_compute_eflags (struct connection
*conn
, uint16_t *flags
)
207 __attribute__((__nonnull__ (1, 2)));
209 /* protocol-handshake-oldstyle.c */
210 extern int protocol_handshake_oldstyle (struct connection
*conn
)
211 __attribute__((__nonnull__ (1)));
213 /* protocol-handshake-newstyle.c */
214 extern int protocol_handshake_newstyle (struct connection
*conn
)
215 __attribute__((__nonnull__ (1)));
218 extern int protocol_recv_request_send_reply (struct connection
*conn
)
219 __attribute__((__nonnull__ (1)));
221 /* The context ID of base:allocation. As far as I can tell it doesn't
222 * matter what this is as long as nbdkit always returns the same
225 #define base_allocation_id 1
228 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
229 extern void crypto_init (bool tls_set_on_cli
);
230 extern void crypto_free (void);
231 extern int crypto_negotiate_tls (struct connection
*conn
,
232 int sockin
, int sockout
)
233 __attribute__((__nonnull__ (1)));
236 #define debug nbdkit_debug
239 #if !HAVE_VFPRINTF_PERCENT_M
241 #define vfprintf nbdkit_vfprintf
242 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
243 __attribute__((__format__ (printf
, 2, 0)));
245 extern void log_stderr_verror (const char *fs
, va_list args
)
246 __attribute__((__format__ (printf
, 1, 0)));
247 extern void log_syslog_verror (const char *fs
, va_list args
)
248 __attribute__((__format__ (printf
, 1, 0)));
252 /* Next filter or plugin in the chain. This is always NULL for
253 * plugins and never NULL for filters.
255 struct backend
*next
;
257 /* A unique index used to fetch the handle from the connections
258 * object. The plugin (last in the chain) has index 0, and the
259 * filters have index 1, 2, ... depending how "far" they are from
264 /* The type of backend: filter or plugin. */
267 /* A copy of the backend name that survives a dlclose. */
270 /* The file the backend was loaded from. */
273 /* The dlopen handle for the backend. */
276 /* Backend callbacks. All are required. */
277 void (*free
) (struct backend
*);
278 int (*thread_model
) (struct backend
*);
279 const char *(*plugin_name
) (struct backend
*);
280 void (*usage
) (struct backend
*);
281 const char *(*version
) (struct backend
*);
282 void (*dump_fields
) (struct backend
*);
283 void (*config
) (struct backend
*, const char *key
, const char *value
);
284 void (*config_complete
) (struct backend
*);
285 const char *(*magic_config_key
) (struct backend
*);
286 int (*open
) (struct backend
*, struct connection
*conn
, int readonly
);
287 int (*prepare
) (struct backend
*, struct connection
*conn
);
288 int (*finalize
) (struct backend
*, struct connection
*conn
);
289 void (*close
) (struct backend
*, struct connection
*conn
);
291 int64_t (*get_size
) (struct backend
*, struct connection
*conn
);
292 int (*can_write
) (struct backend
*, struct connection
*conn
);
293 int (*can_flush
) (struct backend
*, struct connection
*conn
);
294 int (*is_rotational
) (struct backend
*, struct connection
*conn
);
295 int (*can_trim
) (struct backend
*, struct connection
*conn
);
296 int (*can_zero
) (struct backend
*, struct connection
*conn
);
297 int (*can_extents
) (struct backend
*, struct connection
*conn
);
298 int (*can_fua
) (struct backend
*, struct connection
*conn
);
299 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
);
300 int (*can_cache
) (struct backend
*, struct connection
*conn
);
302 int (*pread
) (struct backend
*, struct connection
*conn
, void *buf
,
303 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
304 int (*pwrite
) (struct backend
*, struct connection
*conn
, const void *buf
,
305 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
306 int (*flush
) (struct backend
*, struct connection
*conn
, uint32_t flags
,
308 int (*trim
) (struct backend
*, struct connection
*conn
, uint32_t count
,
309 uint64_t offset
, uint32_t flags
, int *err
);
310 int (*zero
) (struct backend
*, struct connection
*conn
, uint32_t count
,
311 uint64_t offset
, uint32_t flags
, int *err
);
312 int (*extents
) (struct backend
*, struct connection
*conn
, uint32_t count
,
313 uint64_t offset
, uint32_t flags
,
314 struct nbdkit_extents
*extents
, int *err
);
315 int (*cache
) (struct backend
*, struct connection
*conn
, uint32_t count
,
316 uint64_t offset
, uint32_t flags
, int *err
);
319 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
320 const char *filename
, void *dl
, const char *type
)
321 __attribute__((__nonnull__ (1, 4, 5, 6)));
322 extern void backend_load (struct backend
*b
, const char *name
,
324 __attribute__((__nonnull__ (1 /* not 2 */)));
325 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
326 __attribute__((__nonnull__ (1)));
328 extern int backend_open (struct backend
*b
, struct connection
*conn
,
330 __attribute__((__nonnull__ (1, 2)));
331 extern void backend_set_handle (struct backend
*b
, struct connection
*conn
,
333 __attribute__((__nonnull__ (1, 2 /* not 3 */)));
335 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
336 __attribute__((__nonnull__ (1, 2)));
337 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
338 __attribute__((__nonnull__ (1, 2)));
339 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
340 __attribute__((__nonnull__ (1, 2)));
341 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
342 __attribute__((__nonnull__ (1, 2)));
343 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
344 __attribute__((__nonnull__ (1, 2)));
345 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
346 __attribute__((__nonnull__ (1, 2)));
347 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
348 __attribute__((__nonnull__ (1, 2)));
349 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
350 __attribute__((__nonnull__ (1, 2)));
351 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
352 __attribute__((__nonnull__ (1, 2)));
353 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
354 __attribute__((__nonnull__ (1, 2)));
356 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
357 void *buf
, uint32_t count
, uint64_t offset
,
358 uint32_t flags
, int *err
)
359 __attribute__((__nonnull__ (1, 2, 3, 7)));
360 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
361 const void *buf
, uint32_t count
, uint64_t offset
,
362 uint32_t flags
, int *err
)
363 __attribute__((__nonnull__ (1, 2, 3, 7)));
364 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
365 uint32_t flags
, int *err
)
366 __attribute__((__nonnull__ (1, 2, 4)));
367 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
368 uint32_t count
, uint64_t offset
, uint32_t flags
,
370 __attribute__((__nonnull__ (1, 2, 6)));
371 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
372 uint32_t count
, uint64_t offset
, uint32_t flags
,
374 __attribute__((__nonnull__ (1, 2, 6)));
375 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
376 uint32_t count
, uint64_t offset
, uint32_t flags
,
377 struct nbdkit_extents
*extents
, int *err
)
378 __attribute__((__nonnull__ (1, 2, 6, 7)));
379 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
380 uint32_t count
, uint64_t offset
,
381 uint32_t flags
, int *err
)
382 __attribute__((__nonnull__ (1, 2, 6)));
385 extern struct backend
*plugin_register (size_t index
, const char *filename
,
386 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
387 __attribute__((__nonnull__ (2, 3, 4)));
390 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
391 const char *filename
, void *dl
,
392 struct nbdkit_filter
*(*filter_init
) (void))
393 __attribute__((__nonnull__ (1, 3, 4, 5)));
396 extern void lock_init_thread_model (void);
397 extern const char *name_of_thread_model (int model
);
398 extern void lock_connection (void);
399 extern void unlock_connection (void);
400 extern void lock_request (struct connection
*conn
);
401 extern void unlock_request (struct connection
*conn
);
402 extern void lock_unload (void);
403 extern void unlock_unload (void);
406 extern int *bind_unix_socket (size_t *)
407 __attribute__((__nonnull__ (1)));
408 extern int *bind_tcpip_socket (size_t *)
409 __attribute__((__nonnull__ (1)));
410 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
411 __attribute__((__nonnull__ (1)));
412 extern void free_listening_sockets (int *socks
, size_t nr_socks
)
413 __attribute__((__nonnull__ (1)));
416 extern void threadlocal_init (void);
417 extern void threadlocal_new_server_thread (void);
418 extern void threadlocal_set_name (const char *name
)
419 __attribute__((__nonnull__ (1)));
420 extern const char *threadlocal_get_name (void);
421 extern void threadlocal_set_instance_num (size_t instance_num
);
422 extern size_t threadlocal_get_instance_num (void);
423 extern void threadlocal_set_sockaddr (const struct sockaddr
*addr
,
425 __attribute__((__nonnull__ (1)));
426 /*extern void threadlocal_get_sockaddr ();*/
427 extern void threadlocal_set_error (int err
);
428 extern int threadlocal_get_error (void);
429 extern void *threadlocal_buffer (size_t size
);
430 extern void threadlocal_set_conn (struct connection
*conn
);
431 extern struct connection
*threadlocal_get_conn (void);
433 /* Declare program_name. */
434 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
436 #define program_name program_invocation_short_name
438 #define program_name "nbdkit"
441 #endif /* NBDKIT_INTERNAL_H */