Skip to content

Commit

Permalink
Merge pull request #15 from cloudradar-monitoring/clearify_proc_file_…
Browse files Browse the repository at this point in the history
…errors

PROC: make errors more clear
  • Loading branch information
soupdiver authored Oct 26, 2018
2 parents c716247 + c2195e2 commit 10fcb22
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions processes_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
log "github.com/sirupsen/logrus"
)

var errorProcessTerminated = fmt.Errorf("Process was terminated")

func processes() ([]ProcStat, error) {
if runtime.GOOS == "linux" {
return processesFromProc()
Expand All @@ -25,13 +27,11 @@ func processes() ([]ProcStat, error) {
}

func getHostProc() string {
procPath := "/proc"

if os.Getenv("HOST_PROC") != "" {
procPath = os.Getenv("HOST_PROC")
if hostProc := os.Getenv("HOST_PROC"); hostProc != "" {
return hostProc
}

return procPath
return "/proc"
}

// get process states from /proc/(pid)/stat
Expand All @@ -46,10 +46,9 @@ func processesFromProc() ([]ProcStat, error) {
for _, filename := range filenames {
data, err := readProcFile(filename)
if err != nil {
return nil, err
}

if data == nil {
if err != errorProcessTerminated {
log.Error("readProcFile error ", err.Error())
}
continue
}

Expand All @@ -62,28 +61,28 @@ func processesFromProc() ([]ProcStat, error) {
pid, err := strconv.Atoi(string(stats[0]))
if err != nil {
log.Errorf("Failed to convert PID(%s) to int: %s", stats[0], err.Error())
continue
}

comm, err := readProcFile(getHostProc() + "/" + string(stats[0]) + "/comm")
ppid, err := strconv.Atoi(string(stats[4]))
if err != nil {
log.Errorf("Failed to read comm(%s): %s", stats[0], err.Error())
log.Errorf("Failed to convert PPID(%s) to int: %s", stats[4], err.Error())
}

cmdline, err := readProcFile(getHostProc() + "/" + string(stats[0]) + "/cmdline")
if err != nil {
log.Errorf("Failed to read cmdline(%s): %s", stats[0], err.Error())
}
stat := ProcStat{PID: pid, ParentPID: ppid}

ppid, err := strconv.Atoi(string(stats[4]))
if err != nil {
log.Errorf("Failed to convert PPID(%s) to int: %s", stats[4], err.Error())
comm, err := readProcFile(getHostProc() + "/" + string(stats[0]) + "/comm")
if err != nil && err != errorProcessTerminated {
log.Errorf("Failed to read comm(%s): %s", stats[0], err.Error())
} else if err == nil {
stat.Name = string(bytes.TrimRight(comm, "\n"))
}

stat := ProcStat{
PID: pid,
ParentPID: ppid,
Name: string(bytes.TrimRight(comm, "\n")),
Cmdline: strings.Replace(string(bytes.TrimRight(cmdline, "\x00")), "\x00", " ", -1),
cmdline, err := readProcFile(getHostProc() + "/" + string(stats[0]) + "/cmdline")
if err != nil && err != errorProcessTerminated {
log.Errorf("Failed to read cmdline(%s): %s", stats[0], err.Error())
} else if err == nil {
stat.Cmdline = strings.Replace(string(bytes.TrimRight(cmdline, "\x00")), "\x00", " ", -1)
}

switch stats[2][0] {
Expand Down Expand Up @@ -115,15 +114,15 @@ func processesFromProc() ([]ProcStat, error) {
func readProcFile(filename string) ([]byte, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
// if file doesn't exists it means that process was closed after we got the directory listing
if os.IsNotExist(err) {
// if file doesn't exists
return nil, nil
return nil, errorProcessTerminated
}

// Reading from /proc/<PID> fails with ESRCH if the process has
// been terminated between open() and read().
if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ESRCH {
return nil, nil
return nil, errorProcessTerminated
}

return nil, err
Expand Down

0 comments on commit 10fcb22

Please sign in to comment.