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