Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,38 @@ import "time"
type Process interface {
// PID returns the process ID for this process.
PID() int

// PPID returns the parent process ID for this process.
PPID() int

// UID returns the numeric user ID for this process. On Windows, it
// always returns -1.
UID() int

// GID returns the numeric group ID for this process. On Windows, it
// always returns -1.
GID() int

// ExecutablePath returns the full path to the executable of this
// process. This information might not be available on all platforms or
// if the executable was removed while the process was still running.
ExecutablePath() string

// ExecutableArgs returns the command line arguments for this process,
// including the executable name. This information might not be
// available on all platforms.
ExecutableArgs() []string

// Command returns the command or executable name running this process.
// On some platforms (e.g. macOS and the BSDs) this name might be
// truncated.
Command() string

// CreationTime returns the creation time for this process.
CreationTime() time.Time

// Refresh reloads all data associated with this process from the OS.
Refresh() error
}

// Processes returns all currently running processes.
Expand Down
11 changes: 11 additions & 0 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ func TestFindProcessInit(t *testing.T) {
}
}

func TestProcessRefresh(t *testing.T) {
proc, err := ps.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("FindProcess: %v", err)
}

checkOwnProcess(t, proc)
proc.Refresh()
checkOwnProcess(t, proc)
}

func BenchmarkProcesses(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
Expand Down
17 changes: 17 additions & 0 deletions process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ps

import (
"fmt"
"path/filepath"
"time"
)
Expand Down Expand Up @@ -59,3 +60,19 @@ func (p *unixProcess) ExecutableArgs() []string {
func (p *unixProcess) CreationTime() time.Time {
return p.creationTime
}

func (p *unixProcess) Refresh() error {
proc, err := findProcess(p.pid)
if err != nil {
return fmt.Errorf("failed to refresh process: %w", err)
}
np := proc.(*unixProcess)
p.ppid = np.ppid
p.uid = np.uid
p.gid = np.gid
p.command = np.command
p.executablePath = np.executablePath
p.executableArgs = np.executableArgs
p.creationTime = np.creationTime
return nil
}
14 changes: 14 additions & 0 deletions process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func (p *windowsProcess) CreationTime() time.Time {
return p.creationTime
}

func (p *windowsProcess) Refresh() error {
proc, err := findProcess(p.pid)
if err != nil {
return err
}
np := proc.(*windowsProcess)
p.ppid = np.ppid
p.command = np.command
p.executablePath = np.executablePath
p.executableArgs = np.executableArgs
p.creationTime = np.creationTime
return nil
}

func getCreationTime(pid uint32) time.Time {
c, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
Expand Down