Skip to content

Commit d60413f

Browse files
authored
Merge pull request zyedidia#3495 from dmaluka/sudo-sigint-fix
Fix SIGINT killing micro when saving with sudo
2 parents ac73f18 + af88b4d commit d60413f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

internal/buffer/save.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
3131
var writeCloser io.WriteCloser
3232
var screenb bool
3333
var cmd *exec.Cmd
34+
var c chan os.Signal
3435

3536
if withSudo {
3637
cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "bs=4k", "of="+name)
@@ -39,20 +40,21 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
3940
return
4041
}
4142

42-
c := make(chan os.Signal, 1)
43+
c = make(chan os.Signal, 1)
44+
signal.Reset(os.Interrupt)
4345
signal.Notify(c, os.Interrupt)
44-
go func() {
45-
<-c
46-
cmd.Process.Kill()
47-
}()
4846

4947
screenb = screen.TempFini()
5048
// need to start the process now, otherwise when we flush the file
5149
// contents to its stdin it might hang because the kernel's pipe size
5250
// is too small to handle the full file contents all at once
53-
if e := cmd.Start(); e != nil && err == nil {
51+
if err = cmd.Start(); err != nil {
5452
screen.TempStart(screenb)
55-
return err
53+
54+
signal.Notify(util.Sigterm, os.Interrupt)
55+
signal.Stop(c)
56+
57+
return
5658
}
5759
} else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
5860
return
@@ -80,6 +82,10 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error,
8082
// wait for dd to finish and restart the screen if we used sudo
8183
err := cmd.Wait()
8284
screen.TempStart(screenb)
85+
86+
signal.Notify(util.Sigterm, os.Interrupt)
87+
signal.Stop(c)
88+
8389
if err != nil {
8490
return err
8591
}

0 commit comments

Comments
 (0)