]> git.proxmox.com Git - pve-guest-common.git/commitdiff
guest helpers: avoid checking user/token if one can abort all tasks
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 17 Apr 2024 15:30:52 +0000 (17:30 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 17 Apr 2024 15:30:53 +0000 (17:30 +0200)
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>
src/PVE/GuestHelpers.pm

index c9fe147d66c3a7c6d78687666a479c21f82d111e..592b4a8da4b575fcbfa5e734524d382564ab85be 100644 (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;