/// It should search the job list for something matching the given proc. static bool find_job_by_name(const wchar_t *proc, std::vector<job_id_t> &ids, const parser_t &parser) { bool found = false; for (const auto &j : parser.jobs()) { if (j->command_is_empty()) continue; if (match_pid(j->command(), proc)) { if (!contains(ids, j->job_id)) { // If pids doesn't already have the pgid, add it. ids.push_back(j->job_id); } found = true; } // Check if the specified pid is a child process of the job. for (const process_ptr_t &p : j->processes) { if (p->actual_cmd.empty()) continue; if (match_pid(p->actual_cmd, proc)) { if (!contains(ids, j->job_id)) { // If pids doesn't already have the pgid, add it. ids.push_back(j->job_id); } found = true; } } } return found; }
static bool all_jobs_finished(const parser_t &parser) { for (const auto &j : parser.jobs()) { // If any job is not completed, return false. // If there are stopped jobs, they are ignored. if (j->is_constructed() && !j->is_completed() && !j->is_stopped()) { return false; } } return true; }
static int wait_for_backgrounds(parser_t &parser, bool any_flag) { size_t jobs_len = parser.jobs().size(); while ((!any_flag && !all_jobs_finished(parser)) || (any_flag && !any_jobs_finished(jobs_len, parser))) { if (reader_test_interrupted()) { return 128 + SIGINT; } proc_wait_any(parser); } return 0; }
static bool any_jobs_finished(size_t jobs_len, const parser_t &parser) { bool no_jobs_running = true; // If any job is removed from list, return true. if (jobs_len != parser.jobs().size()) { return true; } for (const auto &j : parser.jobs()) { // If any job is completed, return true. if (j->is_constructed() && (j->is_completed() || j->is_stopped())) { return true; } // Check for jobs running exist or not. if (j->is_constructed() && !j->is_stopped()) { no_jobs_running = false; } } if (no_jobs_running) { return true; } return false; }
/// Return the job id to which the process with pid belongs. /// If a specified process has already finished but the job hasn't, parser_t::job_get_from_pid() /// doesn't work properly, so use this function in wait command. static job_id_t get_job_id_from_pid(pid_t pid, const parser_t &parser) { for (const auto &j : parser.jobs()) { if (j->pgid == pid) { return j->job_id; } // Check if the specified pid is a child process of the job. for (const process_ptr_t &p : j->processes) { if (p->pid == pid) { return j->job_id; } } } return 0; }