This patch provides a simple function which fetches the command line of a process given its PID, and some tests for it. It was introduced for safety reasons in introducing socat-based migration to our Xen-handling code.
Signed-off-by: Hrvoje Ribicic <[email protected]> --- lib/utils/process.py | 18 ++++++++++++++++++ test/py/ganeti.utils.process_unittest.py | 21 ++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/utils/process.py b/lib/utils/process.py index f2c29bb..d5760af 100644 --- a/lib/utils/process.py +++ b/lib/utils/process.py @@ -760,6 +760,24 @@ def _GetProcStatusPath(pid): return "/proc/%d/status" % pid +def GetProcCmdline(pid): + """Returns the command line of a pid. + + @type pid: int + @param pid: Process ID + @rtype: string + + @raise EnvironmentError: If the process does not exist + + """ + proc_path = "/proc/%d/cmdline" % pid + with open(proc_path, 'r') as f: + nulled_cmdline = f.read() + # Spaces are replaced with nul chars in the command line - let's turn them + # back + return nulled_cmdline.replace('\x00', ' ') + + def IsProcessAlive(pid): """Check if a given pid exists on the system. diff --git a/test/py/ganeti.utils.process_unittest.py b/test/py/ganeti.utils.process_unittest.py index 2cfb841..a889a09 100755 --- a/test/py/ganeti.utils.process_unittest.py +++ b/test/py/ganeti.utils.process_unittest.py @@ -30,14 +30,15 @@ """Script for testing ganeti.utils.process""" -import unittest -import tempfile -import shutil import os -import stat -import time import select +import shutil import signal +import stat +import subprocess +import tempfile +import time +import unittest from ganeti import constants from ganeti import utils @@ -742,5 +743,15 @@ class RunInSeparateProcess(unittest.TestCase): utils.RunInSeparateProcess, _exc) +class GetCmdline(unittest.TestCase): + def test(self): + sample_cmd = "sleep 2; true" + pid = subprocess.Popen(sample_cmd, shell=True).pid + cmdline = utils.GetProcCmdline(pid) + # Due to the shell prefix which could vary amongst test environments, the + # best we can do barring painful parsing is check for substrings + self.assertTrue(sample_cmd in cmdline) + + if __name__ == "__main__": testutils.GanetiTestProgram() -- 2.2.0.rc0.207.ga3a616c
