Wire up the signal handlers

This commit is contained in:
Kovid Goyal 2022-08-23 20:31:12 +05:30
parent 526a331f47
commit 99fde8723a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 69 additions and 2 deletions

View File

@ -16,6 +16,8 @@ type Loop struct {
escape_code_parser utils.EscapeCodeParser escape_code_parser utils.EscapeCodeParser
keep_going bool keep_going bool
flush_write_buf bool flush_write_buf bool
death_signal Signal
exit_code int
write_buf []byte write_buf []byte
} }
@ -47,6 +49,29 @@ func (self *Loop) handle_rune(raw rune) error {
return nil return nil
} }
func (self *Loop) on_SIGINT() error {
self.death_signal = SIGINT
self.keep_going = false
return nil
}
func (self *Loop) on_SIGTERM() error {
self.death_signal = SIGTERM
self.keep_going = false
return nil
}
func (self *Loop) on_SIGTSTP() error {
return nil
}
func (self *Loop) on_SIGHUP() error {
self.flush_write_buf = false
self.death_signal = SIGHUP
self.keep_going = false
return nil
}
func CreateLoop() (*Loop, error) { func CreateLoop() (*Loop, error) {
l := Loop{controlling_term: nil} l := Loop{controlling_term: nil}
l.escape_code_parser.HandleCSI = l.handle_csi l.escape_code_parser.HandleCSI = l.handle_csi
@ -115,7 +140,10 @@ func (self *Loop) Run() (err error) {
}() }()
read_buf := make([]byte, utils.DEFAULT_IO_BUFFER_SIZE) read_buf := make([]byte, utils.DEFAULT_IO_BUFFER_SIZE)
signal_buf := make([]byte, 256)
self.death_signal = SIGNULL
self.escape_code_parser.Reset() self.escape_code_parser.Reset()
self.exit_code = 0
for self.keep_going { for self.keep_going {
if len(self.write_buf) > 0 { if len(self.write_buf) > 0 {
selector.RegisterWrite(tty_fd) selector.RegisterWrite(tty_fd)
@ -130,7 +158,7 @@ func (self *Loop) Run() (err error) {
continue continue
} }
if len(self.write_buf) > 0 && selector.IsReadyToWrite(tty_fd) { if len(self.write_buf) > 0 && selector.IsReadyToWrite(tty_fd) {
err := self.write_to_tty() err = self.write_to_tty()
if err != nil { if err != nil {
return err return err
} }
@ -149,6 +177,13 @@ func (self *Loop) Run() (err error) {
return err return err
} }
} }
if selector.IsReadyToRead(int(signal_read_file.Fd())) {
signal_buf = signal_buf[:cap(signal_buf)]
err = self.read_signals(signal_read_file, signal_buf)
if err != nil {
return err
}
}
} }
return nil return nil

View File

@ -94,3 +94,36 @@ func notify_signals(c chan os.Signal, signals ...Signal) func() {
signal.Notify(c, s...) signal.Notify(c, s...)
return func() { signal.Reset(s...) } return func() { signal.Reset(s...) }
} }
func (self *Loop) read_signals(f *os.File, buf []byte) error {
n, err := f.Read(buf)
if err != nil {
return err
}
buf = buf[:n]
for _, s := range buf {
switch Signal(s) {
case SIGINT:
err := self.on_SIGINT()
if err != nil {
return err
}
case SIGTERM:
err := self.on_SIGTERM()
if err != nil {
return err
}
case SIGHUP:
err := self.on_SIGHUP()
if err != nil {
return err
}
case SIGTSTP:
err := self.on_SIGTSTP()
if err != nil {
return err
}
}
}
return nil
}

View File

@ -3,7 +3,6 @@
package utils package utils
import ( import (
"os"
"time" "time"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"