Mercurial > dropbear
comparison test/parent_dropbear_map.py @ 1874:1c9215154d4a
Handle /proc/.../maps being reordered
We now search for the first r-xp line in the file
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Feb 2022 22:13:06 +0800 |
parents | 2b3a8026a6ce |
children |
comparison
equal
deleted
inserted
replaced
1873:2c9d635a1c04 | 1874:1c9215154d4a |
---|---|
7 | 7 |
8 from pathlib import Path | 8 from pathlib import Path |
9 | 9 |
10 | 10 |
11 want_name = "dropbear" | 11 want_name = "dropbear" |
12 # Walks up the parent process tree, prints the first line of /proc/pid/maps when | 12 # Walks up the parent process tree, prints a r-xp line of /proc/pid/maps when |
13 # it finds the wanted name | 13 # it finds the wanted name |
14 | 14 |
15 def main(): | 15 def main(): |
16 | 16 |
17 try: | 17 try: |
20 print(p.name(), file=sys.stderr) | 20 print(p.name(), file=sys.stderr) |
21 print(p.cmdline(), file=sys.stderr) | 21 print(p.cmdline(), file=sys.stderr) |
22 | 22 |
23 if want_name in p.name(): | 23 if want_name in p.name(): |
24 with (Path('/proc') / str(p.pid) / "maps").open() as f: | 24 with (Path('/proc') / str(p.pid) / "maps").open() as f: |
25 map0 = f.readline().rstrip() | 25 for i, l in enumerate(f, 1): |
26 print(map0) | 26 if ' r-xp ' in l: |
27 print(l.rstrip()) | |
28 break | |
27 return | 29 return |
28 | 30 |
29 raise RuntimeError(f"Couldn't find parent {want_name} process") | 31 raise RuntimeError(f"Couldn't find parent {want_name} process") |
30 except Exception as e: | 32 except Exception as e: |
31 print(psutil.Process().parents()) | 33 print(psutil.Process().parents()) |