-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathproc.go
100 lines (86 loc) · 1.92 KB
/
proc.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package common
import (
"debug/buildinfo"
"debug/elf"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
const (
goVersionPrefix = "Go cmd/compile"
)
// ErrVersionNotFound is returned when we can't find Go version info from a binary
var ErrVersionNotFound = errors.New("version info not found")
// GoVersion represents Go toolchain version that a binary is built with.
type GoVersion struct {
major int
minor int
}
// After returns true if it is greater than major.minor
func (v *GoVersion) After(major, minor int) bool {
if v.major > minor {
return true
}
if v.major == major && v.minor > minor {
return true
}
return false
}
// ExtraceGoVersion extracts Go version info from a binary that is built with Go toolchain
func ExtraceGoVersion(path string) (*GoVersion, error) {
bi, e := buildinfo.ReadFile(path)
if e != nil {
return nil, ErrVersionNotFound
}
gv, err := parseGoVersion(bi.GoVersion)
if err != nil {
return nil, err
}
return gv, nil
}
func parseGoVersion(r string) (*GoVersion, error) {
ver := strings.TrimPrefix(r, goVersionPrefix)
if strings.HasPrefix(ver, "go") {
v := strings.SplitN(ver[2:], ".", 3)
var major, minor int
var err error
major, err = strconv.Atoi(v[0])
if err != nil {
return nil, err
}
if len(v) >= 2 {
minor, err = strconv.Atoi(v[1])
if err != nil {
return nil, err
}
}
return &GoVersion{
major: major,
minor: minor,
}, nil
}
return nil, ErrVersionNotFound
}
func GetExecutablePathFromPid(pid int) (string, error) {
symlinkPath := fmt.Sprintf("/proc/%d/exe", pid)
executablePath, err := os.Readlink(symlinkPath)
if err != nil {
return "", err
}
return executablePath, nil
}
func IsGoExecutable(filename string) (bool, error) {
f, err := elf.Open(filename)
if err != nil {
return false, err
}
defer f.Close()
for _, section := range f.Sections {
if section.Name == ".note.go.buildid" {
return true, nil
}
}
return false, nil
}