diff options
author | Valentin Rothberg <rothberg@redhat.com> | 2019-09-20 09:30:49 +0200 |
---|---|---|
committer | Valentin Rothberg <rothberg@redhat.com> | 2019-09-20 09:48:50 +0200 |
commit | 4b5b1a2409784590456f14d501b484000a6f9a52 (patch) | |
tree | b5e83e555319b530ac8b6baa69687300276259c0 /plugins/meta/dnsname/service.go | |
parent | 88eb53f60c7835997ff782514d43df42912417b8 (diff) |
get dnsmasq process: lookup in proc FS
Golang`s `os.FindProcess()` always returns success on UNIX machines -
even when the PID doesn't exist. Hence, add a lookup in the proc FS
to check if the given exists or not. That's okay since dnsmane is
targetting Linux only.
Also rename the method to `getProcess()` and add some comments.
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'plugins/meta/dnsname/service.go')
-rw-r--r-- | plugins/meta/dnsname/service.go | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/plugins/meta/dnsname/service.go b/plugins/meta/dnsname/service.go index 93c4ee3..6922ad4 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) } |