4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
40 #include <nbdkit-plugin.h>
43 disconnect_unload (void)
45 nbdkit_debug ("clean disconnect");
49 disconnect_open (int readonly
)
51 return NBDKIT_HANDLE_NOT_NEEDED
;
55 disconnect_get_size (void *handle
)
60 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
62 /* Reads are delayed to show effect of disconnect on in-flight commands */
64 disconnect_pread (void *handle
, void *buf
, uint32_t count
, uint64_t offset
)
66 memset (buf
, 0, count
);
67 if (nbdkit_nanosleep (2, 0) == -1)
68 nbdkit_debug ("read delay ended early, returning success anyway");
72 /* Writing causes a disconnect; export name determines severity. */
74 disconnect_pwrite (void *handle
, const void *buf
, uint32_t count
,
77 const char *name
= nbdkit_export_name ();
78 bool hard
= name
&& *name
;
79 nbdkit_debug ("%s disconnect triggered!", hard
? "hard" : "soft");
80 nbdkit_disconnect (hard
);
81 /* Despite the disconnect, we still claim the write succeeded */
85 static struct nbdkit_plugin plugin
= {
87 .version
= PACKAGE_VERSION
,
88 .unload
= disconnect_unload
,
89 .open
= disconnect_open
,
90 .get_size
= disconnect_get_size
,
91 .pread
= disconnect_pread
,
92 .pwrite
= disconnect_pwrite
,
95 NBDKIT_REGISTER_PLUGIN (plugin
)