Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-disconnect-plugin.c
blob47a6aa154decb9b5217b890ddddffb2107943530
1 /* nbdkit
2 * Copyright Red Hat
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 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdbool.h>
40 #include <nbdkit-plugin.h>
42 static void
43 disconnect_unload (void)
45 nbdkit_debug ("clean disconnect");
48 static void *
49 disconnect_open (int readonly)
51 return NBDKIT_HANDLE_NOT_NEEDED;
54 static int64_t
55 disconnect_get_size (void *handle)
57 return 1024*1024;
60 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
62 /* Reads are delayed to show effect of disconnect on in-flight commands */
63 static int
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");
69 return 0;
72 /* Writing causes a disconnect; export name determines severity. */
73 static int
74 disconnect_pwrite (void *handle, const void *buf, uint32_t count,
75 uint64_t offset)
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 */
82 return 0;
85 static struct nbdkit_plugin plugin = {
86 .name = "disconnect",
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)