Skip to content

Commit

Permalink
Make signal handling portable (#122)
Browse files Browse the repository at this point in the history
Plan9port and Plan 9 acme handles these signal:

```
char *oknotes[] ={
	"delete",
	"hangup",	// SIGHUP
	"kill",		// SIGTERM
	"exit",
	nil
};
```

I'm not sure what generates the "delete" and "exit" signals, but SIGTERM
was missing from Edwood's list, so I've added that. SIGQUIT is useful
for debugging a Go program (typing ctrl-\ will print stack trace of all
running goroutines), so I removed that. SIGSTOP cannot be handled, so
that was also removed.

Edwood was ignoring all other signals, which seems wrong. I made it so
it'll only ignore what plan9port acme was ignoring:

```
char *ignotes[] = {
	"sys: write on closed pipe",	// SIGPIPE
	"sys: ttin",	// SIGTTIN
	"sys: ttou",	// SIGTTOU
	"sys: tstp",	// SIGTSTP
	nil
};
```

Helps #115
Helps #116
  • Loading branch information
fhs authored and rjkroege committed Sep 14, 2018
1 parent 8af8da8 commit d8b9e71
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
28 changes: 8 additions & 20 deletions acme.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"time"

"9fans.net/go/draw"
Expand Down Expand Up @@ -175,7 +174,8 @@ func main() {
go newwindowthread()
go xfidallocthread(display)

signal.Notify(csignal /*, hangupsignals...*/)
signal.Ignore(ignoreSignals...)
signal.Notify(csignal, hangupSignals...)
for {
select {
case <-cexit:
Expand Down Expand Up @@ -665,29 +665,17 @@ func killprocs() {

var dumping bool

var hangupsignals = []os.Signal{
os.Signal(syscall.SIGINT),
os.Signal(syscall.SIGHUP),
os.Signal(syscall.SIGQUIT),
os.Signal(syscall.SIGSTOP),
}

// TODO(rjk): I'm not sure that this is the right thing to do? It fails to
// handle the situation that is most interesting: trying to save the state
// if we would otherwise crash. It's also conceivably racy.
func shutdown(s os.Signal) {
for _, sig := range hangupsignals {
if sig == s {
if !dumping && os.Getpid() == mainpid {
killprocs()
dumping = true
row.Dump("")
} else {
os.Exit(0)
}
}
if !dumping && os.Getpid() == mainpid {
killprocs()
dumping = true
row.Dump("")
} else {
os.Exit(0)
}
return
}

type errorWriter struct{}
Expand Down
2 changes: 1 addition & 1 deletion fsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func fsysproc() {
for {
fc, err := plan9.ReadFcall(sfd)
if err != nil || fc == nil {
acmeerror("fsysproc: ", err)
acmeerror("fsysproc", err)
}
if x == nil {
cxfidalloc <- nil
Expand Down
18 changes: 18 additions & 0 deletions signal_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build plan9

package main

import (
"os"
"syscall"
)

var ignoreSignals = []os.Signal{
syscall.Note("sys: write on closed pipe"),
}

var hangupSignals = []os.Signal{
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
}
21 changes: 21 additions & 0 deletions signal_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package main

import (
"os"
"syscall"
)

var ignoreSignals = []os.Signal{
syscall.SIGPIPE,
syscall.SIGTTIN,
syscall.SIGTTOU,
syscall.SIGTSTP,
}

var hangupSignals = []os.Signal{
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
}
18 changes: 18 additions & 0 deletions signal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build windows

package main

import (
"os"
"syscall"
)

var ignoreSignals = []os.Signal{
syscall.SIGPIPE,
}

var hangupSignals = []os.Signal{
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
}

0 comments on commit d8b9e71

Please sign in to comment.