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

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