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>
|
2020-07-28 19:46:25 +00:00
|
|
|
#include <poll.h>
|
2020-07-23 12:12:56 +00:00
|
|
|
|
|
|
|
#include "libifupdown/execute.h"
|
|
|
|
|
|
|
|
#define SHELL "/bin/sh"
|
|
|
|
|
2021-09-19 12:28:30 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* POSIX compatible fallback using waitpid(2) and usleep(3) */
|
2021-09-19 10:57:16 +00:00
|
|
|
static inline bool
|
2021-09-19 12:28:30 +00:00
|
|
|
lif_process_monitor_busyloop(pid_t child, int timeout_sec, int *status)
|
2021-09-19 10:57:16 +00:00
|
|
|
{
|
|
|
|
int ticks = 0;
|
|
|
|
|
2021-09-19 12:57:41 +00:00
|
|
|
while (ticks < timeout_sec * 20)
|
2021-09-19 10:57:16 +00:00
|
|
|
{
|
2021-09-19 12:28:30 +00:00
|
|
|
/* Ugly hack: most executors finish very quickly,
|
|
|
|
* so give them a chance to finish before sleeping.
|
|
|
|
*/
|
|
|
|
usleep(50);
|
2021-09-19 10:57:16 +00:00
|
|
|
|
2021-09-19 12:28:30 +00:00
|
|
|
if (waitpid(child, status, WNOHANG) == child)
|
|
|
|
return true;
|
|
|
|
|
2021-09-19 12:57:41 +00:00
|
|
|
usleep(49950);
|
2021-09-19 10:57:16 +00:00
|
|
|
ticks++;
|
|
|
|
}
|
|
|
|
|
2021-09-19 12:28:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__linux__) && defined(__NR_pidfd_open)
|
|
|
|
|
|
|
|
/* TODO: remove this wrapper once musl and glibc gain pidfd_open() directly. */
|
|
|
|
static inline int
|
|
|
|
lif_pidfd_open(pid_t pid, unsigned int flags)
|
|
|
|
{
|
|
|
|
return syscall(__NR_pidfd_open, pid, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
lif_process_monitor_procdesc(pid_t child, int timeout_sec, int *status)
|
|
|
|
{
|
|
|
|
int pidfd = lif_pidfd_open(child, 0);
|
|
|
|
|
|
|
|
/* pidfd_open() not available, fall back to busyloop */
|
|
|
|
if (pidfd == -1 && errno == ENOSYS)
|
|
|
|
return lif_process_monitor_busyloop(child, timeout_sec, status);
|
|
|
|
|
|
|
|
struct pollfd pfd = {
|
|
|
|
.fd = pidfd,
|
|
|
|
.events = POLLIN,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (poll(&pfd, 1, timeout_sec * 1000) < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
waitpid(child, status, 0);
|
|
|
|
close(pidfd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
lif_process_monitor(const char *cmdbuf, pid_t child, int timeout_sec)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
#if defined(__linux__) && defined(__NR_pidfd_open)
|
|
|
|
if (lif_process_monitor_procdesc(child, timeout_sec, &status))
|
|
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
|
|
|
#else
|
|
|
|
if (lif_process_monitor_busyloop(child, timeout_sec, &status))
|
|
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
|
|
|
#endif
|
|
|
|
|
2021-09-19 10:57:16 +00:00
|
|
|
fprintf(stderr, "execution of '%s': timeout after %d seconds\n", cmdbuf, timeout_sec);
|
|
|
|
kill(child, SIGKILL);
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:12:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-19 10:57:16 +00:00
|
|
|
return lif_process_monitor(cmdbuf, child, opts->timeout);
|
2020-07-23 12:12:56 +00:00
|
|
|
}
|
2020-07-25 07:58:20 +00:00
|
|
|
|
2020-07-28 19:01:15 +00:00
|
|
|
bool
|
2020-07-28 19:46:25 +00:00
|
|
|
lif_execute_fmt_with_result(const struct lif_execute_opts *opts, char *buf, size_t bufsize, char *const envp[], const char *fmt, ...)
|
2020-07-28 19:01:15 +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 };
|
|
|
|
|
|
|
|
if (opts->verbose)
|
|
|
|
puts(cmdbuf);
|
|
|
|
|
|
|
|
if (opts->mock)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int pipefds[2];
|
|
|
|
if (pipe(pipefds) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "execute '%s': %s\n", cmdbuf, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_spawn_file_actions_t file_actions;
|
|
|
|
|
|
|
|
posix_spawn_file_actions_init(&file_actions);
|
|
|
|
posix_spawn_file_actions_addclose(&file_actions, pipefds[0]);
|
|
|
|
posix_spawn_file_actions_adddup2(&file_actions, pipefds[1], 1);
|
|
|
|
posix_spawn_file_actions_addclose(&file_actions, pipefds[1]);
|
|
|
|
|
|
|
|
if (posix_spawn(&child, SHELL, &file_actions, NULL, argv, envp) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "execute '%s': %s\n", cmdbuf, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:53:21 +00:00
|
|
|
posix_spawn_file_actions_destroy(&file_actions);
|
|
|
|
|
2020-07-28 19:46:25 +00:00
|
|
|
close(pipefds[1]);
|
|
|
|
|
|
|
|
struct pollfd pfd = {
|
|
|
|
.fd = pipefds[0],
|
|
|
|
.events = POLLIN
|
|
|
|
};
|
|
|
|
|
|
|
|
if (poll(&pfd, 1, -1) < 1)
|
|
|
|
goto no_result;
|
2020-07-28 19:01:15 +00:00
|
|
|
|
|
|
|
if (read(pipefds[0], buf, bufsize) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "reading from pipe: %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:46:25 +00:00
|
|
|
no_result:
|
2021-09-19 10:57:16 +00:00
|
|
|
return lif_process_monitor(cmdbuf, child, opts->timeout);
|
2020-07-28 19:01:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-09-09 21:50:16 +00:00
|
|
|
lif_maybe_run_executor(const struct lif_execute_opts *opts, char *const envp[], const char *executor, const char *phase, const char *lifname)
|
2020-07-25 08:16:51 +00:00
|
|
|
{
|
|
|
|
if (opts->verbose)
|
2020-09-09 21:50:16 +00:00
|
|
|
fprintf(stderr, "ifupdown: %s: attempting to run %s executor for phase %s\n", lifname, executor, phase);
|
2020-07-25 08:16:51 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-07-28 19:46:25 +00:00
|
|
|
|
|
|
|
bool
|
2020-09-09 21:50:16 +00:00
|
|
|
lif_maybe_run_executor_with_result(const struct lif_execute_opts *opts, char *const envp[], const char *executor, char *buf, size_t bufsize, const char *phase, const char *lifname)
|
2020-07-28 19:46:25 +00:00
|
|
|
{
|
|
|
|
if (opts->verbose)
|
2020-09-09 21:50:16 +00:00
|
|
|
fprintf(stderr, "ifupdown: %s: attempting to run %s executor for phase %s\n", lifname, executor, phase);
|
2020-07-28 19:46:25 +00:00
|
|
|
|
|
|
|
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_with_result(opts, buf, bufsize, envp, "%s", pathbuf);
|
|
|
|
}
|