Skip to main content

Crate terminator

Crate terminator 

Source
Expand description

Stop processes gracefully, even on Windows.

On Windows this sends real CTRL_C_EVENT / CTRL_BREAK_EVENT console signals (or closes a GUI window) to an arbitrary process, and can write to its standard input. Unlike the original Go implementation it does not ship and drop a separate “proxy” executable into a temporary folder.

Windows can attach a process to only one console at a time, so reaching a foreign console sometimes needs a short-lived detached process. The crate picks the cheapest route automatically:

  1. Direct: the target shares the caller’s console, nothing to attach.
  2. In-process: the caller has no console, so attach, send, detach.
  3. Helper: the caller has its own console; a detached helper attaches to the target console instead. By default the helper is the current executable re-launched with a marker argument, intercepted before main by a C runtime initializer that this crate registers itself. No call from main is needed (run_helper_if_requested exists only for unusual setups). When this code lives in a DLL, point the crate to an executable that links it with set_helper.

On Unix it sends POSIX signals and can write to a process’ controlling TTY.

Set TERMINATOR_DEBUG=1 to print the chosen route to stderr.

§Quick start

use terminator::{stop, Method};
stop(1234, Method::CtrlC)?;

Enums§

Error
Something that went wrong while trying to stop or message a process.
Helper
Which executable acts as the detached helper when one is required.
Method
A cross-platform way to ask a process to stop.

Functions§

child_tree
Returns the ids of all descendants of the process pid, deepest first.
close_window
Sends WM_CLOSE to window, as if its close button was pressed.
is_running
Returns true while the process with id pid is running.
kill
Forcibly terminate the process with id pid.
main_window
Returns the main window of process pid.
run_helper_if_requested
If this process was started as a helper, perform the requested task and exit. Otherwise return immediately.
send_message
Write msg to the standard input of the process with id pid.
set_helper
Choose which executable is used when a detached helper is required.
stop
Ask the process with id pid to stop using the given Method.
wait_for_stop
Block until the process pid is no longer running or timeout elapses (forever if None). Returns true if the process stopped.

Type Aliases§

Result
Convenience alias used throughout the crate.
WindowHandle
Opaque top-level window handle (an HWND value).