代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。