From c2e4c3c6e9567e1439ef9adb3de070c538992d95 Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 26 Jun 2023 17:45:44 +0800 Subject: [PATCH] Use the argp library to parse the command line arguments passed to the program --- observation/src/biosnoop/biosnoop.c | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 observation/src/biosnoop/biosnoop.c diff --git a/observation/src/biosnoop/biosnoop.c b/observation/src/biosnoop/biosnoop.c new file mode 100644 index 00000000..d8239da0 --- /dev/null +++ b/observation/src/biosnoop/biosnoop.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) +#include "commons.h" +#include "blk_types.h" +#include "biosnoop.h" +#include "biosnoop.skel.h" +#include "trace_helpers.h" + +static volatile sig_atomic_t exiting; + +static struct env { + char *disk; + int duration; + bool timestamp; + bool queued; + bool verbose; + char *cgroupspath; + bool cg; +} env; + +static error_t parse_arg(int key, char *arg, struct argp_state *state) +{ + static int pos_args; + + switch (key) { + case 'h': + argp_state_help(state, stderr, ARGP_HELP_STD_HELP); + break; + case 'v': + env.verbose = true; + break; + case 'Q': + env.queued = true; + break; + case 'c': + env.cg = true; + env.cgroupspath = arg; + break; + case 'd': + env.disk = arg; + if (strlen(arg) + 1 > DISK_NAME_LEN) { + warning("Invalid disk name %s: too long\n", arg); + argp_usage(state); + } + break; + case ARGP_KEY_ARG: + errno = 0; + if (pos_args == 0) { + env.duration = strtoll(arg, NULL, 10); + if (errno || env.duration <= 0) { + warning("Invalid delay (in us): %s\n", arg); + argp_usage(state); + } + } else { + warning("Unrecognized positional argument: %s\n", arg); + argp_usage(state); + } + pos_args++; + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} \ No newline at end of file -- Gitee