]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
processhelp.pm: use `Win32::Process*` perl modules if available
authorViktor Szakats <commit@vsz.me>
Mon, 18 Aug 2025 08:28:42 +0000 (10:28 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 18 Aug 2025 21:46:56 +0000 (23:46 +0200)
`Win32::Process::List` and `Win32::Process`.

To replace external calls to `tasklist.exe` and `taskkill.exe`.

The perl modules are wrappers/binding to Win32 API calls. They avoid
launching external processes with a command shell (including MSYS2),
the external tool, and creating command-lines and parsing tool output.

According to local tests and the CI, one test session calls
`tasklist.exe` 350-400 times. `taskkill.exe` is rarely called:
https://github.com/curl/curl/actions/runs/17012376726?pr=18296

It's hard to predict any possible side-effect of dynamically loading
the two necessary, module DLLs into the Perl process. The MSYS2 runtime
if prone to fail when doing this in fork operations, as seen earlier
with the Win32.DLL module. But, is looks like a symptom, not the root
cause for these failures, because the failures are present with or
without perl.exe loading the Win32.DLL.
Ref: be01b60ce532af6eb18c11fbd565355dbb570a2e #18287

Cherry-picked from #18296
Closes #18308

tests/processhelp.pm

index 43513aae077015f2f142891c7de1004b48474272..f1f8ef525a42988b010d4797fd0cfe9177bdbdd9 100644 (file)
@@ -29,6 +29,12 @@ use warnings;
 
 use Time::HiRes;
 
+use pathhelp qw(
+    os_is_win
+    );
+
+my $has_win32_process;
+
 BEGIN {
     use base qw(Exporter);
 
@@ -43,6 +49,18 @@ BEGIN {
         set_advisor_read_lock
         clear_advisor_read_lock
     );
+
+    if(os_is_win() && $^O ne 'MSWin32') {
+        $has_win32_process = eval {
+            no warnings "all";
+            # https://metacpan.org/pod/Win32::Process
+            require Win32::Process;
+            # https://metacpan.org/pod/Win32::Process::List
+            require Win32::Process::List;
+        };
+    } else {
+      $has_win32_process = 0;
+    }
 }
 
 use serverhelp qw(
@@ -51,10 +69,6 @@ use serverhelp qw(
     datasockf_pidfilename
     );
 
-use pathhelp qw(
-    os_is_win
-    );
-
 use globalconfig qw(
     $dev_null
     );
@@ -114,11 +128,18 @@ sub pidexists {
         if($pid > 4194304 && os_is_win()) {
             $pid -= 4194304;
             if($^O ne 'MSWin32') {
-                my $filter = "PID eq $pid";
-                # https://ss64.com/nt/tasklist.html
-                my $result = `tasklist -fi \"$filter\" 2>$dev_null`;
-                if(index($result, "$pid") != -1) {
-                    return -$pid;
+                if($has_win32_process) {
+                    my %processes = Win32::Process::List->new()->GetProcesses();
+                    if(exists $processes{$pid}) {
+                        return -$pid;
+                    }
+                } else {
+                    my $filter = "PID eq $pid";
+                    # https://ss64.com/nt/tasklist.html
+                    my $result = `tasklist -fi \"$filter\" 2>$dev_null`;
+                    if(index($result, "$pid") != -1) {
+                        return -$pid;
+                    }
                 }
                 return 0;
             }
@@ -145,10 +166,14 @@ sub pidterm {
         if($pid > 4194304 && os_is_win()) {
             $pid -= 4194304;
             if($^O ne 'MSWin32') {
-                # https://ss64.com/nt/taskkill.html
-                my $cmd = "taskkill -f -t -pid $pid >$dev_null 2>&1";
-                print "Executing: '$cmd'\n";
-                system($cmd);
+                if($has_win32_process) {
+                    Win32::Process::KillProcess($pid, 0);
+                } else {
+                    # https://ss64.com/nt/taskkill.html
+                    my $cmd = "taskkill -f -t -pid $pid >$dev_null 2>&1";
+                    print "Executing: '$cmd'\n";
+                    system($cmd);
+                }
                 return;
             }
         }
@@ -170,10 +195,14 @@ sub pidkill {
         if($pid > 4194304 && os_is_win()) {
             $pid -= 4194304;
             if($^O ne 'MSWin32') {
-                # https://ss64.com/nt/taskkill.html
-                my $cmd = "taskkill -f -t -pid $pid >$dev_null 2>&1";
-                print "Executing: '$cmd'\n";
-                system($cmd);
+                if($has_win32_process) {
+                    Win32::Process::KillProcess($pid, 0);
+                } else {
+                    # https://ss64.com/nt/taskkill.html
+                    my $cmd = "taskkill -f -t -pid $pid >$dev_null 2>&1";
+                    print "Executing: '$cmd'\n";
+                    system($cmd);
+                }
                 return;
             }
         }