2012-05-03 09:25:13 +02:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
|
2016-11-29 00:53:50 +01:00
|
|
|
#include "buffer_utils.hh"
|
2018-03-30 00:58:18 +02:00
|
|
|
#include "client.hh"
|
2016-07-20 09:49:04 +02:00
|
|
|
#include "clock.hh"
|
2012-12-10 18:41:01 +01:00
|
|
|
#include "context.hh"
|
2016-11-29 00:53:50 +01:00
|
|
|
#include "display_buffer.hh"
|
2014-11-25 02:00:18 +01:00
|
|
|
#include "event_manager.hh"
|
2016-10-29 12:25:58 +02:00
|
|
|
#include "face_registry.hh"
|
2016-11-29 00:53:50 +01:00
|
|
|
#include "file.hh"
|
2017-03-15 18:55:34 +01:00
|
|
|
#include "flags.hh"
|
2017-03-16 10:57:39 +01:00
|
|
|
#include "option.hh"
|
2018-05-26 02:01:26 +02:00
|
|
|
#include "option_types.hh"
|
2016-11-29 00:53:50 +01:00
|
|
|
#include "regex.hh"
|
2012-09-06 14:28:07 +02:00
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2014-10-13 20:28:02 +02:00
|
|
|
#include <unistd.h>
|
2017-02-03 02:14:42 +01:00
|
|
|
#include <fcntl.h>
|
2017-01-08 23:30:15 +01:00
|
|
|
#include <cstdlib>
|
2019-01-24 13:15:22 +01:00
|
|
|
#include <errno.h>
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2018-12-27 03:06:13 +01:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
#define vfork fork
|
|
|
|
#endif
|
|
|
|
|
2015-10-03 12:21:35 +02:00
|
|
|
extern char **environ;
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
2013-04-17 19:26:44 +02:00
|
|
|
|
2018-02-18 04:52:29 +01:00
|
|
|
ShellManager::ShellManager(ConstArrayView<EnvVarDesc> builtin_env_vars)
|
|
|
|
: m_env_vars{builtin_env_vars}
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2018-11-27 12:21:20 +01:00
|
|
|
auto is_executable = [](StringView path) {
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.zstr(), &st))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool executable = (st.st_mode & S_IXUSR)
|
|
|
|
| (st.st_mode & S_IXGRP)
|
|
|
|
| (st.st_mode & S_IXOTH);
|
|
|
|
return S_ISREG(st.st_mode) and executable;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (const char* shell = getenv("KAKOUNE_POSIX_SHELL"))
|
|
|
|
{
|
|
|
|
if (not is_executable(shell))
|
|
|
|
throw runtime_error{format("KAKOUNE_POSIX_SHELL '{}' is not executable", shell)};
|
|
|
|
m_shell = shell;
|
|
|
|
}
|
|
|
|
else // Get a guaranteed to be POSIX shell binary
|
2016-12-16 00:04:53 +01:00
|
|
|
{
|
2017-01-08 23:30:15 +01:00
|
|
|
auto size = confstr(_CS_PATH, nullptr, 0);
|
2016-12-20 21:14:35 +01:00
|
|
|
String path; path.resize(size-1, 0);
|
2016-12-16 00:04:53 +01:00
|
|
|
confstr(_CS_PATH, path.data(), size);
|
|
|
|
for (auto dir : StringView{path} | split<StringView>(':'))
|
|
|
|
{
|
2018-11-27 12:21:20 +01:00
|
|
|
auto candidate = format("{}/sh", dir);
|
|
|
|
if (is_executable(candidate))
|
2016-12-16 00:04:53 +01:00
|
|
|
{
|
|
|
|
m_shell = std::move(candidate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_shell.empty())
|
|
|
|
throw runtime_error{format("unable to find a posix shell in {}", path)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Kakoune binary location to the path to guarantee that %sh{ ... }
|
|
|
|
// have access to the kak command regardless of if the user installed it
|
|
|
|
{
|
|
|
|
const char* path = getenv("PATH");
|
2020-12-08 23:13:45 +01:00
|
|
|
auto new_path = format("{}../libexec/kak:{}", split_path(get_kak_binary_path()).first, path);
|
2016-12-16 00:04:53 +01:00
|
|
|
setenv("PATH", new_path.c_str(), 1);
|
|
|
|
}
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:36:37 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Pipe
|
|
|
|
{
|
2016-04-12 14:41:21 +02:00
|
|
|
Pipe(bool create = true)
|
|
|
|
: m_fd{-1, -1}
|
|
|
|
{
|
|
|
|
if (create and ::pipe(m_fd) < 0)
|
2015-12-10 09:00:10 +01:00
|
|
|
throw runtime_error(format("unable to create pipe (fds: {}/{}; errno: {})", m_fd[0], m_fd[1], ::strerror(errno)));
|
|
|
|
}
|
2015-10-01 20:36:37 +02:00
|
|
|
~Pipe() { close_read_fd(); close_write_fd(); }
|
|
|
|
|
|
|
|
int read_fd() const { return m_fd[0]; }
|
|
|
|
int write_fd() const { return m_fd[1]; }
|
|
|
|
|
|
|
|
void close_read_fd() { close_fd(m_fd[0]); }
|
|
|
|
void close_write_fd() { close_fd(m_fd[1]); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void close_fd(int& fd) { if (fd != -1) { close(fd); fd = -1; } }
|
|
|
|
int m_fd[2];
|
|
|
|
};
|
|
|
|
|
2015-12-05 11:00:11 +01:00
|
|
|
template<typename Func>
|
2016-12-16 00:04:53 +01:00
|
|
|
pid_t spawn_shell(const char* shell, StringView cmdline,
|
|
|
|
ConstArrayView<String> params,
|
|
|
|
ConstArrayView<String> kak_env,
|
2021-01-22 07:15:59 +01:00
|
|
|
Func setup_child) noexcept
|
2015-10-01 20:36:37 +02:00
|
|
|
{
|
|
|
|
Vector<const char*> envptrs;
|
|
|
|
for (char** envp = environ; *envp; ++envp)
|
|
|
|
envptrs.push_back(*envp);
|
|
|
|
for (auto& env : kak_env)
|
|
|
|
envptrs.push_back(env.c_str());
|
|
|
|
envptrs.push_back(nullptr);
|
|
|
|
|
|
|
|
auto cmdlinezstr = cmdline.zstr();
|
2018-11-27 12:21:20 +01:00
|
|
|
Vector<const char*> execparams = { "sh", "-c", cmdlinezstr };
|
2015-10-01 20:36:37 +02:00
|
|
|
if (not params.empty())
|
|
|
|
execparams.push_back(shell);
|
|
|
|
for (auto& param : params)
|
|
|
|
execparams.push_back(param.c_str());
|
|
|
|
execparams.push_back(nullptr);
|
|
|
|
|
2018-11-27 11:49:57 +01:00
|
|
|
if (pid_t pid = vfork())
|
2015-10-01 20:36:37 +02:00
|
|
|
return pid;
|
|
|
|
|
2015-12-05 11:00:11 +01:00
|
|
|
setup_child();
|
2015-10-01 20:36:37 +02:00
|
|
|
|
|
|
|
execve(shell, (char* const*)execparams.data(), (char* const*)envptrs.data());
|
2021-01-22 07:15:59 +01:00
|
|
|
char buffer[1024];
|
|
|
|
write(STDERR_FILENO, format_to(buffer, "execve failed: {}\n", errno));
|
2018-12-07 15:36:06 +01:00
|
|
|
_exit(-1);
|
2015-10-01 20:36:37 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:00:11 +01:00
|
|
|
Vector<String> generate_env(StringView cmdline, const Context& context, const ShellContext& shell_context)
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2019-06-19 14:59:42 +02:00
|
|
|
static const Regex re(R"(\bkak_(quoted_)?(\w+)\b)");
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2021-01-22 07:17:13 +01:00
|
|
|
Vector<String> env;
|
2018-12-19 11:47:44 +01:00
|
|
|
for (auto&& match : RegexIterator{cmdline.begin(), cmdline.end(), re})
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2019-06-19 14:59:42 +02:00
|
|
|
StringView name{match[2].first, match[2].second};
|
2012-05-29 12:39:03 +02:00
|
|
|
|
2015-10-01 20:36:37 +02:00
|
|
|
auto match_name = [&](const String& s) {
|
2018-03-25 07:47:19 +02:00
|
|
|
return s.substr(0_byte, name.length()) == name and
|
|
|
|
s.substr(name.length(), 1_byte) == "=";
|
2015-10-01 20:36:37 +02:00
|
|
|
};
|
2021-01-22 07:17:13 +01:00
|
|
|
if (any_of(env, match_name))
|
2015-10-01 20:36:37 +02:00
|
|
|
continue;
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2015-10-01 20:36:37 +02:00
|
|
|
try
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2021-01-22 07:17:13 +01:00
|
|
|
Quoting quoting = match[1].matched ? Quoting::Shell : Quoting::Raw;
|
|
|
|
auto var_it = shell_context.env_vars.find(name);
|
2020-03-01 03:28:06 +01:00
|
|
|
String value = var_it != shell_context.env_vars.end() ?
|
|
|
|
var_it->value : join(ShellManager::instance().get_val(name, context) | transform(quoter(quoting)), ' ', false);
|
2015-10-01 20:36:37 +02:00
|
|
|
|
2019-06-19 14:59:42 +02:00
|
|
|
StringView quoted{match[1].first, match[1].second};
|
2021-01-22 07:17:13 +01:00
|
|
|
env.push_back(format("kak_{}{}={}", quoted, name, value));
|
2015-10-01 20:36:37 +02:00
|
|
|
} catch (runtime_error&) {}
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 07:17:13 +01:00
|
|
|
return env;
|
2015-12-05 11:00:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:59:15 +01:00
|
|
|
FDWatcher make_pipe_reader(Pipe& pipe, String& contents)
|
|
|
|
{
|
|
|
|
return {pipe.read_fd(), FdEvents::Read,
|
|
|
|
[&contents, &pipe](FDWatcher& watcher, FdEvents, EventMode) {
|
|
|
|
char buffer[1024];
|
|
|
|
while (fd_readable(pipe.read_fd()))
|
|
|
|
{
|
|
|
|
size_t size = ::read(pipe.read_fd(), buffer, sizeof(buffer));
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
pipe.close_read_fd();
|
|
|
|
watcher.disable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
contents += StringView{buffer, buffer+size};
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
FDWatcher make_pipe_writer(Pipe& pipe, StringView contents)
|
|
|
|
{
|
|
|
|
int flags = fcntl(pipe.write_fd(), F_GETFL, 0);
|
|
|
|
fcntl(pipe.write_fd(), F_SETFL, flags | O_NONBLOCK);
|
|
|
|
return {pipe.write_fd(), FdEvents::Write,
|
|
|
|
[contents, &pipe](FDWatcher& watcher, FdEvents, EventMode) mutable {
|
|
|
|
while (fd_writable(pipe.write_fd()))
|
|
|
|
{
|
|
|
|
ssize_t size = ::write(pipe.write_fd(), contents.begin(),
|
|
|
|
(size_t)contents.length());
|
|
|
|
if (size > 0)
|
|
|
|
contents = contents.substr(ByteCount{(int)size});
|
|
|
|
if (size == -1 and (errno == EAGAIN or errno == EWOULDBLOCK))
|
|
|
|
return;
|
|
|
|
if (size < 0 or contents.empty())
|
|
|
|
{
|
|
|
|
pipe.close_write_fd();
|
|
|
|
watcher.disable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:00:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<String, int> ShellManager::eval(
|
|
|
|
StringView cmdline, const Context& context, StringView input,
|
|
|
|
Flags flags, const ShellContext& shell_context)
|
|
|
|
{
|
|
|
|
const DebugFlags debug_flags = context.options()["debug"].get<DebugFlags>();
|
|
|
|
const bool profile = debug_flags & DebugFlags::Profile;
|
|
|
|
if (debug_flags & DebugFlags::Shell)
|
|
|
|
write_to_debug_buffer(format("shell:\n{}\n----\n", cmdline));
|
|
|
|
|
2016-07-20 09:49:04 +02:00
|
|
|
auto start_time = profile ? Clock::now() : Clock::time_point{};
|
2015-12-05 11:00:11 +01:00
|
|
|
|
|
|
|
auto kak_env = generate_env(cmdline, context, shell_context);
|
|
|
|
|
2016-07-20 09:49:04 +02:00
|
|
|
auto spawn_time = profile ? Clock::now() : Clock::time_point{};
|
2015-11-21 13:11:19 +01:00
|
|
|
|
2016-04-12 14:41:21 +02:00
|
|
|
Pipe child_stdin{not input.empty()}, child_stdout, child_stderr;
|
2016-12-16 00:04:53 +01:00
|
|
|
pid_t pid = spawn_shell(m_shell.c_str(), cmdline, shell_context.params, kak_env,
|
2015-12-05 11:00:11 +01:00
|
|
|
[&child_stdin, &child_stdout, &child_stderr] {
|
2018-06-28 14:56:50 +02:00
|
|
|
auto move = [](int oldfd, int newfd)
|
|
|
|
{
|
|
|
|
if (oldfd == newfd)
|
|
|
|
return;
|
|
|
|
dup2(oldfd, newfd); close(oldfd);
|
|
|
|
};
|
2015-12-05 11:00:11 +01:00
|
|
|
|
2016-04-12 14:41:21 +02:00
|
|
|
if (child_stdin.read_fd() != -1)
|
|
|
|
{
|
|
|
|
close(child_stdin.write_fd());
|
|
|
|
move(child_stdin.read_fd(), 0);
|
|
|
|
}
|
2017-04-17 21:39:24 +02:00
|
|
|
else
|
2017-04-22 10:39:00 +02:00
|
|
|
move(open("/dev/null", O_RDONLY), 0);
|
2015-12-05 11:00:11 +01:00
|
|
|
|
|
|
|
close(child_stdout.read_fd());
|
|
|
|
move(child_stdout.write_fd(), 1);
|
|
|
|
|
|
|
|
close(child_stderr.read_fd());
|
|
|
|
move(child_stderr.write_fd(), 2);
|
|
|
|
});
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2015-10-01 20:36:37 +02:00
|
|
|
child_stdin.close_read_fd();
|
|
|
|
child_stdout.close_write_fd();
|
|
|
|
child_stderr.close_write_fd();
|
|
|
|
|
2016-10-29 12:25:58 +02:00
|
|
|
auto wait_time = Clock::now();
|
2015-11-21 13:11:19 +01:00
|
|
|
|
2015-10-01 20:36:37 +02:00
|
|
|
String stdout_contents, stderr_contents;
|
2021-03-04 10:59:15 +01:00
|
|
|
auto stdout_reader = make_pipe_reader(child_stdout, stdout_contents);
|
|
|
|
auto stderr_reader = make_pipe_reader(child_stderr, stderr_contents);
|
|
|
|
auto stdin_writer = make_pipe_writer(child_stdin, input);
|
2015-10-01 20:36:37 +02:00
|
|
|
|
|
|
|
// block SIGCHLD to make sure we wont receive it before
|
|
|
|
// our call to pselect, that will end up blocking indefinitly.
|
|
|
|
sigset_t mask, orig_mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &orig_mask);
|
|
|
|
auto restore_mask = on_scope_end([&] { sigprocmask(SIG_SETMASK, &orig_mask, nullptr); });
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
// check for termination now that SIGCHLD is blocked
|
2017-03-17 00:08:10 +01:00
|
|
|
bool terminated = waitpid(pid, &status, WNOHANG) != 0;
|
2015-10-01 20:36:37 +02:00
|
|
|
|
2016-10-29 12:25:58 +02:00
|
|
|
using namespace std::chrono;
|
|
|
|
static constexpr seconds wait_timeout{1};
|
2018-04-01 01:08:54 +02:00
|
|
|
Optional<DisplayLine> previous_status;
|
2016-11-30 10:55:05 +01:00
|
|
|
Timer wait_timer{wait_time + wait_timeout, [&](Timer& timer)
|
|
|
|
{
|
|
|
|
auto wait_duration = Clock::now() - wait_time;
|
2018-03-30 00:58:18 +02:00
|
|
|
if (context.has_client())
|
|
|
|
{
|
|
|
|
auto& client = context.client();
|
2018-04-01 01:08:54 +02:00
|
|
|
if (not previous_status)
|
|
|
|
previous_status = client.current_status();
|
|
|
|
|
2018-03-30 00:58:18 +02:00
|
|
|
client.print_status({ format("waiting for shell command to finish ({}s)",
|
|
|
|
duration_cast<seconds>(wait_duration).count()),
|
2018-04-07 07:36:39 +02:00
|
|
|
context.faces()["Information"] });
|
2018-03-30 00:58:18 +02:00
|
|
|
client.redraw_ifn();
|
|
|
|
}
|
2016-11-30 10:55:05 +01:00
|
|
|
timer.set_next_date(Clock::now() + wait_timeout);
|
|
|
|
}, EventMode::Urgent};
|
2016-10-29 12:25:58 +02:00
|
|
|
|
2017-02-03 02:14:42 +01:00
|
|
|
while (not terminated or child_stdin.write_fd() != -1 or
|
2015-10-01 20:36:37 +02:00
|
|
|
((flags & Flags::WaitForStdout) and
|
|
|
|
(child_stdout.read_fd() != -1 or child_stderr.read_fd() != -1)))
|
|
|
|
{
|
|
|
|
EventManager::instance().handle_next_events(EventMode::Urgent, &orig_mask);
|
|
|
|
if (not terminated)
|
2017-03-17 00:08:10 +01:00
|
|
|
terminated = waitpid(pid, &status, WNOHANG) != 0;
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
2015-10-01 20:36:37 +02:00
|
|
|
|
|
|
|
if (not stderr_contents.empty())
|
|
|
|
write_to_debug_buffer(format("shell stderr: <<<\n{}>>>", stderr_contents));
|
|
|
|
|
2015-11-21 13:11:19 +01:00
|
|
|
if (profile)
|
|
|
|
{
|
2016-07-20 09:49:04 +02:00
|
|
|
auto end_time = Clock::now();
|
2017-06-07 21:06:47 +02:00
|
|
|
auto full = duration_cast<microseconds>(end_time - start_time);
|
|
|
|
auto spawn = duration_cast<microseconds>(wait_time - spawn_time);
|
|
|
|
auto wait = duration_cast<microseconds>(end_time - wait_time);
|
|
|
|
write_to_debug_buffer(format("shell execution took {} us (spawn: {}, wait: {})",
|
2015-11-21 13:11:19 +01:00
|
|
|
(size_t)full.count(), (size_t)spawn.count(), (size_t)wait.count()));
|
|
|
|
}
|
|
|
|
|
2018-04-01 01:08:54 +02:00
|
|
|
if (previous_status) // restore the status line
|
2018-03-30 00:58:18 +02:00
|
|
|
{
|
2018-04-01 01:08:54 +02:00
|
|
|
context.print_status(std::move(*previous_status));
|
2018-03-30 00:58:18 +02:00
|
|
|
context.client().redraw_ifn();
|
|
|
|
}
|
2016-11-30 14:46:20 +01:00
|
|
|
|
2016-12-03 13:41:36 +01:00
|
|
|
return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 };
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
Vector<String> ShellManager::get_val(StringView name, const Context& context) const
|
2014-06-18 20:28:48 +02:00
|
|
|
{
|
2017-06-07 20:46:16 +02:00
|
|
|
auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) {
|
|
|
|
return desc.prefix ? prefix_match(name, desc.str) : name == desc.str;
|
|
|
|
});
|
2014-06-18 20:28:48 +02:00
|
|
|
|
|
|
|
if (env_var == m_env_vars.end())
|
|
|
|
throw runtime_error("no such env var: " + name);
|
2015-09-03 14:21:35 +02:00
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
return env_var->func(name, context);
|
2014-06-18 20:28:48 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 20:21:43 +02:00
|
|
|
CandidateList ShellManager::complete_env_var(StringView prefix,
|
|
|
|
ByteCount cursor_pos) const
|
|
|
|
{
|
2016-10-11 00:44:18 +02:00
|
|
|
return complete(prefix, cursor_pos,
|
2018-03-13 04:24:03 +01:00
|
|
|
m_env_vars | transform(&EnvVarDesc::str));
|
2016-04-17 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|