1 Star 0 Fork 2

安易科技(北京)有限公司/chameleon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
procfs.go 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
Derek Ray 提交于 2025-05-20 17:14 +08:00 . feat(specs): add enforcer specs cache
package procfs
import (
"fmt"
"gitee.com/anesec/chameleon/pkg/features"
"gitee.com/anesec/chameleon/pkg/fs"
"gitee.com/anesec/chameleon/pkg/types"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
var hostProcfs procfs
func InitHostProcfs() error {
if hostProcfs.mountPoint = os.Getenv(types.ChameleonHostProcfs); hostProcfs.mountPoint == "" {
if features.UseHostPidNamespace() {
hostProcfs.mountPoint = "/proc"
_ = os.Setenv(types.ChameleonHostProcfs, hostProcfs.mountPoint)
}
}
if hostProcfs.mountPoint == "" {
return fmt.Errorf("unable to determine host procfs mount point")
}
stat := syscall.Statfs_t{}
err := syscall.Statfs(hostProcfs.mountPoint, &stat)
if err != nil {
return fmt.Errorf("unable to determine host procfs mount point, %w", err)
}
// 0x9fa0 is PROC_SUPER_MAGIC: https://elixir.bootlin.com/linux/v6.1/source/include/uapi/linux/magic.h#L87
if stat.Type != 0x9fa0 {
return fmt.Errorf("unable to determine host procfs mount point, invalid procfs type: %d", stat.Type)
}
index := strings.LastIndex(hostProcfs.mountPoint, "/proc")
if index == -1 {
return fmt.Errorf("unable to determine host procfs mount point %q", hostProcfs.mountPoint)
}
if hostProcfs.FS, err = fs.New(hostProcfs.mountPoint[:index]); err != nil {
return fmt.Errorf("unable to determine host procfs mount point, %w", err)
}
return nil
}
func MountPoint() string {
return hostProcfs.mountPoint
}
func NewProcess(pid int) (*Process, error) {
return hostProcfs.NewProcess(pid)
}
func AllProcesses() ([]*Process, error) {
return hostProcfs.AllProcesses()
}
type Procfs interface {
MountPoint() string
NewProcess(pid int) (*Process, error)
AllProcesses() ([]*Process, error)
}
func New(mountPoint string) (Procfs, error) {
pfs := &procfs{
mountPoint: mountPoint,
}
stat := syscall.Statfs_t{}
err := syscall.Statfs(pfs.mountPoint, &stat)
if err != nil {
return nil, fmt.Errorf("invalid procfs mount point, %w", err)
}
// 0x9fa0 is PROC_SUPER_MAGIC: https://elixir.bootlin.com/linux/v6.1/source/include/uapi/linux/magic.h#L87
if stat.Type != 0x9fa0 {
return nil, fmt.Errorf("invalid procfs type: %d", stat.Type)
}
index := strings.LastIndex(pfs.mountPoint, "/proc")
if index == -1 {
return nil, fmt.Errorf("invalid procfs mount point %q", pfs.mountPoint)
}
if pfs.FS, err = fs.New(pfs.mountPoint[:index]); err != nil {
return nil, err
}
return pfs, nil
}
type procfs struct {
*fs.FS
mountPoint string
}
func (pfs *procfs) MountPoint() string {
return pfs.mountPoint
}
func (pfs *procfs) NewProcess(pid int) (*Process, error) {
proc := &Process{
pid: pid,
root: filepath.Join(pfs.mountPoint, strconv.Itoa(pid)),
pfs: pfs,
}
if _, err := pfs.Stat(proc.root); err != nil {
return nil, fmt.Errorf("process %d %w", pid, os.ErrNotExist)
}
return proc, nil
}
func (pfs *procfs) AllProcesses() ([]*Process, error) {
file, err := pfs.Open(pfs.mountPoint)
if err != nil {
return nil, err
}
defer file.Close()
names, err := file.Readdirnames(-1)
if err != nil {
return nil, err
}
var (
procs []*Process
pid int
)
for _, name := range names {
if len(name) > 0 && name[0] > '0' && name[0] <= '9' {
if pid, err = strconv.Atoi(name); err != nil {
continue
}
procs = append(procs, &Process{
pid: pid,
root: filepath.Join(pfs.mountPoint, name),
pfs: pfs,
})
}
}
return procs, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/anesec/chameleon.git
git@gitee.com:anesec/chameleon.git
anesec
chameleon
chameleon
205da4a0ed50

搜索帮助