server: Remove tricksy initialization of struct b_conn_handle.
[nbdkit/ericb.git] / server / internal.h
bloba376d739f5ce9492ba0d52f1d364343871d99afe
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"
47 #ifdef __APPLE__
48 #define UNIX_PATH_MAX 104
49 #else
50 #define UNIX_PATH_MAX 108
51 #endif
53 #if HAVE_VALGRIND
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__)
58 # define DO_DLCLOSE 0
59 #else
60 # define DO_DLCLOSE 1
61 #endif
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)
71 /* main.c */
72 struct debug_flag {
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 */
80 enum log_to {
81 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
82 we forked into the background in which
83 case log to syslog */
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;
93 extern unsigned mask_handshake;
94 extern bool newstyle;
95 extern bool no_sr;
96 extern const char *port;
97 extern bool read_only;
98 extern const char *run;
99 extern bool listen_stdin;
100 extern const char *selinux_label;
101 extern int threads;
102 extern int tls;
103 extern const char *tls_certificates_dir;
104 extern const char *tls_psk;
105 extern bool tls_verify_peer;
106 extern char *unixsocket;
107 extern const char *user, *group;
108 extern bool verbose;
110 extern struct backend *backend;
111 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
113 /* quit.c */
114 extern volatile int quit;
115 extern int quit_fd;
116 extern void set_up_quit_pipe (void);
117 extern void handle_quit (int sig);
118 extern void set_quit (void);
120 /* signals.c */
121 extern void set_up_signals (void);
123 /* background.c */
124 extern bool forked_into_background;
125 extern void fork_into_background (void);
127 /* captive.c */
128 extern void run_command (void);
130 /* socket-activation.c */
131 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
132 extern unsigned int get_socket_activation (void);
134 /* usergroup.c */
135 extern void change_user (void);
137 /* connections.c */
138 struct connection;
140 /* Flags for connection_send_function */
141 enum {
142 SEND_MORE = 1, /* Hint to use MSG_MORE/corking to group send()s */
145 typedef int (*connection_recv_function) (struct connection *,
146 void *buf, size_t len)
147 __attribute__((__nonnull__ (1, 2)));
148 typedef int (*connection_send_function) (struct connection *,
149 const void *buf, size_t len,
150 int flags)
151 __attribute__((__nonnull__ (1, 2)));
152 typedef void (*connection_close_function) (struct connection *)
153 __attribute__((__nonnull__ (1)));
155 struct b_conn_handle {
156 void *handle;
158 uint64_t exportsize;
159 int can_write;
160 int can_flush;
161 int is_rotational;
162 int can_trim;
163 int can_zero;
164 int can_fast_zero;
165 int can_fua;
166 int can_multi_conn;
167 int can_extents;
168 int can_cache;
171 static inline void
172 reset_b_conn_handle (struct b_conn_handle *h)
174 h->handle = NULL;
175 h->exportsize = -1;
176 h->can_write = -1;
177 h->can_flush = -1;
178 h->is_rotational = -1;
179 h->can_trim = -1;
180 h->can_zero = -1;
181 h->can_fast_zero = -1;
182 h->can_fua = -1;
183 h->can_multi_conn = -1;
184 h->can_extents = -1;
185 h->can_cache = -1;
188 struct connection {
189 pthread_mutex_t request_lock;
190 pthread_mutex_t read_lock;
191 pthread_mutex_t write_lock;
192 pthread_mutex_t status_lock;
193 int status; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
194 int status_pipe[2]; /* track status changes via poll when nworkers > 1 */
195 void *crypto_session;
196 int nworkers;
198 struct b_conn_handle *handles;
199 size_t nr_handles;
201 char *exportname;
202 uint32_t cflags;
203 uint16_t eflags;
204 bool using_tls;
205 bool structured_replies;
206 bool meta_context_base_allocation;
208 int sockin, sockout;
209 connection_recv_function recv;
210 connection_send_function send;
211 connection_close_function close;
214 extern int handle_single_connection (int sockin, int sockout);
215 extern void *connection_get_handle (struct connection *conn, size_t i)
216 __attribute__((__nonnull__ (1)));
217 extern int connection_get_status (struct connection *conn)
218 __attribute__((__nonnull__ (1)));
219 extern int connection_set_status (struct connection *conn, int value)
220 __attribute__((__nonnull__ (1)));
222 /* protocol-handshake.c */
223 extern int protocol_handshake (struct connection *conn)
224 __attribute__((__nonnull__ (1)));
225 extern int protocol_common_open (struct connection *conn,
226 uint64_t *exportsize, uint16_t *flags)
227 __attribute__((__nonnull__ (1, 2, 3)));
229 /* protocol-handshake-oldstyle.c */
230 extern int protocol_handshake_oldstyle (struct connection *conn)
231 __attribute__((__nonnull__ (1)));
233 /* protocol-handshake-newstyle.c */
234 extern int protocol_handshake_newstyle (struct connection *conn)
235 __attribute__((__nonnull__ (1)));
237 /* protocol.c */
238 extern int protocol_recv_request_send_reply (struct connection *conn)
239 __attribute__((__nonnull__ (1)));
241 /* The context ID of base:allocation. As far as I can tell it doesn't
242 * matter what this is as long as nbdkit always returns the same
243 * number.
245 #define base_allocation_id 1
247 /* crypto.c */
248 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
249 extern void crypto_init (bool tls_set_on_cli);
250 extern void crypto_free (void);
251 extern int crypto_negotiate_tls (struct connection *conn,
252 int sockin, int sockout)
253 __attribute__((__nonnull__ (1)));
255 /* debug.c */
256 #define debug nbdkit_debug
258 /* log-*.c */
259 #if !HAVE_VFPRINTF_PERCENT_M
260 #include <stdio.h>
261 #define vfprintf nbdkit_vfprintf
262 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
263 __attribute__((__format__ (printf, 2, 0)));
264 #endif
265 extern void log_stderr_verror (const char *fs, va_list args)
266 __attribute__((__format__ (printf, 1, 0)));
267 extern void log_syslog_verror (const char *fs, va_list args)
268 __attribute__((__format__ (printf, 1, 0)));
270 /* backend.c */
271 struct backend {
272 /* Next filter or plugin in the chain. This is always NULL for
273 * plugins and never NULL for filters.
275 struct backend *next;
277 /* A unique index used to fetch the handle from the connections
278 * object. The plugin (last in the chain) has index 0, and the
279 * filters have index 1, 2, ... depending how "far" they are from
280 * the plugin.
282 size_t i;
284 /* The type of backend: filter or plugin. */
285 const char *type;
287 /* A copy of the backend name that survives a dlclose. */
288 char *name;
290 /* The file the backend was loaded from. */
291 char *filename;
293 /* The dlopen handle for the backend. */
294 void *dl;
296 /* Backend callbacks. All are required. */
297 void (*free) (struct backend *);
298 int (*thread_model) (struct backend *);
299 const char *(*plugin_name) (struct backend *);
300 void (*usage) (struct backend *);
301 const char *(*version) (struct backend *);
302 void (*dump_fields) (struct backend *);
303 void (*config) (struct backend *, const char *key, const char *value);
304 void (*config_complete) (struct backend *);
305 const char *(*magic_config_key) (struct backend *);
306 int (*open) (struct backend *, struct connection *conn, int readonly);
307 int (*prepare) (struct backend *, struct connection *conn, int readonly);
308 int (*finalize) (struct backend *, struct connection *conn);
309 void (*close) (struct backend *, struct connection *conn);
311 int64_t (*get_size) (struct backend *, struct connection *conn);
312 int (*can_write) (struct backend *, struct connection *conn);
313 int (*can_flush) (struct backend *, struct connection *conn);
314 int (*is_rotational) (struct backend *, struct connection *conn);
315 int (*can_trim) (struct backend *, struct connection *conn);
316 int (*can_zero) (struct backend *, struct connection *conn);
317 int (*can_fast_zero) (struct backend *, struct connection *conn);
318 int (*can_extents) (struct backend *, struct connection *conn);
319 int (*can_fua) (struct backend *, struct connection *conn);
320 int (*can_multi_conn) (struct backend *, struct connection *conn);
321 int (*can_cache) (struct backend *, struct connection *conn);
323 int (*pread) (struct backend *, struct connection *conn, void *buf,
324 uint32_t count, uint64_t offset, uint32_t flags, int *err);
325 int (*pwrite) (struct backend *, struct connection *conn, const void *buf,
326 uint32_t count, uint64_t offset, uint32_t flags, int *err);
327 int (*flush) (struct backend *, struct connection *conn, uint32_t flags,
328 int *err);
329 int (*trim) (struct backend *, struct connection *conn, uint32_t count,
330 uint64_t offset, uint32_t flags, int *err);
331 int (*zero) (struct backend *, struct connection *conn, uint32_t count,
332 uint64_t offset, uint32_t flags, int *err);
333 int (*extents) (struct backend *, struct connection *conn, uint32_t count,
334 uint64_t offset, uint32_t flags,
335 struct nbdkit_extents *extents, int *err);
336 int (*cache) (struct backend *, struct connection *conn, uint32_t count,
337 uint64_t offset, uint32_t flags, int *err);
340 extern void backend_init (struct backend *b, struct backend *next, size_t index,
341 const char *filename, void *dl, const char *type)
342 __attribute__((__nonnull__ (1, 4, 5, 6)));
343 extern void backend_load (struct backend *b, const char *name,
344 void (*load) (void))
345 __attribute__((__nonnull__ (1 /* not 2 */)));
346 extern void backend_unload (struct backend *b, void (*unload) (void))
347 __attribute__((__nonnull__ (1)));
349 extern int backend_open (struct backend *b, struct connection *conn,
350 int readonly)
351 __attribute__((__nonnull__ (1, 2)));
352 extern int backend_prepare (struct backend *b, struct connection *conn)
353 __attribute__((__nonnull__ (1, 2)));
354 extern void backend_close (struct backend *b, struct connection *conn)
355 __attribute__((__nonnull__ (1, 2)));
356 extern void backend_set_handle (struct backend *b, struct connection *conn,
357 void *handle)
358 __attribute__((__nonnull__ (1, 2, 3)));
359 extern bool backend_valid_range (struct backend *b, struct connection *conn,
360 uint64_t offset, uint32_t count)
361 __attribute__((__nonnull__ (1, 2)));
363 extern int64_t backend_get_size (struct backend *b, struct connection *conn)
364 __attribute__((__nonnull__ (1, 2)));
365 extern int backend_can_write (struct backend *b, struct connection *conn)
366 __attribute__((__nonnull__ (1, 2)));
367 extern int backend_can_flush (struct backend *b, struct connection *conn)
368 __attribute__((__nonnull__ (1, 2)));
369 extern int backend_is_rotational (struct backend *b, struct connection *conn)
370 __attribute__((__nonnull__ (1, 2)));
371 extern int backend_can_trim (struct backend *b, struct connection *conn)
372 __attribute__((__nonnull__ (1, 2)));
373 extern int backend_can_zero (struct backend *b, struct connection *conn)
374 __attribute__((__nonnull__ (1, 2)));
375 extern int backend_can_fast_zero (struct backend *b, struct connection *conn)
376 __attribute__((__nonnull__ (1, 2)));
377 extern int backend_can_extents (struct backend *b, struct connection *conn)
378 __attribute__((__nonnull__ (1, 2)));
379 extern int backend_can_fua (struct backend *b, struct connection *conn)
380 __attribute__((__nonnull__ (1, 2)));
381 extern int backend_can_multi_conn (struct backend *b, struct connection *conn)
382 __attribute__((__nonnull__ (1, 2)));
383 extern int backend_can_cache (struct backend *b, struct connection *conn)
384 __attribute__((__nonnull__ (1, 2)));
386 extern int backend_pread (struct backend *b, struct connection *conn,
387 void *buf, uint32_t count, uint64_t offset,
388 uint32_t flags, int *err)
389 __attribute__((__nonnull__ (1, 2, 3, 7)));
390 extern int backend_pwrite (struct backend *b, struct connection *conn,
391 const void *buf, uint32_t count, uint64_t offset,
392 uint32_t flags, int *err)
393 __attribute__((__nonnull__ (1, 2, 3, 7)));
394 extern int backend_flush (struct backend *b, struct connection *conn,
395 uint32_t flags, int *err)
396 __attribute__((__nonnull__ (1, 2, 4)));
397 extern int backend_trim (struct backend *b, struct connection *conn,
398 uint32_t count, uint64_t offset, uint32_t flags,
399 int *err)
400 __attribute__((__nonnull__ (1, 2, 6)));
401 extern int backend_zero (struct backend *b, struct connection *conn,
402 uint32_t count, uint64_t offset, uint32_t flags,
403 int *err)
404 __attribute__((__nonnull__ (1, 2, 6)));
405 extern int backend_extents (struct backend *b, struct connection *conn,
406 uint32_t count, uint64_t offset, uint32_t flags,
407 struct nbdkit_extents *extents, int *err)
408 __attribute__((__nonnull__ (1, 2, 6, 7)));
409 extern int backend_cache (struct backend *b, struct connection *conn,
410 uint32_t count, uint64_t offset,
411 uint32_t flags, int *err)
412 __attribute__((__nonnull__ (1, 2, 6)));
414 /* plugins.c */
415 extern struct backend *plugin_register (size_t index, const char *filename,
416 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
417 __attribute__((__nonnull__ (2, 3, 4)));
419 /* filters.c */
420 extern struct backend *filter_register (struct backend *next, size_t index,
421 const char *filename, void *dl,
422 struct nbdkit_filter *(*filter_init) (void))
423 __attribute__((__nonnull__ (1, 3, 4, 5)));
425 /* locks.c */
426 extern void lock_init_thread_model (void);
427 extern const char *name_of_thread_model (int model);
428 extern void lock_connection (void);
429 extern void unlock_connection (void);
430 extern void lock_request (struct connection *conn);
431 extern void unlock_request (struct connection *conn);
432 extern void lock_unload (void);
433 extern void unlock_unload (void);
435 /* sockets.c */
436 extern int *bind_unix_socket (size_t *)
437 __attribute__((__nonnull__ (1)));
438 extern int *bind_tcpip_socket (size_t *)
439 __attribute__((__nonnull__ (1)));
440 extern void accept_incoming_connections (int *socks, size_t nr_socks)
441 __attribute__((__nonnull__ (1)));
442 extern void free_listening_sockets (int *socks, size_t nr_socks)
443 __attribute__((__nonnull__ (1)));
445 /* threadlocal.c */
446 extern void threadlocal_init (void);
447 extern void threadlocal_new_server_thread (void);
448 extern void threadlocal_set_name (const char *name)
449 __attribute__((__nonnull__ (1)));
450 extern const char *threadlocal_get_name (void);
451 extern void threadlocal_set_instance_num (size_t instance_num);
452 extern size_t threadlocal_get_instance_num (void);
453 extern void threadlocal_set_error (int err);
454 extern int threadlocal_get_error (void);
455 extern void *threadlocal_buffer (size_t size);
456 extern void threadlocal_set_conn (struct connection *conn);
457 extern struct connection *threadlocal_get_conn (void);
459 /* Declare program_name. */
460 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
461 #include <errno.h>
462 #define program_name program_invocation_short_name
463 #else
464 #define program_name "nbdkit"
465 #endif
467 #endif /* NBDKIT_INTERNAL_H */