Merge "Fix race condition when getting cmdline" into stable/rocky
This commit is contained in:
commit
495312c92a
|
@ -313,19 +313,24 @@ def process_is_running(pid):
|
||||||
def get_cmdline_from_pid(pid):
|
def get_cmdline_from_pid(pid):
|
||||||
if not process_is_running(pid):
|
if not process_is_running(pid):
|
||||||
return []
|
return []
|
||||||
with open('/proc/%s/cmdline' % pid, 'r') as f:
|
# NOTE(jh): Even after the above check, the process may terminate
|
||||||
cmdline = f.readline().split('\0')[:-1]
|
# before the open below happens
|
||||||
|
try:
|
||||||
|
with open('/proc/%s/cmdline' % pid, 'r') as f:
|
||||||
|
cmdline = f.readline().split('\0')[:-1]
|
||||||
|
except IOError:
|
||||||
|
return []
|
||||||
|
|
||||||
# NOTE(slaweq): sometimes it may happen that values in
|
# NOTE(slaweq): sometimes it may happen that values in
|
||||||
# /proc/{pid}/cmdline are separated by space instead of NUL char,
|
# /proc/{pid}/cmdline are separated by space instead of NUL char,
|
||||||
# in such case we would have everything in one element of cmdline_args
|
# in such case we would have everything in one element of cmdline_args
|
||||||
# list and it would not match to expected cmd so we need to try to
|
# list and it would not match to expected cmd so we need to try to
|
||||||
# split it by spaces
|
# split it by spaces
|
||||||
if len(cmdline) == 1:
|
if len(cmdline) == 1:
|
||||||
cmdline = cmdline[0].split(' ')
|
cmdline = cmdline[0].split(' ')
|
||||||
|
|
||||||
LOG.debug("Found cmdline %s for process with PID %s.", cmdline, pid)
|
LOG.debug("Found cmdline %s for process with PID %s.", cmdline, pid)
|
||||||
return cmdline
|
return cmdline
|
||||||
|
|
||||||
|
|
||||||
def cmd_matches_expected(cmd, expected_cmd):
|
def cmd_matches_expected(cmd, expected_cmd):
|
||||||
|
|
|
@ -336,6 +336,16 @@ class TestGetCmdlineFromPid(base.BaseTestCase):
|
||||||
mock_open.assert_not_called()
|
mock_open.assert_not_called()
|
||||||
self.assertEqual([], cmdline)
|
self.assertEqual([], cmdline)
|
||||||
|
|
||||||
|
def test_cmdline_process_disappearing(self):
|
||||||
|
self.process_is_running_mock.return_value = True
|
||||||
|
mock_open = self.useFixture(
|
||||||
|
tools.OpenFixture('/proc/%s/cmdline' % self.pid, 'process')
|
||||||
|
).mock_open
|
||||||
|
mock_open.side_effect = IOError()
|
||||||
|
cmdline = utils.get_cmdline_from_pid(self.pid)
|
||||||
|
mock_open.assert_called_once_with('/proc/%s/cmdline' % self.pid, 'r')
|
||||||
|
self.assertEqual([], cmdline)
|
||||||
|
|
||||||
|
|
||||||
class TestFindChildPids(base.BaseTestCase):
|
class TestFindChildPids(base.BaseTestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue