summaryrefslogtreecommitdiff
path: root/process/process.go
blob: c0e25e2293c28b14e4d427ad7fd4aafaee0585b6 (plain)
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
package process

import (
	"fmt"
	"io/ioutil"
	"log"
	"regexp"
	"strconv"
)

type Process struct {
	Pid     int
	Cmdline string
}

func (self *Process) String() string {
	str := "========================="

	str = str + fmt.Sprintf("PID: %d\n", self.Pid)
	str = str + fmt.Sprintf("cmdline: %s\n", self.Cmdline)

	return str
}

func (self *Process) Print() {
	fmt.Println(self)
}

func Gather(res chan<- Process) {
	re, _ := regexp.Compile("^[0-9]+$")

	dir, err := ioutil.ReadDir("/proc/")
	if err != nil {
		log.Fatal(err)
	}

	for _, direntry := range dir {
		name := direntry.Name()
		if re.MatchString(name) {
			pid, _ := strconv.Atoi(name)
			bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
			if err != nil {
				log.Fatal(err)
			}
			if len(bytes) > 0 {
				res <- Process{Pid: pid, Cmdline: string(bytes)}
			}
		}
	}
}