Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[process] Fix #607 properly check PID existence #713

Merged
merged 3 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,6 @@ func PidExists(pid int32) (bool, error) {
return PidExistsWithContext(context.Background(), pid)
}

func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
pids, err := Pids()
if err != nil {
return false, err
}

for _, i := range pids {
if i == pid {
return true, err
}
}

return false, err
}

// Background returns true if the process is in background, false otherwise.
func (p *Process) Background() (bool, error) {
return p.BackgroundWithContext(context.Background())
Expand Down
15 changes: 15 additions & 0 deletions process/process_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func NewProcess(pid int32) (*Process, error) {
return nil, common.ErrNotImplementedError
}

func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
pids, err := PidsWithContext(ctx)
if err != nil {
return false, err
}

for _, i := range pids {
if i == pid {
return true, err
}
}

return false, err
}

func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
Expand Down
29 changes: 29 additions & 0 deletions process/process_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package process

import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -69,6 +70,34 @@ func getTerminalMap() (map[uint64]string, error) {
return ret, nil
}

func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
if pid <= 0 {
return false, fmt.Errorf("invalid pid %v", pid)
}
proc, err := os.FindProcess(int(pid))
if err != nil {
return false, err
}
err = proc.Signal(syscall.Signal(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line actually breaks the whole story if you are running inside container, and you are tracking host processes.
More details can be found here:
influxdata/telegraf#6813

if err == nil {
return true, nil
}
if err.Error() == "os: process already finished" {
return false, nil
}
errno, ok := err.(syscall.Errno)
if !ok {
return false, err
}
switch errno {
case syscall.ESRCH:
return false, nil
case syscall.EPERM:
return true, nil
}
return false, err
}

// SendSignal sends a unix.Signal to the process.
// Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported.
func (p *Process) SendSignal(sig syscall.Signal) error {
Expand Down
15 changes: 15 additions & 0 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ func PidsWithContext(ctx context.Context) ([]int32, error) {

}

func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
pids, err := Pids()
if err != nil {
return false, err
}

for _, i := range pids {
if i == pid {
return true, err
}
}

return false, err
}

func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
Expand Down