Skip to content

Commit

Permalink
Fix SIGINT killing micro when saving with sudo
Browse files Browse the repository at this point in the history
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 zyedidia#2612 for RunInteractiveShell()
which was fixed by zyedidia#3357. So fix it the same way as in zyedidia#3357.
  • Loading branch information
dmaluka committed Oct 6, 2024
1 parent ac73f18 commit 4baac3d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions internal/buffer/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,19 +40,20 @@ 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
// contents to its stdin it might hang because the kernel's pipe size
// 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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4baac3d

Please sign in to comment.