Skip to content

Commit

Permalink
[needs-restarting] add -s to list services (RhBug:1772939)
Browse files Browse the repository at this point in the history
= changelog =
msg:           [needs-restarting] add -s to list services (RhBug:1772939)
type:          bugfix
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=1772939

Closes: #395
Approved by: kontura
  • Loading branch information
inknos authored and rh-atomic-bot committed Dec 7, 2020
1 parent 854deff commit 33fde61
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dnf-plugins-core.spec
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ Summary: Core Plugins for DNF
%{?python_provide:%python_provide python2-%{name}}
BuildRequires: python2-dnf >= %{dnf_lowest_compatible}
%if 0%{?rhel} && 0%{?rhel} <= 7
BuildRequires: dbus-python
BuildRequires: python-nose
%else
BuildRequires: python2-dbus
BuildRequires: python2-nose
%endif
BuildRequires: python2-devel
Expand All @@ -110,8 +112,10 @@ Requires: python2-distro
Requires: python2-dnf >= %{dnf_lowest_compatible}
Requires: python2-hawkey >= %{hawkey_version}
%if 0%{?rhel} && 0%{?rhel} <= 7
Requires: dbus-python
Requires: python-dateutil
%else
Requires: python2-dbus
Requires: python2-dateutil
%endif
Provides: python2-dnf-plugins-extras-debug = %{version}-%{release}
Expand Down Expand Up @@ -140,12 +144,14 @@ Additionally provides generate_completion_cache passive plugin.
%package -n python3-%{name}
Summary: Core Plugins for DNF
%{?python_provide:%python_provide python3-%{name}}
BuildRequires: python3-dbus
BuildRequires: python3-devel
BuildRequires: python3-dnf >= %{dnf_lowest_compatible}
BuildRequires: python3-nose
%if 0%{?fedora}
Requires: python3-distro
%endif
Requires: python3-dbus
Requires: python3-dnf >= %{dnf_lowest_compatible}
Requires: python3-hawkey >= %{hawkey_version}
Requires: python3-dateutil
Expand Down
3 changes: 3 additions & 0 deletions doc/needs_restarting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ All general DNF options are accepted, see `Options` in :manpage:`dnf(8)` for det
``-r, --reboothint``

Only report whether a reboot is required (exit code 1) or not (exit code 0).

``-s, --services``
Only list the affected systemd services.
33 changes: 33 additions & 0 deletions plugins/needs_restarting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import dnf
import dnf.cli
import dbus
import functools
import os
import re
Expand Down Expand Up @@ -126,6 +127,30 @@ def print_cmd(pid):
print('%d : %s' % (pid, command))


def get_service_dbus(pid):
bus = dbus.SystemBus()
systemd_manager_object = bus.get_object(
'org.freedesktop.systemd1',
'/org/freedesktop/systemd1'
)
systemd_manager_interface = dbus.Interface(
systemd_manager_object,
'org.freedesktop.systemd1.Manager'
)
service_proxy = bus.get_object(
'org.freedesktop.systemd1',
systemd_manager_interface.GetUnitByPID(pid)
)
service_properties = dbus.Interface(
service_proxy, dbus_interface="org.freedesktop.DBus.Properties")
name = service_properties.Get(
"org.freedesktop.systemd1.Unit",
'Id'
)
if name.endswith(".service"):
return name
return

def smap2opened_file(pid, line):
slash = line.find('/')
if slash < 0:
Expand Down Expand Up @@ -205,6 +230,8 @@ def set_argparser(parser):
parser.add_argument('-r', '--reboothint', action='store_true',
help=_("only report whether a reboot is required "
"(exit code 1) or not (exit code 0)"))
parser.add_argument('-s', '--services', action='store_true',
help=_("only report affected systemd services"))

def configure(self):
demands = self.cli.demands
Expand Down Expand Up @@ -251,5 +278,11 @@ def run(self):
if pkg.installtime > process_start(ofile.pid):
stale_pids.add(ofile.pid)

if self.opts.services:
names = set([get_service_dbus(pid) for pid in sorted(stale_pids)])
for name in names:
if name is not None:
print(name)
return 0
for pid in sorted(stale_pids):
print_cmd(pid)

0 comments on commit 33fde61

Please sign in to comment.