Skip to content

Commit

Permalink
Add ProcFS interface and PID() method to ProcPidFS
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Dec 31, 2024
1 parent cc9e216 commit 0da6137
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions restartable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ import flag "github.com/spf13/pflag"

const version string = "2.3.0"

// ProcPidFS defines an interface for /proc/<pid> filesystem access
type ProcPidFS interface {
// ProcFS defines an interface for /proc/ filesystem access
type ProcFS interface {
ReadFile(path string) ([]byte, error)
ReadLink(path string) (string, error)
Close() error
PID() int
}

// ProcPidFS defines an interface for /proc/<pid> filesystem access
type ProcPidFS interface {
ProcFS
PID() int
}

// RealProcPidFS implements ProcPidFS for real /proc/<pid> filesystem
Expand Down Expand Up @@ -91,6 +98,11 @@ func (p *RealProcPidFS) ReadLink(path string) (string, error) {
}
}

// PID returns the process ID
func (p *RealProcPidFS) PID() int {
return p.pid
}

var (
regexDeleted = regexp.MustCompile(`/.* \(deleted\)$`)
regexIgnored = regexp.MustCompile(`^/(dev|memfd:|run| )`)
Expand Down Expand Up @@ -210,6 +222,7 @@ func parseStatusField(data, key string) string {
type ProcessInfo struct {
Command string
Deleted []string
Pid int
Ppid int
Uid int
Service string
Expand Down Expand Up @@ -241,6 +254,7 @@ func getProcessInfo(fs ProcPidFS, fullPath bool, userService bool) (*ProcessInfo
return &ProcessInfo{
Command: quoteString(command),
Deleted: deleted,
Pid: fs.PID(),
Ppid: ppid,
Uid: uid,
Service: getService(fs, userService),
Expand Down Expand Up @@ -334,7 +348,7 @@ func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (Proc
continue
}
if opts.short < 3 {
fmt.Printf("%d\t%d\t%d\t%-20s\t%20s\t%s\n", pid, proc.Ppid, proc.Uid, getUser(proc.Uid), proc.Service, proc.Command)
fmt.Printf("%d\t%d\t%d\t%-20s\t%20s\t%s\n", proc.Pid, proc.Ppid, proc.Uid, getUser(proc.Uid), proc.Service, proc.Command)
} else if proc.Service != "-" {
services[proc.Service] = true
}
Expand Down

0 comments on commit 0da6137

Please sign in to comment.