1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 04:27:35 +00:00

Shell: Store jobs as NonnullRefPtr<Job>

This commit is contained in:
Andreas Kling 2020-08-06 14:09:13 +02:00
parent 5bce0193de
commit b8440b12b7
2 changed files with 16 additions and 14 deletions

View file

@ -165,7 +165,7 @@ public:
int last_return_code { 0 }; int last_return_code { 0 };
Vector<String> directory_stack; Vector<String> directory_stack;
CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
HashMap<u64, RefPtr<Job>> jobs; HashMap<u64, NonnullRefPtr<Job>> jobs;
Vector<String, 256> cached_path; Vector<String, 256> cached_path;
enum ShellEventType { enum ShellEventType {

View file

@ -83,15 +83,17 @@ int main(int argc, char** argv)
Core::EventLoop::register_signal(SIGCHLD, [](int) { Core::EventLoop::register_signal(SIGCHLD, [](int) {
auto& jobs = s_shell->jobs; auto& jobs = s_shell->jobs;
Vector<u64> disowned_jobs; Vector<u64> disowned_jobs;
for (auto& job : jobs) { for (auto& it : jobs) {
auto job_id = it.key;
auto& job = *it.value;
int wstatus = 0; int wstatus = 0;
auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG | WUNTRACED); auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED);
if (child_pid < 0) { if (child_pid < 0) {
if (errno == ECHILD) { if (errno == ECHILD) {
// The child process went away before we could process its death, just assume it exited all ok. // The child process went away before we could process its death, just assume it exited all ok.
// FIXME: This should never happen, the child should stay around until we do the waitpid above. // FIXME: This should never happen, the child should stay around until we do the waitpid above.
dbg() << "Child process gone, cannot get exit code for " << job.key; dbg() << "Child process gone, cannot get exit code for " << job_id;
child_pid = job.value->pid(); child_pid = job.pid();
} else { } else {
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
@ -102,21 +104,21 @@ int main(int argc, char** argv)
continue; continue;
} }
#endif #endif
if (child_pid == job.value->pid()) { if (child_pid == job.pid()) {
if (WIFEXITED(wstatus)) { if (WIFEXITED(wstatus)) {
job.value->set_has_exit(WEXITSTATUS(wstatus)); job.set_has_exit(WEXITSTATUS(wstatus));
} else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) { } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
job.value->set_has_exit(126); job.set_has_exit(126);
} else if (WIFSTOPPED(wstatus)) { } else if (WIFSTOPPED(wstatus)) {
job.value->unblock(); job.unblock();
job.value->set_is_suspended(true); job.set_is_suspended(true);
} }
} }
if (job.value->should_be_disowned()) if (job.should_be_disowned())
disowned_jobs.append(job.key); disowned_jobs.append(job_id);
} }
for (auto key : disowned_jobs) for (auto job_id : disowned_jobs)
jobs.remove(key); jobs.remove(job_id);
}); });
Core::EventLoop::register_signal(SIGTSTP, [](auto) { Core::EventLoop::register_signal(SIGTSTP, [](auto) {