all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Joan Karadimov <joan.karadimov@gmail.com>
To: 15983@debbugs.gnu.org
Cc: sjm@sjm.io, Bozhidar Batsov <bozhidar.batsov@gmail.com>
Subject: bug#15983: 24.3; Emacs Not Killing Child Process
Date: Thu, 19 Dec 2013 17:44:37 +0200	[thread overview]
Message-ID: <CAGVACNVfa8bvqHDOavzUzb6mGCxRHPzu2Pbv4Yi-76Ca+B_Rcw@mail.gmail.com> (raw)
In-Reply-To: <87zjop3fet.fsf@sjm.io>


[-- Attachment #1.1: Type: text/plain, Size: 1101 bytes --]

> Emacs on Windows can only monitor and kill its immediate subprocesses,
> it cannot monitor, let alone kill, any of their descendant processes,> because it has no idea about them.  And the OS doesn't automatically> kill all the processes in the subprocess tree, and there's no way to> send a signal to them all, as on Posix platforms.  If killing the> immediate child process doesn't cause some of its children to exit or> abort, then those grandchildren will be left orphaned.
Windows NT does have the concept of parent processes, but an API call
wasn't exposed in win32 until XP. I wrote a small patch that uses that
and kills all child processes (as long as pid<0). I did some sanity
testing and it works.

There are a few things that this patch leaves unaddressed:- there is
no error handling
- there is no OS detection - this will get ugly on Windows 9x and
NT4/5.0- performance: 3 API calls are made for each descendant
process. This can be reduced to a total 3 calls (regardless of the
child process count)

I'll fix these (and any other issues) if this fix is of any interest.

[-- Attachment #1.2: Type: text/html, Size: 1629 bytes --]

[-- Attachment #2: win32-kill-children --]
[-- Type: application/octet-stream, Size: 1850 bytes --]

diff --git src/w32proc.c src/w32proc.c
index 2b583ef..0b82ba3 100644
--- src/w32proc.c
+++ src/w32proc.c
@@ -43,6 +43,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #undef kill
 
 #include <windows.h>
+#include <tlhelp32.h>
 #ifdef __GNUC__
 /* This definition is missing from mingw32 headers. */
 extern BOOL WINAPI IsValidLocale (LCID, DWORD);
@@ -2270,19 +2271,14 @@ find_child_console (HWND hwnd, LPARAM arg)
   return TRUE;
 }
 
-/* Emulate 'kill', but only for other processes.  */
-int
-sys_kill (pid_t pid, int sig)
+static int
+kill_process (pid_t pid, int sig)
 {
   child_process *cp;
   HANDLE proc_hand;
   int need_to_free = 0;
   int rc = 0;
 
-  /* Each process is in its own process group.  */
-  if (pid < 0)
-    pid = -pid;
-
   /* Only handle signals that will result in the process dying */
   if (sig != 0
       && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
@@ -2488,6 +2484,36 @@ sys_kill (pid_t pid, int sig)
   return rc;
 }
 
+static int
+kill_process_tree (int pid, int sig)
+{
+  PROCESSENTRY32 process_entry;
+  HANDLE snapshot;
+
+  process_entry.dwSize = sizeof (process_entry);
+  snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, pid);
+
+  kill_process (pid, sig);
+  Process32First (snapshot, &process_entry);
+  do
+    {
+      if (process_entry.th32ParentProcessID == pid)
+        kill_process_tree (process_entry.th32ProcessID, sig);
+    } while (Process32Next (snapshot, &process_entry));
+  CloseHandle (snapshot);
+  return 0;
+}
+
+/* Emulate 'kill', but only for other processes.  */
+int
+sys_kill (int pid, int sig)
+{
+  if (pid < 0)
+    return kill_process_tree (-pid, sig);
+  else
+    return kill_process (pid, sig);
+}
+
 /* The following two routines are used to manipulate stdin, stdout, and
    stderr of our child processes.
 

  parent reply	other threads:[~2013-12-19 15:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-27 17:47 bug#15983: 24.3; Emacs Not Killing Child Process sjm
2013-11-27 21:02 ` Eli Zaretskii
2013-12-19 15:44 ` Joan Karadimov [this message]
2013-12-19 15:56   ` bug#15983: Fw: " Bozhidar Batsov
2013-12-19 17:24   ` Eli Zaretskii
2013-12-20 22:28     ` Joan Karadimov
2013-12-21  8:15       ` Eli Zaretskii
2013-12-21 16:22         ` Joan Karadimov
2013-12-21 18:38           ` Eli Zaretskii
2013-12-22  2:03             ` Joan Karadimov
2013-12-22  3:53               ` Eli Zaretskii
2013-12-23 16:02                 ` Joan Karadimov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGVACNVfa8bvqHDOavzUzb6mGCxRHPzu2Pbv4Yi-76Ca+B_Rcw@mail.gmail.com \
    --to=joan.karadimov@gmail.com \
    --cc=15983@debbugs.gnu.org \
    --cc=bozhidar.batsov@gmail.com \
    --cc=sjm@sjm.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.