From cd5481c1c14d8028100e5a2c02a69edbee300715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Thu, 14 Feb 2019 16:26:05 +0000 Subject: [PATCH] trap signals only on posix systems. --- daemon.go | 30 +----------------------------- trap_nonposix.go | 8 ++++++++ trap_posix.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 trap_nonposix.go create mode 100644 trap_posix.go diff --git a/daemon.go b/daemon.go index b7f9d609..e9c78dcb 100644 --- a/daemon.go +++ b/daemon.go @@ -3,10 +3,7 @@ package p2pd import ( "context" "fmt" - "os" - "os/signal" "sync" - "syscall" logging "github.com/ipfs/go-log" libp2p "github.com/libp2p/go-libp2p" @@ -67,7 +64,7 @@ func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtEnabled bool, dhtClie d.listener = l go d.listen() - go d.handleSignals() + go d.trapSignals() return d, nil } @@ -148,28 +145,3 @@ func (d *Daemon) listen() { go d.handleConn(c) } } - -func (d *Daemon) handleSignals() { - ch := make(chan os.Signal, 1) - signal.Notify(ch, syscall.SIGUSR1) - for { - select { - case s := <-ch: - switch s { - case syscall.SIGUSR1: - d.handleSIGUSR1() - default: - log.Warningf("unexpected signal %d", s) - } - case <-d.ctx.Done(): - return - } - } -} - -func (d *Daemon) handleSIGUSR1() { - // this is the state dump signal; for now just dht routing table if present - if d.dht != nil { - d.dht.RoutingTable().Print() - } -} diff --git a/trap_nonposix.go b/trap_nonposix.go new file mode 100644 index 00000000..365bdcc0 --- /dev/null +++ b/trap_nonposix.go @@ -0,0 +1,8 @@ +// +build windows plan9 nacl js + +package p2pd + +func (d *Daemon) trapSignals() { + // TODO: define signals we want to trap on Windows, if any. + return +} diff --git a/trap_posix.go b/trap_posix.go new file mode 100644 index 00000000..c821fc1a --- /dev/null +++ b/trap_posix.go @@ -0,0 +1,34 @@ +// +build !windows,!plan9,!nacl,!js + +package p2pd + +import ( + "os" + "os/signal" + "syscall" +) + +func (d *Daemon) trapSignals() { + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGUSR1) + for { + select { + case s := <-ch: + switch s { + case syscall.SIGUSR1: + d.handleSIGUSR1() + default: + log.Warningf("unexpected signal %d", s) + } + case <-d.ctx.Done(): + return + } + } +} + +func (d *Daemon) handleSIGUSR1() { + // this is the state dump signal; for now just dht routing table if present + if d.dht != nil { + d.dht.RoutingTable().Print() + } +}