diff options
author | Alice Frosi <afrosi@redhat.com> | 2023-07-17 13:05:18 +0200 |
---|---|---|
committer | Alice Frosi <afrosi@redhat.com> | 2023-08-24 15:33:17 +0200 |
commit | 3f2585770384586977483ec4c4b38fe4c3e5fc45 (patch) | |
tree | 46846644b6563b91dca5c45ebe30ef243089c075 | |
parent | 89ccb73a22cb0130ebc354b3b1f6c51a7c72d89d (diff) | |
download | seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar.gz seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar.bz2 seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar.lz seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar.xz seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.tar.zst seitan-3f2585770384586977483ec4c4b38fe4c3e5fc45.zip |
cooker: add flags with getopts
-rw-r--r-- | cooker/gluten.h | 7 | ||||
-rw-r--r-- | cooker/main.c | 87 |
2 files changed, 88 insertions, 6 deletions
diff --git a/cooker/gluten.h b/cooker/gluten.h index e0ea54d..4659583 100644 --- a/cooker/gluten.h +++ b/cooker/gluten.h @@ -9,6 +9,11 @@ #define COOKER #include <gluten.h> +enum scmp_mode { + SCMP_FILTER, + SCMP_PROFILE, +}; + struct gluten_arg_data { struct gluten_offset offset; size_t len; @@ -37,6 +42,8 @@ struct gluten_ctx { struct attr attrs[ATTRS_MAX]; struct arg *selected_arg[6]; + + enum scmp_mode mode; }; /** diff --git a/cooker/main.c b/cooker/main.c index a1e5f7f..85c4746 100644 --- a/cooker/main.c +++ b/cooker/main.c @@ -6,14 +6,90 @@ * * Copyright 2023 Red Hat GmbH * Author: Stefano Brivio <sbrivio@redhat.com> + * Alice Frosi <afrosi@redhat.com> */ +#define _GNU_SOURCE +#include <getopt.h> + #include "parson.h" #include "cooker.h" #include "gluten.h" #include "parse.h" #include "filter.h" +/* Cooker options */ +static struct option options[] = { + { "input", required_argument, NULL, 'i' }, + { "gluten", required_argument, NULL, 'g' }, + { "filter", required_argument, NULL, 'f' }, + { "scmp-profile", required_argument, NULL, 'p' }, +}; + +struct arguments { + char *input_file; + char *gluten_file; + char *filter_file; + char *scmp_profile_file; +}; + +static void usage() +{ + printf("seitan-cooker: generate the BPF filters or seccomp profile and the action byte code for seitan\n" + "Example: setain-cooker -i <input file> -g <gluten_file> -f <filter_file>\n" + "Usage:\n" + "\t-i, --input:\tJSON input file\n" + "\t-g, --gluten:\tBytecode file for seitan action\n" + "\t-f, --filter:\tBPF filter file (cannot be used together with scmp-profile)\n" + "\t-p, --scmp-profile:\tSeccomp profile file (cannot be used together with filter)\n"); + exit(EXIT_FAILURE); +} + +static void parse(int argc, char **argv, struct arguments *arguments) +{ + int option_index = 0; + int oc; + if (arguments == NULL) + usage(); + while ((oc = getopt_long(argc, argv, ":i:g:f:p:", options, + &option_index)) != -1) { + switch (oc) { + case 'i': + arguments->input_file = optarg; + break; + case 'g': + arguments->gluten_file = optarg; + break; + case 'f': + arguments->filter_file = optarg; + break; + case 'p': + arguments->scmp_profile_file = optarg; + break; + default: + usage(); + } + } + if (arguments->input_file == NULL) { + err("missing input file"); + usage(); + } + if (arguments->filter_file != NULL && + arguments->scmp_profile_file != NULL) { + err("the filter and scmp-profile options cannot be used together"); + usage(); + } + if (arguments->filter_file == NULL && + arguments->scmp_profile_file == NULL) { + err("select one of the options between filter and scmp-profile"); + usage(); + } + if (arguments->gluten_file == NULL) { + err("missing gluten file"); + usage(); + } +} + /** * main() - Entry point for cooker * @argc: Argument count @@ -23,18 +99,17 @@ */ int main(int argc, char **argv) { + struct arguments arguments = { 0 }; struct gluten_ctx g = { 0 }; - /* TODO: Options and usage */ - if (argc != 4) - die("%s INPUT GLUTEN BPF", argv[0]); + parse(argc, argv, &arguments); gluten_init(&g); - parse_file(&g, argv[1]); + parse_file(&g, arguments.input_file); - gluten_write(&g, argv[2]); - filter_write(argv[3]); + gluten_write(&g, arguments.gluten_file); + filter_write(arguments.filter_file); return 0; } |