Skip to content

Commit

Permalink
Shell: Do not attempt to yank the TTY from background processes
Browse files Browse the repository at this point in the history
They don't own the TTY anyway!
  • Loading branch information
alimpfard authored and awesomekling committed Aug 19, 2020
1 parent 217eca3 commit cf18bff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Shell/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Job : public RefCounted<Job> {
bool should_be_disowned() const { return m_should_be_disowned; }
void disown() { m_should_be_disowned = true; }
bool is_running_in_background() const { return m_running_in_background; }
bool should_announce_exit() const { return m_should_announce_exit; }
bool is_suspended() const { return m_is_suspended; }
void unblock() const
{
Expand Down Expand Up @@ -113,6 +114,8 @@ class Job : public RefCounted<Job> {
m_running_in_background = running_in_background;
}

void set_should_announce_exit(bool value) { m_should_announce_exit = value; }

void deactivate() const { m_active = false; }

enum class PrintStatusMode {
Expand Down Expand Up @@ -143,6 +146,7 @@ class Job : public RefCounted<Job> {
String m_cmd;
bool m_exited { false };
bool m_running_in_background { false };
bool m_should_announce_exit { false };
int m_exit_code { -1 };
int m_term_sig { -1 };
Core::ElapsedTimer m_command_timer;
Expand Down
19 changes: 11 additions & 8 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
job->on_exit = [](auto job) {
if (!job->exited())
return;
if (job->is_running_in_background())
if (job->is_running_in_background() && job->should_announce_exit())
fprintf(stderr, "Shell: Job %" PRIu64 "(%s) exited\n", job->job_id(), job->cmd().characters());
job->disown();
};
Expand Down Expand Up @@ -670,10 +670,10 @@ NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
jobs_to_wait_for.append(*job);
} else {
if (command.is_pipe_source) {
job->set_running_in_background(true);
jobs_to_wait_for.append(*job);
} else if (command.should_notify_if_in_background) {
job->set_running_in_background(true);
restore_ios();
job->set_should_announce_exit(true);
}
}
}
Expand Down Expand Up @@ -710,23 +710,26 @@ void Shell::block_on_job(RefPtr<Job> job)
if (!job)
return;

ScopeGuard io_restorer { [&]() {
if (job->exited() && !job->is_running_in_background()) {
restore_ios();
}
} };

Core::EventLoop loop;
job->on_exit = [&, old_exit = move(job->on_exit)](auto job) {
if (old_exit)
old_exit(job);
loop.quit(0);
};
if (job->exited()) {
restore_ios();

if (job->exited())
return;
}

loop.exec();

if (job->is_suspended())
job->print_status(Job::PrintStatusMode::Basic);

restore_ios();
}

String Shell::get_history_path()
Expand Down

0 comments on commit cf18bff

Please sign in to comment.