Ejemplo n.º 1
0
    def test_get_cmdline_from_pid_and_pid_invoked_with_cmdline(self):
        cmd = ['tail', '-f', self.test_file_path]
        proc = async_process.AsyncProcess(cmd)
        proc.start(block=True)
        self.addCleanup(proc.stop)

        pid = proc.pid
        self.assertEqual(cmd, utils.get_cmdline_from_pid(pid))
        self.assertTrue(utils.pid_invoked_with_cmdline(pid, cmd))
        self.assertEqual([], utils.get_cmdline_from_pid(-1))
Ejemplo n.º 2
0
 def test_no_process_running(self):
     self.process_is_running_mock.return_value = False
     mock_open = self.useFixture(
         tools.OpenFixture('/proc/%s/cmdline' % self.pid)).mock_open
     cmdline = utils.get_cmdline_from_pid(self.pid)
     mock_open.assert_not_called()
     self.assertEqual([], cmdline)
Ejemplo n.º 3
0
def _kill_listen_processes(namespace, force=False):
    """Identify all listening processes within the given namespace.

    Then, for each one, find its top parent with same cmdline (in case this
    process forked) and issue a SIGTERM to all of them. If force is True,
    then a SIGKILL will be issued to all parents and all their children. Also,
    this function returns the number of listening processes.
    """
    pids = find_listen_pids_namespace(namespace)
    pids_to_kill = {utils.find_fork_top_parent(pid) for pid in pids}
    kill_signal = signal.SIGTERM
    if force:
        kill_signal = signal.SIGKILL
        children = [utils.find_child_pids(pid, True) for pid in pids_to_kill]
        pids_to_kill.update(itertools.chain.from_iterable(children))

    for pid in pids_to_kill:
        # Throw a warning since this particular cleanup may need a specific
        # implementation in the right module. Ideally, netns_cleanup wouldn't
        # kill any processes as the responsible module should've killed them
        # before cleaning up the namespace
        LOG.warning("Killing (%(signal)d) [%(pid)s] %(cmdline)s",
                    {'signal': kill_signal,
                     'pid': pid,
                     'cmdline': ' '.join(utils.get_cmdline_from_pid(pid))[:80]
                     })
        try:
            utils.kill_process(pid, kill_signal, run_as_root=True)
        except Exception as ex:
            LOG.error('An error occurred while killing '
                      '[%(pid)s]: %(msg)s', {'pid': pid, 'msg': ex})
    return len(pids)
Ejemplo n.º 4
0
def _kill_listen_processes(namespace, force=False):
    """Identify all listening processes within the given namespace.

    Then, for each one, find its top parent with same cmdline (in case this
    process forked) and issue a SIGTERM to all of them. If force is True,
    then a SIGKILL will be issued to all parents and all their children. Also,
    this function returns the number of listening processes.
    """
    pids = find_listen_pids_namespace(namespace)
    pids_to_kill = {utils.find_fork_top_parent(pid) for pid in pids}
    kill_signal = signal.SIGTERM
    if force:
        kill_signal = signal.SIGKILL
        children = [utils.find_child_pids(pid, True) for pid in pids_to_kill]
        pids_to_kill.update(itertools.chain.from_iterable(children))

    for pid in pids_to_kill:
        # Throw a warning since this particular cleanup may need a specific
        # implementation in the right module. Ideally, netns_cleanup wouldn't
        # kill any processes as the responsible module should've killed them
        # before cleaning up the namespace
        LOG.warning(_LW("Killing (%(signal)d) [%(pid)s] %(cmdline)s"),
                    {'signal': kill_signal,
                     'pid': pid,
                     'cmdline': ' '.join(utils.get_cmdline_from_pid(pid))[:80]
                     })
        try:
            utils.kill_process(pid, kill_signal, run_as_root=True)
        except Exception as ex:
            LOG.error(_LE('An error occurred while killing '
                          '[%(pid)s]: %(msg)s'), {'pid': pid, 'msg': ex})
    return len(pids)
Ejemplo n.º 5
0
 def test_no_process_running(self):
     self.process_is_running_mock.return_value = False
     mock_open = self.useFixture(
         tools.OpenFixture('/proc/%s/cmdline' % self.pid)
     ).mock_open
     cmdline = utils.get_cmdline_from_pid(self.pid)
     mock_open.assert_not_called()
     self.assertEqual([], cmdline)
Ejemplo n.º 6
0
 def _test_cmdline(self, process, expected_cmd):
     self.process_is_running_mock.return_value = True
     mock_open = self.useFixture(
         tools.OpenFixture('/proc/%s/cmdline' % self.pid, process)
     ).mock_open
     cmdline = utils.get_cmdline_from_pid(self.pid)
     mock_open.assert_called_once_with('/proc/%s/cmdline' % self.pid, 'r')
     self.assertEqual(expected_cmd, cmdline)
Ejemplo n.º 7
0
 def test_cmdline_process_disappearing(self):
     self.process_is_running_mock.return_value = True
     mock_open = self.useFixture(
         lib_fixtures.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)