Skip to content

Commit

Permalink
pkg: support android on docker.
Browse files Browse the repository at this point in the history
Added detection of Android running on containers

Signed-off-by: cfc4n <cfc4n.cs@gmail.com>
  • Loading branch information
cfc4n committed Dec 23, 2023
1 parent d348803 commit 218405f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 92 deletions.
89 changes: 89 additions & 0 deletions pkg/util/ebpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import (
"fmt"
"golang.org/x/sys/unix"
"os"
"strings"
)

const (
ProcContainerCgroupPath = "/proc/1/cgroup"
ProcContainerSchedPath = "/proc/1/sched"
)

// CONFIG CHECK ITEMS
Expand Down Expand Up @@ -154,3 +160,86 @@ func IsEnableBPF() (bool, error) {

return true, nil
}

// IsContainer returns true if the process is running in a container.
func IsContainer() (bool, error) {
b, e := isContainerCgroup()
if e != nil {
return false, e
}

// if b is true, it's a container
if b {
return true, nil
}

// if b is false, continue to check sched
b, e = isContainerSched()
if e != nil {
return false, e
}

return b, nil
}

// isContainerCgroup returns true if the process is running in a container.
// https://www.baeldung.com/linux/is-process-running-inside-container

func isContainerCgroup() (bool, error) {
var f *os.File
var err error
var i int
f, err = os.Open(ProcContainerCgroupPath)
if err != nil {
return false, err
}
defer f.Close()
b := make([]byte, 1024)
i, err = f.Read(b)
if err != nil {
return false, err
}
switch {
case strings.Contains(string(b[:i]), "cpuset:/docker"):
// CGROUP V1 docker container
return true, nil
case strings.Contains(string(b[:i]), "cpuset:/kubepods"):
// k8s container
return true, nil
case strings.Contains(string(b[:i]), "0::/\n"):
// CGROUP V2 docker container
return true, nil
}

return false, nil
}

// isContainerSched returns true if the process is running in a container.
// https://man7.org/linux/man-pages/man7/sched.7.html
func isContainerSched() (bool, error) {
var f *os.File
var err error
var i int
f, err = os.Open(ProcContainerSchedPath)
if err != nil {
return false, err
}
defer f.Close()
b := make([]byte, 1024)
i, err = f.Read(b)
if err != nil {
return false, err
}
switch {
case strings.Contains(string(b[:i]), "bash (1, #threads"):
return true, nil
case strings.Contains(string(b[:i]), "run-on-arch-com (1, #threads"):
return true, nil
case strings.Contains(string(b[:i]), "init (1, #threads:"):
return false, nil
case strings.Contains(string(b[:i]), "systemd (1, #threads"):
return false, nil
}

return false, nil
}
5 changes: 0 additions & 5 deletions pkg/util/ebpf/bpf_androidgki.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,3 @@ func getAndroidConfig(filename string) (map[string]string, error) {
}
return KernelConfig, nil
}

// IsContainedInCgroup returns true if the process is running in a container.
func IsContainer() (bool, error) {
return false, nil
}
89 changes: 2 additions & 87 deletions pkg/util/ebpf/bpf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
)

const (
SysKernelBtfVmlinux = "/sys/kernel/btf/vmlinux"
ConfigDebugInfoBtf = "CONFIG_DEBUG_INFO_BTF"
ProcContainerCgroupPath = "/proc/1/cgroup"
ProcContainerSchedPath = "/proc/1/sched"
SysKernelBtfVmlinux = "/sys/kernel/btf/vmlinux"
ConfigDebugInfoBtf = "CONFIG_DEBUG_INFO_BTF"
)

var (
Expand Down Expand Up @@ -134,86 +132,3 @@ func getLinuxConfig(filename string) (map[string]string, error) {
}
return KernelConfig, nil
}

// IsContainer returns true if the process is running in a container.
func IsContainer() (bool, error) {
b, e := isContainerCgroup()
if e != nil {
return false, e
}

// if b is true, it's a container
if b {
return true, nil
}

// if b is false, continue to check sched
b, e = isCOntainerSched()
if e != nil {
return false, e
}

return b, nil
}

// isContainerCgroup returns true if the process is running in a container.
// https://www.baeldung.com/linux/is-process-running-inside-container

func isContainerCgroup() (bool, error) {
var f *os.File
var err error
var i int
f, err = os.Open(ProcContainerCgroupPath)
if err != nil {
return false, err
}
defer f.Close()
b := make([]byte, 1024)
i, err = f.Read(b)
if err != nil {
return false, err
}
switch {
case strings.Contains(string(b[:i]), "cpuset:/docker"):
// CGROUP V1 docker container
return true, nil
case strings.Contains(string(b[:i]), "cpuset:/kubepods"):
// k8s container
return true, nil
case strings.Contains(string(b[:i]), "0::/\n"):
// CGROUP V2 docker container
return true, nil
}

return false, nil
}

// isCOntainerSched returns true if the process is running in a container.
// https://man7.org/linux/man-pages/man7/sched.7.html
func isCOntainerSched() (bool, error) {
var f *os.File
var err error
var i int
f, err = os.Open(ProcContainerSchedPath)
if err != nil {
return false, err
}
defer f.Close()
b := make([]byte, 1024)
i, err = f.Read(b)
if err != nil {
return false, err
}
switch {
case strings.Contains(string(b[:i]), "bash (1, #threads"):
return true, nil
case strings.Contains(string(b[:i]), "run-on-arch-com (1, #threads"):
return true, nil
case strings.Contains(string(b[:i]), "init (1, #threads:"):
return false, nil
case strings.Contains(string(b[:i]), "systemd (1, #threads"):
return false, nil
}

return false, nil
}

0 comments on commit 218405f

Please sign in to comment.