| 16 | } |
| 17 | |
| 18 | export async function findProcesses(shellServer: ShellServer) { |
| 19 | const ps = 'for pid in `cd /proc && ls -d [0-9]*`; do { echo $pid ; readlink /proc/$pid/cwd ; readlink /proc/$pid/ns/mnt ; cat /proc/$pid/stat | tr "\n" " " ; echo ; xargs -0 < /proc/$pid/environ ; xargs -0 < /proc/$pid/cmdline ; } ; echo --- ; done ; readlink /proc/self/ns/mnt 2>/dev/null'; |
| 20 | const { stdout } = await shellServer.exec(ps, { logOutput: false }); |
| 21 | |
| 22 | const n = 6; |
| 23 | const sections = stdout.split('\n---\n'); |
| 24 | const mntNS = sections.pop()!.trim(); |
| 25 | const processes: Process[] = sections |
| 26 | .map(line => line.split('\n')) |
| 27 | .filter(parts => parts.length >= n) |
| 28 | .map(([pid, cwd, mntNS, stat, env, cmd]) => { |
| 29 | const statM: (string | undefined)[] = /.*\) [^ ]* ([^ ]*) ([^ ]*)/.exec(stat) || []; |
| 30 | return { |
| 31 | pid, |
| 32 | ppid: statM[1], |
| 33 | pgrp: statM[2], |
| 34 | cwd, |
| 35 | mntNS, |
| 36 | cmd, |
| 37 | env: env.split(' ') |
| 38 | .reduce((env, current) => { |
| 39 | const i = current.indexOf('='); |
| 40 | if (i !== -1) { |
| 41 | env[current.substr(0, i)] = current.substr(i + 1); |
| 42 | } |
| 43 | return env; |
| 44 | }, {} as Record<string, string>), |
| 45 | }; |
| 46 | }); |
| 47 | return { |
| 48 | processes, |
| 49 | mntNS, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | export interface ProcessTree { |
| 54 | process: Process; |