giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.08k stars 1.37k forks source link

[SUNOS] `Process.cmdline()` does not handle spaces properly #2357

Open mtelka opened 5 months ago

mtelka commented 5 months ago

Summary

Description

The current Process.cmdline() implementation for SUNOS is this (file psutil/_pssunos.py):

def cmdline(self):
    return self._proc_name_and_args()[1].split(' ')

Unfortunately, this does not work properly when an argument contains space. This causes the psutil/tests/test_process.py::TestProcess::test_long_cmdline test to fail:

________________________ TestProcess.test_long_cmdline _________________________

self = <psutil.tests.test_process.TestProcess testMethod=test_long_cmdline>

    @unittest.skipIf(PYPY, "broken on PYPY")
    def test_long_cmdline(self):
        cmdline = [PYTHON_EXE]
        cmdline.extend(["-v"] * 50)
        cmdline.extend(["-c", "import time; time.sleep(10)"])
        p = self.spawn_psproc(cmdline)
        if OPENBSD:
            # XXX: for some reason the test process may turn into a
            # zombie (don't know why).
            try:
                self.assertEqual(p.cmdline(), cmdline)
            except psutil.ZombieProcess:
                raise self.skipTest("OPENBSD: process turned into zombie")
        else:
>           self.assertEqual(p.cmdline(), cmdline)
E           AssertionError: Lists differ: ['/us[297 chars]', '-v', '-v', '-v', '-c', 'import', 'time;', 'time.sleep(10)'] != ['/us[297 chars]', '-v', '-v', '-v', '-c', 'import time; time.sleep(10)']
E
E           First differing element 52:
E           'import'
E           'import time; time.sleep(10)'
E
E           First list contains 2 additional elements.
E           First extra element 53:
E           'time;'
E
E             ['/usr/bin/python3.9',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-v',
E              '-c',
E           +  'import time; time.sleep(10)']
E           -  'import',
E           -  'time;',
E           -  'time.sleep(10)']

psutil/tests/test_process.py:763: AssertionError

Please note that pargs works properly:

$ pargs 8988                                                                                                                                                                                         
8988:   python -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v -v 
argv[0]: python
argv[1]: -v
argv[2]: -v
argv[3]: -v
argv[4]: -v
argv[5]: -v
argv[6]: -v
argv[7]: -v
argv[8]: -v
argv[9]: -v
argv[10]: -v
argv[11]: -v
argv[12]: -v
argv[13]: -v
argv[14]: -v
argv[15]: -v
argv[16]: -v
argv[17]: -v
argv[18]: -v
argv[19]: -v
argv[20]: -v
argv[21]: -v
argv[22]: -v
argv[23]: -v
argv[24]: -v
argv[25]: -v
argv[26]: -v
argv[27]: -v
argv[28]: -v
argv[29]: -v
argv[30]: -v
argv[31]: -v
argv[32]: -v
argv[33]: -v
argv[34]: -v
argv[35]: -v
argv[36]: -v
argv[37]: -v
argv[38]: -v
argv[39]: -v
argv[40]: -v
argv[41]: -v
argv[42]: -v
argv[43]: -v
argv[44]: -v
argv[45]: -v
argv[46]: -v
argv[47]: -v
argv[48]: -v
argv[49]: -v
argv[50]: -v
argv[51]: -c
argv[52]: import time; time.sleep(60)
$
giampaolo commented 5 months ago

Haven't had access to a SunOS box for a long time now. I'm afraid you're on your own.

mtelka commented 5 months ago

Haven't had access to a SunOS box for a long time now. I'm afraid you're on your own.

BTW, it is possible to install OpenIndiana as a virtual machine. In VirtualBox it works well.

mtelka commented 5 months ago

TestFSAPIsWithInvalidPath.test_proc_cmdline fails similarly too:


self = <psutil.tests.test_unicode.TestFSAPIsWithInvalidPath testMethod=test_proc_cmdline>

    def test_proc_cmdline(self):
        cmd = [self.funky_name, "-c", "import time; time.sleep(10)"]
        subp = self.spawn_testproc(cmd)
        p = psutil.Process(subp.pid)
        cmdline = p.cmdline()
        for part in cmdline:
            self.assertIsInstance(part, str)
        if self.expect_exact_path_match():
>           self.assertEqual(cmdline, cmd)
E           AssertionError: Lists differ: ['/tm[18 chars]94a8zf\udcc0\udc80', '-c', 'import', 'time;', 'time.sleep(10)'] != ['/tm[18 chars]94a8zf\udcc0\udc80', '-c', 'import time; time.sleep(10)']
E           
E           First differing element 2:
E           'import'
E           'import time; time.sleep(10)'
E           
E           First list contains 2 additional elements.
E           First extra element 3:
E           'time;'
E           
E           + ['/tmp/@psutil-1700-7zi94a8zf\udcc0\udc80', '-c', 'import time; time.sleep(10)']
E           - ['/tmp/@psutil-1700-7zi94a8zf\udcc0\udc80',
E           -  '-c',
E           -  'import',
E           -  'time;',
E           -  'time.sleep(10)']

psutil/tests/test_unicode.py:227: AssertionError