Skip to content

Commit

Permalink
proc: fix could not read process comm name error
Browse files Browse the repository at this point in the history
fix #531
  • Loading branch information
ggndnn committed Aug 5, 2016
1 parent 218c2b9 commit c3ade94
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions proc/proc_linux.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package proc

import (
"bytes"
"debug/gosym"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -350,12 +352,30 @@ func (dbp *Process) loadProcessInformation(wg *sync.WaitGroup) {
defer wg.Done()

comm, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", dbp.Pid))
if err != nil {
fmt.Printf("Could not read process comm name: %v\n", err)
os.Exit(1)
if err == nil {
// removes newline character
comm = bytes.TrimSuffix(comm, []byte("\n"))
}

if comm == nil || len(comm) <= 0 {
stat, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", dbp.Pid))
if err != nil {
fmt.Printf("Could not read proc stat: %v\n", err)
os.Exit(1)
}
expr := fmt.Sprintf("%d\\s*\\((.*)\\)", dbp.Pid)
rexp, err := regexp.Compile(expr)
if err != nil {
fmt.Printf("Regexp compile error: %v\n", err)
os.Exit(1)
}
match := rexp.FindSubmatch(stat)
if match == nil {
fmt.Printf("No match found using regexp '%s' in /proc/%d/stat\n", expr, dbp.Pid)
os.Exit(1)
}
comm = match[1]
}
// removes newline character
comm = comm[:len(comm)-1]
dbp.os.comm = strings.Replace(string(comm), "%", "%%", -1)
}

Expand Down

0 comments on commit c3ade94

Please sign in to comment.