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)); \
70 struct debug_flag
*next
;
71 char *name
; /* plugin or filter name */
72 char *flag
; /* flag name */
73 int value
; /* value of flag */
74 bool used
; /* if flag was successfully set */
78 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
79 we forked into the background in which
81 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
82 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
85 extern struct debug_flag
*debug_flags
;
86 extern const char *exportname
;
87 extern bool foreground
;
88 extern const char *ipaddr
;
89 extern enum log_to log_to
;
92 extern const char *port
;
94 extern const char *run
;
95 extern const char *selinux_label
;
98 extern const char *tls_certificates_dir
;
99 extern const char *tls_psk
;
100 extern bool tls_verify_peer
;
101 extern char *unixsocket
;
102 extern const char *user
, *group
;
105 extern struct backend
*backend
;
106 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
109 extern volatile int quit
;
111 extern void set_up_quit_pipe (void);
112 extern void handle_quit (int sig
);
113 extern void set_quit (void);
116 extern void set_up_signals (void);
119 extern bool forked_into_background
;
120 extern void fork_into_background (void);
123 extern void run_command (void);
125 /* socket-activation.c */
126 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
127 extern unsigned int get_socket_activation (void);
130 extern void change_user (void);
135 /* Flags for connection_send_function */
137 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
140 typedef int (*connection_recv_function
) (struct connection
*,
141 void *buf
, size_t len
)
142 __attribute__((__nonnull__ (1, 2)));
143 typedef int (*connection_send_function
) (struct connection
*,
144 const void *buf
, size_t len
,
146 __attribute__((__nonnull__ (1, 2)));
147 typedef void (*connection_close_function
) (struct connection
*)
148 __attribute__((__nonnull__ (1)));
151 pthread_mutex_t request_lock
;
152 pthread_mutex_t read_lock
;
153 pthread_mutex_t write_lock
;
154 pthread_mutex_t status_lock
;
155 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
156 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
157 void *crypto_session
;
177 bool structured_replies
;
178 bool meta_context_base_allocation
;
181 connection_recv_function recv
;
182 connection_send_function send
;
183 connection_close_function close
;
186 extern int handle_single_connection (int sockin
, int sockout
);
187 extern int connection_set_handle (struct connection
*conn
,
188 size_t i
, void *handle
)
189 __attribute__((__nonnull__ (1 /* not 3 */)));
190 extern void *connection_get_handle (struct connection
*conn
, size_t i
)
191 __attribute__((__nonnull__ (1)));
192 extern int connection_get_status (struct connection
*conn
)
193 __attribute__((__nonnull__ (1)));
194 extern int connection_set_status (struct connection
*conn
, int value
)
195 __attribute__((__nonnull__ (1)));
197 /* protocol-handshake.c */
198 extern int protocol_handshake (struct connection
*conn
)
199 __attribute__((__nonnull__ (1)));
200 extern int protocol_compute_eflags (struct connection
*conn
, uint16_t *flags
)
201 __attribute__((__nonnull__ (1, 2)));
203 /* protocol-handshake-oldstyle.c */
204 extern int protocol_handshake_oldstyle (struct connection
*conn
)
205 __attribute__((__nonnull__ (1)));
207 /* protocol-handshake-newstyle.c */
208 extern int protocol_handshake_newstyle (struct connection
*conn
)
209 __attribute__((__nonnull__ (1)));
212 extern int protocol_recv_request_send_reply (struct connection
*conn
)
213 __attribute__((__nonnull__ (1)));
215 /* The context ID of base:allocation. As far as I can tell it doesn't
216 * matter what this is as long as nbdkit always returns the same
219 #define base_allocation_id 1
222 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
223 extern void crypto_init (bool tls_set_on_cli
);
224 extern void crypto_free (void);
225 extern int crypto_negotiate_tls (struct connection
*conn
,
226 int sockin
, int sockout
)
227 __attribute__((__nonnull__ (1)));
230 #define debug nbdkit_debug
233 #if !HAVE_VFPRINTF_PERCENT_M
235 #define vfprintf nbdkit_vfprintf
236 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
237 __attribute__((__format__ (printf
, 2, 0)));
239 extern void log_stderr_verror (const char *fs
, va_list args
)
240 __attribute__((__format__ (printf
, 1, 0)));
241 extern void log_syslog_verror (const char *fs
, va_list args
)
242 __attribute__((__format__ (printf
, 1, 0)));
245 /* Next filter or plugin in the chain. This is always NULL for
246 * plugins and never NULL for filters.
248 struct backend
*next
;
250 /* A unique index used to fetch the handle from the connections
251 * object. The plugin (last in the chain) has index 0, and the
252 * filters have index 1, 2, ... depending how "far" they are from
257 void (*free
) (struct backend
*);
258 int (*thread_model
) (struct backend
*);
259 const char *(*name
) (struct backend
*);
260 const char *(*plugin_name
) (struct backend
*);
261 void (*usage
) (struct backend
*);
262 const char *(*version
) (struct backend
*);
263 void (*dump_fields
) (struct backend
*);
264 void (*config
) (struct backend
*, const char *key
, const char *value
);
265 void (*config_complete
) (struct backend
*);
266 const char *(*magic_config_key
) (struct backend
*);
267 int (*open
) (struct backend
*, struct connection
*conn
, int readonly
);
268 int (*prepare
) (struct backend
*, struct connection
*conn
);
269 int (*finalize
) (struct backend
*, struct connection
*conn
);
270 void (*close
) (struct backend
*, struct connection
*conn
);
272 int64_t (*get_size
) (struct backend
*, struct connection
*conn
);
273 int (*can_write
) (struct backend
*, struct connection
*conn
);
274 int (*can_flush
) (struct backend
*, struct connection
*conn
);
275 int (*is_rotational
) (struct backend
*, struct connection
*conn
);
276 int (*can_trim
) (struct backend
*, struct connection
*conn
);
277 int (*can_zero
) (struct backend
*, struct connection
*conn
);
278 int (*can_extents
) (struct backend
*, struct connection
*conn
);
279 int (*can_fua
) (struct backend
*, struct connection
*conn
);
280 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
);
281 int (*can_cache
) (struct backend
*, struct connection
*conn
);
283 int (*pread
) (struct backend
*, struct connection
*conn
, void *buf
,
284 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
285 int (*pwrite
) (struct backend
*, struct connection
*conn
, const void *buf
,
286 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
287 int (*flush
) (struct backend
*, struct connection
*conn
, uint32_t flags
,
289 int (*trim
) (struct backend
*, struct connection
*conn
, uint32_t count
,
290 uint64_t offset
, uint32_t flags
, int *err
);
291 int (*zero
) (struct backend
*, struct connection
*conn
, uint32_t count
,
292 uint64_t offset
, uint32_t flags
, int *err
);
293 int (*extents
) (struct backend
*, struct connection
*conn
, uint32_t count
,
294 uint64_t offset
, uint32_t flags
,
295 struct nbdkit_extents
*extents
, int *err
);
296 int (*cache
) (struct backend
*, struct connection
*conn
, uint32_t count
,
297 uint64_t offset
, uint32_t flags
, int *err
);
301 extern struct backend
*plugin_register (size_t index
, const char *filename
,
302 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
303 __attribute__((__nonnull__ (2, 3, 4)));
304 extern void set_debug_flags (void *dl
, const char *name
)
305 __attribute__((__nonnull__ (1, 2)));
308 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
309 const char *filename
, void *dl
,
310 struct nbdkit_filter
*(*filter_init
) (void))
311 __attribute__((__nonnull__ (1, 3, 4, 5)));
314 extern void lock_init_thread_model (void);
315 extern const char *name_of_thread_model (int model
);
316 extern void lock_connection (void);
317 extern void unlock_connection (void);
318 extern void lock_request (struct connection
*conn
);
319 extern void unlock_request (struct connection
*conn
);
320 extern void lock_unload (void);
321 extern void unlock_unload (void);
324 extern int *bind_unix_socket (size_t *)
325 __attribute__((__nonnull__ (1)));
326 extern int *bind_tcpip_socket (size_t *)
327 __attribute__((__nonnull__ (1)));
328 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
329 __attribute__((__nonnull__ (1)));
330 extern void free_listening_sockets (int *socks
, size_t nr_socks
)
331 __attribute__((__nonnull__ (1)));
334 extern void threadlocal_init (void);
335 extern void threadlocal_new_server_thread (void);
336 extern void threadlocal_set_name (const char *name
)
337 __attribute__((__nonnull__ (1)));
338 extern const char *threadlocal_get_name (void);
339 extern void threadlocal_set_instance_num (size_t instance_num
);
340 extern size_t threadlocal_get_instance_num (void);
341 extern void threadlocal_set_sockaddr (const struct sockaddr
*addr
,
343 __attribute__((__nonnull__ (1)));
344 /*extern void threadlocal_get_sockaddr ();*/
345 extern void threadlocal_set_error (int err
);
346 extern int threadlocal_get_error (void);
347 extern void *threadlocal_buffer (size_t size
);
348 extern void threadlocal_set_conn (struct connection
*conn
);
349 extern struct connection
*threadlocal_get_conn (void);
351 /* Declare program_name. */
352 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
354 #define program_name program_invocation_short_name
356 #define program_name "nbdkit"
359 #endif /* NBDKIT_INTERNAL_H */