Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
netmon: Add signals handler
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Sep 19, 2018
1 parent 1675410 commit 048616f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions netmon/netmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log/syslog"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/kata-containers/runtime/pkg/signals"
"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -203,6 +207,39 @@ func (n *netmon) cleanup() {
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 {
fields := logrus.Fields{
"name": netmonName,
Expand Down Expand Up @@ -641,6 +678,9 @@ func main() {
os.Exit(1)
}

// Setup signal handlers
n.setupSignalHandler()

// Scan the current interfaces.
if err := n.scanNetwork(); err != nil {
n.logger().WithError(err).Fatal("scanNetwork()")
Expand Down

0 comments on commit 048616f

Please sign in to comment.