| 632 | } |
| 633 | |
| 634 | Process getProcessTreeFromJob(HANDLE h) |
| 635 | { |
| 636 | const auto ids = processesInJob(h); |
| 637 | if (ids.empty()) { |
| 638 | return {}; |
| 639 | } |
| 640 | |
| 641 | std::vector<Process> ps; |
| 642 | |
| 643 | forEachRunningProcess([&](auto&& entry) { |
| 644 | for (auto&& id : ids) { |
| 645 | if (entry.th32ProcessID == id) { |
| 646 | ps.push_back(Process(entry.th32ProcessID, entry.th32ParentProcessID, |
| 647 | QString::fromStdWString(entry.szExeFile))); |
| 648 | |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | return true; |
| 654 | }); |
| 655 | |
| 656 | Process root; |
| 657 | |
| 658 | { |
| 659 | // getting processes whose parent is not in the list |
| 660 | for (auto&& possibleRoot : ps) { |
| 661 | const auto ppid = possibleRoot.ppid(); |
| 662 | bool found = false; |
| 663 | |
| 664 | for (auto&& p : ps) { |
| 665 | if (p.pid() == ppid) { |
| 666 | found = true; |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (!found) { |
| 672 | // this is a root process |
| 673 | root.addChild(possibleRoot); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // removing root processes from the list |
| 678 | auto newEnd = std::remove_if(ps.begin(), ps.end(), [&](auto&& p) { |
| 679 | for (auto&& rp : root.children()) { |
| 680 | if (rp.pid() == p.pid()) { |
| 681 | return true; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | return false; |
| 686 | }); |
| 687 | |
| 688 | ps.erase(newEnd, ps.end()); |
| 689 | } |
| 690 | |
| 691 | // at this point, `processes` should only contain processes that are direct |
no test coverage detected