From 8d389aa3ada1696c677fffd43880716c5616e826 Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Mon, 26 Apr 2021 11:12:54 +0200 Subject: [PATCH] detects running pid only of it doesnt contain grep --- tools/pidof/pidof.go | 2 +- tools/pidof/pidof_test.go | 10 +++++++++- tools/pidof/testdata/psa_alpine_3.txt | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tools/pidof/testdata/psa_alpine_3.txt diff --git a/tools/pidof/pidof.go b/tools/pidof/pidof.go index 2700307..782629b 100644 --- a/tools/pidof/pidof.go +++ b/tools/pidof/pidof.go @@ -40,7 +40,7 @@ func Pidof(name string, cdr Commander) (int, error) { } for _, l := range strings.Split(string(p), "\n") { - if strings.Contains(l, name) { + if strings.Contains(l, name) && !strings.Contains(l, "grep") { // nolint:gocritic rePID, err := regexp.Compile(`(\d+)\s.+`) if err != nil { diff --git a/tools/pidof/pidof_test.go b/tools/pidof/pidof_test.go index 1e5a54a..cb44712 100644 --- a/tools/pidof/pidof_test.go +++ b/tools/pidof/pidof_test.go @@ -1,9 +1,10 @@ -// nolint:gochecknoglobals,paralleltest,goerr113 +// nolint:gochecknoglobals,paralleltest,goerr113,funlen package pidof_test import ( "embed" "fmt" + "log" "testing" "github.com/stretchr/testify/assert" @@ -52,9 +53,16 @@ func TestPidof(t *testing.T) { 2501, nil, }, + { + "/root/.byteexec/wireguard-go w1nd50r", + "psa_alpine_3.txt", + 0, + pidof.ErrPIDNotFound, + }, } for _, table := range tables { + log.Printf("testfile: %s", table.data) out, err := testdata.ReadFile("testdata/" + table.data) assert.NoError(err) diff --git a/tools/pidof/testdata/psa_alpine_3.txt b/tools/pidof/testdata/psa_alpine_3.txt new file mode 100644 index 0000000..ee5f87b --- /dev/null +++ b/tools/pidof/testdata/psa_alpine_3.txt @@ -0,0 +1 @@ + 2566 root 0:00 grep .byteexec/wireguard-go w1nd50r