From 4baac3d3fb5439ad0f678c444b3c8b779e81d6f3 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 6 Oct 2024 16:41:54 +0200 Subject: [PATCH 1/2] 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. --- internal/buffer/save.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/buffer/save.go b/internal/buffer/save.go index efbf04f4c..e02394350 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -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 } From af88b4d2a8b0fb1e67bb5a1be5b2f0dab8b1f8a8 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 6 Oct 2024 16:55:58 +0200 Subject: [PATCH 2/2] Fix error reporting when saving with sudo failed When saving a file with sudo fails (e.g. if we set `sucmd` to a non-existent binary, e.g. `set sucmd aaa`), we erroneously return success instead of the error, as a result we report to the user that that the file has been successfully saved. Fix it. --- internal/buffer/save.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/buffer/save.go b/internal/buffer/save.go index e02394350..af1d9deb2 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -48,13 +48,13 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error, // 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) signal.Notify(util.Sigterm, os.Interrupt) signal.Stop(c) - return err + return } } else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil { return