Version 1.13.0.
[nbdkit/ericb.git] / server / internal.h
blobf341ff133fe1611007f18f3b958d94ca0a650372
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"
46 #ifdef __APPLE__
47 #define UNIX_PATH_MAX 104
48 #else
49 #define UNIX_PATH_MAX 108
50 #endif
52 #ifndef O_CLOEXEC
53 #define O_CLOEXEC 0
54 #endif
56 #ifndef SOCK_CLOEXEC
57 #define SOCK_CLOEXEC 0
58 #endif
60 #if HAVE_VALGRIND
61 # include <valgrind.h>
62 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
63 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
64 #elif defined(__SANITIZE_ADDRESS__)
65 # define DO_DLCLOSE 0
66 #else
67 # define DO_DLCLOSE 1
68 #endif
70 #define container_of(ptr, type, member) ({ \
71 const typeof (((type *) 0)->member) *__mptr = (ptr); \
72 (type *) ((char *) __mptr - offsetof(type, member)); \
75 /* main.c */
76 struct debug_flag {
77 struct debug_flag *next;
78 char *name; /* plugin or filter name */
79 char *flag; /* flag name */
80 int value; /* value of flag */
81 bool used; /* if flag was successfully set */
84 enum log_to {
85 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
86 we forked into the background in which
87 case log to syslog */
88 LOG_TO_STDERR, /* --log=stderr forced on the command line */
89 LOG_TO_SYSLOG, /* --log=syslog forced on the command line */
92 extern struct debug_flag *debug_flags;
93 extern const char *exportname;
94 extern bool foreground;
95 extern const char *ipaddr;
96 extern enum log_to log_to;
97 extern bool newstyle;
98 extern const char *port;
99 extern bool readonly;
100 extern const char *run;
101 extern const char *selinux_label;
102 extern int threads;
103 extern int tls;
104 extern const char *tls_certificates_dir;
105 extern const char *tls_psk;
106 extern bool tls_verify_peer;
107 extern char *unixsocket;
108 extern const char *user, *group;
109 extern bool verbose;
111 extern struct backend *backend;
112 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
114 /* quit.c */
115 extern volatile int quit;
116 extern int quit_fd;
117 extern void set_up_quit_pipe (void);
118 extern void handle_quit (int sig);
119 extern void set_quit (void);
121 /* signals.c */
122 extern void set_up_signals (void);
124 /* background.c */
125 extern bool forked_into_background;
126 extern void fork_into_background (void);
128 /* captive.c */
129 extern void run_command (void);
131 /* socket-activation.c */
132 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
133 extern unsigned int get_socket_activation (void);
135 /* usergroup.c */
136 extern void change_user (void);
138 /* cleanup.c */
139 extern void cleanup_free (void *ptr);
140 #define CLEANUP_FREE __attribute__((cleanup (cleanup_free)))
141 extern void cleanup_extents_free (void *ptr);
142 #define CLEANUP_EXTENTS_FREE __attribute__((cleanup (cleanup_extents_free)))
143 extern void cleanup_unlock (pthread_mutex_t **ptr);
144 #define CLEANUP_UNLOCK __attribute__((cleanup (cleanup_unlock)))
145 #define ACQUIRE_LOCK_FOR_CURRENT_SCOPE(mutex) \
146 CLEANUP_UNLOCK pthread_mutex_t *_lock = mutex; \
147 pthread_mutex_lock (_lock)
149 /* connections.c */
150 struct connection;
152 typedef int (*connection_recv_function) (struct connection *,
153 void *buf, size_t len)
154 __attribute__((__nonnull__ (1, 2)));
155 typedef int (*connection_send_function) (struct connection *,
156 const void *buf, size_t len)
157 __attribute__((__nonnull__ (1, 2)));
158 typedef void (*connection_close_function) (struct connection *)
159 __attribute__((__nonnull__ (1)));
161 struct connection {
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 void *crypto_session;
168 int nworkers;
170 void **handles;
171 size_t nr_handles;
173 uint32_t cflags;
174 uint64_t exportsize;
175 uint16_t eflags;
176 bool readonly;
177 bool can_flush;
178 bool is_rotational;
179 bool can_trim;
180 bool can_zero;
181 bool can_fua;
182 bool can_multi_conn;
183 bool can_extents;
184 bool using_tls;
185 bool structured_replies;
186 bool meta_context_base_allocation;
188 int sockin, sockout;
189 connection_recv_function recv;
190 connection_send_function send;
191 connection_close_function close;
194 extern int handle_single_connection (int sockin, int sockout);
195 extern int connection_set_handle (struct connection *conn,
196 size_t i, void *handle)
197 __attribute__((__nonnull__ (1 /* not 3 */)));
198 extern void *connection_get_handle (struct connection *conn, size_t i)
199 __attribute__((__nonnull__ (1)));
200 extern int connection_get_status (struct connection *conn)
201 __attribute__((__nonnull__ (1)));
202 extern int connection_set_status (struct connection *conn, int value)
203 __attribute__((__nonnull__ (1)));
205 /* protocol-handshake.c */
206 extern int protocol_handshake (struct connection *conn)
207 __attribute__((__nonnull__ (1)));
208 extern int protocol_compute_eflags (struct connection *conn, uint16_t *flags)
209 __attribute__((__nonnull__ (1, 2)));
211 /* protocol-handshake-oldstyle.c */
212 extern int protocol_handshake_oldstyle (struct connection *conn)
213 __attribute__((__nonnull__ (1)));
215 /* protocol-handshake-newstyle.c */
216 extern int protocol_handshake_newstyle (struct connection *conn)
217 __attribute__((__nonnull__ (1)));
219 /* protocol.c */
220 extern int protocol_recv_request_send_reply (struct connection *conn)
221 __attribute__((__nonnull__ (1)));
223 /* The context ID of base:allocation. As far as I can tell it doesn't
224 * matter what this is as long as nbdkit always returns the same
225 * number.
227 #define base_allocation_id 1
229 /* crypto.c */
230 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
231 extern void crypto_init (bool tls_set_on_cli);
232 extern void crypto_free (void);
233 extern int crypto_negotiate_tls (struct connection *conn,
234 int sockin, int sockout)
235 __attribute__((__nonnull__ (1)));
237 /* debug.c */
238 #define debug nbdkit_debug
240 /* log-*.c */
241 #if !HAVE_VFPRINTF_PERCENT_M
242 #include <stdio.h>
243 #define vfprintf nbdkit_vfprintf
244 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
245 __attribute__((__format__ (printf, 2, 0)));
246 #endif
247 extern void log_stderr_verror (const char *fs, va_list args)
248 __attribute__((__format__ (printf, 1, 0)));
249 extern void log_syslog_verror (const char *fs, va_list args)
250 __attribute__((__format__ (printf, 1, 0)));
252 struct backend {
253 /* Next filter or plugin in the chain. This is always NULL for
254 * plugins and never NULL for filters.
256 struct backend *next;
258 /* A unique index used to fetch the handle from the connections
259 * object. The plugin (last in the chain) has index 0, and the
260 * filters have index 1, 2, ... depending how "far" they are from
261 * the plugin.
263 size_t i;
265 void (*free) (struct backend *);
266 int (*thread_model) (struct backend *);
267 const char *(*name) (struct backend *);
268 const char *(*plugin_name) (struct backend *);
269 void (*usage) (struct backend *);
270 const char *(*version) (struct backend *);
271 void (*dump_fields) (struct backend *);
272 void (*config) (struct backend *, const char *key, const char *value);
273 void (*config_complete) (struct backend *);
274 const char *(*magic_config_key) (struct backend *);
275 int (*open) (struct backend *, struct connection *conn, int readonly);
276 int (*prepare) (struct backend *, struct connection *conn);
277 int (*finalize) (struct backend *, struct connection *conn);
278 void (*close) (struct backend *, struct connection *conn);
280 int64_t (*get_size) (struct backend *, struct connection *conn);
281 int (*can_write) (struct backend *, struct connection *conn);
282 int (*can_flush) (struct backend *, struct connection *conn);
283 int (*is_rotational) (struct backend *, struct connection *conn);
284 int (*can_trim) (struct backend *, struct connection *conn);
285 int (*can_zero) (struct backend *, struct connection *conn);
286 int (*can_extents) (struct backend *, struct connection *conn);
287 int (*can_fua) (struct backend *, struct connection *conn);
288 int (*can_multi_conn) (struct backend *, struct connection *conn);
290 int (*pread) (struct backend *, struct connection *conn, void *buf,
291 uint32_t count, uint64_t offset, uint32_t flags, int *err);
292 int (*pwrite) (struct backend *, struct connection *conn, const void *buf,
293 uint32_t count, uint64_t offset, uint32_t flags, int *err);
294 int (*flush) (struct backend *, struct connection *conn, uint32_t flags,
295 int *err);
296 int (*trim) (struct backend *, struct connection *conn, uint32_t count,
297 uint64_t offset, uint32_t flags, int *err);
298 int (*zero) (struct backend *, struct connection *conn, uint32_t count,
299 uint64_t offset, uint32_t flags, int *err);
300 int (*extents) (struct backend *, struct connection *conn, uint32_t count,
301 uint64_t offset, uint32_t flags,
302 struct nbdkit_extents *extents, int *err);
305 /* plugins.c */
306 extern struct backend *plugin_register (size_t index, const char *filename,
307 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
308 __attribute__((__nonnull__ (2, 3, 4)));
309 extern void set_debug_flags (void *dl, const char *name)
310 __attribute__((__nonnull__ (1, 2)));
312 /* filters.c */
313 extern struct backend *filter_register (struct backend *next, size_t index,
314 const char *filename, void *dl,
315 struct nbdkit_filter *(*filter_init) (void))
316 __attribute__((__nonnull__ (1, 3, 4, 5)));
318 /* locks.c */
319 extern void lock_init_thread_model (void);
320 extern void lock_connection (void);
321 extern void unlock_connection (void);
322 extern void lock_request (struct connection *conn)
323 __attribute__((__nonnull__ (1)));
324 extern void unlock_request (struct connection *conn)
325 __attribute__((__nonnull__ (1)));
326 extern void lock_unload (void);
327 extern void unlock_unload (void);
329 /* sockets.c */
330 extern int *bind_unix_socket (size_t *)
331 __attribute__((__nonnull__ (1)));
332 extern int *bind_tcpip_socket (size_t *)
333 __attribute__((__nonnull__ (1)));
334 extern void accept_incoming_connections (int *socks, size_t nr_socks)
335 __attribute__((__nonnull__ (1)));
336 extern void free_listening_sockets (int *socks, size_t nr_socks)
337 __attribute__((__nonnull__ (1)));
339 /* threadlocal.c */
340 extern void threadlocal_init (void);
341 extern void threadlocal_new_server_thread (void);
342 extern void threadlocal_set_name (const char *name)
343 __attribute__((__nonnull__ (1)));
344 extern void threadlocal_set_instance_num (size_t instance_num);
345 extern void threadlocal_set_sockaddr (const struct sockaddr *addr,
346 socklen_t addrlen)
347 __attribute__((__nonnull__ (1)));
348 extern const char *threadlocal_get_name (void);
349 extern size_t threadlocal_get_instance_num (void);
350 extern void threadlocal_set_error (int err);
351 extern int threadlocal_get_error (void);
352 /*extern void threadlocal_get_sockaddr ();*/
354 /* Declare program_name. */
355 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
356 #include <errno.h>
357 #define program_name program_invocation_short_name
358 #else
359 #define program_name "nbdkit"
360 #endif
362 #endif /* NBDKIT_INTERNAL_H */