Fix SIGINT killing micro when saving with sudo

When we are saving a file with sudo, if we interrupt sudo via Ctrl-c,
it doesn't just kill sudo, it kills micro itself.

The cause is the same as in the issue #2612 for RunInteractiveShell()
which was fixed by #3357. So fix it the same way as in #3357.
This commit is contained in:
Dmytro Maluka 2024-10-06 16:41:54 +02:00
parent ac73f18191
commit 4baac3d3fb

View File

@ -31,6 +31,7 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
var writeCloser io.WriteCloser
var screenb bool
var cmd *exec.Cmd
var c chan os.Signal
if withSudo {
cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "bs=4k", "of="+name)
@ -39,12 +40,9 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
return
}
c := make(chan os.Signal, 1)
c = make(chan os.Signal, 1)
signal.Reset(os.Interrupt)
signal.Notify(c, os.Interrupt)
go func() {
<-c
cmd.Process.Kill()
}()
screenb = screen.TempFini()
// need to start the process now, otherwise when we flush the file
@ -52,6 +50,10 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
// is too small to handle the full file contents all at once
if e := cmd.Start(); e != nil && err == nil {
screen.TempStart(screenb)
signal.Notify(util.Sigterm, os.Interrupt)
signal.Stop(c)
return err
}
} else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
@ -80,6 +82,10 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
// wait for dd to finish and restart the screen if we used sudo
err := cmd.Wait()
screen.TempStart(screenb)
signal.Notify(util.Sigterm, os.Interrupt)
signal.Stop(c)
if err != nil {
return err
}