Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

needs-restarting: Add --exclude-services #556

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/needs_restarting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ All general DNF options are accepted, see `Options` in :manpage:`dnf(8)` for det
``-s, --services``
Only list the affected systemd services.

``--exclude-services``
Don't list stale processes that correspond to a systemd service.

-------------
Configuration
-------------
Expand Down
27 changes: 21 additions & 6 deletions plugins/needs_restarting.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def set_argparser(parser):
"(exit code 1) or not (exit code 0)"))
parser.add_argument('-s', '--services', action='store_true',
help=_("only report affected systemd services"))
parser.add_argument('--exclude-services', action='store_true',
help=_("don't list stale processes that correspond to a systemd service"))

def configure(self):
demands = self.cli.demands
Expand Down Expand Up @@ -303,19 +305,32 @@ def run(self):
return None

stale_pids = set()
stale_service_names = set()
uid = os.geteuid() if self.opts.useronly else None
for ofile in list_opened_files(uid):
pkg = owning_pkg_fn(ofile.presumed_name)
pid = ofile.pid
if pkg is None:
continue
if pkg.installtime > process_start(ofile.pid):
stale_pids.add(ofile.pid)
if pkg.installtime <= process_start(pid):
continue
if self.opts.services or self.opts.exclude_services:
service_name = get_service_dbus(pid)
if service_name is None:
stale_pids.add(pid)
else:
stale_service_names.add(service_name)
if not self.opts.exclude_services:
stale_pids.add(pid)
Comment on lines +323 to +324
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize this before but now that the --services option prints only stale_service_names there is no point in adding to the stale_pids when --services is set right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose, but I think it's fine to leave it.

else:
# If neither --services nor --exclude-services is set, don't
# query D-Bus at all.
stale_pids.add(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)
for stale_service_name in sorted(stale_service_names):
print(stale_service_name)
return 0

for pid in sorted(stale_pids):
print_cmd(pid)
Loading