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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
Copyright (c) 2020 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
#include "tester.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
static void *
mem_alloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
error(ENOMEM, "Failed to allocate memory (%zu bytes)", size);
}
return ptr;
}
static void
mem_free(void *ptr)
{
free(ptr);
}
static bool
buffer_create(context_t *ctx, size_t size)
{
ctx->buffer.base = mem_alloc(size);
if (ctx->buffer.base == NULL) {
return false;
}
ctx->buffer.size = size;
ctx->buffer.len = 0;
ctx->buffer.pos = 0;
return true;
}
static void
buffer_destroy(context_t *ctx)
{
mem_free(ctx->buffer.base);
ctx->buffer.size = 0;
ctx->buffer.len = 0;
}
static int32_t
buffer_get(context_t *ctx)
{
ssize_t len;
if (ctx->buffer.pos >= ctx->buffer.len) {
len = read(0, ctx->buffer.base, ctx->buffer.size);
if (len < 0) {
return error(errno, "read() failed");
}
if (len == 0) {
return 0;
}
ctx->buffer.len = len;
ctx->buffer.pos = 0;
}
return ctx->buffer.base[ctx->buffer.pos++];
}
static int32_t
str_skip_spaces(context_t *ctx, int32_t current)
{
while ((current > 0) && (current != '\n') && isspace(current)) {
current = buffer_get(ctx);
}
return current;
}
static int32_t
str_token(context_t *ctx, char *buffer, uint32_t size, int32_t current)
{
uint32_t len;
current = str_skip_spaces(ctx, current);
len = 0;
while ((size > 0) && (current > 0) && (current != '\n') &&
!isspace(current)) {
len++;
*buffer++ = current;
size--;
current = buffer_get(ctx);
}
if (len == 0) {
return error(ENODATA, "Expecting a token");
}
if (size == 0) {
return error(ENOBUFS, "Token too long");
}
*buffer = 0;
return current;
}
static int32_t
str_number(context_t *ctx, uint64_t min, uint64_t max, uint64_t *value,
int32_t current)
{
char text[32], *ptr;
uint64_t num;
current = str_token(ctx, text, sizeof(text), current);
if (current > 0) {
num = strtoul(text, &ptr, 0);
if ((*ptr != 0) || (num < min) || (num > max)) {
return error(ERANGE, "Invalid number");
}
*value = num;
}
return current;
}
static int32_t
str_eol(context_t *ctx, int32_t current)
{
current = str_skip_spaces(ctx, current);
if (current != '\n') {
return error(EINVAL, "Expecting end of command");
}
return current;
}
static void
str_skip(context_t *ctx, int32_t current)
{
while ((current > 0) && (current != '\n')) {
current = buffer_get(ctx);
}
}
static int32_t
cmd_parse_obj(context_t *ctx, arg_t *arg, int32_t current)
{
obj_t *obj;
uint64_t id;
current = str_number(ctx, 0, ctx->obj_count, &id, current);
if (current <= 0) {
return current;
}
obj = &ctx->objs[id];
if (obj->type != arg->obj.type) {
if (obj->type != OBJ_TYPE_NONE) {
return error(EBUSY, "Object is in use");
}
return error(ENOENT, "Object is not defined");
}
arg->obj.ref = obj;
return current;
}
static int32_t
cmd_parse_num(context_t *ctx, arg_t *arg, int32_t current)
{
return str_number(ctx, arg->num.min, arg->num.max, &arg->num.value,
current);
}
static int32_t
cmd_parse_str(context_t *ctx, arg_t *arg, int32_t current)
{
return str_token(ctx, arg->str.data, arg->str.size, current);
}
static int32_t
cmd_parse_args(context_t *ctx, command_t *cmd, int32_t current)
{
arg_t *arg;
for (arg = cmd->args; arg->type != ARG_TYPE_NONE; arg++) {
switch (arg->type) {
case ARG_TYPE_OBJ:
current = cmd_parse_obj(ctx, arg, current);
break;
case ARG_TYPE_NUM:
current = cmd_parse_num(ctx, arg, current);
break;
case ARG_TYPE_STR:
current = cmd_parse_str(ctx, arg, current);
break;
default:
return error(EINVAL, "Unknown argument type");
}
}
if (current < 0) {
return current;
}
current = str_eol(ctx, current);
if (current <= 0) {
return error(EINVAL, "Syntax error");
}
return cmd->handler(ctx, cmd);
}
static int32_t
cmd_parse(context_t *ctx, command_t *cmds)
{
char text[32];
command_t *cmd;
int32_t current;
cmd = cmds;
do {
current = str_token(ctx, text, sizeof(text), buffer_get(ctx));
if (current <= 0) {
return current;
}
while (cmd->name != NULL) {
if (strcmp(cmd->name, text) == 0) {
if (cmd->handler != NULL) {
return cmd_parse_args(ctx, cmd, current);
}
cmd = cmd->cmds;
break;
}
cmd++;
}
} while (cmd->name != NULL);
str_skip(ctx, current);
return error(ENOTSUP, "Unknown command");
}
static void
cmd_fini(context_t *ctx, command_t *cmds)
{
command_t *cmd;
arg_t *arg;
for (cmd = cmds; cmd->name != NULL; cmd++) {
if (cmd->handler == NULL) {
cmd_fini(ctx, cmd->cmds);
} else {
for (arg = cmd->args; arg->type != ARG_TYPE_NONE; arg++) {
switch (arg->type) {
case ARG_TYPE_STR:
mem_free(arg->str.data);
arg->str.data = NULL;
break;
default:
break;
}
}
}
}
}
static bool
cmd_init(context_t *ctx, command_t *cmds)
{
command_t *cmd;
arg_t *arg;
for (cmd = cmds; cmd->name != NULL; cmd++) {
if (cmd->handler == NULL) {
if (!cmd_init(ctx, cmd->cmds)) {
return false;
}
} else {
for (arg = cmd->args; arg->type != ARG_TYPE_NONE; arg++) {
switch (arg->type) {
case ARG_TYPE_STR:
arg->str.data = mem_alloc(arg->str.size);
if (arg->str.data == NULL) {
return false;
}
break;
default:
break;
}
}
}
}
return true;
}
static bool
objs_create(context_t *ctx, uint32_t count)
{
uint32_t i;
ctx->objs = mem_alloc(sizeof(obj_t) * count);
if (ctx->objs == NULL) {
return false;
}
ctx->obj_count = count;
for (i = 0; i < count; i++) {
ctx->objs[i].type = OBJ_TYPE_NONE;
}
return true;
}
static int32_t
objs_destroy(context_t *ctx)
{
uint32_t i;
int32_t err;
err = 0;
for (i = 0; i < ctx->obj_count; i++) {
if (ctx->objs[i].type != OBJ_TYPE_NONE) {
err = error(ENOTEMPTY, "Objects not destroyed");
break;
}
}
mem_free(ctx->objs);
ctx->objs = NULL;
ctx->obj_count = 0;
return err;
}
static context_t *
init(size_t size, uint32_t objs, command_t *cmds)
{
context_t *ctx;
ctx = mem_alloc(sizeof(context_t));
if (ctx == NULL) {
goto failed;
}
if (!buffer_create(ctx, size)) {
goto failed_ctx;
}
if (!objs_create(ctx, objs)) {
goto failed_buffer;
}
if (!cmd_init(ctx, cmds)) {
goto failed_objs;
}
ctx->active = true;
return ctx;
failed_objs:
cmd_fini(ctx, cmds);
objs_destroy(ctx);
failed_buffer:
buffer_destroy(ctx);
failed_ctx:
mem_free(ctx);
failed:
return NULL;
}
static int32_t
fini(context_t *ctx, command_t *cmds)
{
int32_t ret;
cmd_fini(ctx, cmds);
buffer_destroy(ctx);
ret = objs_destroy(ctx);
ctx->active = false;
return ret;
}
static int32_t
exec_quit(context_t *ctx, command_t *cmd)
{
ctx->active = false;
return 0;
}
static command_t commands[] = {{"fd", NULL, CMD_SUB(fd_commands)},
{"quit", exec_quit, CMD_ARGS()},
CMD_END};
int32_t
main(int32_t argc, char *argv[])
{
context_t *ctx;
int32_t res;
ctx = init(1024, 16, commands);
if (ctx == NULL) {
return 1;
}
do {
res = cmd_parse(ctx, commands);
if (res < 0) {
out_err(-res);
}
} while (ctx->active);
res = fini(ctx, commands);
if (res >= 0) {
out_ok();
return 0;
}
out_err(-res);
return 1;
}
|