* Returns a process tree rooting at the give process ID.
( rootPid: number, logger: ILogger, )
| 74 | * Returns a process tree rooting at the give process ID. |
| 75 | */ |
| 76 | async function getProcessTree( |
| 77 | rootPid: number, |
| 78 | logger: ILogger, |
| 79 | ): Promise<IProcessTreeNode | undefined> { |
| 80 | const map = new Map<number, IProcessTreeNode>(); |
| 81 | |
| 82 | map.set(0, { children: [], pid: 0, ppid: 0, command: '', args: '' }); |
| 83 | |
| 84 | try { |
| 85 | await processTree.lookup(({ pid, ppid, command, args }) => { |
| 86 | if (pid !== ppid) { |
| 87 | map.set(pid, { pid, ppid, command, args, children: [] }); |
| 88 | } |
| 89 | return map; |
| 90 | }, null); |
| 91 | } catch (err) { |
| 92 | logger.warn(LogTag.Internal, 'Error getting child process tree', err); |
| 93 | return undefined; |
| 94 | } |
| 95 | |
| 96 | const values = map.values(); |
| 97 | for (const p of values) { |
| 98 | const parent = map.get(p.ppid); |
| 99 | if (parent && parent !== p) { |
| 100 | parent.children.push(p); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!isNaN(rootPid) && rootPid > 0) { |
| 105 | return map.get(rootPid); |
| 106 | } |
| 107 | |
| 108 | return map.get(0); |
| 109 | } |