kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

shell_manager.cc (14015B)


      1 #include "shell_manager.hh"
      2 
      3 #include "debug.hh"
      4 #include "client.hh"
      5 #include "clock.hh"
      6 #include "context.hh"
      7 #include "command_manager.hh"
      8 #include "display_buffer.hh"
      9 #include "event_manager.hh"
     10 #include "face_registry.hh"
     11 #include "option_manager.hh"
     12 #include "file.hh"
     13 #include "flags.hh"
     14 #include "option_types.hh"
     15 #include "regex.hh"
     16 
     17 #include <chrono>
     18 #include <cstring>
     19 #include <sys/types.h>
     20 #include <sys/wait.h>
     21 #include <unistd.h>
     22 #include <fcntl.h>
     23 #include <cstdlib>
     24 #include <errno.h>
     25 
     26 #if defined(__CYGWIN__) || defined(__APPLE__)
     27 #define vfork fork
     28 #endif
     29 
     30 extern char **environ;
     31 
     32 namespace Kakoune
     33 {
     34 
     35 ShellManager::ShellManager(ConstArrayView<EnvVarDesc> builtin_env_vars)
     36     : m_env_vars{builtin_env_vars}
     37 {
     38     auto is_executable = [](StringView path) {
     39         struct stat st;
     40         if (stat(path.zstr(), &st))
     41             return false;
     42 
     43         bool executable = (st.st_mode & S_IXUSR)
     44                         | (st.st_mode & S_IXGRP)
     45                         | (st.st_mode & S_IXOTH);
     46         return S_ISREG(st.st_mode) and executable;
     47     };
     48 
     49     if (const char* shell = getenv("KAKOUNE_POSIX_SHELL"))
     50     {
     51         if (not is_executable(shell))
     52             throw runtime_error{format("KAKOUNE_POSIX_SHELL '{}' is not executable", shell)};
     53         m_shell = shell;
     54     }
     55     else // Get a guaranteed to be POSIX shell binary
     56     {
     57         #if defined(_CS_PATH)
     58         auto size = confstr(_CS_PATH, nullptr, 0);
     59         String path; path.resize(size-1, 0);
     60         confstr(_CS_PATH, path.data(), size);
     61         #else
     62         StringView path = "/bin:/usr/bin";
     63         #endif
     64         for (auto dir : StringView{path} | split<StringView>(':'))
     65         {
     66             auto candidate = format("{}/sh", dir);
     67             if (is_executable(candidate))
     68             {
     69                 m_shell = std::move(candidate);
     70                 break;
     71             }
     72         }
     73         if (m_shell.empty())
     74             throw runtime_error{format("unable to find a posix shell in {}", path)};
     75     }
     76 
     77     // Add Kakoune binary location to the path to guarantee that %sh{ ... }
     78     // have access to the kak command regardless of if the user installed it
     79     {
     80         const char* path = getenv("PATH");
     81         auto new_path = format("{}../libexec/kak:{}", split_path(get_kak_binary_path()).first, path);
     82         setenv("PATH", new_path.c_str(), 1);
     83     }
     84 }
     85 
     86 namespace
     87 {
     88 
     89 Shell spawn_shell(const char* shell, StringView cmdline,
     90                   ConstArrayView<String> params,
     91                   ConstArrayView<String> kak_env,
     92                   bool open_stdin) noexcept
     93 {
     94     Vector<const char*> envptrs;
     95     for (char** envp = environ; *envp; ++envp)
     96         envptrs.push_back(*envp);
     97     for (auto& env : kak_env)
     98         envptrs.push_back(env.c_str());
     99     envptrs.push_back(nullptr);
    100 
    101     auto cmdlinezstr = cmdline.zstr();
    102     Vector<const char*> execparams = { "sh", "-c", cmdlinezstr };
    103     if (not params.empty())
    104         execparams.push_back(shell);
    105     for (auto& param : params)
    106         execparams.push_back(param.c_str());
    107     execparams.push_back(nullptr);
    108 
    109     auto make_pipe = []() -> Array<UniqueFd, 2> {
    110         if (int pipefd[2] = {-1, -1}; ::pipe(pipefd) == 0)
    111             return {UniqueFd{pipefd[0]}, UniqueFd{pipefd[1]}};
    112         throw runtime_error(format("unable to create pipe, errno: {}", ::strerror(errno)));
    113     };
    114 
    115     auto stdin_pipe = open_stdin ? make_pipe() : Array{UniqueFd{open("/dev/null", O_RDONLY)}, UniqueFd{}};
    116     auto stdout_pipe = make_pipe();
    117     auto stderr_pipe = make_pipe();
    118     if (pid_t pid = vfork())
    119         return {pid, std::move(stdin_pipe[1]), std::move(stdout_pipe[0]), std::move(stderr_pipe[0])};
    120 
    121     constexpr auto renamefd = [](int oldfd, int newfd) {
    122         if (oldfd == newfd)
    123             return;
    124         dup2(oldfd, newfd);
    125         close(oldfd);
    126     };
    127 
    128     renamefd((int)stdin_pipe[0], 0);
    129     renamefd((int)stdout_pipe[1], 1);
    130     renamefd((int)stderr_pipe[1], 2);
    131 
    132     close((int)stdin_pipe[1]);
    133     close((int)stdout_pipe[0]);
    134     close((int)stderr_pipe[0]);
    135 
    136     execve(shell, (char* const*)execparams.data(), (char* const*)envptrs.data());
    137     char buffer[1024];
    138     write(STDERR_FILENO, format_to(buffer, "execve failed: {}\n", strerror(errno)));
    139     _exit(-1);
    140     return {-1, {}, {}, {}};
    141 }
    142 
    143 template<typename GetValue>
    144 Vector<String> generate_env(StringView cmdline, ConstArrayView<String> params, const Context& context, GetValue&& get_value)
    145 {
    146     static const Regex re(R"(\bkak_(quoted_)?(\w+)\b)");
    147 
    148     Vector<String> env;
    149     auto add_matches = [&](StringView s) {
    150         for (auto&& match : RegexIterator{s.begin(), s.end(), re})
    151         {
    152             StringView name{match[2].first, match[2].second};
    153             StringView shell_name{match[0].first, match[0].second};
    154 
    155             auto match_name = [&](const String& s) {
    156                 return s.substr(0_byte, shell_name.length()) == shell_name and
    157                        s.substr(shell_name.length(), 1_byte) == "=";
    158             };
    159             if (any_of(env, match_name))
    160                 continue;
    161 
    162             try
    163             {
    164                 StringView quoted{match[1].first, match[1].second};
    165                 Quoting quoting = match[1].matched ? Quoting::Shell : Quoting::Raw;
    166                 env.push_back(format("kak_{}{}={}", quoted, name, get_value(name, quoting)));
    167             } catch (runtime_error&) {}
    168         }
    169     };
    170     add_matches(cmdline);
    171     for (auto&& param : params)
    172         add_matches(param);
    173 
    174     return env;
    175 }
    176 
    177 template<typename OnClose>
    178 FDWatcher make_reader(int fd, String& contents, OnClose&& on_close)
    179 {
    180     return {fd, FdEvents::Read, EventMode::Urgent,
    181             [&contents, on_close](FDWatcher& watcher, FdEvents, EventMode) {
    182         const int fd = watcher.fd();
    183         char buffer[1024];
    184         while (fd_readable(fd))
    185         {
    186             ssize_t size = ::read(fd, buffer, sizeof(buffer));
    187             if (size <= 0)
    188             {
    189                 if (size < 0 and errno == EAGAIN)
    190                     continue; // try again
    191 
    192                 watcher.disable();
    193                 on_close(size == 0);
    194                 return;
    195             }
    196             contents += StringView{buffer, buffer+size};
    197         }
    198     }};
    199 }
    200 
    201 FDWatcher make_pipe_writer(UniqueFd& fd, const FunctionRef<StringView ()>& generator)
    202 {
    203     int flags = fcntl((int)fd, F_GETFL, 0);
    204     fcntl((int)fd, F_SETFL, flags | O_NONBLOCK);
    205     return {(int)fd, FdEvents::Write, EventMode::Urgent,
    206             [&generator, &fd, contents=generator()](FDWatcher& watcher, FdEvents, EventMode) mutable {
    207         while (fd_writable((int)fd))
    208         {
    209             ssize_t size = ::write((int)fd, contents.begin(),
    210                                    (size_t)contents.length());
    211             if (size > 0)
    212             {
    213                 contents = contents.substr(ByteCount{(int)size});
    214                 if (contents.empty())
    215                     contents = generator();
    216             }
    217             if (size == -1 and (errno == EAGAIN or errno == EWOULDBLOCK))
    218                 return;
    219             if (size < 0 or contents.empty())
    220             {
    221                 fd.close();
    222                 watcher.disable();
    223                 return;
    224             }
    225         }
    226     }};
    227 }
    228 
    229 struct CommandFifos
    230 {
    231     String base_dir;
    232     String command;
    233     FDWatcher command_watcher;
    234 
    235     CommandFifos(Context& context, const ShellContext& shell_context)
    236       : base_dir(format("{}/kak-fifo.XXXXXX", tmpdir())),
    237         command_watcher([&] {
    238             if (mkdtemp(base_dir.data()) == nullptr or
    239                 mkfifo(command_fifo_path().c_str(), 0600) != 0 or
    240                 mkfifo(response_fifo_path().c_str(), 0600) != 0)
    241                 throw runtime_error(format("unable to create command/response fifos, errno: {}", ::strerror(errno)));
    242 
    243             int fd = open(command_fifo_path().c_str(), O_RDONLY | O_NONBLOCK);
    244             return make_reader(fd, command, [&, fd](bool graceful) {
    245                 if (not graceful)
    246                 {
    247                     write_to_debug_buffer(format("error reading from command fifo '{}'", strerror(errno)));
    248                     return;
    249                 }
    250                 CommandManager::instance().execute(command, context, shell_context);
    251                 command.clear();
    252                 command_watcher.reset_fd(fd);
    253             });
    254         }())
    255     {
    256     }
    257 
    258     ~CommandFifos()
    259     {
    260         command_watcher.close_fd();
    261         unlink(command_fifo_path().c_str());
    262         unlink(response_fifo_path().c_str());
    263         rmdir(base_dir.c_str());
    264     }
    265 
    266     String command_fifo_path() const { return format("{}/command-fifo", base_dir); }
    267     String response_fifo_path() const { return format("{}/response-fifo", base_dir); }
    268 };
    269 
    270 }
    271 
    272 std::pair<String, int> ShellManager::eval(
    273     StringView cmdline, const Context& context, FunctionRef<StringView ()> input_generator,
    274     Flags flags, const ShellContext& shell_context)
    275 {
    276     const DebugFlags debug_flags = context.options()["debug"].get<DebugFlags>();
    277     const bool profile = debug_flags & DebugFlags::Profile;
    278     if (debug_flags & DebugFlags::Shell)
    279         write_to_debug_buffer(format("shell:\n{}\n----\nargs: {}\n----\n", cmdline, join(shell_context.params | transform(shell_quote), ' ')));
    280 
    281     auto start_time = profile ? Clock::now() : Clock::time_point{};
    282 
    283     Optional<CommandFifos> command_fifos;
    284 
    285     auto kak_env = generate_env(cmdline, shell_context.params, context, [&](StringView name, Quoting quoting) {
    286         if (name == "command_fifo" or name == "response_fifo")
    287         {
    288             if (not command_fifos)
    289                 command_fifos.emplace(const_cast<Context&>(context), shell_context);
    290             return name == "command_fifo" ?
    291                 command_fifos->command_fifo_path() : command_fifos->response_fifo_path();
    292         }
    293 
    294         if (auto it = shell_context.env_vars.find(name); it != shell_context.env_vars.end())
    295             return it->value;
    296         return join(get_val(name, context) | transform(quoter(quoting)), ' ', false);
    297     });
    298 
    299     auto spawn_time = profile ? Clock::now() : Clock::time_point{};
    300     auto shell = spawn_shell(m_shell.c_str(), cmdline, shell_context.params, kak_env, true);
    301     auto wait_time = Clock::now();
    302 
    303     String stdout_contents, stderr_contents;
    304     auto stdout_reader = make_reader((int)shell.out, stdout_contents, [&](bool){ shell.out.close(); });
    305     auto stderr_reader = make_reader((int)shell.err, stderr_contents, [&](bool){ shell.err.close(); });
    306     auto stdin_writer = make_pipe_writer(shell.in, input_generator);
    307 
    308     // block SIGCHLD to make sure we wont receive it before
    309     // our call to pselect, that will end up blocking indefinitly.
    310     sigset_t mask, orig_mask;
    311     sigemptyset(&mask);
    312     sigaddset(&mask, SIGCHLD);
    313     sigprocmask(SIG_BLOCK, &mask, &orig_mask);
    314     auto restore_mask = OnScopeEnd([&] { sigprocmask(SIG_SETMASK, &orig_mask, nullptr); });
    315 
    316     int status = 0;
    317     // check for termination now that SIGCHLD is blocked
    318     bool terminated = waitpid((int)shell.pid, &status, WNOHANG) != 0;
    319     bool failed = false;
    320 
    321     using namespace std::chrono;
    322     BusyIndicator busy_indicator{context, [&](seconds elapsed) {
    323         return DisplayLine{format("waiting for shell command to finish{} ({}s)",
    324                                   terminated ? " (shell terminated)" : "", elapsed.count()),
    325                            context.faces()[failed ? "Error" : "Information"]};
    326     }};
    327 
    328     bool cancelling = false;
    329     while (not terminated or shell.in or
    330            ((flags & Flags::WaitForStdout) and (shell.out or shell.err)))
    331     {
    332         try
    333         {
    334             EventManager::instance().handle_next_events(EventMode::Urgent, &orig_mask);
    335         }
    336         catch (cancel&)
    337         {
    338             kill((int)shell.pid, SIGINT);
    339             cancelling = true;
    340         }
    341         catch (runtime_error& error)
    342         {
    343             write_to_debug_buffer(format("error while waiting for shell: {}", error.what()));
    344             failed = true;
    345         }
    346         if (not terminated)
    347             terminated = waitpid((int)shell.pid, &status, WNOHANG) == (int)shell.pid;
    348     }
    349 
    350     if (not stderr_contents.empty())
    351         write_to_debug_buffer(format("shell stderr: <<<\n{}>>>", stderr_contents));
    352 
    353     if (profile)
    354     {
    355         auto end_time = Clock::now();
    356         auto full = duration_cast<microseconds>(end_time - start_time);
    357         auto spawn = duration_cast<microseconds>(wait_time - spawn_time);
    358         auto wait = duration_cast<microseconds>(end_time - wait_time);
    359         write_to_debug_buffer(format("shell execution took {} us (spawn: {}, wait: {})",
    360                                      (size_t)full.count(), (size_t)spawn.count(), (size_t)wait.count()));
    361     }
    362 
    363     if (cancelling)
    364         throw cancel{};
    365 
    366     return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 };
    367 }
    368 
    369 Shell ShellManager::spawn(StringView cmdline, const Context& context,
    370                           bool open_stdin, const ShellContext& shell_context)
    371 {
    372     auto kak_env = generate_env(cmdline, shell_context.params, context, [&](StringView name, Quoting quoting) {
    373         if (auto it = shell_context.env_vars.find(name); it != shell_context.env_vars.end())
    374             return it->value;
    375         return join(get_val(name, context) | transform(quoter(quoting)), ' ', false);
    376     });
    377 
    378     return spawn_shell(m_shell.c_str(), cmdline, shell_context.params, kak_env, open_stdin);
    379 }
    380 
    381 Vector<String> ShellManager::get_val(StringView name, const Context& context) const
    382 {
    383     auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) {
    384         return desc.prefix ? prefix_match(name, desc.str) : name == desc.str;
    385     });
    386 
    387     if (env_var == m_env_vars.end())
    388         throw runtime_error("no such variable: " + name);
    389 
    390     return env_var->func(name, context);
    391 }
    392 
    393 CandidateList ShellManager::complete_env_var(StringView prefix,
    394                                              ByteCount cursor_pos) const
    395 {
    396     return complete(prefix, cursor_pos,
    397                     m_env_vars | transform(&EnvVarDesc::str));
    398 }
    399 
    400 }