libifupdown: execute: add lif_maybe_try_executor()

This commit is contained in:
Ariadne Conill 2020-07-25 02:16:51 -06:00
parent 4a5a37a620
commit 96112d5dc9
3 changed files with 21 additions and 1 deletions

View file

@ -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)

View file

@ -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);
}

View file

@ -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