diff options
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/context.py | 53 | ||||
-rw-r--r-- | tests/test_process.py | 124 | ||||
-rwxr-xr-x | whatmaps/command.py | 52 | ||||
-rw-r--r-- | whatmaps/process.py | 77 |
5 files changed, 256 insertions, 50 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/context.py b/tests/context.py new file mode 100644 index 0000000..d859c2f --- /dev/null +++ b/tests/context.py @@ -0,0 +1,53 @@ +# this context.py should be included by all tests +# idea from http://kennethreitz.com/repository-structure-and-python.html + +import os +import shutil +import sys +import tempfile + +import whatmaps + +sys.path.insert(0, os.path.abspath('..')) + +# the top or root dir of the git-buildpackage source tree to be used by tests +projectdir = os.path.dirname(os.path.dirname(os.path.abspath(whatmaps.__file__))) + +_chdir_backup = None +_tmpdirs = [] + +def chdir(dir): + global _chdir_backup + if not _chdir_backup: + _chdir_backup = os.path.abspath(os.curdir) + os.chdir(str(dir)) + +def new_tmpdir(name): + global _tmpdirs + prefix='whatmaps_%s_' % name + tmpdir = TmpDir(prefix) + _tmpdirs.append(tmpdir) + return tmpdir + +def teardown(): + if _chdir_backup: + os.chdir(_chdir_backup) + for tmpdir in _tmpdirs: + tmpdir.rmdir() + del _tmpdirs[:] + +class TmpDir(object): + + def __init__(self, suffix='', prefix='tmp'): + self.path = tempfile.mkdtemp(suffix=suffix, prefix=prefix) + + def rmdir(self): + if self.path and not os.getenv("WHATMAPS_TESTS_NOCLEAN"): + shutil.rmtree(self.path) + self.path = None + + def __repr__(self): + return self.path + + def join(self, *args): + return os.path.join(self.path, *args) diff --git a/tests/test_process.py b/tests/test_process.py new file mode 100644 index 0000000..da47ebd --- /dev/null +++ b/tests/test_process.py @@ -0,0 +1,124 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +"""Test L{whatmaps.process} config""" + +import os +import sys +import unittest +import random + +from whatmaps.process import Process + +from . import context + +class TestWhatmapsProcess(unittest.TestCase): + def setUp(self): + self.tmpdir = context.new_tmpdir(__name__) + self.procfs = str(self.tmpdir) + self.pid = random.randint(1, 65535) + self.piddir = os.path.join(self.procfs, str(self.pid)) + os.mkdir(self.piddir) + self.exe = os.path.join(self.piddir, 'exe') + self.cmdline = os.path.join(self.piddir, 'cmdline') + self.maps = os.path.join(self.piddir, 'maps') + self._write_cmdline('doesnotmatter') # Write at least an empty cmdline + self._write_exe_symlink('acommand') + self._write_maps([['f32b43221000-7f32b4522000', + '---p', + '00020000', + 'fe:02', + '1704011', + '/lib/x86_64-linux-gnu/libselinux.so.1'], + ['7f32b4521000-7f32b4623000', + 'r--p', + '00020000', + 'fe:02', + '1704011', + '/lib/x86_64-linux-gnu/libselinux.so.1'], + ]) + + def _write_exe_symlink(self, name): + exe = os.path.join(str(self.tmpdir), name) + os.symlink(exe, self.exe) + + def _write_cmdline(self, text=''): + f = open(self.cmdline, 'w') + f.write(text) + f.close() + + def _write_maps(self, data): + f = open(self.maps, 'w') + f.write('\n'.join([' '.join(r) for r in data])) + f.close() + + def test_nonexistent(self): + """No exe link should create an 'empty' object""" + os.unlink(self.exe) + p = Process(self.pid, self.procfs) + self.assertIsNone(p.exe) + self.assertIsNone(p.cmdline) + + def test_deleted(self): + """Handle symlink to deleted binaries""" + exe = '/does/not/matter' + os.unlink(self.exe) + os.symlink(os.path.join(self.piddir, '%s (deleted)' % exe), + self.exe) + + p = Process(self.pid, procfs=self.procfs) + self.assertEqual(p.exe, exe) + self.assertTrue(p.deleted, True) + self.assertEqual(p.cmdline, 'doesnotmatter') + + def test_existing(self): + p = Process(self.pid, procfs=self.procfs) + exe = os.path.join(str(self.tmpdir), 'acommand') + self.assertEqual(p.exe, exe) + self.assertEqual(p.cmdline, 'doesnotmatter') + self.assertFalse(p.deleted) + self.assertEqual(str(p), + "<Process object pid:%d>" % self.pid) + + def test_maps(self): + """Check whether the process maps a shared object at path""" + p = Process(self.pid, procfs=self.procfs) + self.assertFalse(p.maps('/does/not/exist')) + self.assertTrue(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1')) + + def test_no_maps(self): + """Check if we don't fail if the process went away""" + os.unlink(self.maps) + p = Process(self.pid, procfs=self.procfs) + self.assertFalse(p.maps('/does/not/exist')) + self.assertFalse(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1')) + + def test_broken_maps(self): + """Continue on unparseable map file""" + # Provoke index error by to few items in line + self._write_maps([['do', 'few', 'items']]) + p = Process(self.pid, procfs=self.procfs) + self.assertFalse(p.maps('/does/not/exist')) + self.assertFalse(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1')) + + @unittest.skipIf(os.getuid() == 0, "Skip if root") + def test_broken_unreadable_map(self): + """Raise error if map file is unreadable""" + os.chmod(self.maps, 0) + p = Process(self.pid, procfs=self.procfs) + self.assertRaises(IOError, p.maps, '/does/not/exist') + + def tearDown(self): + context.teardown() diff --git a/whatmaps/command.py b/whatmaps/command.py index 98de395..502f7d3 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -34,61 +34,13 @@ try: except ImportError: lsb_release = None +from whatmaps.process import Process + class PkgError(Exception): pass -class Process(object): - """A process - Linux only so far, needs /proc mounted""" - deleted_re = re.compile(r"(?P<exe>.*) \(deleted\)$") - - def __init__(self, pid): - self.pid = pid - self.mapped = [] - try: - self.exe = os.readlink('/proc/%d/exe' % self.pid) - m = self.deleted_re.match(self.exe) - if m: - self.exe = m.group('exe') - logging.debug("Using deleted exe %s", self.exe) - if not os.path.exists(self.exe): - logging.debug("%s doesn't exist", self.exe) - self.cmdline = open('/proc/%d/cmdline' % self.pid).read() - except OSError: - self.exe = None - self.cmdline = None - - def _read_maps(self): - """Read the SOs from /proc/<pid>/maps""" - try: - f = file('/proc/%d/maps' % self.pid) - except IOError as e: - # ignore killed process - if e.errno != errno.ENOENT: - raise - return - for line in f: - try: - so = line.split()[5].strip() - self.mapped.append(so) - except IndexError: - pass - - def maps(self, path): - """Check if the process maps the object at path""" - if not self.mapped: - self._read_maps() - - if path in self.mapped: - return True - else: - return False - - def __repr__(self): - return "<Process object pid:%d>" % self.pid - - class Distro(object): """ A distribution diff --git a/whatmaps/process.py b/whatmaps/process.py new file mode 100644 index 0000000..3fe23f3 --- /dev/null +++ b/whatmaps/process.py @@ -0,0 +1,77 @@ +#!/usr/bin/python -u +# vim: set fileencoding=utf-8 : +# +# (C) 2010,2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import errno +import logging +import os +import re + +class Process(object): + """A process - Linux only so far, needs /proc mounted""" + deleted_re = re.compile(r"(?P<exe>.*) \(deleted\)$") + + def __init__(self, pid, procfs=None): + self.procfs = procfs or '/proc' + self.pid = pid + self.mapped = [] + self.deleted = False + try: + self.exe = os.readlink(self._procpath(str(self.pid), 'exe')) + m = self.deleted_re.match(self.exe) + if m: + self.exe = m.group('exe') + self.deleted = True + logging.error("Using deleted exe %s", self.exe) + if not os.path.exists(self.exe): + logging.debug("%s doesn't exist", self.exe) + self.cmdline = open(self._procpath('%d/cmdline' % self.pid)).read() + except OSError: + self.exe = None + self.cmdline = None + + def _procpath(self, *args): + """ + Return a path relative to the current procfs bsae + """ + return os.path.join(self.procfs, *args) + + def _read_maps(self): + """Read the SOs from /proc/<pid>/maps""" + try: + f = file(self._procpath('%d/maps' % self.pid)) + except IOError as e: + # ignore killed process + if e.errno != errno.ENOENT: + raise + return + for line in f: + try: + so = line.split()[5].strip() + self.mapped.append(so) + except IndexError: + pass + + def maps(self, path): + """Check if the process maps the object at path""" + if not self.mapped: + self._read_maps() + + return True if path in self.mapped else False + + def __repr__(self): + return "<Process object pid:%d>" % self.pid |