From b32ad85634d09adf67f8db653be2fc35db9e794f Mon Sep 17 00:00:00 2001 From: Sean O'Keeffe Date: Wed, 3 May 2017 12:38:32 +0100 Subject: [PATCH] Get daemon names from dbus --- .travis.yml | 15 +++++++- data/applications.xml | 60 -------------------------------- requirements.txt | 1 + tracer.spec | 1 + tracer/resources/SystemdUnit.py | 35 +++++++++++++++++++ tracer/resources/applications.py | 6 ++++ 6 files changed, 57 insertions(+), 61 deletions(-) create mode 100644 tracer/resources/SystemdUnit.py diff --git a/.travis.yml b/.travis.yml index 4e200f7..d303e41 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: python -sudo: false +dist: trusty +sudo: required python: - "2.7" @@ -8,7 +9,19 @@ python: - "3.5" - "nightly" +before_install: + - sudo apt-get update + - > + sudo apt-get -yq install + libdbus-1-dev + libdbus-glib-1-dev + dbus + install: + # These first 2 lines are a nasty Travis hack, see https://github.com/travis-ci/travis-ci/issues/653 + - which python3.3-config && sed -i "s|not getvar('Py_ENABLE_SHARED')|True|" $(which python3.3-config) || true + - sed -i "s|not getvar('Py_ENABLE_SHARED')|True|" $(which python)-config || true + - pip install -r requirements.txt - pip install . # See setup.py - pip install coveralls diff --git a/data/applications.xml b/data/applications.xml index 85d1b91..9beb26e 100644 --- a/data/applications.xml +++ b/data/applications.xml @@ -76,61 +76,6 @@ - Common applications --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -153,16 +98,11 @@ - - - - - diff --git a/requirements.txt b/requirements.txt index 8dd1a19..bcd08b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ BeautifulSoup4 sphinx_rtd_theme Pygments lxml +dbus-python diff --git a/tracer.spec b/tracer.spec index dfbb9c4..be8adec 100644 --- a/tracer.spec +++ b/tracer.spec @@ -84,6 +84,7 @@ Requires: rpm-python3 Requires: python3-beautifulsoup4 Requires: python3-psutil Requires: python3-lxml +Requires: python3-dbus Requires: %{name}-common = %{version}-%{release} %if %{with suggest} Suggests: python3-argcomplete diff --git a/tracer/resources/SystemdUnit.py b/tracer/resources/SystemdUnit.py new file mode 100644 index 0000000..c5b0c31 --- /dev/null +++ b/tracer/resources/SystemdUnit.py @@ -0,0 +1,35 @@ +#-*- coding: utf-8 -*- +# SystemdUnit.py +# Module for getting data from Systemd about Units + +# Copyright (C) 2017 Sean O'Keeffe +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# the GNU General Public License v.2, or (at your option) any later version. +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY expressed or implied, including the implied warranties 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., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# + +from dbus import Interface, SystemBus, DBusException + +class SystemdUnit(object): + def __init__(self, pid): + self.__systemd = SystemBus().get_object('org.freedesktop.systemd1','/org/freedesktop/systemd1') + self.__manager = Interface(self.__systemd, dbus_interface='org.freedesktop.systemd1.Manager') + self.__proxy = SystemBus().get_object('org.freedesktop.systemd1', self.__manager.GetUnitByPID(pid)) + + def has_service_property(self, attr): + try: + propty = self.__proxy.Get('org.freedesktop.systemd1.Service', attr, dbus_interface='org.freedesktop.DBus.Properties') + except DBusException: + return False + return bool(propty) + + def get_unit_property(self, attr): + return self.__proxy.Get('org.freedesktop.systemd1.Unit', attr, dbus_interface='org.freedesktop.DBus.Properties') diff --git a/tracer/resources/applications.py b/tracer/resources/applications.py index 174b8df..ce761c1 100644 --- a/tracer/resources/applications.py +++ b/tracer/resources/applications.py @@ -25,6 +25,7 @@ from tracer.resources.lang import _ from tracer.resources.processes import Processes from tracer.resources.system import System +from tracer.resources.SystemdUnit import SystemdUnit import os import re @@ -258,6 +259,11 @@ def instances(self): class AffectedApplication(Application): @property def name(self): + if System.init_system() == "systemd": + if not SystemdUnit(self.instances[0].pid).has_service_property('PAMName'): + Id = SystemdUnit(self.instances[0].pid).get_unit_property('Id') + if re.search("\.service$", Id): + return re.sub('\.service$', '', Id) if self.is_interpreted: return self.instances[0].real_name return self._attributes["name"]