2020-07-23 12:12:56 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/execute.c
|
|
|
|
* Purpose: execution of individual commands
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <spawn.h>
|
2020-07-25 07:58:20 +00:00
|
|
|
#include <sys/stat.h>
|
2020-07-23 12:12:56 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "libifupdown/execute.h"
|
|
|
|
|
|
|
|
#define SHELL "/bin/sh"
|
|
|
|
|
|
|
|
bool
|
2020-07-23 14:22:26 +00:00
|
|
|
lif_execute_fmt(const struct lif_execute_opts *opts, char *const envp[], const char *fmt, ...)
|
2020-07-23 12:12:56 +00:00
|
|
|
{
|
|
|
|
char cmdbuf[4096];
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
vsnprintf(cmdbuf, sizeof cmdbuf, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
pid_t child;
|
|
|
|
char *argv[] = { SHELL, "-c", cmdbuf, NULL };
|
|
|
|
|
2020-07-23 14:22:26 +00:00
|
|
|
if (opts->verbose)
|
|
|
|
puts(cmdbuf);
|
|
|
|
|
|
|
|
if (opts->mock)
|
|
|
|
return true;
|
|
|
|
|
2020-07-23 12:12:56 +00:00
|
|
|
if (posix_spawn(&child, SHELL, NULL, NULL, argv, envp) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "execute '%s': %s\n", cmdbuf, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status;
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
|
|
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
|
|
|
}
|
2020-07-25 07:58:20 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
lif_file_is_executable(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
return false;
|
|
|
|
|
2020-07-25 08:49:44 +00:00
|
|
|
return !access(path, X_OK);
|
2020-07-25 07:58:20 +00:00
|
|
|
}
|
2020-07-25 08:16:51 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|