summaryrefslogtreecommitdiff
path: root/test/parent_dropbear_map.py
blob: 34a8b9fe2c929d92f03a1fed12af86e16ce0d023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

import os
import sys
import time
import psutil

from pathlib import Path


want_name = "dropbear"
# Walks up the parent process tree, prints the first line of /proc/pid/maps when
# it finds the wanted name

def main():

	try:
		for p in psutil.Process().parents():
			print(p.pid, file=sys.stderr)
			print(p.name(), file=sys.stderr)
			print(p.cmdline(), file=sys.stderr)

			if want_name in p.name():
				with (Path('/proc') / str(p.pid) / "maps").open() as f:
					map0 = f.readline().rstrip()
				print(map0)
				return

		raise RuntimeError(f"Couldn't find parent {want_name} process")
	except Exception as e:
		print(psutil.Process().parents())
		for p in psutil.Process().parents():
			print(p.name())
		print(e)
		# time.sleep(100)
		raise

if __name__ == "__main__":
	main()