From 96112d5dc9b221b509b8f27cf7eff693cb9f05e0 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 25 Jul 2020 02:16:51 -0600 Subject: [PATCH] libifupdown: execute: add lif_maybe_try_executor() --- cmd/ifupdown.c | 4 +++- libifupdown/execute.c | 16 ++++++++++++++++ libifupdown/execute.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/ifupdown.c b/cmd/ifupdown.c index 1ed6afe..4983525 100644 --- a/cmd/ifupdown.c +++ b/cmd/ifupdown.c @@ -28,7 +28,9 @@ struct match_options { }; static bool up; -static struct lif_execute_opts exec_opts = {}; +static struct lif_execute_opts exec_opts = { + .executor_path = EXECUTOR_PATH, +}; void ifupdown_usage(void) diff --git a/libifupdown/execute.c b/libifupdown/execute.c index b1242d5..32607e3 100644 --- a/libifupdown/execute.c +++ b/libifupdown/execute.c @@ -73,3 +73,19 @@ lif_file_is_executable(const char *path) return access(path, X_OK); } + +bool +lif_maybe_run_executor(const struct lif_execute_opts *opts, char *const envp[], const char *executor) +{ + if (opts->verbose) + fprintf(stderr, "ifupdown: attempting to run %s executor\n", executor); + + char pathbuf[4096]; + + snprintf(pathbuf, sizeof pathbuf, "%s/%s", opts->executor_path, executor); + + if (!lif_file_is_executable(pathbuf)) + return true; + + return lif_execute_fmt(opts, envp, "%s", pathbuf); +} diff --git a/libifupdown/execute.h b/libifupdown/execute.h index 3fce6ff..fc03dbc 100644 --- a/libifupdown/execute.h +++ b/libifupdown/execute.h @@ -22,9 +22,11 @@ struct lif_execute_opts { bool verbose; bool mock; + const char *executor_path; }; extern bool lif_execute_fmt(const struct lif_execute_opts *opts, char *const envp[], const char *fmt, ...); extern bool lif_file_is_executable(const char *path); +extern bool lif_maybe_run_executor(const struct lif_execute_opts *opts, char *const envp[], const char *executor); #endif