1 Commits
Author SHA1 Message Date
sneak 5df9dc098d Put the terminal back when a signal ends the mnemonic prompt (closes #91)
check / check (push) Waiting to run
A SIGINT, SIGTERM or SIGHUP at the mnemonic prompt killed the tool
with echo still off. The prompt now catches them with signals.Notify
from before echo goes off, and reads the line in a goroutine. Once the
line is read or a signal arrives, it puts the terminal settings back
and stops catching; if a signal has arrived by then, it sends the tool
the same signal again, so the tool is still ended by it (130 for SIGINT
in a shell). A signal the tool was started with set to be ignored stays
ignored. A test sends SIGINT at the prompt on a pseudo-terminal.

Unchanged: the Windows prompt, which uses term.ReadPassword.

Model: opus-5-5
2026-10-14 22:16:11 +00:00
+20 -10
View File
@@ -21,8 +21,9 @@ import (
// of input and goes on waiting.
//
// Meanwhile SIGINT, SIGTERM and SIGHUP are caught, as signals.Notify
// catches them. One that arrives puts the terminal back as it was and
// then ends the tool, as it would have ended it if it were not caught.
// catches them. One that has arrived by the time the line is read puts
// the terminal back as it was and then ends the tool, as it would have
// ended it if it were not caught.
func readWithoutEcho() (string, error) {
fd := int(os.Stdin.Fd())
@@ -48,11 +49,6 @@ func readWithoutEcho() (string, error) {
return "", fmt.Errorf("turning echo off: %w", err)
}
restore := func() {
_ = unix.IoctlSetTermios(fd, ioctlWriteTermios, settings)
}
defer restore()
var (
typed string
failed error
@@ -68,12 +64,26 @@ func readWithoutEcho() (string, error) {
close(read)
}()
var ending os.Signal
select {
case <-read:
case ending := <-received:
restore()
signal.Stop(received)
case ending = <-received:
}
// The terminal is put back while the signals are still caught, so
// that none can end the tool with echo off. Stop returns only once
// every signal the tool has received has been handed over, so one
// that came as the line was read is in received by then.
_ = unix.IoctlSetTermios(fd, ioctlWriteTermios, settings)
signal.Stop(received)
if len(received) > 0 {
ending = <-received
}
if ending != nil {
return "", endBy(ending)
}