1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
// SPDX-License-Identifier: (MIT OR Apache-2.0)
#include <getopt.h>
#include <signal.h>
#include "util.h"
static void (*cleanup)(void);
static void signal_handler(int signo)
{
cleanup();
}
/* Call fn() at exit or when the process aborts */
void register_cleanup(void (*fn)(void))
{
struct sigaction sigact = {
.sa_handler = signal_handler,
.sa_flags = SA_RESETHAND,
};
cleanup = fn;
sigemptyset(&sigact.sa_mask);
sigaction(SIGABRT, &sigact, NULL);
atexit(fn);
}
/* Like mkstemp(3) except it also sets the file size */
int create_file(char *namebuf, off_t length)
{
int fd = mkstemp(namebuf);
assert(fd >= 0);
assert(ftruncate(fd, length) == 0);
return fd;
}
static char *cleanup_filename;
static void cleanup_file(void)
{
if (cleanup_filename) {
unlink(cleanup_filename);
free(cleanup_filename);
cleanup_filename = NULL;
}
}
void create(struct blkio **b, struct test_opts *opts, char *filename,
size_t file_size)
{
const char *path;
ok(blkio_create(opts->driver, b));
assert(*b);
if (opts->path) {
path = opts->path;
} else {
int fd;
cleanup_filename = strdup(filename);
assert(cleanup_filename);
register_cleanup(cleanup_file);
fd = create_file(cleanup_filename, file_size);
assert(close(fd) == 0);
/* mkstemp manipulates the string, so let's copy it back */
strcpy(filename, cleanup_filename);
path = filename;
}
ok(blkio_set_str(*b, "path", path));
}
void create_and_connect(struct blkio **b, struct test_opts *opts,
char *filename, size_t file_size)
{
create(b, opts, filename, file_size);
ok(blkio_connect(*b));
}
static const char optstring[] = "d:p:";
static const struct option longopts[] = {
{
.name = "driver",
.has_arg = required_argument,
.val = 'd',
},
{
.name = "path",
.has_arg = required_argument,
.val = 'p',
},
{
.name = "help",
.has_arg = no_argument,
.val = '?',
},
{},
};
static void usage(char *exe_name)
{
fprintf(stderr, "Usage: %s [--help] --driver=<name>\n"
"\n"
"Options:\n"
" --help Print this help message\n"
" -d | --driver <name> Driver name to use in the test\n"
" -p | --path <path> File/device path to use in the test\n"
"",
exe_name);
exit(EXIT_FAILURE);
}
void parse_generic_opts(struct test_opts *opts, int argc, char **argv)
{
opts->driver = NULL;
opts->path = NULL;
for (;;) {
int opt = getopt_long(argc, argv, optstring, longopts, NULL);
if (opt == -1)
break;
switch (opt) {
case 'd':
opts->driver = optarg;
break;
case 'p':
opts->path = optarg;
break;
case '?':
default:
usage(argv[0]);
}
}
if (!opts->driver) {
usage(argv[0]);
}
}
bool driver_is_io_uring(char *driver) {
return strcmp(driver, "io_uring") == 0;
}
bool driver_is_virtio_blk(char *driver) {
char *virtio_blk = "virtio-blk-";
return strncmp(driver, virtio_blk, strlen(virtio_blk)) == 0;
}
bool driver_is_virtio_blk_vhost_vdpa(char *driver) {
return strcmp(driver, "virtio-blk-vhost-vdpa") == 0;
}
ssize_t pread_full(int fd, void *buf, size_t count, off_t offset) {
size_t n = 0;
while (n < count) {
ssize_t res;
do {
res = pread(fd, buf + n, count - n, offset + n);
} while (res == -1 && errno == EINTR);
if (res < 0) {
return res;
}
if (res == 0) {
break; // reached EOF
}
n += res;
}
return n;
}
ssize_t pwrite_full(int fd, const void *buf, size_t count, off_t offset) {
size_t n = 0;
while (n < count) {
ssize_t res;
do {
res = pwrite(fd, buf + n, count - n, offset + n);
} while (res == -1 && errno == EINTR);
if (res < 0) {
return res;
}
assert(res != 0);
n += res;
}
return n;
}
|