docs, tests: Improved method to probe for plugins and filters.
[nbdkit/ericb.git] / docs / nbdkit-plugin.pod
blob0873cf6027e781075cc1547e212fd5c3bb6b62c6
1 =head1 NAME
3 nbdkit-plugin - how to write nbdkit plugins
5 =head1 SYNOPSIS
7  #define NBDKIT_API_VERSION 2
8  
9  #include <nbdkit-plugin.h>
11  #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
13  static void *
14  myplugin_open (void)
15  {
16    /* create a handle ... */
17    return handle;
18  }
20  static struct nbdkit_plugin plugin = {
21    .name              = "myplugin",
22    .open              = myplugin_open,
23    .get_size          = myplugin_get_size,
24    .pread             = myplugin_pread,
25    .pwrite            = myplugin_pwrite,
26    /* etc */
27  };
29  NBDKIT_REGISTER_PLUGIN(plugin)
31 When this has been compiled to a shared library, do:
33  nbdkit [--args ...] ./myplugin.so [key=value ...]
35 When debugging, use the I<-fv> options:
37  nbdkit -fv ./myplugin.so [key=value ...]
39 =head1 DESCRIPTION
41 An nbdkit plugin is a new source device which can be served using the
42 Network Block Device (NBD) protocol.  This manual page describes how
43 to create an nbdkit plugin in C.
45 To see example plugins:
46 L<https://github.com/libguestfs/nbdkit/tree/master/plugins>
48 Plugins written in C have an ABI guarantee: a plugin compiled against
49 an older version of nbdkit will still work correctly when loaded with
50 a newer nbdkit.  We also try (but cannot guarantee) to support plugins
51 compiled against a newer version of nbdkit when loaded with an older
52 nbdkit, although the plugin may have reduced functionality if it
53 depends on features only provided by newer nbdkit.
55 For plugins written in C, we also provide an API guarantee: a plugin
56 written against an older header will still compile unmodified with a
57 newer nbdkit.
59 The API guarantee does not always apply to plugins written in other
60 (non-C) languages which may have to adapt to changes when recompiled
61 against a newer nbdkit.
63 To write plugins in other languages, see:
64 __LANG_PLUGIN_LINKS__.
66 =head1 C<#define NBDKIT_API_VERSION 2>
68 Plugins must choose which API version they want to use, by defining
69 NBDKIT_API_VERSION to a positive integer prior to including
70 C<nbdkit-plugin.h> (or any other nbdkit header).  The default version
71 is 1 for backwards-compatibility with nbdkit v1.1.26 and earlier;
72 however, it is recommended that new plugins be written to the maximum
73 version (currently 2) as it enables more features and better
74 interaction with nbdkit filters.  Therefore, the rest of this document
75 only covers the version 2 interface.  A newer nbdkit will always
76 support plugins written in C which were compiled against any prior API
77 version.
79 =head1 C<#include E<lt>nbdkit-plugin.hE<gt>>
81 All plugins should start by including this header file, after
82 optionally choosing an API version.
84 =head1 C<#define THREAD_MODEL>
86 All plugins must define a thread model.  See L</THREADS> below for
87 details.  It is generally safe to use:
89  #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
91 =head1 C<struct nbdkit_plugin>
93 All plugins must define and register one C<struct nbdkit_plugin>,
94 which contains the name of the plugin and pointers to callback
95 functions.
97  static struct nbdkit_plugin plugin = {
98    .name              = "myplugin",
99    .longname          = "My Plugin",
100    .description       = "This is my great plugin for nbdkit",
101    .open              = myplugin_open,
102    .get_size          = myplugin_get_size,
103    .pread             = myplugin_pread,
104    .pwrite            = myplugin_pwrite,
105    /* etc */
106  };
108  NBDKIT_REGISTER_PLUGIN(plugin)
110 The C<.name> field is the name of the plugin.
112 The callbacks are described below (see L</CALLBACKS>).  Only C<.name>,
113 C<.open>, C<.get_size> and C<.pread> are required.  All other
114 callbacks can be omitted.  However almost all plugins should have a
115 C<.close> callback.  Most real-world plugins will also want to declare
116 some of the other callbacks.
118 The nbdkit server calls the callbacks in the following order over the
119 lifetime of the plugin:
121 =over 4
123 =item C<.load>
125 is called once just after the plugin is loaded into memory.
127 =item C<.config> and C<.config_complete>
129 C<.config> is called zero or more times during command line parsing.
130 C<.config_complete> is called once after all configuration information
131 has been passed to the plugin (but not during C<nbdkit --dump-plugin>).
133 Both are called after loading the plugin but before any connections
134 are accepted.
136 =item C<.thread_model>
138 In normal operation, C<.thread_model> is called once after
139 C<.config_complete> has validated all configuration information, and
140 before any connections are accepted.  However, during C<nbdkit
141 --dump-plugin>, it is called after any C<.config> calls but without
142 C<.config_complete> (so a plugin which determines the results from a
143 script must be prepared for a missing script).
145 =item C<.preconnect>
147 Called when a TCP connection has been made to the server.  This
148 happens early, before NBD or TLS negotiation.
150 =item C<.open>
152 A new client has connected and finished the NBD handshake.  TLS
153 negotiation (if required) has been completed successfully.
155 =item C<.can_write>, C<.get_size> and other option negotiation callbacks
157 These are called during option negotiation with the client, but before
158 any data is served.  These callbacks may return different values
159 across different C<.open> calls, but within a single connection, they
160 are called at most once and cached by nbdkit for that connection.
162 =item C<.pread>, C<.pwrite> and other data serving callbacks
164 After option negotiation has finished, these may be called to serve
165 data.  Depending on the thread model chosen, they might be called in
166 parallel from multiple threads.  The data serving callbacks include a
167 flags argument; the results of the negotiation callbacks influence
168 whether particular flags will ever be passed to a data callback.
170 =item C<.close>
172 The client has disconnected.
174 =item C<.preconnect>, C<.open> ... C<.close>
176 The sequence C<.preconnect>, C<.open> ... C<.close> can be called
177 repeatedly over the lifetime of the plugin, and can be called in
178 parallel (depending on the thread model).
180 =item C<.unload>
182 is called once just before the plugin is unloaded from memory.
184 =back
186 =head1 FLAGS
188 The following flags are defined by nbdkit, and used in various data
189 serving callbacks as follows:
191 =over 4
193 =item C<NBDKIT_FLAG_MAY_TRIM>
195 This flag is used by the C<.zero> callback; there is no way to disable
196 this flag, although a plugin that does not support trims as a way to
197 write zeroes may ignore the flag without violating expected semantics.
199 =item C<NBDKIT_FLAG_FUA>
201 This flag represents Forced Unit Access semantics.  It is used by the
202 C<.pwrite>, C<.zero>, and C<.trim> callbacks to indicate that the
203 plugin must not return a result until the action has landed in
204 persistent storage.  This flag will not be sent to the plugin unless
205 C<.can_fua> is provided and returns C<NBDKIT_FUA_NATIVE>.
207 =back
209 The following defines are valid as successful return values for
210 C<.can_fua>:
212 =over 4
214 =item C<NBDKIT_FUA_NONE>
216 Forced Unit Access is not supported; the client must manually request
217 a flush after writes have completed.  The C<NBDKIT_FLAG_FUA> flag will
218 not be passed to the plugin's write callbacks.
220 =item C<NBDKIT_FUA_EMULATE>
222 The client may request Forced Unit Access, but it is implemented by
223 emulation, where nbdkit calls C<.flush> after a write operation; this
224 is semantically correct, but may hurt performance as it tends to flush
225 more data than just what the client requested.  The C<NBDKIT_FLAG_FUA>
226 flag will not be passed to the plugin's write callbacks.
228 =item C<NBDKIT_FUA_NATIVE>
230 The client may request Forced Unit Access, which results in the
231 C<NBDKIT_FLAG_FUA> flag being passed to the plugin's write callbacks
232 (C<.pwrite>, C<.trim>, and C<.zero>).  When the flag is set, these
233 callbacks must not return success until the client's request has
234 landed in persistent storage.
236 =back
238 The following defines are valid as successful return values for
239 C<.can_cache>:
241 =over 4
243 =item C<NBDKIT_CACHE_NONE>
245 The server does not advertise caching support, and rejects any
246 client-requested caching. Any C<.cache> callback is ignored.
248 =item C<NBDKIT_CACHE_EMULATE>
250 The nbdkit server advertises cache support to the client, where the
251 client may request that the server cache a region of the export to
252 potentially speed up future read and/or write operations on that
253 region. The nbdkit server implements the caching by calling C<.pread>
254 and ignoring the results. This option exists to ease the
255 implementation of a common form of caching; any C<.cache> callback is
256 ignored.
258 =item C<NBDKIT_CACHE_NATIVE>
260 The nbdkit server advertises cache support to the client, where the
261 client may request that the server cache a region of the export to
262 potentially speed up future read and/or write operations on that
263 region. The nbdkit server calls the C<.cache> callback to perform the
264 caching; if that callback is missing, the client's cache request
265 succeeds without doing anything.
267 =back
269 =head1 ERROR HANDLING
271 If there is an error in the plugin, the plugin should call
272 C<nbdkit_error> to report an error message; additionally, if the
273 callback is involved in serving data, the plugin should call
274 C<nbdkit_set_error> to influence the error code that will be sent to
275 the client.  These two functions can be called in either order.  Then,
276 the callback should return the appropriate error indication,
277 eg. C<NULL> or C<-1>.
279 If the call to C<nbdkit_set_error> is omitted while serving data, then
280 the global variable C<errno> may be used.  For plugins which have
281 C<.errno_is_preserved != 0> the core code will use C<errno>.  In
282 plugins written in non-C languages, we usually cannot trust that
283 C<errno> will not be overwritten when returning from that language to
284 C.  In that case, either the plugin must call C<nbdkit_set_error> or
285 hard-coded C<EIO> is used.
287 C<nbdkit_error> has the following prototype and works like
288 L<printf(3)>:
290  void nbdkit_error (const char *fs, ...);
291  void nbdkit_verror (const char *fs, va_list args);
293 For convenience, C<nbdkit_error> preserves the value of C<errno>, and
294 also supports the glibc extension of a single C<%m> in a format string
295 expanding to C<strerror(errno)>, even on platforms that don't support
296 that natively.
298 C<nbdkit_set_error> can be called at any time, but only has an impact
299 during callbacks for serving data, and only when the callback returns
300 an indication of failure.  It has the following prototype:
302  void nbdkit_set_error (int err);
304 =head1 CALLBACKS
306 =head2 C<.name>
308  const char *name;
310 This field (a string) is required, and B<must> contain only ASCII
311 alphanumeric characters and be unique amongst all plugins.
313 =head2 C<.version>
315  const char *version;
317 Plugins may optionally set a version string which is displayed in help
318 and debugging output.
320 =head2 C<.longname>
322  const char *longname;
324 An optional free text name of the plugin.  This field is used in error
325 messages.
327 =head2 C<.description>
329  const char *description;
331 An optional multi-line description of the plugin.
333 =head2 C<.load>
335  void load (void);
337 This is called once just after the plugin is loaded into memory.  You
338 can use this to perform any global initialization needed by the
339 plugin.
341 =head2 C<.unload>
343  void unload (void);
345 This may be called once just before the plugin is unloaded from
346 memory.  Note that it's not guaranteed that C<.unload> will always be
347 called (eg. the server might be killed or segfault), so you should try
348 to make the plugin as robust as possible by not requiring cleanup.
349 See also L</SHUTDOWN> below.
351 =head2 C<.dump_plugin>
353  void dump_plugin (void);
355 This optional callback is called when the
356 S<C<nbdkit plugin --dump-plugin>> command is used.  It should print
357 any additional informative C<key=value> fields to stdout as needed.
358 Prefixing the keys with the name of the plugin will avoid conflicts.
360 =head2 C<.config>
362  int config (const char *key, const char *value);
364 On the nbdkit command line, after the plugin filename, come an
365 optional list of C<key=value> arguments.  These are passed to the
366 plugin through this callback when the plugin is first loaded and
367 before any connections are accepted.
369 This callback may be called zero or more times.
371 Both C<key> and C<value> parameters will be non-NULL.  The strings are
372 owned by nbdkit but will remain valid for the lifetime of the plugin,
373 so the plugin does not need to copy them.
375 The key will be a non-empty string beginning with an ASCII alphabetic
376 character (C<A-Z> C<a-z>).  The rest of the key must contain only
377 ASCII alphanumeric plus period, underscore or dash characters (C<A-Z>
378 C<a-z> C<0-9> C<.> C<_> C<->).  The value may be an arbitrary string,
379 including an empty string.
381 The names of C<key>s accepted by plugins is up to the plugin, but you
382 should probably look at other plugins and follow the same conventions.
384 If the value is a relative path, then note that the server changes
385 directory when it starts up.  See L</FILENAMES AND PATHS> above.
387 If the C<.config> callback is not provided by the plugin, and the user
388 tries to specify any C<key=value> arguments, then nbdkit will exit
389 with an error.
391 If there is an error, C<.config> should call C<nbdkit_error> with an
392 error message and return C<-1>.
394 =head2 C<.magic_config_key>
396  const char *magic_config_key;
398 This optional string can be used to set a "magic" key used when
399 parsing plugin parameters.  It affects how "bare parameters" (those
400 which do not contain an C<=> character) are parsed on the command
401 line.
403 If C<magic_config_key != NULL> then any bare parameters are passed to
404 the C<.config> method as: S<C<config (magic_config_key, argv[i]);>>.
406 If C<magic_config_key> is not set then we behave as in nbdkit E<lt>
407 1.7: If the first parameter on the command line is bare then it is
408 passed to the C<.config> method as: S<C<config ("script", value);>>.
409 Any other bare parameters give errors.
411 =head2 C<.config_complete>
413  int config_complete (void);
415 This optional callback is called after all the configuration has been
416 passed to the plugin.  It is a good place to do checks, for example
417 that the user has passed the required parameters to the plugin.
419 If there is an error, C<.config_complete> should call C<nbdkit_error>
420 with an error message and return C<-1>.
422 =head2 C<.config_help>
424  const char *config_help;
426 This optional multi-line help message should summarize any
427 C<key=value> parameters that it takes.  It does I<not> need to repeat
428 what already appears in C<.description>.
430 If the plugin doesn't take any config parameters you should probably
431 omit this.
433 =head2 C<.thread_model>
435  int thread_model (void)
437 This optional callback is called after all the configuration has been
438 passed to the plugin.  It can be used to force a stricter thread model
439 based on configuration, compared to C<THREAD_MODEL>.  See L</THREADS>
440 below for details.  Attempts to request a looser (more parallel) model
441 are silently ignored.
443 If there is an error, C<.thread_model> should call C<nbdkit_error>
444 with an error message and return C<-1>.
446 =head2 C<.preconnect>
448  int preconnect (int readonly);
450 This optional callback is called when a TCP connection has been made
451 to the server.  This happens early, before NBD or TLS negotiation.  If
452 TLS authentication is required to access the server, then it has
453 B<not> been negotiated at this point.
455 For security reasons (to avoid denial of service attacks) this
456 callback should be written to be as fast and take as few resources as
457 possible.  If you use this callback, only use it to do basic access
458 control, such as checking C<nbdkit_peer_name> against a whitelist (see
459 L</PEER NAME> and L<nbdkit-ip-filter(1)>).  It may be better to do
460 access control outside the server, for example using TCP wrappers or a
461 firewall.
463 The C<readonly> flag informs the plugin that the server was started
464 with the I<-r> flag on the command line.
466 Returning C<0> will allow the connection to continue.  If there is an
467 error or you want to deny the connection, call C<nbdkit_error> with an
468 error message and return C<-1>.
470 =head2 C<.open>
472  void *open (int readonly);
474 This is called when a new client connects to the nbdkit server.  The
475 callback should allocate a handle and return it.  This handle
476 is passed back to other callbacks and could be freed in the C<.close>
477 callback.
479 Note that the handle is completely opaque to nbdkit, but it must not
480 be NULL.  If you don't need to use a handle, return
481 C<NBDKIT_HANDLE_NOT_NEEDED> which is a static non-NULL pointer.
483 The C<readonly> flag informs the plugin that the server was started
484 with the I<-r> flag on the command line which forces connections to be
485 read-only.  Note that the plugin may I<additionally> force the
486 connection to be readonly (even if this flag is false) by returning
487 false from the C<.can_write> callback.  So if your plugin can only
488 serve read-only, you can ignore this parameter.
490 This callback is called after the NBD handshake has completed, which
491 includes TLS authentication (if required).  If the plugin defines a
492 C<.preconnect> callback, then it must be called and return with
493 success before C<.open> is called.
495 If there is an error, C<.open> should call C<nbdkit_error> with an
496 error message and return C<NULL>.
498 =head2 C<.close>
500  void close (void *handle);
502 This is called when the client closes the connection.  It should clean
503 up any per-connection resources.
505 Note there is no way in the NBD protocol to communicate close errors
506 back to the client, for example if your plugin calls L<close(2)> and
507 you are checking for errors (as you should do).  Therefore the best
508 you can do is to log the error on the server.  Well-behaved NBD
509 clients I<should> try to flush the connection before it is closed and
510 check for errors, but obviously this is outside the scope of nbdkit.
512 =head2 C<.get_size>
514  int64_t get_size (void *handle);
516 This is called during the option negotiation phase of the protocol
517 to get the size (in bytes) of the block device being exported.
519 The returned size must be E<ge> 0.  If there is an error, C<.get_size>
520 should call C<nbdkit_error> with an error message and return C<-1>.
522 =head2 C<.can_write>
524  int can_write (void *handle);
526 This is called during the option negotiation phase to find out if the
527 handle supports writes.
529 If there is an error, C<.can_write> should call C<nbdkit_error> with
530 an error message and return C<-1>.
532 This callback is not required.  If omitted, then we return true iff a
533 C<.pwrite> callback has been defined.
535 =head2 C<.can_flush>
537  int can_flush (void *handle);
539 This is called during the option negotiation phase to find out if the
540 handle supports the flush-to-disk operation.
542 If there is an error, C<.can_flush> should call C<nbdkit_error> with
543 an error message and return C<-1>.
545 This callback is not required.  If omitted, then we return true iff a
546 C<.flush> callback has been defined.
548 =head2 C<.is_rotational>
550  int is_rotational (void *handle);
552 This is called during the option negotiation phase to find out if the
553 backing disk is a rotational medium (like a traditional hard disk) or
554 not (like an SSD).  If true, this may cause the client to reorder
555 requests to make them more efficient for a slow rotating disk.
557 If there is an error, C<.is_rotational> should call C<nbdkit_error>
558 with an error message and return C<-1>.
560 This callback is not required.  If omitted, then we return false.
562 =head2 C<.can_trim>
564  int can_trim (void *handle);
566 This is called during the option negotiation phase to find out if the
567 plugin supports the trim/discard operation for punching holes in the
568 backing storage.
570 If there is an error, C<.can_trim> should call C<nbdkit_error> with an
571 error message and return C<-1>.
573 This callback is not required.  If omitted, then we return true iff a
574 C<.trim> callback has been defined.
576 =head2 C<.can_zero>
578  int can_zero (void *handle);
580 This is called during the option negotiation phase to find out if the
581 plugin wants the C<.zero> callback to be utilized.  Support for
582 writing zeroes is still advertised to the client (unless the
583 L<nbdkit-nozero-filter(1)> is also used), so returning false merely
584 serves as a way to avoid complicating the C<.zero> callback to have to
585 fail with C<ENOTSUP> or C<EOPNOTSUPP> on the connections where it will
586 never be more efficient than using C<.pwrite> up front.
588 If there is an error, C<.can_zero> should call C<nbdkit_error> with an
589 error message and return C<-1>.
591 This callback is not required.  If omitted, then for a normal zero
592 request, nbdkit always tries C<.zero> first if it is present, and
593 gracefully falls back to C<.pwrite> if C<.zero> was absent or failed
594 with C<ENOTSUP> or C<EOPNOTSUPP>.
596 =head2 C<.can_fast_zero>
598  int can_fast_zero (void *handle);
600 This is called during the option negotiation phase to find out if the
601 plugin wants to advertise support for fast zero requests.  If this
602 support is not advertised, a client cannot attempt fast zero requests,
603 and has no way to tell if writing zeroes offers any speedups compared
604 to using C<.pwrite> (other than compressed network traffic).  If
605 support is advertised, then C<.zero> will have
606 C<NBDKIT_FLAG_FAST_ZERO> set when the client has requested a fast
607 zero, in which case the plugin must fail with C<ENOTSUP> or
608 C<EOPNOTSUPP> up front if the request would not offer any benefits
609 over C<.pwrite>.  Advertising support for fast zero requests does not
610 require that writing zeroes be fast, only that the result (whether
611 success or failure) is fast, so this should be advertised when
612 feasible.
614 If there is an error, C<.can_fast_zero> should call C<nbdkit_error>
615 with an error message and return C<-1>.
617 This callback is not required.  If omitted, then nbdkit returns true
618 if C<.zero> is absent or C<.can_zero> returns false (in those cases,
619 nbdkit fails all fast zero requests, as its fallback to C<.pwrite> is
620 not inherently faster), otherwise false (since it cannot be determined
621 in advance if the plugin's C<.zero> will properly honor the semantics
622 of C<NBDKIT_FLAG_FAST_ZERO>).
624 =head2 C<.can_extents>
626  int can_extents (void *handle);
628 This is called during the option negotiation phase to find out if the
629 plugin supports detecting allocated (non-sparse) regions of the disk
630 with the C<.extents> callback.
632 If there is an error, C<.can_extents> should call C<nbdkit_error> with
633 an error message and return C<-1>.
635 This callback is not required.  If omitted, then we return true iff a
636 C<.extents> callback has been defined.
638 =head2 C<.can_fua>
640  int can_fua (void *handle);
642 This is called during the option negotiation phase to find out if the
643 plugin supports the Forced Unit Access (FUA) flag on write, zero, and
644 trim requests.  If this returns C<NBDKIT_FUA_NONE>, FUA support is not
645 advertised to the client; if this returns C<NBDKIT_FUA_EMULATE>, the
646 C<.flush> callback must work (even if C<.can_flush> returns false),
647 and FUA support is emulated by calling C<.flush> after any write
648 operation; if this returns C<NBDKIT_FUA_NATIVE>, then the C<.pwrite>,
649 C<.zero>, and C<.trim> callbacks (if implemented) must handle the flag
650 C<NBDKIT_FLAG_FUA>, by not returning until that action has landed in
651 persistent storage.
653 If there is an error, C<.can_fua> should call C<nbdkit_error> with an
654 error message and return C<-1>.
656 This callback is not required unless a plugin wants to specifically
657 handle FUA requests.  If omitted, nbdkit checks whether C<.flush>
658 exists, and behaves as if this function returns C<NBDKIT_FUA_NONE> or
659 C<NBDKIT_FUA_EMULATE> as appropriate.
661 =head2 C<.can_multi_conn>
663  int can_multi_conn (void *handle);
665 This is called during the option negotiation phase to find out if the
666 plugin is prepared to handle multiple connections from a single
667 client.  If the plugin sets this to true then a client may try to open
668 multiple connections to the nbdkit server and spread requests across
669 all connections to maximize parallelism.  If the plugin sets it to
670 false (which is the default) then well-behaved clients should only
671 open a single connection, although we cannot control what clients do
672 in practice.
674 Specifically it means that either the plugin does not cache requests
675 at all.  Or if it does cache them then the effects of a C<.flush>
676 request or setting C<NBDKIT_FLAG_FUA> on a request must be visible
677 across all connections to the plugin before the plugin replies to that
678 request.
680 Properly working clients should send the same export name for each of
681 these connections.
683 If you use Linux L<nbd-client(8)> option S<I<-C num>> with
684 S<num E<gt> 1> then Linux checks this flag and will refuse to connect
685 if C<.can_multi_conn> is false.
687 If there is an error, C<.can_multi_conn> should call C<nbdkit_error>
688 with an error message and return C<-1>.
690 This callback is not required.  If omitted, then we return false.
692 =head2 C<.can_cache>
694  int can_cache (void *handle);
696 This is called during the option negotiation phase to find out if the
697 plugin supports a cache operation. The nature of the caching is
698 unspecified (including whether there are limits on how much can be
699 cached at once, and whether writes to a cached region have
700 write-through or write-back semantics), but the command exists to let
701 clients issue a hint to the server that they will be accessing that
702 region of the export.
704 If this returns C<NBDKIT_CACHE_NONE>, cache support is not advertised
705 to the client; if this returns C<NBDKIT_CACHE_EMULATE>, caching is
706 emulated by the server calling C<.pread> and ignoring the results; if
707 this returns C<NBDKIT_CACHE_NATIVE>, then the C<.cache> callback will
708 be used.  If there is an error, C<.can_cache> should call
709 C<nbdkit_error> with an error message and return C<-1>.
711 This callback is not required.  If omitted, then we return
712 C<NBDKIT_CACHE_NONE> if the C<.cache> callback is missing, or
713 C<NBDKIT_CACHE_NATIVE> if it is defined.
715 =head2 C<.pread>
717  int pread (void *handle, void *buf, uint32_t count, uint64_t offset,
718             uint32_t flags);
720 During the data serving phase, nbdkit calls this callback to read data
721 from the backing store.  C<count> bytes starting at C<offset> in the
722 backing store should be read and copied into C<buf>.  nbdkit takes
723 care of all bounds- and sanity-checking, so the plugin does not need
724 to worry about that.
726 The parameter C<flags> exists in case of future NBD protocol
727 extensions; at this time, it will be 0 on input.
729 The callback must read the whole C<count> bytes if it can.  The NBD
730 protocol doesn't allow partial reads (instead, these would be errors).
731 If the whole C<count> bytes was read, the callback should return C<0>
732 to indicate there was I<no> error.
734 If there is an error (including a short read which couldn't be
735 recovered from), C<.pread> should call C<nbdkit_error> with an error
736 message, and C<nbdkit_set_error> to record an appropriate error
737 (unless C<errno> is sufficient), then return C<-1>.
739 =head2 C<.pwrite>
741  int pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset,
742              uint32_t flags);
744 During the data serving phase, nbdkit calls this callback to write
745 data to the backing store.  C<count> bytes starting at C<offset> in
746 the backing store should be written using the data in C<buf>.  nbdkit
747 takes care of all bounds- and sanity-checking, so the plugin does not
748 need to worry about that.
750 This function will not be called if C<.can_write> returned false.  The
751 parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based on
752 the result of C<.can_fua>.
754 The callback must write the whole C<count> bytes if it can.  The NBD
755 protocol doesn't allow partial writes (instead, these would be
756 errors).  If the whole C<count> bytes was written successfully, the
757 callback should return C<0> to indicate there was I<no> error.
759 If there is an error (including a short write which couldn't be
760 recovered from), C<.pwrite> should call C<nbdkit_error> with an error
761 message, and C<nbdkit_set_error> to record an appropriate error
762 (unless C<errno> is sufficient), then return C<-1>.
764 =head2 C<.flush>
766  int flush (void *handle, uint32_t flags);
768 During the data serving phase, this callback is used to
769 L<fdatasync(2)> the backing store, ie. to ensure it has been
770 completely written to a permanent medium.  If that is not possible
771 then you can omit this callback.
773 This function will not be called directly by the client if
774 C<.can_flush> returned false; however, it may still be called by
775 nbdkit if C<.can_fua> returned C<NBDKIT_FUA_EMULATE>.  The parameter
776 C<flags> exists in case of future NBD protocol extensions; at this
777 time, it will be 0 on input.
779 If there is an error, C<.flush> should call C<nbdkit_error> with an
780 error message, and C<nbdkit_set_error> to record an appropriate error
781 (unless C<errno> is sufficient), then return C<-1>.
783 =head2 C<.trim>
785  int trim (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
787 During the data serving phase, this callback is used to "punch holes"
788 in the backing store.  If that is not possible then you can omit this
789 callback.
791 This function will not be called if C<.can_trim> returned false.  The
792 parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based on
793 the result of C<.can_fua>.
795 If there is an error, C<.trim> should call C<nbdkit_error> with an
796 error message, and C<nbdkit_set_error> to record an appropriate error
797 (unless C<errno> is sufficient), then return C<-1>.
799 =head2 C<.zero>
801  int zero (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
803 During the data serving phase, this callback is used to write C<count>
804 bytes of zeroes at C<offset> in the backing store.
806 This function will not be called if C<.can_zero> returned false.  On
807 input, the parameter C<flags> may include C<NBDKIT_FLAG_MAY_TRIM>
808 unconditionally, C<NBDKIT_FLAG_FUA> based on the result of
809 C<.can_fua>, and C<NBDKIT_FLAG_FAST_ZERO> based on the result of
810 C<.can_fast_zero>.
812 If C<NBDKIT_FLAG_MAY_TRIM> is requested, the operation can punch a
813 hole instead of writing actual zero bytes, but only if subsequent
814 reads from the hole read as zeroes.
816 If C<NBDKIT_FLAG_FAST_ZERO> is requested, the plugin must decide up
817 front if the implementation is likely to be faster than a
818 corresponding C<.pwrite>; if not, then it must immediately fail with
819 C<ENOTSUP> or C<EOPNOTSUPP> (whether by C<nbdkit_set_error> or
820 C<errno>) and preferably without modifying the exported image.  It is
821 acceptable to always fail a fast zero request (as a fast failure is
822 better than attempting the write only to find out after the fact that
823 it was not fast after all).  Note that on Linux, support for
824 C<ioctl(BLKZEROOUT)> is insufficient for determining whether a zero
825 request to a block device will be fast (because the kernel will
826 perform a slow fallback when needed).
828 The callback must write the whole C<count> bytes if it can.  The NBD
829 protocol doesn't allow partial writes (instead, these would be
830 errors).  If the whole C<count> bytes was written successfully, the
831 callback should return C<0> to indicate there was I<no> error.
833 If there is an error, C<.zero> should call C<nbdkit_error> with an
834 error message, and C<nbdkit_set_error> to record an appropriate error
835 (unless C<errno> is sufficient), then return C<-1>.
837 If this callback is omitted, or if it fails with C<ENOTSUP> or
838 C<EOPNOTSUPP> (whether by C<nbdkit_set_error> or C<errno>), then
839 C<.pwrite> will be used as an automatic fallback except when the
840 client requested a fast zero.
842 =head2 C<.extents>
844  int extents (void *handle, uint32_t count, uint64_t offset,
845               uint32_t flags, struct nbdkit_extents *extents);
847 During the data serving phase, this callback is used to detect
848 allocated, sparse and zeroed regions of the disk.
850 This function will not be called if C<.can_extents> returned false.
851 nbdkit's default behaviour in this case is to treat the whole virtual
852 disk as if it was allocated.  Also, this function will not be called
853 by a client that does not request structured replies (the I<--no-sr>
854 option of nbdkit can be used to test behavior when C<.extents> is
855 unavailable to the client).
857 The callback should detect and return the list of extents overlapping
858 the range C<[offset...offset+count-1]>.  The C<extents> parameter
859 points to an opaque object which the callback should fill in by
860 calling C<nbdkit_add_extent>.  See L</Extents list> below.
862 If there is an error, C<.extents> should call C<nbdkit_error> with an
863 error message, and C<nbdkit_set_error> to record an appropriate error
864 (unless C<errno> is sufficient), then return C<-1>.
866 =head3 Extents list
868 The plugin C<extents> callback is passed an opaque pointer C<struct
869 nbdkit_extents *extents>.  This structure represents a list of
870 L<filesystem extents|https://en.wikipedia.org/wiki/Extent_(file_systems)>
871 describing which areas of the disk are allocated, which are sparse
872 (“holes”), and, if supported, which are zeroes.
874 The C<extents> callback should scan the disk starting at C<offset> and
875 call C<nbdkit_add_extent> for each extent found.
877 Extents overlapping the range C<[offset...offset+count-1]> should be
878 returned if possible.  However nbdkit ignores extents E<lt> offset so
879 the plugin may, if it is easier to implement, return all extent
880 information for the whole disk.  The plugin may return extents beyond
881 the end of the range.  It may also return extent information for less
882 than the whole range, but it must return at least one extent
883 overlapping C<offset>.
885 The extents B<must> be added in ascending order, and B<must> be
886 contiguous.
888 The C<flags> parameter of the C<.extents> callback may contain the
889 flag C<NBDKIT_FLAG_REQ_ONE>.  This means that the client is only
890 requesting information about the extent overlapping C<offset>.  The
891 plugin may ignore this flag, or as an optimization it may return just
892 a single extent for C<offset>.
894  int nbdkit_add_extent (struct nbdkit_extents *extents,
895                         uint64_t offset, uint64_t length, uint32_t type);
897 Add an extent covering C<[offset...offset+length-1]> of one of
898 the following four types:
900 =over 4
902 =item C<type = 0>
904 A normal, allocated data extent.
906 =item C<type = NBDKIT_EXTENT_HOLE|NBDKIT_EXTENT_ZERO>
908 An unallocated extent, a.k.a. a “hole”, which reads back as zeroes.
909 This is the normal type of hole applicable to most disks.
911 =item C<type = NBDKIT_EXTENT_ZERO>
913 An allocated extent which is known to contain only zeroes.
915 =item C<type = NBDKIT_EXTENT_HOLE>
917 An unallocated extent (hole) which does not read back as zeroes.  Note
918 this should only be used in specialized circumstances such as when
919 writing a plugin for (or to emulate) certain SCSI drives which do not
920 guarantee that trimmed blocks read back as zeroes.
922 =back
924 C<nbdkit_extent_add> returns C<0> on success or C<-1> on failure.  On
925 failure C<nbdkit_error> and/or C<nbdkit_set_error> has already been
926 called.  C<errno> will be set to a suitable value.
928 =head2 C<.cache>
930  int cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
932 During the data serving phase, this callback is used to give the
933 plugin a hint that the client intends to make further accesses to the
934 given region of the export.  The nature of caching is not specified
935 further by the NBD specification (for example, a server may place
936 limits on how much may be cached at once, and there is no way to
937 control if writes to a cached area have write-through or write-back
938 semantics).  In fact, the cache command can always fail and still be
939 compliant, and success might not guarantee a performance gain.  If
940 this callback is omitted, then the results of C<.can_cache> determine
941 whether nbdkit will reject cache requests, treat them as instant
942 success, or emulate caching by calling C<.pread> over the same region
943 and ignoring the results.
945 This function will not be called if C<.can_cache> did not return
946 C<NBDKIT_CACHE_NATIVE>.  The parameter C<flags> exists in case of
947 future NBD protocol extensions; at this time, it will be 0 on input. A
948 plugin must fail this function if C<flags> includes an unrecognized
949 flag, as that may indicate a requirement that the plugin comply must
950 with a specific caching semantic.
952 If there is an error, C<.cache> should call C<nbdkit_error> with an
953 error message, and C<nbdkit_set_error> to record an appropriate error
954 (unless C<errno> is sufficient), then return C<-1>.
956 =head1 OTHER FIELDS
958 The plugin struct also contains an integer field used as a
959 boolean in C code, but unlikely to be exposed in other language
960 bindings:
962 =over 4
964 =item C<.errno_is_preserved>
966 This defaults to 0; if non-zero, nbdkit can reliably use the value of
967 C<errno> when a callback reports failure, rather than the plugin
968 having to call C<nbdkit_set_error>.
970 =back
972 =head1 THREADS
974 Each nbdkit plugin must declare its maximum thread safety model by
975 defining the C<THREAD_MODEL> macro.  (This macro is used by
976 C<NBDKIT_REGISTER_PLUGIN>).  Additionally, a plugin may implement the
977 C<.thread_model> callback, called right after C<.config_complete> to
978 make a runtime decision on which thread model to use.  The nbdkit
979 server chooses the most restrictive model between the plugin's
980 C<THREAD_MODEL>, the C<.thread_model> if present, any restrictions
981 requested by filters, and any limitations imposed by the system (for
982 example, a system without atomic C<FD_CLOEXEC> will serialize all
983 requests, so as to avoid nbdkit leaking a new file descriptor from one
984 thread into a child process created by another thread).
986 In C<nbdkit --dump-plugin PLUGIN> output, the C<max_thread_model> line
987 matches the C<THREAD_MODEL> macro, and the C<thread_model> line
988 matches what the system finally settled on after applying all
989 restrictions.
991 The possible settings for C<THREAD_MODEL> are defined below.
993 =over 4
995 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS>
997 Only a single handle can be open at any time, and all requests happen
998 from one thread.
1000 Note this means only one client can connect to the server at any time.
1001 If a second client tries to connect it will block waiting for the
1002 first client to close the connection.
1004 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>
1006 I<This is a safe default for most plugins>.
1008 Multiple handles can be open at the same time, but requests are
1009 serialized so that for the plugin as a whole only one
1010 open/read/write/close (etc) request will be in progress at any time.
1012 This is a useful setting if the library you are using is not
1013 thread-safe.  However performance may not be good.
1015 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS>
1017 Multiple handles can be open and multiple data requests can happen in
1018 parallel.  However only one request will happen per handle at a time
1019 (but requests on different handles might happen concurrently).
1021 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL>
1023 Multiple handles can be open and multiple data requests can happen in
1024 parallel (even on the same handle).  The server may reorder replies,
1025 answering a later request before an earlier one.
1027 All the libraries you use must be thread-safe and reentrant, and any
1028 code that creates a file descriptor should atomically set
1029 C<FD_CLOEXEC> if you do not want it accidentally leaked to another
1030 thread's child process.  You may also need to provide mutexes for
1031 fields in your connection handle.
1033 =back
1035 If none of the above thread models are suitable, then use
1036 C<NBDKIT_THREAD_MODEL_PARALLEL> and implement your own locking using
1037 C<pthread_mutex_t> etc.
1039 =head1 SHUTDOWN
1041 When nbdkit receives certain signals it will shut down (see
1042 L<nbdkit(1)/SIGNALS>).  The server will wait for any currently running
1043 plugin callbacks to finish and also call the C<.unload> callback
1044 before unloading the plugin.
1046 Note that it's not guaranteed this can always happen (eg. the server
1047 might be killed by C<SIGKILL> or segfault).
1049 =head1 PARSING COMMAND LINE PARAMETERS
1051 =head2 Parsing numbers
1053 There are several functions for parsing numbers.  These all deal
1054 correctly with overflow, out of range and parse errors, and you should
1055 use them instead of unsafe functions like L<sscanf(3)>, L<atoi(3)> and
1056 similar.
1058  int nbdkit_parse_int (const char *what, const char *str, int *r);
1059  int nbdkit_parse_unsigned (const char *what,
1060                             const char *str, unsigned *r);
1061  int nbdkit_parse_int8_t (const char *what,
1062                           const char *str, int8_t *r);
1063  int nbdkit_parse_uint8_t (const char *what,
1064                            const char *str, uint8_t *r);
1065  int nbdkit_parse_int16_t (const char *what,
1066                            const char *str, int16_t *r);
1067  int nbdkit_parse_uint16_t (const char *what,
1068                             const char *str, uint16_t *r);
1069  int nbdkit_parse_int32_t (const char *what,
1070                            const char *str, int32_t *r);
1071  int nbdkit_parse_uint32_t (const char *what,
1072                             const char *str, uint32_t *r);
1073  int nbdkit_parse_int64_t (const char *what,
1074                            const char *str, int64_t *r);
1075  int nbdkit_parse_uint64_t (const char *what,
1076                             const char *str, uint64_t *r);
1078 Parse string C<str> into an integer of various types.  These functions
1079 parse a decimal, hexadecimal (C<"0x...">) or octal (C<"0...">) number.
1081 On success the functions return C<0> and set C<*r> to the parsed value
1082 (unless C<*r == NULL> in which case the result is discarded).  On
1083 error, C<nbdkit_error> is called and the functions return C<-1>.  On
1084 error C<*r> is always unchanged.
1086 The C<what> parameter is printed in error messages to provide context.
1087 It should usually be a short descriptive string of what you are trying
1088 to parse, eg:
1090  if (nbdkit_parse_int ("random seed", argv[1], &seed) == -1)
1091    return -1;
1093 might print an error:
1095  random seed: could not parse number: "lalala"
1097 =head2 Parsing sizes
1099 Use the C<nbdkit_parse_size> utility function to parse human-readable
1100 size strings such as "100M" into the size in bytes.
1102  int64_t nbdkit_parse_size (const char *str);
1104 C<str> can be a string in a number of common formats.  The function
1105 returns the size in bytes.  If there was an error, it returns C<-1>.
1107 =head2 Parsing booleans
1109 Use the C<nbdkit_parse_bool> utility function to parse human-readable
1110 strings such as "on" into a boolean value.
1112  int nbdkit_parse_bool (const char *str);
1114 C<str> can be a string containing a case-insensitive form of various
1115 common toggle values.  The function returns 0 or 1 if the parse was
1116 successful.  If there was an error, it returns C<-1>.
1118 =head2 Reading passwords
1120 The C<nbdkit_read_password> utility function can be used to read
1121 passwords from config parameters:
1123  int nbdkit_read_password (const char *value, char **password);
1125 For example:
1127  char *password = NULL;
1129  static int
1130  myplugin_config (const char *key, const char *value)
1132    ..
1133    if (strcmp (key, "password") == 0) {
1134      free (password);
1135      if (nbdkit_read_password (value, &password) == -1)
1136        return -1;
1137    }
1138    ..
1141 The C<password> result string is allocated by malloc, and so you may
1142 need to free it.
1144 This function recognizes several password formats.  A password may be
1145 used directly on the command line, eg:
1147  nbdkit myplugin password=mostsecret
1149 But more securely this function can also read a password
1150 interactively:
1152  nbdkit myplugin password=-
1154 or from a file:
1156  nbdkit myplugin password=+/tmp/secret
1158 or from a file descriptor inherited by nbdkit:
1160  nbdkit myplugin password=-99
1162 (If the password begins with a C<-> or C<+> character then it must be
1163 passed in a file).
1165 =head1 FILENAMES AND PATHS
1167 The server usually (not always) changes directory to C</> before it
1168 starts serving connections.  This means that any relative paths passed
1169 during configuration will not work when the server is running
1170 (example: S<C<nbdkit plugin.so disk.img>>).
1172 To avoid problems, prepend relative paths with the current directory
1173 before storing them in the handle.  Or open files and store the file
1174 descriptor.
1176 =head2 C<nbdkit_absolute_path>
1178  char *nbdkit_absolute_path (const char *filename);
1180 The utility function C<nbdkit_absolute_path> converts any path to an
1181 absolute path: if it is relative, then all this function does is
1182 prepend the current working directory to the path, with no extra
1183 checks.
1185 Note that this function works I<only> when used in the C<.config>, and
1186 C<.config_complete> callbacks.
1188 If conversion was not possible, this calls C<nbdkit_error> and returns
1189 C<NULL>.  Note that this function does not check that the file exists.
1191 The returned string must be freed by the caller.
1193 =head2 C<nbdkit_realpath>
1195  char *nbdkit_realpath (const char *filename);
1197 The utility function C<nbdkit_realpath> converts any path to an
1198 absolute path, resolving symlinks.  Under the hood it uses the
1199 C<realpath> function, and thus it fails if the path does not exist,
1200 or it is not possible to access to any of the components of the path.
1202 Note that this function works I<only> when used in the C<.config>, and
1203 C<.config_complete> callbacks.
1205 If the path resolution was not possible, this calls C<nbdkit_error>
1206 and returns C<NULL>.
1208 The returned string must be freed by the caller.
1210 =head2 umask
1212 All plugins will see a L<umask(2)> of C<0022>.
1214 =head1 SLEEPING
1216 A plugin that needs to sleep may call L<sleep(2)>, L<nanosleep(2)> and
1217 similar.  However that can cause nbdkit to delay excessively when
1218 shutting down (since it must wait for any plugin or filter which is
1219 sleeping).  To avoid this there is a special wrapper around nanosleep
1220 which plugins and filters should use instead.
1222 =head2 C<nbdkit_nanosleep>
1224  int nbdkit_nanosleep (unsigned sec, unsigned nsec);
1226 The utility function C<nbdkit_nanosleep> suspends the current thread,
1227 and returns 0 if it slept at least as many seconds and nanoseconds as
1228 requested, or -1 after calling C<nbdkit_error> if there is no point in
1229 continuing the current command.  Attempts to sleep more than
1230 C<INT_MAX> seconds are treated as an error.
1232 =head1 EXPORT NAME
1234 If the client negotiated an NBD export name with nbdkit then plugins
1235 may read this from any connected callbacks.  Nbdkit's normal behaviour
1236 is to accept any export name passed by the client, log it in debug
1237 output, but otherwise ignore it.  By using C<nbdkit_export_name>
1238 plugins may choose to filter by export name or serve different
1239 content.
1241 =head2 C<nbdkit_export_name>
1243  const char *nbdkit_export_name (void);
1245 Return the optional NBD export name if one was negotiated with the
1246 current client (this uses thread-local magic so no parameter is
1247 required).  The returned string is only valid while the client is
1248 connected, so if you need to store it in the plugin you must copy it.
1250 The export name is a free-form text string, it is not necessarily a
1251 path or filename and it does not need to begin with a C<'/'>
1252 character.  The NBD protocol describes the empty string (C<"">) as a
1253 representing a "default export" or to be used in cases where the
1254 export name does not make sense.  B<The export name is untrusted
1255 client data, be cautious when parsing it.>
1257 On error, C<nbdkit_error> is called and the call returns C<NULL>.
1259 =head1 PEER NAME
1261 It is possible to get the address of the client when you are running
1262 in any connected callback.
1264 =head2 C<nbdkit_peer_name>
1266  int nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen);
1268 Return the peer (client) address, if available.  The C<addr> and
1269 C<addrlen> parameters behave like L<getpeername(2)>.  In particular
1270 you must initialize C<addrlen> with the size of the buffer pointed to
1271 by C<addr>, and if C<addr> is not large enough then the address will
1272 be truncated.
1274 In some cases this is not available or the address returned will be
1275 meaningless (eg. if there is a proxy between the client and nbdkit).
1276 This call uses thread-local magic so no parameter is required to
1277 specify the current connection.
1279 On success this returns C<0>.  On error, C<nbdkit_error> is called and
1280 this call returns C<-1>.
1282 =head1 DEBUGGING
1284 Run the server with I<-f> and I<-v> options so it doesn't fork and you
1285 can see debugging information:
1287  nbdkit -fv ./myplugin.so [key=value [key=value [...]]]
1289 To print debugging information from within the plugin, call
1290 C<nbdkit_debug>, which has the following prototype and works like
1291 L<printf(3)>:
1293  void nbdkit_debug (const char *fs, ...);
1294  void nbdkit_vdebug (const char *fs, va_list args);
1296 For convenience, C<nbdkit_debug> preserves the value of C<errno>, and
1297 also supports the glibc extension of a single C<%m> in a format string
1298 expanding to C<strerror(errno)>, even on platforms that don't support
1299 that natively. Note that C<nbdkit_debug> only prints things when the
1300 server is in verbose mode (I<-v> option).
1302 =head2 Debug Flags
1304 The I<-v> option switches general debugging on or off, and this
1305 debugging should be used for messages which are useful for all users
1306 of your plugin.
1308 In cases where you want to enable specific extra debugging to track
1309 down bugs in plugins or filters — mainly for use by the plugin/filter
1310 developers themselves — you can define Debug Flags.  These are global
1311 ints called C<myplugin_debug_*>:
1313  int myplugin_debug_foo;
1314  int myplugin_debug_bar;
1315  ...
1316  if (myplugin_debug_foo) {
1317    nbdkit_debug ("lots of extra debugging about foo: ...");
1320 Debug Flags can be controlled on the command line using the I<-D> (or
1321 I<--debug>) option:
1323  nbdkit -f -v -D myplugin.foo=1 -D myplugin.bar=2 myplugin [...]
1325 Note C<myplugin> is the name passed to C<.name> in the C<struct
1326 nbdkit_plugin>.
1328 You should only use this feature for debug settings.  For general
1329 settings use ordinary plugin parameters.  Debug Flags can only be C
1330 ints.  They are not supported by non-C language plugins.
1332 For convenience C<'.'> characters are replaced with C<'_'> characters
1333 in the variable name, so both of these parameters:
1335  -D myplugin.foo_bar=1
1336  -D myplugin.foo.bar=1
1338 correspond to the plugin variable C<myplugin_debug_foo_bar>.
1340 =head1 INSTALLING THE PLUGIN
1342 The plugin is a C<*.so> file and possibly a manual page.  You can of
1343 course install the plugin C<*.so> file wherever you want, and users
1344 will be able to use it by running:
1346  nbdkit /path/to/plugin.so [args]
1348 However B<if> the shared library has a name of the form
1349 C<nbdkit-I<name>-plugin.so> B<and if> the library is installed in the
1350 C<$plugindir> directory, then users can be run it by only typing:
1352  nbdkit name [args]
1354 The location of the C<$plugindir> directory is set when nbdkit is
1355 compiled and can be found by doing:
1357  nbdkit --dump-config
1359 If using the pkg-config/pkgconf system then you can also find the
1360 plugin directory at compile time by doing:
1362  pkgconf nbdkit --variable=plugindir
1364 =head1 PKG-CONFIG/PKGCONF
1366 nbdkit provides a pkg-config/pkgconf file called C<nbdkit.pc> which
1367 should be installed on the correct path when the nbdkit plugin
1368 development environment is installed.  You can use this in autoconf
1369 F<configure.ac> scripts to test for the development environment:
1371  PKG_CHECK_MODULES([NBDKIT], [nbdkit >= 1.2.3])
1373 The above will fail unless nbdkit E<ge> 1.2.3 and the header file is
1374 installed, and will set C<NBDKIT_CFLAGS> and C<NBDKIT_LIBS>
1375 appropriately for compiling plugins.
1377 You can also run pkg-config/pkgconf directly, for example:
1379  if ! pkgconf nbdkit --exists; then
1380    echo "you must install the nbdkit plugin development environment"
1381    exit 1
1382  fi
1384 You can also substitute the plugindir variable by doing:
1386  PKG_CHECK_VAR([NBDKIT_PLUGINDIR], [nbdkit], [plugindir])
1388 which defines C<$(NBDKIT_PLUGINDIR)> in automake-generated Makefiles.
1390 =head1 WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES
1392 You can also write nbdkit plugins in Lua, OCaml, Perl, Python, Ruby,
1393 Rust, shell script or Tcl.  Other programming languages may be offered
1394 in future.
1396 For more information see:
1397 __LANG_PLUGIN_LINKS__.
1399 Plugins written in scripting languages may also be installed in
1400 C<$plugindir>.  These must be called C<nbdkit-I<name>-plugin> without
1401 any extension.  They must be executable, and they must use the shebang
1402 header (see L<nbdkit(1)/Shebang scripts>).  For example a plugin
1403 written in Perl called C<foo.pl> might be installed like this:
1405  $ head -1 foo.pl
1406  #!/usr/sbin/nbdkit perl
1408  $ sudo install -m 0755 foo.pl $plugindir/nbdkit-foo-plugin
1410 and then users will be able to run it like this:
1412  $ nbdkit foo [args ...]
1414 =head1 SEE ALSO
1416 L<nbdkit(1)>,
1417 L<nbdkit-nozero-filter(3)>,
1418 L<nbdkit-filter(3)>.
1420 Standard plugins provided by nbdkit:
1422 __PLUGIN_LINKS__.
1424 =head1 AUTHORS
1426 Eric Blake
1428 Richard W.M. Jones
1430 Pino Toscano
1432 =head1 COPYRIGHT
1434 Copyright (C) 2013-2018 Red Hat Inc.