Password input: Handle SIGTSTP

This commit is contained in:
Kovid Goyal 2022-09-01 13:53:04 +05:30
parent 67115530b4
commit ea8fb10c05
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 103 additions and 21 deletions

View File

@ -4,7 +4,9 @@ package tty
import ( import (
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io"
"os" "os"
"time" "time"
@ -207,6 +209,22 @@ func (self *Term) RestoreAndClose() error {
return self.Close() return self.Close()
} }
func (self *Term) SuspendAndRun(callback func() error) error {
var state unix.Termios
err := self.Tcgetattr(&state)
if err != nil {
return err
}
if len(self.states) > 0 {
err := self.Tcsetattr(TCSANOW, &self.states[0])
if err != nil {
return err
}
}
defer self.Tcsetattr(TCSANOW, &state)
return callback()
}
func clamp(v, lo, hi int64) int64 { func clamp(v, lo, hi int64) int64 {
if v < lo { if v < lo {
return lo return lo
@ -245,6 +263,17 @@ func (self *Term) Write(b []byte) (int, error) {
return self.os_file.Write(b) return self.os_file.Write(b)
} }
func (self *Term) WriteAll(b []byte) error {
for len(b) > 0 {
n, err := self.os_file.Write(b)
if err != nil && !errors.Is(err, io.ErrShortWrite) {
return err
}
b = b[n:]
}
return nil
}
func (self *Term) WriteString(b string) (int, error) { func (self *Term) WriteString(b string) (int, error) {
return self.os_file.WriteString(b) return self.os_file.WriteString(b)
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"kitty/tools/tty" "kitty/tools/tty"
"os"
"time" "time"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -45,8 +44,8 @@ type Loop struct {
timers, timers_temp []*timer timers, timers_temp []*timer
timer_id_counter, write_msg_id_counter IdType timer_id_counter, write_msg_id_counter IdType
wakeup_channel chan byte wakeup_channel chan byte
signal_channel chan os.Signal
pending_writes []*write_msg pending_writes []*write_msg
on_SIGTSTP func() error
// Callbacks // Callbacks
@ -71,6 +70,9 @@ type Loop struct {
// Called when any input from tty is received // Called when any input from tty is received
OnReceivedData func(data []byte) error OnReceivedData func(data []byte) error
// Called when resuming from a SIGTSTP or Ctrl-z
OnResumeFromStop func() error
} }
func New(options ...func(self *Loop)) (*Loop, error) { func New(options ...func(self *Loop)) (*Loop, error) {

View File

@ -75,7 +75,6 @@ func (self *Loop) handle_csi(raw []byte) error {
} }
func (self *Loop) handle_key_event(ev *KeyEvent) error { func (self *Loop) handle_key_event(ev *KeyEvent) error {
self.DebugPrintln(ev)
if self.OnKeyEvent != nil { if self.OnKeyEvent != nil {
err := self.OnKeyEvent(ev) err := self.OnKeyEvent(ev)
if err != nil { if err != nil {
@ -177,14 +176,6 @@ func (self *Loop) on_SIGTERM() error {
return nil return nil
} }
func (self *Loop) on_SIGTSTP() error {
signal.Reset(unix.SIGTSTP)
unix.Kill(os.Getpid(), unix.SIGTSTP)
time.Sleep(20 * time.Millisecond)
signal.Notify(self.signal_channel, unix.SIGTSTP)
return nil
}
func (self *Loop) on_SIGHUP() error { func (self *Loop) on_SIGHUP() error {
self.death_signal = unix.SIGHUP self.death_signal = unix.SIGHUP
self.keep_going = false self.keep_going = false
@ -192,9 +183,9 @@ func (self *Loop) on_SIGHUP() error {
} }
func (self *Loop) run() (err error) { func (self *Loop) run() (err error) {
self.signal_channel = make(chan os.Signal, 256) signal_channel := make(chan os.Signal, 256)
handled_signals := []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGTSTP, unix.SIGHUP, unix.SIGWINCH, unix.SIGPIPE} handled_signals := []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGTSTP, unix.SIGHUP, unix.SIGWINCH, unix.SIGPIPE}
signal.Notify(self.signal_channel, handled_signals...) signal.Notify(signal_channel, handled_signals...)
defer signal.Reset(handled_signals...) defer signal.Reset(handled_signals...)
controlling_term, err := tty.OpenControllingTerm() controlling_term, err := tty.OpenControllingTerm()
@ -203,10 +194,10 @@ func (self *Loop) run() (err error) {
} }
self.controlling_term = controlling_term self.controlling_term = controlling_term
defer func() { defer func() {
self.controlling_term.RestoreAndClose() controlling_term.RestoreAndClose()
self.controlling_term = nil self.controlling_term = nil
}() }()
err = self.controlling_term.ApplyOperations(tty.TCSANOW, tty.SetRaw) err = controlling_term.ApplyOperations(tty.TCSANOW, tty.SetRaw)
if err != nil { if err != nil {
return nil return nil
} }
@ -239,6 +230,7 @@ func (self *Loop) run() (err error) {
return err return err
} }
self.QueueWriteBytesDangerous(self.terminal_options.SetStateEscapeCodes()) self.QueueWriteBytesDangerous(self.terminal_options.SetStateEscapeCodes())
needs_reset_escape_codes := true
defer func() { defer func() {
// notify tty reader that we are shutting down // notify tty reader that we are shutting down
@ -248,7 +240,9 @@ func (self *Loop) run() (err error) {
if finalizer != "" { if finalizer != "" {
self.QueueWriteString(finalizer) self.QueueWriteString(finalizer)
} }
self.QueueWriteBytesDangerous(self.terminal_options.ResetStateEscapeCodes()) if needs_reset_escape_codes {
self.QueueWriteBytesDangerous(self.terminal_options.ResetStateEscapeCodes())
}
// flush queued data and wait for it to be written for a timeout, then wait for writer to shutdown // flush queued data and wait for it to be written for a timeout, then wait for writer to shutdown
flush_writer(w_w, tty_write_channel, write_done_channel, self.pending_writes, 2*time.Second) flush_writer(w_w, tty_write_channel, write_done_channel, self.pending_writes, 2*time.Second)
self.pending_writes = nil self.pending_writes = nil
@ -257,8 +251,8 @@ func (self *Loop) run() (err error) {
} }
}() }()
go write_to_tty(w_r, self.controlling_term, tty_write_channel, err_channel, write_done_channel) go write_to_tty(w_r, controlling_term, tty_write_channel, err_channel, write_done_channel)
go read_from_tty(r_r, self.controlling_term, tty_read_channel, err_channel, tty_reading_done_channel) go read_from_tty(r_r, controlling_term, tty_read_channel, err_channel, tty_reading_done_channel)
if self.OnInitialize != nil { if self.OnInitialize != nil {
finalizer, err = self.OnInitialize() finalizer, err = self.OnInitialize()
@ -267,6 +261,33 @@ func (self *Loop) run() (err error) {
} }
} }
self.on_SIGTSTP = func() error {
write_id := self.QueueWriteBytesDangerous(self.terminal_options.ResetStateEscapeCodes())
needs_reset_escape_codes = false
err := self.wait_for_write_to_complete(write_id, tty_write_channel, write_done_channel, 2*time.Second)
if err != nil {
return err
}
err = controlling_term.SuspendAndRun(func() error {
unix.Kill(os.Getpid(), unix.SIGSTOP)
time.Sleep(20 * time.Millisecond)
return nil
})
if err != nil {
return err
}
write_id = self.QueueWriteBytesDangerous(self.terminal_options.SetStateEscapeCodes())
needs_reset_escape_codes = true
err = self.wait_for_write_to_complete(write_id, tty_write_channel, write_done_channel, 2*time.Second)
if err != nil {
return err
}
if self.OnResumeFromStop != nil {
return self.OnResumeFromStop()
}
return nil
}
for self.keep_going { for self.keep_going {
self.flush_pending_writes(tty_write_channel) self.flush_pending_writes(tty_write_channel)
timeout_chan := no_timeout_channel timeout_chan := no_timeout_channel
@ -296,7 +317,7 @@ func (self *Loop) run() (err error) {
return err return err
} }
} }
case s := <-self.signal_channel: case s := <-signal_channel:
err = self.on_signal(s.(unix.Signal)) err = self.on_signal(s.(unix.Signal))
if err != nil { if err != nil {
return err return err

View File

@ -69,6 +69,31 @@ func (self *Loop) flush_pending_writes(tty_write_channel chan<- *write_msg) {
} }
} }
func (self *Loop) wait_for_write_to_complete(sentinel IdType, tty_write_channel chan<- *write_msg, write_done_channel <-chan IdType, timeout time.Duration) error {
for len(self.pending_writes) > 0 {
select {
case tty_write_channel <- self.pending_writes[0]:
self.pending_writes = self.pending_writes[1:]
case write_id, more := <-write_done_channel:
if write_id == sentinel {
return nil
}
if self.OnWriteComplete != nil {
err := self.OnWriteComplete(write_id)
if err != nil {
return err
}
}
if !more {
return fmt.Errorf("The write_done_channel was unexpectedly closed")
}
case <-time.After(timeout):
return os.ErrDeadlineExceeded
}
}
return nil
}
func (self *Loop) add_write_to_pending_queue(data *write_msg) { func (self *Loop) add_write_to_pending_queue(data *write_msg) {
self.pending_writes = append(self.pending_writes, data) self.pending_writes = append(self.pending_writes, data)
} }
@ -183,7 +208,7 @@ func flush_writer(pipe_w *os.File, tty_write_channel chan<- *write_msg, write_do
} }
}() }()
deadline := time.Now().Add(timeout) deadline := time.Now().Add(timeout)
for len(pending_writes) > 0 { for len(pending_writes) > 0 && !writer_quit {
timeout = deadline.Sub(time.Now()) timeout = deadline.Sub(time.Now())
if timeout <= 0 { if timeout <= 0 {
return return
@ -213,7 +238,7 @@ func flush_writer(pipe_w *os.File, tty_write_channel chan<- *write_msg, write_do
writer_quit = true writer_quit = true
} }
case <-time.After(timeout): case <-time.After(timeout):
break return
} }
} }
return return

View File

@ -79,6 +79,11 @@ func ReadPassword(prompt string, kill_if_signaled bool) (password string, err er
return nil return nil
} }
lp.OnResumeFromStop = func() error {
lp.QueueWriteString("\r" + prompt + shadow)
return nil
}
err = lp.Run() err = lp.Run()
if err != nil { if err != nil {
return return