Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SIGINT killing micro when saving with sudo #3495

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 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,20 +40,21 @@ 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 {
if err = cmd.Start(); err != nil {
screen.TempStart(screenb)
return err

signal.Notify(util.Sigterm, os.Interrupt)
signal.Stop(c)

return
}
} else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
return
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