Blame SOURCES/0002-needs-restarting-add-s-to-list-services-RhBug-177293.patch

fce868
From b2a912724d737ca7ac4350885b54117f5e043046 Mon Sep 17 00:00:00 2001
fce868
From: Nicola Sella <nsella@redhat.com>
fce868
Date: Thu, 5 Mar 2020 12:45:39 +0100
fce868
Subject: [PATCH 2/2] [needs-restarting] add -s to list services
fce868
 (RhBug:1772939)
fce868
fce868
= changelog =
fce868
msg:           [needs-restarting] add -s to list services (RhBug:1772939)
fce868
type:          bugfix
fce868
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=1772939
fce868
fce868
Closes: #395
fce868
Approved by: kontura
fce868
---
fce868
 dnf-plugins-core.spec       |  6 ++++++
fce868
 doc/needs_restarting.rst    |  3 +++
fce868
 plugins/needs_restarting.py | 33 +++++++++++++++++++++++++++++++++
fce868
 3 files changed, 42 insertions(+)
fce868
fce868
diff --git a/dnf-plugins-core.spec b/dnf-plugins-core.spec
fce868
index 42d0884..012dde8 100644
fce868
--- a/dnf-plugins-core.spec
fce868
+++ b/dnf-plugins-core.spec
fce868
@@ -99,8 +99,10 @@ Summary:        Core Plugins for DNF
fce868
 %{?python_provide:%python_provide python2-%{name}}
fce868
 BuildRequires:  python2-dnf >= %{dnf_lowest_compatible}
fce868
 %if 0%{?rhel} && 0%{?rhel} <= 7
fce868
+BuildRequires:  dbus-python
fce868
 BuildRequires:  python-nose
fce868
 %else
fce868
+BuildRequires:  python2-dbus
fce868
 BuildRequires:  python2-nose
fce868
 %endif
fce868
 BuildRequires:  python2-devel
fce868
@@ -110,8 +112,10 @@ Requires:       python2-distro
fce868
 Requires:       python2-dnf >= %{dnf_lowest_compatible}
fce868
 Requires:       python2-hawkey >= %{hawkey_version}
fce868
 %if 0%{?rhel} && 0%{?rhel} <= 7
fce868
+Requires:       dbus-python
fce868
 Requires:       python-dateutil
fce868
 %else
fce868
+Requires:       python2-dbus
fce868
 Requires:       python2-dateutil
fce868
 %endif
fce868
 Provides:       python2-dnf-plugins-extras-debug = %{version}-%{release}
fce868
@@ -140,12 +144,14 @@ Additionally provides generate_completion_cache passive plugin.
fce868
 %package -n python3-%{name}
fce868
 Summary:    Core Plugins for DNF
fce868
 %{?python_provide:%python_provide python3-%{name}}
fce868
+BuildRequires:  python3-dbus
fce868
 BuildRequires:  python3-devel
fce868
 BuildRequires:  python3-dnf >= %{dnf_lowest_compatible}
fce868
 BuildRequires:  python3-nose
fce868
 %if 0%{?fedora}
fce868
 Requires:       python3-distro
fce868
 %endif
fce868
+Requires:       python3-dbus
fce868
 Requires:       python3-dnf >= %{dnf_lowest_compatible}
fce868
 Requires:       python3-hawkey >= %{hawkey_version}
fce868
 Requires:       python3-dateutil
fce868
diff --git a/doc/needs_restarting.rst b/doc/needs_restarting.rst
fce868
index e79b43f..1a3fbbe 100644
fce868
--- a/doc/needs_restarting.rst
fce868
+++ b/doc/needs_restarting.rst
fce868
@@ -48,3 +48,6 @@ All general DNF options are accepted, see `Options` in :manpage:`dnf(8)` for det
fce868
 ``-r, --reboothint``
fce868
 
fce868
     Only report whether a reboot is required (exit code 1) or not (exit code 0).
fce868
+
fce868
+``-s, --services``
fce868
+    Only list the affected systemd services.
fce868
diff --git a/plugins/needs_restarting.py b/plugins/needs_restarting.py
fce868
index 69203f4..f6bf525 100644
fce868
--- a/plugins/needs_restarting.py
fce868
+++ b/plugins/needs_restarting.py
fce868
@@ -29,6 +29,7 @@ from dnfpluginscore import logger, _
fce868
 
fce868
 import dnf
fce868
 import dnf.cli
fce868
+import dbus
fce868
 import functools
fce868
 import os
fce868
 import re
fce868
@@ -126,6 +127,30 @@ def print_cmd(pid):
fce868
     print('%d : %s' % (pid, command))
fce868
 
fce868
 
fce868
+def get_service_dbus(pid):
fce868
+    bus = dbus.SystemBus()
fce868
+    systemd_manager_object = bus.get_object(
fce868
+        'org.freedesktop.systemd1',
fce868
+        '/org/freedesktop/systemd1'
fce868
+    )
fce868
+    systemd_manager_interface = dbus.Interface(
fce868
+        systemd_manager_object,
fce868
+        'org.freedesktop.systemd1.Manager'
fce868
+    )
fce868
+    service_proxy = bus.get_object(
fce868
+        'org.freedesktop.systemd1',
fce868
+        systemd_manager_interface.GetUnitByPID(pid)
fce868
+    )
fce868
+    service_properties = dbus.Interface(
fce868
+        service_proxy, dbus_interface="org.freedesktop.DBus.Properties")
fce868
+    name = service_properties.Get(
fce868
+        "org.freedesktop.systemd1.Unit",
fce868
+        'Id'
fce868
+    )
fce868
+    if name.endswith(".service"):
fce868
+        return name
fce868
+    return
fce868
+
fce868
 def smap2opened_file(pid, line):
fce868
     slash = line.find('/')
fce868
     if slash < 0:
fce868
@@ -205,6 +230,8 @@ class NeedsRestartingCommand(dnf.cli.Command):
fce868
         parser.add_argument('-r', '--reboothint', action='store_true',
fce868
                             help=_("only report whether a reboot is required "
fce868
                                    "(exit code 1) or not (exit code 0)"))
fce868
+        parser.add_argument('-s', '--services', action='store_true',
fce868
+                            help=_("only report affected systemd services"))
fce868
 
fce868
     def configure(self):
fce868
         demands = self.cli.demands
fce868
@@ -251,5 +278,11 @@ class NeedsRestartingCommand(dnf.cli.Command):
fce868
             if pkg.installtime > process_start(ofile.pid):
fce868
                 stale_pids.add(ofile.pid)
fce868
 
fce868
+        if self.opts.services:
fce868
+            names = set([get_service_dbus(pid) for pid in sorted(stale_pids)])
fce868
+            for name in names:
fce868
+                if name is not None:
fce868
+                    print(name)
fce868
+            return 0
fce868
         for pid in sorted(stale_pids):
fce868
             print_cmd(pid)
fce868
-- 
fce868
2.26.2
fce868