From 74cf4e1920db1edae6728cfe632e4ca7aea5be59 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 19 Aug 2014 16:14:03 -0700 Subject: porting: `pidof` portability for OSX/FreeBSD - Provide a portable `pidof` just to be used specifically with glusterfs regression tests on OSX and FreeBSD. This was written after countless hrs of effort to get a sane `pidof` working on either of the environments. `pidof` comes at the wake of lack of proper procfs support and also incompatible way of handling process names since glusterd/glusterfs are symbolic links to 'glusterfsd' - tests/utils/* directory should be part of 'PATH' to avoid abspath calculation using $(dirname) - cleanup() - rpcinfo command prints error on FreeBSD/OSX fix it Change-Id: I35f86273624cb279da1c8fae056ca27669e251d8 BUG: 1131713 Signed-off-by: Harshavardhana Reviewed-on: http://review.gluster.org/8499 Reviewed-by: Jeff Darcy Tested-by: Gluster Build System --- tests/utils/pidof.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 tests/utils/pidof.py (limited to 'tests/utils/pidof.py') diff --git a/tests/utils/pidof.py b/tests/utils/pidof.py new file mode 100755 index 00000000000..575b899b6cc --- /dev/null +++ b/tests/utils/pidof.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import sys + +try: + import psutil +except ImportError: + print("Please install psutil --> pip install psutil") + sys.exit(1) + +def pmap_find(p, name): + for m in p.memory_maps(grouped=True): + if m.path.endswith("%s.so" % name): + return True + continue + return False + +def pidof(processname): + for p in psutil.process_iter(): + if p.pid == 0: + continue + if "gluster" in processname: + if processname == "glusterd" and pmap_find(p, "glusterd"): + print (p.pid) + if processname == "glusterfs" and pmap_find(p, "client"): + print (p.pid) + if processname == "glusterfsd" and pmap_find(p, "posix-acl"): + print (p.pid) + continue + if processname.strip() == p.name(): + print (p.pid) + +def main(argv): + if len(argv) < 2: + sys.stderr.write("Usage: %s \n" % (argv[0],)) + return 1 + try: + pidof(argv[1]) + except Exception as err: + print err + sys.stderr.write("Please be root - %s\n" % err); + sys.exit(1) + +if __name__ == "__main__": + main(sys.argv) -- cgit