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 <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2024-04-17 17:30:52 +02:00
parent 253a2ea93d
commit 1d06c1bf95

View File

@ -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;