From 3fa20b4300633d769d2be4de332ed57463a8c2c2 Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Tue, 17 Jan 2023 09:01:37 +0100 Subject: seitan: add flags for pid and input file Avoid hardcoded values and set the option from command line: Example: ./seitan -i action -p 1234 Signed-off-by: Alice Frosi --- seitan.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/seitan.c b/seitan.c index 178dc38..2e71b27 100644 --- a/seitan.c +++ b/seitan.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,50 @@ #include #include +static char doc[] = "Usage: seitan: setain -pid -i "; + +/* Seitan options */ +static struct argp_option options[] = { + { "input", 'i', "FILE", 0, "Action input file", 0 }, + { "pid", 'p', "pid", 0, "Pid of process to monitor", 0 }, + { 0 } +}; + +struct arguments { + char *input_file; + int pid; +}; + +static error_t parse_opt(int key, char *arg, struct argp_state *state) +{ + struct arguments *arguments = state->input; + + switch (key) { + case 'p': + arguments->pid = atoi(arg); + break; + case 'i': + arguments->input_file = arg; + break; + case ARGP_KEY_END: + if (arguments->input_file == NULL) + argp_error(state, "missing input file"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +static struct argp argp = { .options = options, + .parser = parse_opt, + .args_doc = NULL, + .doc = doc, + .children = NULL, + .help_filter = NULL, + .argp_domain = NULL }; + static int nl_init(void) { int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); @@ -168,16 +213,19 @@ int main(int argc, char **argv) char resp_b[BUFSIZ], req_b[BUFSIZ]; struct seccomp_notif_resp *resp = (struct seccomp_notif_resp *)resp_b; struct seccomp_notif *req = (struct seccomp_notif *)req_b; + struct arguments arguments; int fd; - fd = open("t.out", O_CLOEXEC | O_RDONLY); + arguments.pid = -1; + argp_parse(&argp, argc, argv, 0, 0, &arguments); + fd = open(arguments.input_file, O_CLOEXEC | O_RDONLY); read(fd, t, sizeof(t)); close(fd); - if (argc < 2) + if (arguments.pid < 0) while ((ret = event(s)) == -EAGAIN); else - ret = atoi(argv[1]); + ret = arguments.pid; if (ret < 0) exit(EXIT_FAILURE); -- cgit v1.2.3