Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable contextcheck linter #1210

Merged
merged 2 commits into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Expand Up @@ -13,6 +13,7 @@ issues:
linters:
enable:
- asciicheck
- contextcheck
- durationcheck
- errorlint
- gci
Expand Down
12 changes: 8 additions & 4 deletions cpu/cpu.go
Expand Up @@ -142,11 +142,11 @@ func Percent(interval time.Duration, percpu bool) ([]float64, error) {

func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) {
if interval <= 0 {
return percentUsedFromLastCall(percpu)
return percentUsedFromLastCallWithContext(ctx, percpu)
}

// Get CPU usage at the start of the interval.
cpuTimes1, err := Times(percpu)
cpuTimes1, err := TimesWithContext(ctx, percpu)
if err != nil {
return nil, err
}
Expand All @@ -156,7 +156,7 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool
}

// And at the end of the interval.
cpuTimes2, err := Times(percpu)
cpuTimes2, err := TimesWithContext(ctx, percpu)
if err != nil {
return nil, err
}
Expand All @@ -165,7 +165,11 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool
}

func percentUsedFromLastCall(percpu bool) ([]float64, error) {
cpuTimes, err := Times(percpu)
return percentUsedFromLastCallWithContext(context.Background(), percpu)
}

func percentUsedFromLastCallWithContext(ctx context.Context, percpu bool) ([]float64, error) {
cpuTimes, err := TimesWithContext(ctx, percpu)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions docker/docker_linux.go
Expand Up @@ -162,11 +162,11 @@ func CgroupCPUUsageDocker(containerid string) (float64, error) {
}

func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) {
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
return CgroupCPUWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}

func CgroupCPUDockerUsageWithContext(ctx context.Context, containerid string) (float64, error) {
return CgroupCPUUsage(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
return CgroupCPUUsageWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}

func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
Expand Down Expand Up @@ -274,7 +274,7 @@ func CgroupMemDocker(containerID string) (*CgroupMemStat, error) {
}

func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*CgroupMemStat, error) {
return CgroupMem(containerID, common.HostSys("fs/cgroup/memory/docker"))
return CgroupMemWithContext(ctx, containerID, common.HostSys("fs/cgroup/memory/docker"))
}

// getCgroupFilePath constructs file path to get targeted stats file.
Expand Down
2 changes: 1 addition & 1 deletion internal/common/common_linux.go
Expand Up @@ -56,7 +56,7 @@ func NumProcs() (uint64, error) {
}

func BootTimeWithContext(ctx context.Context) (uint64, error) {
system, role, err := Virtualization()
system, role, err := VirtualizationWithContext(ctx)
if err != nil {
return 0, err
}
Expand Down
42 changes: 31 additions & 11 deletions net/net_linux.go
Expand Up @@ -392,7 +392,7 @@ func Connections(kind string) ([]ConnectionStat, error) {
}

func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
return ConnectionsPid(kind, 0)
return ConnectionsPidWithContext(ctx, kind, 0)
}

// Return a list of network connections opened returning at most `max`
Expand All @@ -402,7 +402,7 @@ func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
}

func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return ConnectionsPidMax(kind, 0, max)
return ConnectionsPidMaxWithContext(ctx, kind, 0, max)
}

// Return a list of network connections opened, omitting `Uids`.
Expand Down Expand Up @@ -463,7 +463,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
var err error
var inodes map[string][]inodeMap
if pid == 0 {
inodes, err = getProcInodesAll(root, max)
inodes, err = getProcInodesAllWithContext(ctx, root, max)
} else {
inodes, err = getProcInodes(root, pid, max)
if len(inodes) == 0 {
Expand All @@ -474,10 +474,14 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
if err != nil {
return nil, fmt.Errorf("cound not get pid(s), %d: %w", pid, err)
}
return statsFromInodes(root, pid, tmap, inodes, skipUids)
return statsFromInodesWithContext(ctx, root, pid, tmap, inodes, skipUids)
}

func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) {
return statsFromInodesWithContext(context.Background(), root, pid, tmap, inodes, skipUids)
}

func statsFromInodesWithContext(ctx context.Context, root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) {
dupCheckMap := make(map[string]struct{})
var ret []ConnectionStat

Expand All @@ -493,7 +497,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode
}
switch t.family {
case syscall.AF_INET, syscall.AF_INET6:
ls, err = processInet(path, t, inodes, pid)
ls, err = processInetWithContext(ctx, path, t, inodes, pid)
case syscall.AF_UNIX:
ls, err = processUnix(path, t, inodes, pid)
}
Expand Down Expand Up @@ -666,7 +670,11 @@ func (p *process) fillFromStatus() error {
}

func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
pids, err := Pids()
return getProcInodesAllWithContext(context.Background(), root, max)
}

