41 lines
841 B
Go
41 lines
841 B
Go
// +build linux
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Refresh reloads all the data associated with this process.
|
|
func (p *UnixProcess) Refresh() error {
|
|
statPath := fmt.Sprintf("/proc/%d/stat", p.pid)
|
|
dataBytes, err := ioutil.ReadFile(statPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// First, parse out the image name
|
|
data := string(dataBytes)
|
|
binStart := strings.IndexRune(data, '(') + 1
|
|
binEnd := strings.IndexRune(data[binStart:], ')')
|
|
p.binary = data[binStart : binStart+binEnd]
|
|
|
|
stats := strings.Split(data, " ")
|
|
// http://man7.org/linux/man-pages/man5/proc.5.html
|
|
p.rss, err = strconv.Atoi(stats[23])
|
|
|
|
// Move past the image name and start parsing the rest
|
|
data = data[binStart+binEnd+2:]
|
|
_, err = fmt.Sscanf(data,
|
|
"%c %d %d %d",
|
|
&p.state,
|
|
&p.ppid,
|
|
&p.pgrp,
|
|
&p.sid)
|
|
|
|
return err
|
|
}
|