parseLsofPids function

List<int> parseLsofPids(
  1. String output, {
  2. required int selfPid,
})

The PIDs lsof's -F output names, minus selfPid.

-F is a record format, not a table: one tagged field per line, and only a p record carries a PID. Every other row — a header, a +D warning, a permission complaint — names no process and is dropped rather than guessed at.

Implementation

List<int> parseLsofPids(String output, {required int selfPid}) => [
  for (final line in output.split('\n'))
    if (line.startsWith('p'))
      // `selfPid` is this process: never in the kill set, whatever lsof says.
      if (int.tryParse(line.substring(1)) case final resident?)
        if (resident != selfPid) resident,
];