Shell: Allow redirections and pipes on builtins

Fixes #3072.
This commit is contained in:
AnotherTest 2020-08-14 22:30:48 +04:30 committed by Andreas Kling
parent 0dac7af6c5
commit c589625418
Notes: sideshowbarker 2024-07-19 03:36:12 +09:00
6 changed files with 81 additions and 13 deletions

View file

@ -56,6 +56,38 @@ void FileDescriptionCollector::add(int fd)
m_fds.append(fd);
}
SavedFileDescriptors::SavedFileDescriptors(const NonnullRefPtrVector<AST::Rewiring>& intended_rewirings)
{
for (auto& rewiring : intended_rewirings) {
int new_fd = dup(rewiring.source_fd);
if (new_fd < 0) {
if (errno != EBADF)
perror("dup");
// The fd that will be overwritten isn't open right now,
// it will be cleaned up by the exec()-side collector
// and we have nothing to do here, so just ignore this error.
continue;
}
auto flags = fcntl(new_fd, F_GETFL);
auto rc = fcntl(new_fd, F_SETFL, flags | FD_CLOEXEC);
ASSERT(rc == 0);
m_saves.append({ rewiring.source_fd, new_fd });
m_collector.add(new_fd);
}
}
SavedFileDescriptors::~SavedFileDescriptors()
{
for (auto& save : m_saves) {
if (dup2(save.saved, save.original) < 0) {
perror("dup2(~SavedFileDescriptors)");
continue;
}
}
}
int main(int argc, char** argv)
{
Core::EventLoop loop;