summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/look.zig b/src/look.zig
index 90945013..c98e7988 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -1064,6 +1064,43 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool {
}
}
+/// DELIVER A SIGNAL TO WHATEVER IS ON THIS PANE'S TTY — the host half of
+/// `pty/ctl`'s `sig` verb, shared by every frontend that owns pane shells so
+/// that the target is decided once instead of three times.
+///
+/// THE TARGET IS THE FOREGROUND PROCESS GROUP, not the shell's pid, and the
+/// difference is the whole usefulness of the verb. `tcgetpgrp` on the master
+/// answers with the number the kernel would deliver a ^C to (see the comment
+/// on the declaration above), which is the running build, the pager, the
+/// agent — the thing a script means when it says `sig INT`. Aimed at the pid
+/// instead, `sig INT` would reach an interactive shell, which ignores SIGINT
+/// while it waits for a job: the verb would appear to work and do nothing on
+/// the one case anybody wants it for. At an idle prompt the two are the same
+/// number, because forkpty made the shell its own group leader.
+///
+/// The pid is the FALLBACK, for an OS or a host whose master end will not
+/// answer the ioctl. There `sig KILL` still ends the shell, which is the case
+/// where being ignored is not an acceptable outcome.
+pub fn signalTty(shell_pid: libc.pid_t, master_fd: c_int, which: pardes.PtySignal) void {
+ // Whatever `std.c.SIG` spells these as on this platform, unconverted: the
+ // one call below wants exactly that type (see the `kill` beside `harvest`).
+ const sig = switch (which) {
+ .int => libc.SIG.INT,
+ .term => libc.SIG.TERM,
+ .hup => libc.SIG.HUP,
+ .quit => libc.SIG.QUIT,
+ .kill => libc.SIG.KILL,
+ };
+ const fg = tcgetpgrp(master_fd);
+ // A process GROUP is addressed as its negated leader; `fg` is already a
+ // group id, so this is `kill(-fg)` and not `kill(-leader_of(fg))`.
+ if (fg > 0) {
+ _ = libc.kill(-fg, sig);
+ return;
+ }
+ if (shell_pid > 0) _ = libc.kill(shell_pid, sig);
+}
+
/// Read a small /proc text in one go. These files are generated on read and
/// answer completely in a single call at these sizes; a short read would only
/// truncate a field, which every parser below treats as "no answer".