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

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