diff options
author | Christian Breunig <christian@breunig.cc> | 2023-05-28 21:14:42 +0200 |
---|---|---|
committer | Christian Breunig <christian@breunig.cc> | 2023-05-28 21:17:43 +0200 |
commit | 9c677c81be6a6e62958c73b038c2a36f1f629108 (patch) | |
tree | 958e04112283879d3e68efdb4641ecfad26556dd /python/vyos | |
parent | 1c83615492b626f4d897a6a0fda9324353aae416 (diff) | |
download | vyos-1x-9c677c81be6a6e62958c73b038c2a36f1f629108.tar.gz vyos-1x-9c677c81be6a6e62958c73b038c2a36f1f629108.zip |
vyos.util: extend process_named_running() signature with cmdline
process_named_running() was introduced in commit 16b2fc8fc4ca ("dns-forwarding:
T2298: fix path to control file") and thus remained more or less unchanged.
Smoketests use process_named_running() heavily and might spawn multiple
processes with the same name but ifferent options (e.g. dhcp6c or dhclient) and
it was yet not possible to properly filter on the "real-deal" like the process
bound to a given interface.
One can now optionally specify a string that is searched inside the command
line argument list of the process.
Example:
>>> process_named_running('dhcp6c', 'veth0')
['/usr/sbin/dhcp6c', '-D', '-k', '/run/dhcp6c/dhcp6c.veth0.sock', '-c',
'/run/dhcp6c/dhcp6c.veth0.conf', '-p', '/run/dhcp6c/dhcp6c.veth0.pid', 'veth0']
4215
>>> process_named_running('dhcp6c', 'veth1')
['/usr/sbin/dhcp6c', '-D', '-k', '/run/dhcp6c/dhcp6c.veth1.sock', '-c',
'/run/dhcp6c/dhcp6c.veth1.conf', '-p', '/run/dhcp6c/dhcp6c.veth1.pid', 'veth1']
4253
Where the debug list returned is the commandline searched.
Diffstat (limited to 'python/vyos')
-rw-r--r-- | python/vyos/util.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index d5330db13..61ce59324 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -463,14 +463,17 @@ def process_running(pid_file): pid = f.read().strip() return pid_exists(int(pid)) -def process_named_running(name): +def process_named_running(name, cmdline: str=None): """ Checks if process with given name is running and returns its PID. If Process is not running, return None """ from psutil import process_iter - for p in process_iter(): - if name in p.name(): - return p.pid + for p in process_iter(['name', 'pid', 'cmdline']): + if cmdline: + if p.info['name'] == name and cmdline in p.info['cmdline']: + return p.info['pid'] + elif p.info['name'] == name: + return p.info['pid'] return None def is_list_equal(first: list, second: list) -> bool: |