diff --git a/plugins/meta/dnsname/dnsname_test.go b/plugins/meta/dnsname/dnsname_test.go index 9646d8bfd3d3f27a4301c4242dacfc49cd5bfa28..5bc4032741ae4177162f56fc2cd9c041a3d15a58 100644 --- a/plugins/meta/dnsname/dnsname_test.go +++ b/plugins/meta/dnsname/dnsname_test.go @@ -127,7 +127,7 @@ var _ = Describe("dnsname tests", func() { Expect(err).To(BeNil()) // Check that the dns masq instance is running - pid, err := d.getPidProcess() + pid, err := d.getProcess() Expect(err).To(BeNil()) // Send it a signal 0; if alive, error will be nil err = pid.Signal(syscall.Signal(0)) @@ -166,7 +166,7 @@ var _ = Describe("dnsname tests", func() { d, err := newDNSMasqFile("foobar.io", "dummy0", "test") Expect(err).To(BeNil()) - pid, err := d.getPidProcess() + pid, err := d.getProcess() Expect(err).To(BeNil()) err = pid.Signal(syscall.Signal(0)) Expect(err).To(BeNil()) diff --git a/plugins/meta/dnsname/main.go b/plugins/meta/dnsname/main.go index 863048020bd5442b515b571fca35d8a0f91134a1..4b49f13417b72413802d84b57a6bb9908b6bfaf8 100644 --- a/plugins/meta/dnsname/main.go +++ b/plugins/meta/dnsname/main.go @@ -186,7 +186,7 @@ func cmdCheck(args *skel.CmdArgs) error { } }() - pid, err := dnsNameConf.getPidProcess() + pid, err := dnsNameConf.getProcess() if err != nil { return err } diff --git a/plugins/meta/dnsname/service.go b/plugins/meta/dnsname/service.go index 93c4ee3b2a02750720cee5212a6aad0889fb767d..6922ad462106e0e00cb84d2acbcf910c22af78a2 100644 --- a/plugins/meta/dnsname/service.go +++ b/plugins/meta/dnsname/service.go @@ -10,6 +10,7 @@ import ( "strings" "syscall" + "github.com/pkg/errors" "golang.org/x/sys/unix" ) @@ -38,7 +39,7 @@ func (d dnsNameFile) hup() error { if _, err := os.Stat(d.PidFile); os.IsNotExist(err) { return d.start() } - pid, err := d.getPidProcess() + pid, err := d.getProcess() if err != nil { return err } @@ -57,6 +58,7 @@ func isRunning(pid *os.Process) bool { return true } +// start starts the dnsmasq instance. func (d dnsNameFile) start() error { args := []string{ "-u", @@ -67,17 +69,18 @@ func (d dnsNameFile) start() error { return cmd.Run() } +// stop stops the dnsmasq instance. func (d dnsNameFile) stop() error { - pid, err := d.getPidProcess() + pid, err := d.getProcess() if err != nil { return err } return pid.Kill() } -// getPidProcess reads the PID for the dnsmasq instance and returns it in the -// form of an int -func (d dnsNameFile) getPidProcess() (*os.Process, error) { +// getProcess reads the PID for the dnsmasq instance and returns an +// *os.Process. Returns an error if the PID does not exist. +func (d dnsNameFile) getProcess() (*os.Process, error) { pidFileContents, err := ioutil.ReadFile(d.PidFile) if err != nil { return nil, err @@ -86,6 +89,9 @@ func (d dnsNameFile) getPidProcess() (*os.Process, error) { if err != nil { return nil, err } + if _, err := os.Stat(fmt.Sprintf("/proc/%d/", pid)); os.IsNotExist(err) { + return nil, errors.Wrapf(err, "dnsmasq process with PID %d does not exist", pid) + } return os.FindProcess(pid) }