From 1d06c1bf957fb869f292702d5651a58b8ed1c41b Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 17 Apr 2024 17:30:52 +0200 Subject: [PATCH] guest helpers: avoid checking user/token if one can abort all tasks If the user can already stop all tasks there is no point in spending some work on every task to check if the user could also stop if without those powerful permissions. To avoid to much indentation rework the filter to an early-next style. Signed-off-by: Thomas Lamprecht --- src/PVE/GuestHelpers.pm | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/PVE/GuestHelpers.pm b/src/PVE/GuestHelpers.pm index c9fe147..592b4a8 100644 --- a/src/PVE/GuestHelpers.pm +++ b/src/PVE/GuestHelpers.pm @@ -426,11 +426,10 @@ sub abort_guest_tasks { my $active_tasks = PVE::INotify::read_file('active'); my $aborted_tasks = []; for my $task (@$active_tasks) { - if (!$task->{saved} - && $task->{type} eq $type - && $task->{id} eq $vmid - ) { - my $can_abort_task; + next if $task->{saved} || $task->{type} ne $type || $task->{id} ne $vmid; # filter + + my $can_abort_task = $can_abort_all; + if (!$can_abort_task) { # tasks started by a token can be aborted by the token or token owner, # tasks started by a user can be aborted by the user if (PVE::AccessControl::pve_verify_tokenid($task->{user}, 1)) { @@ -440,12 +439,12 @@ sub abort_guest_tasks { } else { $can_abort_task = $authuser eq $task->{user}; } + } - if ($can_abort_all || $can_abort_task) { - # passing `1` for parameter $killit aborts the task - PVE::RPCEnvironment->check_worker($task->{upid}, 1); - push @$aborted_tasks, $task->{upid}; - } + if ($can_abort_task) { + # passing `1` for parameter $killit aborts the task + PVE::RPCEnvironment->check_worker($task->{upid}, 1); + push @$aborted_tasks, $task->{upid}; } } return $aborted_tasks;