func getProcInodesAllWithContext(ctx context.Context, root string, max int) (map[string][]inodeMap, error) {
pids, err := PidsWithContext(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -695,6 +703,10 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
// "0500000A:0016" -> "10.0.0.5", 22
// "0085002452100113070057A13F025401:0035" -> "2400:8500:1301:1052:a157:7:154:23f", 53
func decodeAddress(family uint32, src string) (Addr, error) {
return decodeAddressWithContext(context.Background(), family, src)
}

func decodeAddressWithContext(ctx context.Context, family uint32, src string) (Addr, error) {
t := strings.Split(src, ":")
if len(t) != 2 {
return Addr{}, fmt.Errorf("does not contain port, %s", src)
Expand All @@ -711,9 +723,9 @@ func decodeAddress(family uint32, src string) (Addr, error) {
var ip net.IP
// Assumes this is little_endian
if family == syscall.AF_INET {
ip = net.IP(Reverse(decoded))
ip = net.IP(ReverseWithContext(ctx, decoded))
} else { // IPv6
ip, err = parseIPv6HexString(decoded)
ip, err = parseIPv6HexStringWithContext(ctx, decoded)
if err != nil {
return Addr{}, err
}
Expand All @@ -738,19 +750,27 @@ func ReverseWithContext(ctx context.Context, s []byte) []byte {

// parseIPv6HexString parse array of bytes to IPv6 string
func parseIPv6HexString(src []byte) (net.IP, error) {
return parseIPv6HexStringWithContext(context.Background(), src)
}

func parseIPv6HexStringWithContext(ctx context.Context, src []byte) (net.IP, error) {
if len(src) != 16 {
return nil, fmt.Errorf("invalid IPv6 string")
}

buf := make([]byte, 0, 16)
for i := 0; i < len(src); i += 4 {
r := Reverse(src[i : i+4])
r := ReverseWithContext(ctx, src[i:i+4])
buf = append(buf, r...)
}
return net.IP(buf), nil
}

func processInet(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
return processInetWithContext(context.Background(), file, kind, inodes, filterPid)
}

func processInetWithContext(ctx context.Context, file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
if strings.HasSuffix(file, "6") && !common.PathExists(file) {
// IPv6 not supported, return empty.
return []connTmp{}, nil
Expand Down Expand Up @@ -793,11 +813,11 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
} else {
status = "NONE"
}
la, err := decodeAddress(kind.family, laddr)
la, err := decodeAddressWithContext(ctx, kind.family, laddr)
if err != nil {
continue
}
ra, err := decodeAddress(kind.family, raddr)
ra, err := decodeAddressWithContext(ctx, kind.family, raddr)
if err != nil {
continue
}
Expand Down
34 changes: 21 additions & 13 deletions process/process_linux.go
Expand Up @@ -83,7 +83,7 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {

func (p *Process) NameWithContext(ctx context.Context) (string, error) {
if p.name == "" {
if err := p.fillNameWithContext(); err != nil {
if err := p.fillNameWithContext(ctx); err != nil {
return "", err
}
}
Expand All @@ -92,7 +92,7 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
if p.tgid == 0 {
if err := p.fillFromStatusWithContext(); err != nil {
if err := p.fillFromStatusWithContext(ctx); err != nil {
return 0, err
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
}

func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return []string{""}, err
}
Expand All @@ -149,23 +149,23 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
}

func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return []int32{}, err
}
return p.uids, nil
}

func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return []int32{}, err
}
return p.gids, nil
}

func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return []int32{}, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) (
if err != nil {
return nil, err
}
if err := p.fillFromStatusWithContext(); err != nil {
if err := p.fillFromStatusWithContext(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e
}

func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return nil, err
}
Expand All @@ -274,7 +274,7 @@ func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
}

func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
err := p.fillFromStatusWithContext()
err := p.fillFromStatusWithContext(ctx)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -783,12 +783,12 @@ func (p *Process) fillFromStatmWithContext() (*MemoryInfoStat, *MemoryInfoExStat
}

// Get name from /proc/(pid)/comm or /proc/(pid)/status
func (p *Process) fillNameWithContext() error {
func (p *Process) fillNameWithContext(ctx context.Context) error {
err := p.fillFromCommWithContext()
if err == nil && p.name != "" && len(p.name) < 15 {
return nil
}
return p.fillFromStatusWithContext()
return p.fillFromStatusWithContext(ctx)
}

// Get name from /proc/(pid)/comm
Expand All @@ -805,7 +805,11 @@ func (p *Process) fillFromCommWithContext() error {
}

// Get various status from /proc/(pid)/status
func (p *Process) fillFromStatusWithContext() error {
func (p *Process) fillFromStatus() error {
return p.fillFromStatusWithContext(context.Background())
}

func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "status")
contents, err := ioutil.ReadFile(statPath)
Expand All @@ -826,7 +830,7 @@ func (p *Process) fillFromStatusWithContext() error {
case "Name":
p.name = strings.Trim(value, " \t")
if len(p.name) >= 15 {
cmdlineSlice, err := p.CmdlineSlice()
cmdlineSlice, err := p.CmdlineSliceWithContext(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -1003,6 +1007,10 @@ func (p *Process) fillFromStatusWithContext() error {
return nil
}

func (p *Process) fillFromTIDStat(tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) {
return p.fillFromTIDStatWithContext(context.Background(), tid)
}

func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) {
pid := p.Pid
var statPath string
Expand Down
7 changes: 3 additions & 4 deletions process/process_linux_test.go
Expand Up @@ -4,7 +4,6 @@
package process

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -133,7 +132,7 @@ func Test_fillFromStatusWithContext(t *testing.T) {
continue
}
p, _ := NewProcess(int32(pid))
if err := p.fillFromStatusWithContext(); err != nil {
if err := p.fillFromStatus(); err != nil {
t.Error(err)
}
}
Expand All @@ -155,7 +154,7 @@ func Benchmark_fillFromStatusWithContext(b *testing.B) {
pid := 1060
p, _ := NewProcess(int32(pid))
for i := 0; i < b.N; i++ {
p.fillFromStatusWithContext()
p.fillFromStatus()
}
}

Expand All @@ -175,7 +174,7 @@ func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) {
continue
}
p, _ := NewProcess(int32(pid))
_, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(context.Background(), -1)
_, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStat(-1)
if err != nil {
t.Error(err)
}
Expand Down