Make getting cmdline of a PID a bit more convenient

This commit is contained in:
Kovid Goyal 2022-06-04 15:07:08 +05:30
parent b3578a4fa5
commit 5a0a980648
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 18 additions and 9 deletions

View File

@ -43,11 +43,11 @@ if is_macos:
ans[pgid].append(pid)
return ans
def cmdline_of_process(pid: int) -> List[str]:
def cmdline_of_pid(pid: int) -> List[str]:
return cmdline_(pid)
else:
def cmdline_of_process(pid: int) -> List[str]:
def cmdline_of_pid(pid: int) -> List[str]:
with open(f'/proc/{pid}/cmdline', 'rb') as f:
return list(filter(None, f.read().decode('utf-8').split('\0')))
@ -307,6 +307,15 @@ class Child:
os.close(self.terminal_ready_fd)
self.terminal_ready_fd = -1
def cmdline_of_pid(self, pid: int) -> List[str]:
try:
ans = cmdline_of_pid(pid)
except Exception:
ans = []
if not ans and pid == self.pid:
ans = list(self.argv)
return ans
@property
def foreground_processes(self) -> List[ProcessDesc]:
if self.child_fd is None:
@ -318,7 +327,7 @@ class Child:
def process_desc(pid: int) -> ProcessDesc:
ans: ProcessDesc = {'pid': pid, 'cmdline': None, 'cwd': None}
with suppress(Exception):
ans['cmdline'] = cmdline_of_process(pid)
ans['cmdline'] = self.cmdline_of_pid(pid)
with suppress(Exception):
ans['cwd'] = cwd_of_process(pid) or None
return ans
@ -331,7 +340,7 @@ class Child:
def cmdline(self) -> List[str]:
try:
assert self.pid is not None
return cmdline_of_process(self.pid) or list(self.argv)
return self.cmdline_of_pid(self.pid) or list(self.argv)
except Exception:
return list(self.argv)
@ -339,7 +348,7 @@ class Child:
def foreground_cmdline(self) -> List[str]:
try:
assert self.pid_for_cwd is not None
return cmdline_of_process(self.pid_for_cwd) or self.cmdline
return self.cmdline_of_pid(self.pid_for_cwd) or self.cmdline
except Exception:
return self.cmdline

View File

@ -747,7 +747,6 @@ def remote_edit(msg: str, window: Window) -> None:
def clone_and_launch(msg: str, window: Window) -> None:
from .child import cmdline_of_process
from .shell_integration import serialize_env
c = CloneCmd(msg)
if c.cwd and not c.opts.cwd:
@ -782,8 +781,9 @@ def clone_and_launch(msg: str, window: Window) -> None:
patch_cmdline('env', entry, cmdline)
c.opts.env = []
else:
try:
cmdline = cmdline_of_process(c.pid)
cmdline = window.child.cmdline_of_pid(c.pid)
except Exception:
cmdline = []
if not cmdline:

View File

@ -919,11 +919,11 @@ def is_kitty_gui_cmdline(*cmd: str) -> bool:
def reload_conf_in_all_kitties() -> None:
import signal
from kitty.child import cmdline_of_process
from kitty.child import cmdline_of_pid
for pid in get_all_processes():
try:
cmd = cmdline_of_process(pid)
cmd = cmdline_of_pid(pid)
except Exception:
continue
if cmd and is_kitty_gui_cmdline(*cmd):