-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpinfo_linux.go
More file actions
executable file
·57 lines (39 loc) · 899 Bytes
/
pinfo_linux.go
File metadata and controls
executable file
·57 lines (39 loc) · 899 Bytes
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
54
package pdiscover
import (
"os"
"strconv"
"io/ioutil"
)
func procFileName(pid int, name string) string {
return "/proc/" + strconv.Itoa(pid) + "/" + name
}
func GetProcInfo(pid int) map[string]string {
linkFields := []string{"exe","cwd","root"}
valFields := []string{"cmdline","environ"}
fields := map[string]string{}
for _,name := range linkFields {
val, err := os.Readlink(procFileName(pid, name))
if err != nil {
return nil
}
fields[name] = val
}
for _,name := range valFields {
val, err := ioutil.ReadFile(procFileName(pid, name))
if err != nil {
return nil
}
fields[name] = string(val)
}
return fields
}
func GetOwnProcPath() (string,error) {
return os.Readlink("/proc/self/exe")
}
func GetProcPath(pid int) (string,error) {
procInfo := GetProcInfo(pid)
if procInfo == nil {
return "",ErrInvalidProcInfo
}
return procInfo["exe"],nil
}