netmon: Add signals handler

After the signals package has been created and shared with the CLI,
this commit calls into it in order to properly handle the signals
directed to the network monitor process.

Fixes #718

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-09-18 23:10:51 -07:00
parent 1675410256
commit 048616fb8d

View File

@ -7,6 +7,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -14,10 +15,13 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
"time" "time"
"github.com/kata-containers/runtime/pkg/signals"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog" lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
@ -203,6 +207,39 @@ func (n *netmon) cleanup() {
close(n.rtDoneCh) close(n.rtDoneCh)
} }
// setupSignalHandler sets up signal handling, starting a go routine to deal
// with signals as they arrive.
func (n *netmon) setupSignalHandler() {
signals.SetLogger(n.logger())
sigCh := make(chan os.Signal, 8)
for _, sig := range signals.HandledSignals() {
signal.Notify(sigCh, sig)
}
go func() {
for {
sig := <-sigCh
nativeSignal, ok := sig.(syscall.Signal)
if !ok {
err := errors.New("unknown signal")
netmonLog.WithError(err).WithField("signal", sig.String()).Error()
continue
}
if signals.FatalSignal(nativeSignal) {
netmonLog.WithField("signal", sig).Error("received fatal signal")
signals.Die(nil)
} else if n.debug && signals.NonFatalSignal(nativeSignal) {
netmonLog.WithField("signal", sig).Debug("handling signal")
signals.Backtrace()
}
}
}()
}
func (n *netmon) logger() *logrus.Entry { func (n *netmon) logger() *logrus.Entry {
fields := logrus.Fields{ fields := logrus.Fields{
"name": netmonName, "name": netmonName,
@ -641,6 +678,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Setup signal handlers
n.setupSignalHandler()
// Scan the current interfaces. // Scan the current interfaces.
if err := n.scanNetwork(); err != nil { if err := n.scanNetwork(); err != nil {
n.logger().WithError(err).Fatal("scanNetwork()") n.logger().WithError(err).Fatal("scanNetwork()")