~pmd/netps

netps/go-ps/process_linux.go -rwxr-xr-x 993 bytes
94ec6ec5 — Patrick McDonough Adding license 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// +build linux

package ps

import (
	"fmt"
	"io/ioutil"
	"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]

	// Move past the image name and start parsing the rest
	data = data[binStart+binEnd+2:]
	_, err = fmt.Sscanf(data,
		"%c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
		&p.state,
		&p.ppid,
		&p.pgrp,
		&p.sid,
		&p.ttynr,
		&p.tpgid,
		&p.flags,
		&p.minflt,
		&p.cminflt,
		&p.majflt,
		&p.cmajflt,
		&p.utime,
		&p.stime,
		&p.cutime,
		&p.cstime,
		&p.priority,
		&p.nice,
		&p.numthreads,
		&p.itrealvalue,
		&p.starttime,
		&p.vsize,
		&p.rss)

	return err
}