main: Add option to disable SR advertisement
[nbdkit/ericb.git] / server / internal.h
blob22e13b6d93e4000f90ee3201d2d7be7645600d11
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 /* main.c */
69 struct debug_flag {
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 */
77 enum log_to {
78 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
79 we forked into the background in which
80 case log to syslog */
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;
90 extern bool newstyle;
91 extern bool no_sr;
92 extern const char *port;
93 extern bool readonly;
94 extern const char *run;
95 extern const char *selinux_label;
96 extern int threads;
97 extern int tls;
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;
103 extern bool verbose;
105 extern struct backend *backend;
106 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
108 /* quit.c */
109 extern volatile int quit;
110 extern int quit_fd;
111 extern void set_up_quit_pipe (void);
112 extern void handle_quit (int sig);
113 extern void set_quit (void);
115 /* signals.c */
116 extern void set_up_signals (void);
118 /* background.c */
119 extern bool forked_into_background;
120 extern void fork_into_background (void);
122 /* captive.c */
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);
129 /* usergroup.c */
130 extern void change_user (void);
132 /* connections.c */
133 struct connection;
135 /* Flags for connection_send_function */
136 enum {
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,
145 int flags)
146 __attribute__((__nonnull__ (1, 2)));
147 typedef void (*connection_close_function) (struct connection *)
148 __attribute__((__nonnull__ (1)));
150 struct connection {
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;
158 int nworkers;
160 void **handles;
161 size_t nr_handles;
163 uint32_t cflags;
164 uint64_t exportsize;
165 uint16_t eflags;
166 bool readonly;
167 bool can_flush;
168 bool is_rotational;
169 bool can_trim;
170 bool can_zero;
171 bool can_fua;
172 bool can_multi_conn;
173 bool can_cache;
174 bool emulate_cache;
175 bool can_extents;
176 bool using_tls;
177 bool structured_replies;
178 bool meta_context_base_allocation;
180 int sockin, sockout;
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)));
211 /* protocol.c */
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
217 * number.
219 #define base_allocation_id 1
221 /* crypto.c */
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)));
229 /* debug.c */
230 #define debug nbdkit_debug
232 /* log-*.c */
233 #if !HAVE_VFPRINTF_PERCENT_M
234 #include <stdio.h>
235 #define vfprintf nbdkit_vfprintf
236 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
237 __attribute__((__format__ (printf, 2, 0)));
238 #endif
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)));
244 struct backend {
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
253 * the plugin.
255 size_t i;
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,
288 int *err);
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);
300 /* plugins.c */
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)));
307 /* filters.c */
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)));
313 /* locks.c */
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);
323 /* sockets.c */
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)));
333 /* threadlocal.c */
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,
342 socklen_t addrlen)
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
353 #include <errno.h>
354 #define program_name program_invocation_short_name
355 #else
356 #define program_name "nbdkit"
357 #endif
359 #endif /* NBDKIT_INTERNAL_H */