go-libp2p-daemon/trap_posix.go

68 lines
1.5 KiB
Go
Raw Normal View History

2019-02-14 16:26:05 +00:00
// +build !windows,!plan9,!nacl,!js
package p2pd
import (
2019-04-08 18:31:22 +00:00
"fmt"
2019-02-14 16:26:05 +00:00
"os"
"os/signal"
"syscall"
)
func (d *Daemon) trapSignals() {
ch := make(chan os.Signal, 1)
2019-04-24 16:27:56 +00:00
signal.Notify(ch, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM)
2019-02-14 16:26:05 +00:00
for {
select {
case s := <-ch:
switch s {
case syscall.SIGUSR1:
d.handleSIGUSR1()
2019-04-24 16:27:56 +00:00
case syscall.SIGINT, syscall.SIGTERM:
2019-04-23 23:01:20 +00:00
d.Close()
2019-04-24 16:27:56 +00:00
os.Exit(0x80 + int(s.(syscall.Signal)))
2019-02-14 16:26:05 +00:00
default:
2019-04-24 14:21:02 +00:00
log.Warningf("uncaught signal %d", s)
2019-02-14 16:26:05 +00:00
}
case <-d.ctx.Done():
return
}
}
}
func (d *Daemon) handleSIGUSR1() {
// this is our signal to dump diagnostics info.
2019-02-14 16:26:05 +00:00
if d.dht != nil {
fmt.Println("DHT Routing Table:")
2019-02-14 16:26:05 +00:00
d.dht.RoutingTable().Print()
fmt.Println()
fmt.Println()
2019-02-14 16:26:05 +00:00
}
2019-04-08 18:31:22 +00:00
conns := d.host.Network().Conns()
fmt.Printf("Connections and streams (%d):\n", len(conns))
for _, c := range conns {
protos, _ := d.host.Peerstore().GetProtocols(c.RemotePeer()) // error value here is useless
protoVersion, err := d.host.Peerstore().Get(c.RemotePeer(), "ProtocolVersion")
if err != nil {
protoVersion = "(unknown)"
}
agent, err := d.host.Peerstore().Get(c.RemotePeer(), "AgentVersion")
if err != nil {
agent = "(unknown)"
}
2019-04-08 18:31:22 +00:00
streams := c.GetStreams()
fmt.Printf("peer: %s, multiaddr: %s\n", c.RemotePeer().Pretty(), c.RemoteMultiaddr())
fmt.Printf("\tprotoVersion: %s, agent: %s\n", protoVersion, agent)
fmt.Printf("\tprotocols: %v\n", protos)
fmt.Printf("\tstreams (%d):\n", len(streams))
2019-04-08 18:31:22 +00:00
for _, s := range streams {
fmt.Println("\t\tprotocol: ", s.Protocol())
2019-04-08 18:31:22 +00:00
}
}
2019-02-14 16:26:05 +00:00
}