portOwners function

Future<PortOwners> portOwners(
  1. int port
)

The processes listening on TCP port; lsof exits 1 with no output when nobody does.

Implementation

Future<PortOwners> portOwners(int port) async {
  final out = await _lsof(['-nP', '-iTCP:$port', '-sTCP:LISTEN', '-Fp']);
  if (out == null) return (known: false, pids: const <int>[]);
  final pids = <int>[];
  for (final line in out.split('\n')) {
    final pid = line.startsWith('p') ? int.tryParse(line.substring(1)) : null;
    if (pid != null && !pids.contains(pid)) pids.add(pid);
  }
  return (known: true, pids: pids);
}