Skip to content

Commit

Permalink
trap signals only on posix systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Feb 14, 2019
1 parent 9727bf7 commit 87fce04
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
30 changes: 1 addition & 29 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
}
8 changes: 8 additions & 0 deletions trap_nonposix.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions trap_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build !windows,!plan9,!nacl,!js

package p2pd

import (
"fmt"
"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()
}
}

0 comments on commit 87fce04

Please sign in to comment.