process_registry_wsl.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Detect background spawns routed through a ``wsl[.exe]`` launcher chain (#120546)."""
  2. from __future__ import annotations
  3. import os
  4. import shlex
  5. _WSL_LAUNCHER_NAMES = frozenset({"wsl", "wsl.exe"})
  6. def _is_wsl_launcher_command(command: str) -> bool:
  7. """True when *command* routes through a ``wsl[.exe]`` launcher chain (#120546).
  8. The host PID recorded for such a spawn belongs to the short-lived launcher;
  9. grandchildren inside the VM outlive it, so the entry must say so instead of
  10. letting host-side hunting fail silently.
  11. """
  12. if not isinstance(command, str) or not command.strip():
  13. return False
  14. candidates = []
  15. try:
  16. candidates.append((shlex.split(command, posix=True) or [""])[0])
  17. except ValueError:
  18. pass
  19. # POSIX shlex eats Windows backslashes (``C:\...\wsl.exe``), so also try
  20. # the naive first token where path separators survive.
  21. words = command.strip().split()
  22. if words:
  23. candidates.append(words[0])
  24. for first in candidates:
  25. base = os.path.basename(first.replace("\\", "/")).strip("'\"").lower()
  26. if base in _WSL_LAUNCHER_NAMES:
  27. return True
  28. return False