File 0004-low-utils-Use-proc-for-process-discovery.patch of Package crmsh

From 931abc46808ff427134031765835fb3f803a40ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kristoffer=20Gr=C3=B6nlund?= <krig@koru.se>
Date: Thu, 16 Feb 2017 13:51:08 +0100
Subject: [PATCH 4/4] low: utils: Use /proc for process discovery

Parsing ps output turns out to be tricky, so
use the /proc filesystem for process discovery
instead.

Fixes: #182
---
 crmsh/utils.py | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/crmsh/utils.py b/crmsh/utils.py
index e48cef59..8b2d48d3 100644
--- a/crmsh/utils.py
+++ b/crmsh/utils.py
@@ -952,14 +952,25 @@ def is_int(s):
 
 
 def is_process(s):
-    cmd = "ps -e -o pid,command | grep -qs '%s'" % s
-    if options.regression_tests:
-        print ".EXT", cmd
-    proc = subprocess.Popen(cmd,
-                            shell=True,
-                            stdout=subprocess.PIPE)
-    proc.wait()
-    return proc.returncode == 0
+    """
+    Returns true if argument is the name of a running process.
+
+    s: process name
+    returns Boolean
+    """
+    from os.path import join, basename
+    # find pids of running processes
+    pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
+    for pid in pids:
+        try:
+            cmdline = open(join('/proc', pid, 'cmdline'), 'rb').read()
+            procname = basename(cmdline.replace('\x00', ' ').split(' ')[0])
+            if procname == s:
+                return True
+        except os.error:
+            # a process may have died since we got the list of pids
+            pass
+    return False
 
 
 def print_stacktrace():
-- 
2.11.1

openSUSE Build Service is sponsored by