]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/munet/cleanup.py
1 # -*- coding: utf-8 eval: (blacken-mode 1) -*-
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # September 30 2021, Christian Hopps <chopps@labn.net>
6 # Copyright 2021, LabN Consulting, L.L.C.
8 """Provides functionality to cleanup processes on posix systems."""
15 def get_pids_with_env(has_var
, has_val
=None):
17 for pidenv
in glob
.iglob("/proc/*/environ"):
18 pid
= pidenv
.split("/")[2]
20 with
open(pidenv
, "rb") as rfb
:
22 x
.decode("utf-8").split("=", 1) for x
in rfb
.read().split(b
"\0")
24 envlist
= [[x
[0], ""] if len(x
) == 1 else x
for x
in envlist
]
25 envdict
= dict(envlist
)
26 if has_var
not in envdict
:
30 elif envdict
[has_var
] == str(has_val
):
33 # E.g., process exited and files are gone
38 def _kill_piddict(pids_by_upid
, sig
):
39 ourpid
= str(os
.getpid())
40 for upid
, pids
in pids_by_upid
:
41 logging
.info("Sending %s to (%s) of munet pid %s", sig
, ", ".join(pids
), upid
)
45 cmdline
= open(f
"/proc/{pid}/cmdline", "r", encoding
="ascii").read()
46 cmdline
= cmdline
.replace("\x00", " ")
47 logging
.info("killing proc %s (%s)", pid
, cmdline
)
48 os
.kill(int(pid
), sig
)
54 ourpid
= str(os
.getpid())
55 piddict
= get_pids_with_env("MUNET_PID", ourpid
)
56 pids
= [x
for x
in piddict
if x
!= ourpid
]
62 def _get_other_pids():
63 piddict
= get_pids_with_env("MUNET_PID")
64 unet_pids
= {d
["MUNET_PID"] for d
in piddict
.values()}
65 pids_by_upid
= {p
: set() for p
in unet_pids
}
66 for pid
, envdict
in piddict
.items():
67 unet_pid
= envdict
["MUNET_PID"]
68 pids_by_upid
[unet_pid
].add(pid
)
69 # Filter out any child pid sets whos munet pid is still running
70 return {x
: y
for x
, y
in pids_by_upid
.items() if x
not in y
}
73 def _get_pids_by_upid(ours
):
75 return _get_our_pids()
76 return _get_other_pids()
79 def _cleanup_pids(ours
):
80 pids_by_upid
= _get_pids_by_upid(ours
).items()
84 t
= "current" if ours
else "previous"
85 logging
.info("Reaping %s munet processes", t
)
87 # _kill_piddict(pids_by_upid, signal.SIGTERM)
89 # # Give them 5 second to exit cleanly
90 # logging.info("Waiting up to 5s to allow for clean exit of abandon'd pids")
91 # for _ in range(0, 5):
92 # pids_by_upid = _get_pids_by_upid(ours).items()
93 # if not pids_by_upid:
97 pids_by_upid
= _get_pids_by_upid(ours
).items()
98 _kill_piddict(pids_by_upid
, signal
.SIGKILL
)
101 def cleanup_current():
102 """Attempt to cleanup preview runs.
104 Currently this only scans for old processes.
109 def cleanup_previous():
110 """Attempt to cleanup preview runs.
112 Currently this only scans for old processes.