]> git.eshelyaron.com Git - emacs.git/commitdiff
; * src/w32proc.c (sys_kill): Handle negative PID when sig == 0.
authorEli Zaretskii <eliz@gnu.org>
Thu, 13 Jun 2024 08:03:52 +0000 (11:03 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sat, 15 Jun 2024 17:21:10 +0000 (19:21 +0200)
(cherry picked from commit 72e6b05221502cf722f7bddf2d100e5eff424dd8)

src/w32proc.c

index 4d237992b14e8b38487e13086e1e96a501b8d3c5..40181e09830042fd0202cfdb1f3b53ca66ef5f36 100644 (file)
@@ -2727,6 +2727,7 @@ sys_kill (pid_t pid, int sig)
   HANDLE proc_hand;
   int need_to_free = 0;
   int rc = 0;
+  pid_t orig_pid = pid;
 
   /* Each process is in its own process group.  */
   if (pid < 0)
@@ -2734,7 +2735,8 @@ sys_kill (pid_t pid, int sig)
 
   /* Only handle signals that can be mapped to a similar behavior on Windows */
   if (sig != 0
-      && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP && sig != SIGTRAP)
+      && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT
+      && sig != SIGHUP && sig != SIGTRAP)
     {
       errno = EINVAL;
       return -1;
@@ -2760,11 +2762,34 @@ sys_kill (pid_t pid, int sig)
              errno = EPERM;
              return -1;
            case ERROR_INVALID_PARAMETER: /* process PID does not exist */
-             errno = ESRCH;
-             return -1;
+             {
+               if (orig_pid == pid)
+                 {
+                   errno = ESRCH;
+                   return -1;
+                 }
+               /* If we received a negative value, try again with the
+                   original one we received.  */
+               proc_hand = OpenProcess (PROCESS_QUERY_INFORMATION,
+                                        0, orig_pid);
+               if (proc_hand == NULL)
+                 {
+                   err = GetLastError ();
+                   switch (err)
+                     {
+                     case ERROR_ACCESS_DENIED:
+                       errno = EPERM;
+                       return -1;
+                     case ERROR_INVALID_PARAMETER:
+                       errno = ESRCH;
+                       return -1;
+                     }
+                 }
+               break;
+             }
            }
        }
-      else
+      if (proc_hand != NULL)
        CloseHandle (proc_hand);
       return 0;
     }