diff --git a/README.md b/README.md index 5b2a4ba85..bb1be6514 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,9 @@ mkdir ~/MyWork/;cd ~/MyWork/;git clone https://github.com/hktalent/log4j-scan ```` - Intelligently identify honeypots and skip targets. This function is disabled by default. You can set EnableHoneyportDetection=true to enable - Highly customizable: allow to define your own dictionary through config/config.json configuration, or control more details, including but not limited to: nuclei, httpx, naabu, etc. -- support HTTP Request Smuggling: CL-TE、TE-CL、TE-TE +- support HTTP Request Smuggling: CL-TE、TE-CL、TE-TE、CL_CL、BaseErr + image + - Support via parameter Cookie='PHPSession=xxxx' ./scan4all -host xxxx.com, compatible with nuclei, httpx, go-poc, x-ray POC, filefuzz, http Smuggling # work process diff --git a/go.mod b/go.mod index 9552ccb6a..318aee3ce 100644 --- a/go.mod +++ b/go.mod @@ -117,7 +117,9 @@ require ( github.com/projectdiscovery/nuclei/v2 v2.7.5 github.com/projectdiscovery/subfinder/v2 v2.5.2 github.com/satori/go.uuid v1.2.0 + github.com/shirou/gopsutil/v3 v3.22.7 github.com/sijms/go-ora/v2 v2.4.25 + github.com/simonnilsson/ask v0.2.0 github.com/spf13/viper v1.12.0 github.com/stacktitan/smb v0.0.0-20190531122847-da9a425dceb8 github.com/tj/go-update v2.2.5-0.20200519121640-62b4b798fd68+incompatible @@ -241,8 +243,6 @@ require ( github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect github.com/segmentio/ksuid v1.0.4 // indirect - github.com/shirou/gopsutil/v3 v3.22.6 // indirect - github.com/simonnilsson/ask v0.2.0 // indirect github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 446a9be3f..999878304 100644 --- a/go.sum +++ b/go.sum @@ -755,8 +755,9 @@ github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shirou/gopsutil/v3 v3.21.7/go.mod h1:RGl11Y7XMTQPmHh8F0ayC6haKNBgH4PXMJuTAcMOlz4= -github.com/shirou/gopsutil/v3 v3.22.6 h1:FnHOFOh+cYAM0C30P+zysPISzlknLC5Z1G4EAElznfQ= github.com/shirou/gopsutil/v3 v3.22.6/go.mod h1:EdIubSnZhbAvBS1yJ7Xi+AShB/hxwLHOMz4MCYz7yMs= +github.com/shirou/gopsutil/v3 v3.22.7 h1:flKnuCMfUUrO+oAvwAd6GKZgnPzr098VA/UJ14nhJd4= +github.com/shirou/gopsutil/v3 v3.22.7/go.mod h1:s648gW4IywYzUfE/KjXxUsqrqx/T2xO5VqOXxONeRfI= github.com/sijms/go-ora/v2 v2.4.25 h1:PtyMsRGHd6FNttdaMEg4NdHuRYxg29NKwsxPuTmCEJ4= github.com/sijms/go-ora/v2 v2.4.25/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk= github.com/simonnilsson/ask v0.2.0 h1:CpiQtuLyRlrPNVk6aQjaFL51XmBlCIxaqssxentXBBA= diff --git a/lib/util/sysinfo.go b/lib/util/sysinfo.go new file mode 100644 index 000000000..8a6a139eb --- /dev/null +++ b/lib/util/sysinfo.go @@ -0,0 +1,171 @@ +package util + +import ( + "fmt" + "github.com/shirou/gopsutil/v3/cpu" + "github.com/shirou/gopsutil/v3/disk" + "github.com/shirou/gopsutil/v3/host" + "github.com/shirou/gopsutil/v3/mem" + "github.com/shirou/gopsutil/v3/net" + systemNet "net" + "strconv" + "time" +) + +// 分区 +type Part struct { + Path string `json:"path"` + FsType string `json:"fstype"` + Total float64 `json:"total"` + Free float64 `json:"free"` + Used float64 `json:"used"` + UsedPercent int `json:"usedPercent"` +} + +// 分区集合 +type Parts []Part + +// CPU +type CpuSingle struct { + Num string `json:"num"` + Percent int `json:"percent"` +} + +type CpuInfo struct { + CpuAvg float64 `json:"cpuAvg"` + CpuAll []CpuSingle `json:"cpuAll"` +} + +const GB = 1024 * 1024 * 1024 + +func decimal(v string) float64 { + value, _ := strconv.ParseFloat(v, 64) + return value +} + +// 1.主机IP +func GetLocalIP() (ip string) { + addresses, err := systemNet.InterfaceAddrs() + if err != nil { + return "" + } + for _, addr := range addresses { + ipAddr, ok := addr.(*systemNet.IPNet) + if !ok { + continue + } + if ipAddr.IP.IsLoopback() { + continue + } + if !ipAddr.IP.IsGlobalUnicast() { + continue + } + return ipAddr.IP.String() + } + return "" +} + +// 2.主机信息 +func GetHostInfo() (result *host.InfoStat, err error) { + result, err = host.Info() + return result, err +} + +// 3.磁盘信息 +func GetDiskInfo() (result Parts, err error) { + parts, err := disk.Partitions(true) + if err != nil { + return result, err + } + for _, part := range parts { + diskInfo, err := disk.Usage(part.Mountpoint) + if err == nil { + result = append(result, Part{ + Path: diskInfo.Path, + FsType: diskInfo.Fstype, + Total: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Total/GB))), + Free: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Free/GB))), + Used: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Used/GB))), + UsedPercent: int(diskInfo.UsedPercent), + }) + } else { + return result, err + } + } + return result, err +} + +// 4.CPU使用率 +func GetCpuPercent() (result CpuInfo, err error) { + infos, err := cpu.Percent(1*time.Second, true) + if err != nil { + return result, err + } + var total float64 = 0 + for index, value := range infos { + result.CpuAll = append(result.CpuAll, CpuSingle{ + Num: fmt.Sprintf("#%d", index+1), + Percent: int(value), + }) + total += value + } + result.CpuAvg = decimal(fmt.Sprintf("%.1f", total/float64(len(infos)))) + return result, err +} + +// 5.内存信息 +func GetMemInfo() (float64, []map[string]interface{}) { + info, err := mem.VirtualMemory() + if err != nil { + fmt.Println(err) + return 0, nil + } + return decimal(fmt.Sprintf("%.1f", info.UsedPercent)), []map[string]interface{}{ + {"key": "使用率[%]", "value": decimal(fmt.Sprintf("%.1f", info.UsedPercent))}, + {"key": "总量[GB]", "value": int(info.Total / GB)}, + {"key": "使用量[GB]", "value": int(info.Used / GB)}, + {"key": "剩余量[GB]", "value": int(info.Free / GB)}, + } +} + +// 6.获取网卡信息 +func GetNetInfo() (result []net.IOCountersStat, err error) { + info, err := net.IOCounters(true) + if err != nil { + return result, err + } + return info, err +} + +// 7.计算上下行带宽 +func GetNetSpeed() (speed map[string]map[string]uint64, err error) { + speed = map[string]map[string]uint64{} + info, err := net.IOCounters(true) + if err != nil { + return speed, err + } + for _, item := range info { + if item.BytesSent != 0 { + speed[item.Name] = map[string]uint64{ + "send": item.BytesSent, + "recv": item.BytesRecv, + } + } + } + + time.Sleep(1 * time.Second) + + info, err = net.IOCounters(true) + if err != nil { + return speed, err + } + for _, item := range info { + if item.BytesSent != 0 { + speed[item.Name] = map[string]uint64{ + "send": item.BytesSent - speed[item.Name]["send"], + "recv": item.BytesRecv - speed[item.Name]["recv"], + } + } + } + return speed, nil +} diff --git a/vendor/github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/request.go b/vendor/github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/request.go index 8f0d572c3..007c4dfea 100644 --- a/vendor/github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/request.go +++ b/vendor/github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/request.go @@ -346,7 +346,7 @@ func (request *Request) ExecuteWithResults(reqURL string, dynamicValues, previou const drainReqSize = int64(8 * 1024) var errStopExecution = errors.New("stop execution due to unresolved variables") -var someMapMutex = sync.RWMutex{} + // executeRequest executes the actual generated request and returns error if occurred func (request *Request) executeRequest(reqURL string, generatedRequest *generatedRequest, previousEvent output.InternalEvent, hasInteractMatchers bool, callback protocols.OutputEventCallback, requestCount int) error { request.setCustomHeaders(generatedRequest) @@ -582,7 +582,6 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate if request.options.Interactsh != nil { request.options.Interactsh.MakePlaceholders(generatedRequest.interactshURLs, outputEvent) } - someMapMutex.Lock() for k, v := range previousEvent { finalEvent[k] = v } @@ -598,7 +597,7 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate finalEvent[key] = v } } - someMapMutex.Unlock() + // prune signature internal values if any request.pruneSignatureInternalValues(generatedRequest.meta) diff --git a/vendor/github.com/shirou/gopsutil/v3/cpu/cpu.go b/vendor/github.com/shirou/gopsutil/v3/cpu/cpu.go index 831440d09..83bc23d45 100644 --- a/vendor/github.com/shirou/gopsutil/v3/cpu/cpu.go +++ b/vendor/github.com/shirou/gopsutil/v3/cpu/cpu.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math" + "runtime" "strconv" "strings" "sync" @@ -86,10 +87,12 @@ func (c TimesStat) String() string { return `{` + strings.Join(v, ",") + `}` } -// Total returns the total number of seconds in a CPUTimesStat +// Deprecated: Total returns the total number of seconds in a CPUTimesStat +// Please do not use this internal function. func (c TimesStat) Total() float64 { - total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + - c.Steal + c.Idle + total := c.User + c.System + c.Idle + c.Nice + c.Iowait + c.Irq + + c.Softirq + c.Steal + c.Guest + c.GuestNice + return total } @@ -99,9 +102,15 @@ func (c InfoStat) String() string { } func getAllBusy(t TimesStat) (float64, float64) { - busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + - t.Softirq + t.Steal - return busy + t.Idle, busy + tot := t.Total() + if runtime.GOOS == "linux" { + tot -= t.Guest // Linux 2.6.24+ + tot -= t.GuestNice // Linux 3.2.0+ + } + + busy := tot - t.Idle - t.Iowait + + return tot, busy } func calculateBusy(t1, t2 TimesStat) float64 { diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk.go new file mode 100644 index 000000000..dd4cc1d5f --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk.go @@ -0,0 +1,96 @@ +package disk + +import ( + "context" + "encoding/json" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +var invoke common.Invoker = common.Invoke{} + +type UsageStat struct { + Path string `json:"path"` + Fstype string `json:"fstype"` + Total uint64 `json:"total"` + Free uint64 `json:"free"` + Used uint64 `json:"used"` + UsedPercent float64 `json:"usedPercent"` + InodesTotal uint64 `json:"inodesTotal"` + InodesUsed uint64 `json:"inodesUsed"` + InodesFree uint64 `json:"inodesFree"` + InodesUsedPercent float64 `json:"inodesUsedPercent"` +} + +type PartitionStat struct { + Device string `json:"device"` + Mountpoint string `json:"mountpoint"` + Fstype string `json:"fstype"` + Opts []string `json:"opts"` +} + +type IOCountersStat struct { + ReadCount uint64 `json:"readCount"` + MergedReadCount uint64 `json:"mergedReadCount"` + WriteCount uint64 `json:"writeCount"` + MergedWriteCount uint64 `json:"mergedWriteCount"` + ReadBytes uint64 `json:"readBytes"` + WriteBytes uint64 `json:"writeBytes"` + ReadTime uint64 `json:"readTime"` + WriteTime uint64 `json:"writeTime"` + IopsInProgress uint64 `json:"iopsInProgress"` + IoTime uint64 `json:"ioTime"` + WeightedIO uint64 `json:"weightedIO"` + Name string `json:"name"` + SerialNumber string `json:"serialNumber"` + Label string `json:"label"` +} + +func (d UsageStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} + +func (d PartitionStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} + +func (d IOCountersStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} + +// Usage returns a file system usage. path is a filesystem path such +// as "/", not device file path like "/dev/vda1". If you want to use +// a return value of disk.Partitions, use "Mountpoint" not "Device". +func Usage(path string) (*UsageStat, error) { + return UsageWithContext(context.Background(), path) +} + +// Partitions returns disk partitions. If all is false, returns +// physical devices only (e.g. hard disks, cd-rom drives, USB keys) +// and ignore all others (e.g. memory partitions such as /dev/shm) +// +// 'all' argument is ignored for BSD, see: https://github.com/giampaolo/psutil/issues/906 +func Partitions(all bool) ([]PartitionStat, error) { + return PartitionsWithContext(context.Background(), all) +} + +func IOCounters(names ...string) (map[string]IOCountersStat, error) { + return IOCountersWithContext(context.Background(), names...) +} + +// SerialNumber returns Serial Number of given device or empty string +// on error. Name of device is expected, eg. /dev/sda +func SerialNumber(name string) (string, error) { + return SerialNumberWithContext(context.Background(), name) +} + +// Label returns label of given device or empty string on error. +// Name of device is expected, eg. /dev/sda +// Supports label based on devicemapper name +// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm +func Label(name string) (string, error) { + return LabelWithContext(context.Background(), name) +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix.go new file mode 100644 index 000000000..bc71712ea --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix.go @@ -0,0 +1,22 @@ +//go:build aix +// +build aix + +package disk + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + return nil, common.ErrNotImplementedError +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_cgo.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_cgo.go new file mode 100644 index 000000000..aa534df30 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_cgo.go @@ -0,0 +1,76 @@ +//go:build aix && cgo +// +build aix,cgo + +package disk + +import ( + "context" + "fmt" + + "github.com/power-devops/perfstat" +) + +var FSType map[int]string + +func init() { + FSType = map[int]string{ + 0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", + 16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs", + 33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs", + 39: "ahafs", 40: "sterm-nfs", 41: "asmfs", + } +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } + ret := make([]PartitionStat, len(f)) + + for _, fs := range f { + fstyp, exists := FSType[fs.FSType] + if !exists { + fstyp = "unknown" + } + info := PartitionStat{ + Device: fs.Device, + Mountpoint: fs.MountPoint, + Fstype: fstyp, + } + ret = append(ret, info) + } + + return ret, err +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } + + blocksize := uint64(512) + for _, fs := range f { + if path == fs.MountPoint { + fstyp, exists := FSType[fs.FSType] + if !exists { + fstyp = "unknown" + } + info := UsageStat{ + Path: path, + Fstype: fstyp, + Total: uint64(fs.TotalBlocks) * blocksize, + Free: uint64(fs.FreeBlocks) * blocksize, + Used: uint64(fs.TotalBlocks-fs.FreeBlocks) * blocksize, + InodesTotal: uint64(fs.TotalInodes), + InodesFree: uint64(fs.FreeInodes), + InodesUsed: uint64(fs.TotalInodes - fs.FreeInodes), + } + info.UsedPercent = (float64(info.Used) / float64(info.Total)) * 100.0 + info.InodesUsedPercent = (float64(info.InodesUsed) / float64(info.InodesTotal)) * 100.0 + return &info, nil + } + } + return nil, fmt.Errorf("mountpoint %s not found", path) +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_nocgo.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_nocgo.go new file mode 100644 index 000000000..eb25cbdae --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_aix_nocgo.go @@ -0,0 +1,18 @@ +//go:build aix && !cgo +// +build aix,!cgo + +package disk + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + return []PartitionStat{}, common.ErrNotImplementedError +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + return nil, common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin.go new file mode 100644 index 000000000..0877b7611 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin.go @@ -0,0 +1,87 @@ +//go:build darwin +// +build darwin + +package disk + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/unix" +) + +// PartitionsWithContext returns disk partition. +// 'all' argument is ignored, see: https://github.com/giampaolo/psutil/issues/906 +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + var ret []PartitionStat + + count, err := unix.Getfsstat(nil, unix.MNT_WAIT) + if err != nil { + return ret, err + } + fs := make([]unix.Statfs_t, count) + if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil { + return ret, err + } + for _, stat := range fs { + opts := []string{"rw"} + if stat.Flags&unix.MNT_RDONLY != 0 { + opts = []string{"ro"} + } + if stat.Flags&unix.MNT_SYNCHRONOUS != 0 { + opts = append(opts, "sync") + } + if stat.Flags&unix.MNT_NOEXEC != 0 { + opts = append(opts, "noexec") + } + if stat.Flags&unix.MNT_NOSUID != 0 { + opts = append(opts, "nosuid") + } + if stat.Flags&unix.MNT_UNION != 0 { + opts = append(opts, "union") + } + if stat.Flags&unix.MNT_ASYNC != 0 { + opts = append(opts, "async") + } + if stat.Flags&unix.MNT_DONTBROWSE != 0 { + opts = append(opts, "nobrowse") + } + if stat.Flags&unix.MNT_AUTOMOUNTED != 0 { + opts = append(opts, "automounted") + } + if stat.Flags&unix.MNT_JOURNALED != 0 { + opts = append(opts, "journaled") + } + if stat.Flags&unix.MNT_MULTILABEL != 0 { + opts = append(opts, "multilabel") + } + if stat.Flags&unix.MNT_NOATIME != 0 { + opts = append(opts, "noatime") + } + if stat.Flags&unix.MNT_NODEV != 0 { + opts = append(opts, "nodev") + } + d := PartitionStat{ + Device: common.ByteToString(stat.Mntfromname[:]), + Mountpoint: common.ByteToString(stat.Mntonname[:]), + Fstype: common.ByteToString(stat.Fstypename[:]), + Opts: opts, + } + + ret = append(ret, d) + } + + return ret, nil +} + +func getFsType(stat unix.Statfs_t) string { + return common.ByteToString(stat.Fstypename[:]) +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_cgo.go new file mode 100644 index 000000000..b041c8d72 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_cgo.go @@ -0,0 +1,45 @@ +//go:build darwin && cgo +// +build darwin,cgo + +package disk + +/* +#cgo LDFLAGS: -framework CoreFoundation -framework IOKit +#include +#include +#include "iostat_darwin.h" +*/ +import "C" + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + var buf [C.NDRIVE]C.DriveStats + n, err := C.gopsutil_v3_readdrivestat(&buf[0], C.int(len(buf))) + if err != nil { + return nil, err + } + ret := make(map[string]IOCountersStat, 0) + for i := 0; i < int(n); i++ { + d := IOCountersStat{ + ReadBytes: uint64(buf[i].read), + WriteBytes: uint64(buf[i].written), + ReadCount: uint64(buf[i].nread), + WriteCount: uint64(buf[i].nwrite), + ReadTime: uint64(buf[i].readtime / 1000 / 1000), // note: read/write time are in ns, but we want ms. + WriteTime: uint64(buf[i].writetime / 1000 / 1000), + IoTime: uint64((buf[i].readtime + buf[i].writetime) / 1000 / 1000), + Name: C.GoString(&buf[i].name[0]), + } + if len(names) > 0 && !common.StringsHas(names, d.Name) { + continue + } + + ret[d.Name] = d + } + return ret, nil +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_nocgo.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_nocgo.go new file mode 100644 index 000000000..99bb8ba24 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_darwin_nocgo.go @@ -0,0 +1,14 @@ +//go:build darwin && !cgo +// +build darwin,!cgo + +package disk + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + return nil, common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_fallback.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_fallback.go new file mode 100644 index 000000000..476873340 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_fallback.go @@ -0,0 +1,30 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !windows && !solaris && !aix +// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix + +package disk + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + return nil, common.ErrNotImplementedError +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + return []PartitionStat{}, common.ErrNotImplementedError +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + return nil, common.ErrNotImplementedError +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd.go new file mode 100644 index 000000000..753ce9ace --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd.go @@ -0,0 +1,193 @@ +//go:build freebsd +// +build freebsd + +package disk + +import ( + "bufio" + "bytes" + "context" + "encoding/binary" + "fmt" + "strconv" + "strings" + + "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +// PartitionsWithContext returns disk partition. +// 'all' argument is ignored, see: https://github.com/giampaolo/psutil/issues/906 +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + var ret []PartitionStat + + // get length + count, err := unix.Getfsstat(nil, unix.MNT_WAIT) + if err != nil { + return ret, err + } + + fs := make([]unix.Statfs_t, count) + if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil { + return ret, err + } + + for _, stat := range fs { + opts := []string{"rw"} + if stat.Flags&unix.MNT_RDONLY != 0 { + opts = []string{"ro"} + } + if stat.Flags&unix.MNT_SYNCHRONOUS != 0 { + opts = append(opts, "sync") + } + if stat.Flags&unix.MNT_NOEXEC != 0 { + opts = append(opts, "noexec") + } + if stat.Flags&unix.MNT_NOSUID != 0 { + opts = append(opts, "nosuid") + } + if stat.Flags&unix.MNT_UNION != 0 { + opts = append(opts, "union") + } + if stat.Flags&unix.MNT_ASYNC != 0 { + opts = append(opts, "async") + } + if stat.Flags&unix.MNT_SUIDDIR != 0 { + opts = append(opts, "suiddir") + } + if stat.Flags&unix.MNT_SOFTDEP != 0 { + opts = append(opts, "softdep") + } + if stat.Flags&unix.MNT_NOSYMFOLLOW != 0 { + opts = append(opts, "nosymfollow") + } + if stat.Flags&unix.MNT_GJOURNAL != 0 { + opts = append(opts, "gjournal") + } + if stat.Flags&unix.MNT_MULTILABEL != 0 { + opts = append(opts, "multilabel") + } + if stat.Flags&unix.MNT_ACLS != 0 { + opts = append(opts, "acls") + } + if stat.Flags&unix.MNT_NOATIME != 0 { + opts = append(opts, "noatime") + } + if stat.Flags&unix.MNT_NOCLUSTERR != 0 { + opts = append(opts, "noclusterr") + } + if stat.Flags&unix.MNT_NOCLUSTERW != 0 { + opts = append(opts, "noclusterw") + } + if stat.Flags&unix.MNT_NFS4ACLS != 0 { + opts = append(opts, "nfsv4acls") + } + + d := PartitionStat{ + Device: common.ByteToString(stat.Mntfromname[:]), + Mountpoint: common.ByteToString(stat.Mntonname[:]), + Fstype: common.ByteToString(stat.Fstypename[:]), + Opts: opts, + } + + ret = append(ret, d) + } + + return ret, nil +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + // statinfo->devinfo->devstat + // /usr/include/devinfo.h + ret := make(map[string]IOCountersStat) + + r, err := unix.Sysctl("kern.devstat.all") + if err != nil { + return nil, err + } + buf := []byte(r) + length := len(buf) + + count := int(uint64(length) / uint64(sizeOfdevstat)) + + buf = buf[8:] // devstat.all has version in the head. + // parse buf to devstat + for i := 0; i < count; i++ { + b := buf[i*sizeOfdevstat : i*sizeOfdevstat+sizeOfdevstat] + d, err := parsedevstat(b) + if err != nil { + continue + } + un := strconv.Itoa(int(d.Unit_number)) + name := common.IntToString(d.Device_name[:]) + un + + if len(names) > 0 && !common.StringsHas(names, name) { + continue + } + + ds := IOCountersStat{ + ReadCount: d.Operations[devstat_READ], + WriteCount: d.Operations[devstat_WRITE], + ReadBytes: d.Bytes[devstat_READ], + WriteBytes: d.Bytes[devstat_WRITE], + ReadTime: uint64(d.Duration[devstat_READ].Compute() * 1000), + WriteTime: uint64(d.Duration[devstat_WRITE].Compute() * 1000), + IoTime: uint64(d.Busy_time.Compute() * 1000), + Name: name, + } + ds.SerialNumber, _ = SerialNumberWithContext(ctx, name) + ret[name] = ds + } + + return ret, nil +} + +func (b bintime) Compute() float64 { + BINTIME_SCALE := 5.42101086242752217003726400434970855712890625e-20 + return float64(b.Sec) + float64(b.Frac)*BINTIME_SCALE +} + +// BT2LD(time) ((long double)(time).sec + (time).frac * BINTIME_SCALE) + +func parsedevstat(buf []byte) (devstat, error) { + var ds devstat + br := bytes.NewReader(buf) + // err := binary.Read(br, binary.LittleEndian, &ds) + err := common.Read(br, binary.LittleEndian, &ds) + if err != nil { + return ds, err + } + + return ds, nil +} + +func getFsType(stat unix.Statfs_t) string { + return common.ByteToString(stat.Fstypename[:]) +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + geomOut, err := invoke.CommandWithContext(ctx, "geom", "disk", "list", name) + if err != nil { + return "", fmt.Errorf("exec geom: %w", err) + } + s := bufio.NewScanner(bytes.NewReader(geomOut)) + serial := "" + for s.Scan() { + flds := strings.Fields(s.Text()) + if len(flds) == 2 && flds[0] == "ident:" { + if flds[1] != "(null)" { + serial = flds[1] + } + break + } + } + if err = s.Err(); err != nil { + return "", err + } + return serial, nil +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_386.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_386.go new file mode 100644 index 000000000..7fa1783dc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_386.go @@ -0,0 +1,63 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package disk + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeofLongDouble = 0x8 + + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfdevstat = 0xf0 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 + _C_long_double int64 +) + +type devstat struct { + Sequence0 uint32 + Allocated int32 + Start_count uint32 + End_count uint32 + Busy_from bintime + Dev_links _Ctype_struct___0 + Device_number uint32 + Device_name [16]int8 + Unit_number int32 + Bytes [4]uint64 + Operations [4]uint64 + Duration [4]bintime + Busy_time bintime + Creation_time bintime + Block_size uint32 + Tag_types [3]uint64 + Flags uint32 + Device_type uint32 + Priority uint32 + Id *byte + Sequence1 uint32 +} + +type bintime struct { + Sec int32 + Frac uint64 +} + +type _Ctype_struct___0 struct { + Empty uint32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_amd64.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_amd64.go new file mode 100644 index 000000000..d86a308be --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_amd64.go @@ -0,0 +1,66 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package disk + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeofLongDouble = 0x8 + + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfdevstat = 0x120 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 + _C_long_double int64 +) + +type devstat struct { + Sequence0 uint32 + Allocated int32 + Start_count uint32 + End_count uint32 + Busy_from bintime + Dev_links _Ctype_struct___0 + Device_number uint32 + Device_name [16]int8 + Unit_number int32 + Bytes [4]uint64 + Operations [4]uint64 + Duration [4]bintime + Busy_time bintime + Creation_time bintime + Block_size uint32 + Pad_cgo_0 [4]byte + Tag_types [3]uint64 + Flags uint32 + Device_type uint32 + Priority uint32 + Pad_cgo_1 [4]byte + ID *byte + Sequence1 uint32 + Pad_cgo_2 [4]byte +} + +type bintime struct { + Sec int64 + Frac uint64 +} + +type _Ctype_struct___0 struct { + Empty uint64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm.go new file mode 100644 index 000000000..7fa1783dc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm.go @@ -0,0 +1,63 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package disk + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeofLongDouble = 0x8 + + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfdevstat = 0xf0 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 + _C_long_double int64 +) + +type devstat struct { + Sequence0 uint32 + Allocated int32 + Start_count uint32 + End_count uint32 + Busy_from bintime + Dev_links _Ctype_struct___0 + Device_number uint32 + Device_name [16]int8 + Unit_number int32 + Bytes [4]uint64 + Operations [4]uint64 + Duration [4]bintime + Busy_time bintime + Creation_time bintime + Block_size uint32 + Tag_types [3]uint64 + Flags uint32 + Device_type uint32 + Priority uint32 + Id *byte + Sequence1 uint32 +} + +type bintime struct { + Sec int32 + Frac uint64 +} + +type _Ctype_struct___0 struct { + Empty uint32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm64.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm64.go new file mode 100644 index 000000000..f6b3f80df --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_freebsd_arm64.go @@ -0,0 +1,66 @@ +//go:build freebsd && arm64 +// +build freebsd,arm64 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs disk/types_freebsd.go + +package disk + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeofLongDouble = 0x8 + + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfdevstat = 0x120 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 + _C_long_double int64 +) + +type devstat struct { + Sequence0 uint32 + Allocated int32 + Start_count uint32 + End_count uint32 + Busy_from bintime + Dev_links _Ctype_struct___0 + Device_number uint32 + Device_name [16]int8 + Unit_number int32 + Bytes [4]uint64 + Operations [4]uint64 + Duration [4]bintime + Busy_time bintime + Creation_time bintime + Block_size uint32 + Tag_types [3]uint64 + Flags uint32 + Device_type uint32 + Priority uint32 + Id *byte + Sequence1 uint32 + Pad_cgo_0 [4]byte +} +type bintime struct { + Sec int64 + Frac uint64 +} + +type _Ctype_struct___0 struct { + Empty uint64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_linux.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_linux.go new file mode 100644 index 000000000..3911af9c6 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_linux.go @@ -0,0 +1,538 @@ +//go:build linux +// +build linux + +package disk + +import ( + "bufio" + "bytes" + "context" + "errors" + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + "strconv" + "strings" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/unix" +) + +const ( + sectorSize = 512 +) + +const ( + // man statfs + ADFS_SUPER_MAGIC = 0xadf5 + AFFS_SUPER_MAGIC = 0xADFF + BDEVFS_MAGIC = 0x62646576 + BEFS_SUPER_MAGIC = 0x42465331 + BFS_MAGIC = 0x1BADFACE + BINFMTFS_MAGIC = 0x42494e4d + BTRFS_SUPER_MAGIC = 0x9123683E + CGROUP_SUPER_MAGIC = 0x27e0eb + CIFS_MAGIC_NUMBER = 0xFF534D42 + CODA_SUPER_MAGIC = 0x73757245 + COH_SUPER_MAGIC = 0x012FF7B7 + CRAMFS_MAGIC = 0x28cd3d45 + DEBUGFS_MAGIC = 0x64626720 + DEVFS_SUPER_MAGIC = 0x1373 + DEVPTS_SUPER_MAGIC = 0x1cd1 + EFIVARFS_MAGIC = 0xde5e81e4 + EFS_SUPER_MAGIC = 0x00414A53 + EXT_SUPER_MAGIC = 0x137D + EXT2_OLD_SUPER_MAGIC = 0xEF51 + EXT2_SUPER_MAGIC = 0xEF53 + EXT3_SUPER_MAGIC = 0xEF53 + EXT4_SUPER_MAGIC = 0xEF53 + FUSE_SUPER_MAGIC = 0x65735546 + FUTEXFS_SUPER_MAGIC = 0xBAD1DEA + HFS_SUPER_MAGIC = 0x4244 + HFSPLUS_SUPER_MAGIC = 0x482b + HOSTFS_SUPER_MAGIC = 0x00c0ffee + HPFS_SUPER_MAGIC = 0xF995E849 + HUGETLBFS_MAGIC = 0x958458f6 + ISOFS_SUPER_MAGIC = 0x9660 + JFFS2_SUPER_MAGIC = 0x72b6 + JFS_SUPER_MAGIC = 0x3153464a + MINIX_SUPER_MAGIC = 0x137F /* orig. minix */ + MINIX_SUPER_MAGIC2 = 0x138F /* 30 char minix */ + MINIX2_SUPER_MAGIC = 0x2468 /* minix V2 */ + MINIX2_SUPER_MAGIC2 = 0x2478 /* minix V2, 30 char names */ + MINIX3_SUPER_MAGIC = 0x4d5a /* minix V3 fs, 60 char names */ + MQUEUE_MAGIC = 0x19800202 + MSDOS_SUPER_MAGIC = 0x4d44 + NCP_SUPER_MAGIC = 0x564c + NFS_SUPER_MAGIC = 0x6969 + NILFS_SUPER_MAGIC = 0x3434 + NTFS_SB_MAGIC = 0x5346544e + OCFS2_SUPER_MAGIC = 0x7461636f + OPENPROM_SUPER_MAGIC = 0x9fa1 + PIPEFS_MAGIC = 0x50495045 + PROC_SUPER_MAGIC = 0x9fa0 + PSTOREFS_MAGIC = 0x6165676C + QNX4_SUPER_MAGIC = 0x002f + QNX6_SUPER_MAGIC = 0x68191122 + RAMFS_MAGIC = 0x858458f6 + REISERFS_SUPER_MAGIC = 0x52654973 + ROMFS_MAGIC = 0x7275 + SELINUX_MAGIC = 0xf97cff8c + SMACK_MAGIC = 0x43415d53 + SMB_SUPER_MAGIC = 0x517B + SOCKFS_MAGIC = 0x534F434B + SQUASHFS_MAGIC = 0x73717368 + SYSFS_MAGIC = 0x62656572 + SYSV2_SUPER_MAGIC = 0x012FF7B6 + SYSV4_SUPER_MAGIC = 0x012FF7B5 + TMPFS_MAGIC = 0x01021994 + UDF_SUPER_MAGIC = 0x15013346 + UFS_MAGIC = 0x00011954 + USBDEVICE_SUPER_MAGIC = 0x9fa2 + V9FS_MAGIC = 0x01021997 + VXFS_SUPER_MAGIC = 0xa501FCF5 + XENFS_SUPER_MAGIC = 0xabba1974 + XENIX_SUPER_MAGIC = 0x012FF7B4 + XFS_SUPER_MAGIC = 0x58465342 + _XIAFS_SUPER_MAGIC = 0x012FD16D + + AFS_SUPER_MAGIC = 0x5346414F + AUFS_SUPER_MAGIC = 0x61756673 + ANON_INODE_FS_SUPER_MAGIC = 0x09041934 + BPF_FS_MAGIC = 0xCAFE4A11 + CEPH_SUPER_MAGIC = 0x00C36400 + CGROUP2_SUPER_MAGIC = 0x63677270 + CONFIGFS_MAGIC = 0x62656570 + ECRYPTFS_SUPER_MAGIC = 0xF15F + F2FS_SUPER_MAGIC = 0xF2F52010 + FAT_SUPER_MAGIC = 0x4006 + FHGFS_SUPER_MAGIC = 0x19830326 + FUSEBLK_SUPER_MAGIC = 0x65735546 + FUSECTL_SUPER_MAGIC = 0x65735543 + GFS_SUPER_MAGIC = 0x1161970 + GPFS_SUPER_MAGIC = 0x47504653 + MTD_INODE_FS_SUPER_MAGIC = 0x11307854 + INOTIFYFS_SUPER_MAGIC = 0x2BAD1DEA + ISOFS_R_WIN_SUPER_MAGIC = 0x4004 + ISOFS_WIN_SUPER_MAGIC = 0x4000 + JFFS_SUPER_MAGIC = 0x07C0 + KAFS_SUPER_MAGIC = 0x6B414653 + LUSTRE_SUPER_MAGIC = 0x0BD00BD0 + NFSD_SUPER_MAGIC = 0x6E667364 + NSFS_MAGIC = 0x6E736673 + PANFS_SUPER_MAGIC = 0xAAD7AAEA + RPC_PIPEFS_SUPER_MAGIC = 0x67596969 + SECURITYFS_SUPER_MAGIC = 0x73636673 + TRACEFS_MAGIC = 0x74726163 + UFS_BYTESWAPPED_SUPER_MAGIC = 0x54190100 + VMHGFS_SUPER_MAGIC = 0xBACBACBC + VZFS_SUPER_MAGIC = 0x565A4653 + ZFS_SUPER_MAGIC = 0x2FC12FC1 +) + +// coreutils/src/stat.c +var fsTypeMap = map[int64]string{ + ADFS_SUPER_MAGIC: "adfs", /* 0xADF5 local */ + AFFS_SUPER_MAGIC: "affs", /* 0xADFF local */ + AFS_SUPER_MAGIC: "afs", /* 0x5346414F remote */ + ANON_INODE_FS_SUPER_MAGIC: "anon-inode FS", /* 0x09041934 local */ + AUFS_SUPER_MAGIC: "aufs", /* 0x61756673 remote */ + // AUTOFS_SUPER_MAGIC: "autofs", /* 0x0187 local */ + BEFS_SUPER_MAGIC: "befs", /* 0x42465331 local */ + BDEVFS_MAGIC: "bdevfs", /* 0x62646576 local */ + BFS_MAGIC: "bfs", /* 0x1BADFACE local */ + BINFMTFS_MAGIC: "binfmt_misc", /* 0x42494E4D local */ + BPF_FS_MAGIC: "bpf", /* 0xCAFE4A11 local */ + BTRFS_SUPER_MAGIC: "btrfs", /* 0x9123683E local */ + CEPH_SUPER_MAGIC: "ceph", /* 0x00C36400 remote */ + CGROUP_SUPER_MAGIC: "cgroupfs", /* 0x0027E0EB local */ + CGROUP2_SUPER_MAGIC: "cgroup2fs", /* 0x63677270 local */ + CIFS_MAGIC_NUMBER: "cifs", /* 0xFF534D42 remote */ + CODA_SUPER_MAGIC: "coda", /* 0x73757245 remote */ + COH_SUPER_MAGIC: "coh", /* 0x012FF7B7 local */ + CONFIGFS_MAGIC: "configfs", /* 0x62656570 local */ + CRAMFS_MAGIC: "cramfs", /* 0x28CD3D45 local */ + DEBUGFS_MAGIC: "debugfs", /* 0x64626720 local */ + DEVFS_SUPER_MAGIC: "devfs", /* 0x1373 local */ + DEVPTS_SUPER_MAGIC: "devpts", /* 0x1CD1 local */ + ECRYPTFS_SUPER_MAGIC: "ecryptfs", /* 0xF15F local */ + EFIVARFS_MAGIC: "efivarfs", /* 0xDE5E81E4 local */ + EFS_SUPER_MAGIC: "efs", /* 0x00414A53 local */ + EXT_SUPER_MAGIC: "ext", /* 0x137D local */ + EXT2_SUPER_MAGIC: "ext2/ext3", /* 0xEF53 local */ + EXT2_OLD_SUPER_MAGIC: "ext2", /* 0xEF51 local */ + F2FS_SUPER_MAGIC: "f2fs", /* 0xF2F52010 local */ + FAT_SUPER_MAGIC: "fat", /* 0x4006 local */ + FHGFS_SUPER_MAGIC: "fhgfs", /* 0x19830326 remote */ + FUSEBLK_SUPER_MAGIC: "fuseblk", /* 0x65735546 remote */ + FUSECTL_SUPER_MAGIC: "fusectl", /* 0x65735543 remote */ + FUTEXFS_SUPER_MAGIC: "futexfs", /* 0x0BAD1DEA local */ + GFS_SUPER_MAGIC: "gfs/gfs2", /* 0x1161970 remote */ + GPFS_SUPER_MAGIC: "gpfs", /* 0x47504653 remote */ + HFS_SUPER_MAGIC: "hfs", /* 0x4244 local */ + HFSPLUS_SUPER_MAGIC: "hfsplus", /* 0x482b local */ + HPFS_SUPER_MAGIC: "hpfs", /* 0xF995E849 local */ + HUGETLBFS_MAGIC: "hugetlbfs", /* 0x958458F6 local */ + MTD_INODE_FS_SUPER_MAGIC: "inodefs", /* 0x11307854 local */ + INOTIFYFS_SUPER_MAGIC: "inotifyfs", /* 0x2BAD1DEA local */ + ISOFS_SUPER_MAGIC: "isofs", /* 0x9660 local */ + ISOFS_R_WIN_SUPER_MAGIC: "isofs", /* 0x4004 local */ + ISOFS_WIN_SUPER_MAGIC: "isofs", /* 0x4000 local */ + JFFS_SUPER_MAGIC: "jffs", /* 0x07C0 local */ + JFFS2_SUPER_MAGIC: "jffs2", /* 0x72B6 local */ + JFS_SUPER_MAGIC: "jfs", /* 0x3153464A local */ + KAFS_SUPER_MAGIC: "k-afs", /* 0x6B414653 remote */ + LUSTRE_SUPER_MAGIC: "lustre", /* 0x0BD00BD0 remote */ + MINIX_SUPER_MAGIC: "minix", /* 0x137F local */ + MINIX_SUPER_MAGIC2: "minix (30 char.)", /* 0x138F local */ + MINIX2_SUPER_MAGIC: "minix v2", /* 0x2468 local */ + MINIX2_SUPER_MAGIC2: "minix v2 (30 char.)", /* 0x2478 local */ + MINIX3_SUPER_MAGIC: "minix3", /* 0x4D5A local */ + MQUEUE_MAGIC: "mqueue", /* 0x19800202 local */ + MSDOS_SUPER_MAGIC: "msdos", /* 0x4D44 local */ + NCP_SUPER_MAGIC: "novell", /* 0x564C remote */ + NFS_SUPER_MAGIC: "nfs", /* 0x6969 remote */ + NFSD_SUPER_MAGIC: "nfsd", /* 0x6E667364 remote */ + NILFS_SUPER_MAGIC: "nilfs", /* 0x3434 local */ + NSFS_MAGIC: "nsfs", /* 0x6E736673 local */ + NTFS_SB_MAGIC: "ntfs", /* 0x5346544E local */ + OPENPROM_SUPER_MAGIC: "openprom", /* 0x9FA1 local */ + OCFS2_SUPER_MAGIC: "ocfs2", /* 0x7461636f remote */ + PANFS_SUPER_MAGIC: "panfs", /* 0xAAD7AAEA remote */ + PIPEFS_MAGIC: "pipefs", /* 0x50495045 remote */ + PROC_SUPER_MAGIC: "proc", /* 0x9FA0 local */ + PSTOREFS_MAGIC: "pstorefs", /* 0x6165676C local */ + QNX4_SUPER_MAGIC: "qnx4", /* 0x002F local */ + QNX6_SUPER_MAGIC: "qnx6", /* 0x68191122 local */ + RAMFS_MAGIC: "ramfs", /* 0x858458F6 local */ + REISERFS_SUPER_MAGIC: "reiserfs", /* 0x52654973 local */ + ROMFS_MAGIC: "romfs", /* 0x7275 local */ + RPC_PIPEFS_SUPER_MAGIC: "rpc_pipefs", /* 0x67596969 local */ + SECURITYFS_SUPER_MAGIC: "securityfs", /* 0x73636673 local */ + SELINUX_MAGIC: "selinux", /* 0xF97CFF8C local */ + SMB_SUPER_MAGIC: "smb", /* 0x517B remote */ + SOCKFS_MAGIC: "sockfs", /* 0x534F434B local */ + SQUASHFS_MAGIC: "squashfs", /* 0x73717368 local */ + SYSFS_MAGIC: "sysfs", /* 0x62656572 local */ + SYSV2_SUPER_MAGIC: "sysv2", /* 0x012FF7B6 local */ + SYSV4_SUPER_MAGIC: "sysv4", /* 0x012FF7B5 local */ + TMPFS_MAGIC: "tmpfs", /* 0x01021994 local */ + TRACEFS_MAGIC: "tracefs", /* 0x74726163 local */ + UDF_SUPER_MAGIC: "udf", /* 0x15013346 local */ + UFS_MAGIC: "ufs", /* 0x00011954 local */ + UFS_BYTESWAPPED_SUPER_MAGIC: "ufs", /* 0x54190100 local */ + USBDEVICE_SUPER_MAGIC: "usbdevfs", /* 0x9FA2 local */ + V9FS_MAGIC: "v9fs", /* 0x01021997 local */ + VMHGFS_SUPER_MAGIC: "vmhgfs", /* 0xBACBACBC remote */ + VXFS_SUPER_MAGIC: "vxfs", /* 0xA501FCF5 local */ + VZFS_SUPER_MAGIC: "vzfs", /* 0x565A4653 local */ + XENFS_SUPER_MAGIC: "xenfs", /* 0xABBA1974 local */ + XENIX_SUPER_MAGIC: "xenix", /* 0x012FF7B4 local */ + XFS_SUPER_MAGIC: "xfs", /* 0x58465342 local */ + _XIAFS_SUPER_MAGIC: "xia", /* 0x012FD16D local */ + ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */ +} + +// readMountFile reads mountinfo or mounts file under the specified root path +// (eg, /proc/1, /proc/self, etc) +func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) { + filename = path.Join(root, "mountinfo") + lines, err = common.ReadLines(filename) + if err != nil { + var pathErr *os.PathError + if !errors.As(err, &pathErr) { + return + } + // if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26) + useMounts = true + filename = path.Join(root, "mounts") + lines, err = common.ReadLines(filename) + if err != nil { + return + } + return + } + return +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + // by default, try "/proc/1/..." first + root := common.HostProc(path.Join("1")) + + // force preference for dirname of HOST_PROC_MOUNTINFO, if set #1271 + hpmPath := os.Getenv("HOST_PROC_MOUNTINFO") + if hpmPath != "" { + root = filepath.Dir(hpmPath) + } + + lines, useMounts, filename, err := readMountFile(root) + if err != nil { + if hpmPath != "" { // don't fallback with HOST_PROC_MOUNTINFO + return nil, err + } + // fallback to "/proc/self/..." #1159 + lines, useMounts, filename, err = readMountFile(common.HostProc(path.Join("self"))) + if err != nil { + return nil, err + } + } + + fs, err := getFileSystems() + if err != nil && !all { + return nil, err + } + + ret := make([]PartitionStat, 0, len(lines)) + + for _, line := range lines { + var d PartitionStat + if useMounts { + fields := strings.Fields(line) + + d = PartitionStat{ + Device: fields[0], + Mountpoint: unescapeFstab(fields[1]), + Fstype: fields[2], + Opts: strings.Fields(fields[3]), + } + + if !all { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } + } + } else { + // a line of 1/mountinfo has the following structure: + // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue + // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) + + // split the mountinfo line by the separator hyphen + parts := strings.Split(line, " - ") + if len(parts) != 2 { + return nil, fmt.Errorf("found invalid mountinfo line in file %s: %s ", filename, line) + } + + fields := strings.Fields(parts[0]) + blockDeviceID := fields[2] + mountPoint := fields[4] + mountOpts := strings.Split(fields[5], ",") + + if rootDir := fields[3]; rootDir != "" && rootDir != "/" { + mountOpts = append(mountOpts, "bind") + } + + fields = strings.Fields(parts[1]) + fstype := fields[0] + device := fields[1] + + d = PartitionStat{ + Device: device, + Mountpoint: unescapeFstab(mountPoint), + Fstype: fstype, + Opts: mountOpts, + } + + if !all { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } + } + + if strings.HasPrefix(d.Device, "/dev/mapper/") { + devpath, err := filepath.EvalSymlinks(common.HostDev(strings.Replace(d.Device, "/dev", "", -1))) + if err == nil { + d.Device = devpath + } + } + + // /dev/root is not the real device name + // so we get the real device name from its major/minor number + if d.Device == "/dev/root" { + devpath, err := os.Readlink(common.HostSys("/dev/block/" + blockDeviceID)) + if err == nil { + d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1) + } + } + } + ret = append(ret, d) + } + + return ret, nil +} + +// getFileSystems returns supported filesystems from /proc/filesystems +func getFileSystems() ([]string, error) { + filename := common.HostProc("filesystems") + lines, err := common.ReadLines(filename) + if err != nil { + return nil, err + } + var ret []string + for _, line := range lines { + if !strings.HasPrefix(line, "nodev") { + ret = append(ret, strings.TrimSpace(line)) + continue + } + t := strings.Split(line, "\t") + if len(t) != 2 || t[1] != "zfs" { + continue + } + ret = append(ret, strings.TrimSpace(t[1])) + } + + return ret, nil +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + filename := common.HostProc("diskstats") + lines, err := common.ReadLines(filename) + if err != nil { + return nil, err + } + ret := make(map[string]IOCountersStat) + empty := IOCountersStat{} + + // use only basename such as "/dev/sda1" to "sda1" + for i, name := range names { + names[i] = filepath.Base(name) + } + + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) < 14 { + // malformed line in /proc/diskstats, avoid panic by ignoring. + continue + } + name := fields[2] + + if len(names) > 0 && !common.StringsHas(names, name) { + continue + } + + reads, err := strconv.ParseUint((fields[3]), 10, 64) + if err != nil { + return ret, err + } + mergedReads, err := strconv.ParseUint((fields[4]), 10, 64) + if err != nil { + return ret, err + } + rbytes, err := strconv.ParseUint((fields[5]), 10, 64) + if err != nil { + return ret, err + } + rtime, err := strconv.ParseUint((fields[6]), 10, 64) + if err != nil { + return ret, err + } + writes, err := strconv.ParseUint((fields[7]), 10, 64) + if err != nil { + return ret, err + } + mergedWrites, err := strconv.ParseUint((fields[8]), 10, 64) + if err != nil { + return ret, err + } + wbytes, err := strconv.ParseUint((fields[9]), 10, 64) + if err != nil { + return ret, err + } + wtime, err := strconv.ParseUint((fields[10]), 10, 64) + if err != nil { + return ret, err + } + iopsInProgress, err := strconv.ParseUint((fields[11]), 10, 64) + if err != nil { + return ret, err + } + iotime, err := strconv.ParseUint((fields[12]), 10, 64) + if err != nil { + return ret, err + } + weightedIO, err := strconv.ParseUint((fields[13]), 10, 64) + if err != nil { + return ret, err + } + d := IOCountersStat{ + ReadBytes: rbytes * sectorSize, + WriteBytes: wbytes * sectorSize, + ReadCount: reads, + WriteCount: writes, + MergedReadCount: mergedReads, + MergedWriteCount: mergedWrites, + ReadTime: rtime, + WriteTime: wtime, + IopsInProgress: iopsInProgress, + IoTime: iotime, + WeightedIO: weightedIO, + } + if d == empty { + continue + } + d.Name = name + + d.SerialNumber, _ = SerialNumberWithContext(ctx, name) + d.Label, _ = LabelWithContext(ctx, name) + + ret[name] = d + } + return ret, nil +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + var stat unix.Stat_t + err := unix.Stat(name, &stat) + if err != nil { + return "", err + } + major := unix.Major(uint64(stat.Rdev)) + minor := unix.Minor(uint64(stat.Rdev)) + + // Try to get the serial from udev data + udevDataPath := common.HostRun(fmt.Sprintf("udev/data/b%d:%d", major, minor)) + if udevdata, err := ioutil.ReadFile(udevDataPath); err == nil { + scanner := bufio.NewScanner(bytes.NewReader(udevdata)) + for scanner.Scan() { + values := strings.Split(scanner.Text(), "=") + if len(values) == 2 && values[0] == "E:ID_SERIAL" { + return values[1], nil + } + } + } + + // Try to get the serial from sysfs, look at the disk device (minor 0) directly + // because if it is a partition it is not going to contain any device information + devicePath := common.HostSys(fmt.Sprintf("dev/block/%d:0/device", major)) + model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model")) + serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial")) + if len(model) > 0 && len(serial) > 0 { + return fmt.Sprintf("%s_%s", string(model), string(serial)), nil + } + return "", nil +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + // Try label based on devicemapper name + dmname_filename := common.HostSys(fmt.Sprintf("block/%s/dm/name", name)) + + if !common.PathExists(dmname_filename) { + return "", nil + } + + dmname, err := ioutil.ReadFile(dmname_filename) + if err != nil { + return "", err + } + return strings.TrimSpace(string(dmname)), nil +} + +func getFsType(stat unix.Statfs_t) string { + t := int64(stat.Type) + ret, ok := fsTypeMap[t] + if !ok { + return "" + } + return ret +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd.go new file mode 100644 index 000000000..81ff23994 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd.go @@ -0,0 +1,159 @@ +//go:build openbsd +// +build openbsd + +package disk + +import ( + "bytes" + "context" + "encoding/binary" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/unix" +) + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + var ret []PartitionStat + + // get length + count, err := unix.Getfsstat(nil, unix.MNT_WAIT) + if err != nil { + return ret, err + } + + fs := make([]unix.Statfs_t, count) + if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil { + return ret, err + } + + for _, stat := range fs { + opts := []string{"rw"} + if stat.F_flags&unix.MNT_RDONLY != 0 { + opts = []string{"rw"} + } + if stat.F_flags&unix.MNT_SYNCHRONOUS != 0 { + opts = append(opts, "sync") + } + if stat.F_flags&unix.MNT_NOEXEC != 0 { + opts = append(opts, "noexec") + } + if stat.F_flags&unix.MNT_NOSUID != 0 { + opts = append(opts, "nosuid") + } + if stat.F_flags&unix.MNT_NODEV != 0 { + opts = append(opts, "nodev") + } + if stat.F_flags&unix.MNT_ASYNC != 0 { + opts = append(opts, "async") + } + if stat.F_flags&unix.MNT_SOFTDEP != 0 { + opts = append(opts, "softdep") + } + if stat.F_flags&unix.MNT_NOATIME != 0 { + opts = append(opts, "noatime") + } + if stat.F_flags&unix.MNT_WXALLOWED != 0 { + opts = append(opts, "wxallowed") + } + + d := PartitionStat{ + Device: common.ByteToString(stat.F_mntfromname[:]), + Mountpoint: common.ByteToString(stat.F_mntonname[:]), + Fstype: common.ByteToString(stat.F_fstypename[:]), + Opts: opts, + } + + ret = append(ret, d) + } + + return ret, nil +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + ret := make(map[string]IOCountersStat) + + r, err := unix.SysctlRaw("hw.diskstats") + if err != nil { + return nil, err + } + buf := []byte(r) + length := len(buf) + + count := int(uint64(length) / uint64(sizeOfDiskstats)) + + // parse buf to Diskstats + for i := 0; i < count; i++ { + b := buf[i*sizeOfDiskstats : i*sizeOfDiskstats+sizeOfDiskstats] + d, err := parseDiskstats(b) + if err != nil { + continue + } + name := common.IntToString(d.Name[:]) + + if len(names) > 0 && !common.StringsHas(names, name) { + continue + } + + ds := IOCountersStat{ + ReadCount: d.Rxfer, + WriteCount: d.Wxfer, + ReadBytes: d.Rbytes, + WriteBytes: d.Wbytes, + Name: name, + } + ret[name] = ds + } + + return ret, nil +} + +// BT2LD(time) ((long double)(time).sec + (time).frac * BINTIME_SCALE) + +func parseDiskstats(buf []byte) (Diskstats, error) { + var ds Diskstats + br := bytes.NewReader(buf) + // err := binary.Read(br, binary.LittleEndian, &ds) + err := common.Read(br, binary.LittleEndian, &ds) + if err != nil { + return ds, err + } + + return ds, nil +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + stat := unix.Statfs_t{} + err := unix.Statfs(path, &stat) + if err != nil { + return nil, err + } + bsize := stat.F_bsize + + ret := &UsageStat{ + Path: path, + Fstype: getFsType(stat), + Total: (uint64(stat.F_blocks) * uint64(bsize)), + Free: (uint64(stat.F_bavail) * uint64(bsize)), + InodesTotal: (uint64(stat.F_files)), + InodesFree: (uint64(stat.F_ffree)), + } + + ret.InodesUsed = (ret.InodesTotal - ret.InodesFree) + ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0 + ret.Used = (uint64(stat.F_blocks) - uint64(stat.F_bfree)) * uint64(bsize) + ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0 + + return ret, nil +} + +func getFsType(stat unix.Statfs_t) string { + return common.ByteToString(stat.F_fstypename[:]) +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_386.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_386.go new file mode 100644 index 000000000..f4c139f5e --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_386.go @@ -0,0 +1,38 @@ +//go:build openbsd && 386 +// +build openbsd,386 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs disk/types_openbsd.go + +package disk + +const ( + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfDiskstats = 0x60 +) + +type Diskstats struct { + Name [16]int8 + Busy int32 + Rxfer uint64 + Wxfer uint64 + Seek uint64 + Rbytes uint64 + Wbytes uint64 + Attachtime Timeval + Timestamp Timeval + Time Timeval +} +type Timeval struct { + Sec int64 + Usec int32 +} + +type Diskstat struct{} +type bintime struct{} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_amd64.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_amd64.go new file mode 100644 index 000000000..c1bd52ef8 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_amd64.go @@ -0,0 +1,36 @@ +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs types_openbsd.go + +package disk + +const ( + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfDiskstats = 0x70 +) + +type Diskstats struct { + Name [16]int8 + Busy int32 + Pad_cgo_0 [4]byte + Rxfer uint64 + Wxfer uint64 + Seek uint64 + Rbytes uint64 + Wbytes uint64 + Attachtime Timeval + Timestamp Timeval + Time Timeval +} +type Timeval struct { + Sec int64 + Usec int64 +} + +type Diskstat struct{} +type bintime struct{} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm.go new file mode 100644 index 000000000..86054a626 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm.go @@ -0,0 +1,38 @@ +//go:build openbsd && arm +// +build openbsd,arm + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs disk/types_openbsd.go + +package disk + +const ( + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfDiskstats = 0x60 +) + +type Diskstats struct { + Name [16]int8 + Busy int32 + Rxfer uint64 + Wxfer uint64 + Seek uint64 + Rbytes uint64 + Wbytes uint64 + Attachtime Timeval + Timestamp Timeval + Time Timeval +} +type Timeval struct { + Sec int64 + Usec int32 +} + +type Diskstat struct{} +type bintime struct{} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm64.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm64.go new file mode 100644 index 000000000..ae1cf57e1 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_openbsd_arm64.go @@ -0,0 +1,38 @@ +//go:build openbsd && arm64 +// +build openbsd,arm64 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs disk/types_openbsd.go + +package disk + +const ( + devstat_NO_DATA = 0x00 + devstat_READ = 0x01 + devstat_WRITE = 0x02 + devstat_FREE = 0x03 +) + +const ( + sizeOfDiskstats = 0x70 +) + +type Diskstats struct { + Name [16]int8 + Busy int32 + Rxfer uint64 + Wxfer uint64 + Seek uint64 + Rbytes uint64 + Wbytes uint64 + Attachtime Timeval + Timestamp Timeval + Time Timeval +} +type Timeval struct { + Sec int64 + Usec int64 +} + +type Diskstat struct{} +type bintime struct{} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_solaris.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_solaris.go new file mode 100644 index 000000000..9c4a798d0 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_solaris.go @@ -0,0 +1,147 @@ +//go:build solaris +// +build solaris + +package disk + +import ( + "bufio" + "bytes" + "context" + "fmt" + "math" + "os" + "strings" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/unix" +) + +const ( + // _DEFAULT_NUM_MOUNTS is set to `cat /etc/mnttab | wc -l` rounded up to the + // nearest power of two. + _DEFAULT_NUM_MOUNTS = 32 + + // _MNTTAB default place to read mount information + _MNTTAB = "/etc/mnttab" +) + +// A blacklist of read-only virtual filesystems. Writable filesystems are of +// operational concern and must not be included in this list. +var fsTypeBlacklist = map[string]struct{}{ + "ctfs": {}, + "dev": {}, + "fd": {}, + "lofs": {}, + "lxproc": {}, + "mntfs": {}, + "objfs": {}, + "proc": {}, +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + ret := make([]PartitionStat, 0, _DEFAULT_NUM_MOUNTS) + + // Scan mnttab(4) + f, err := os.Open(_MNTTAB) + if err != nil { + } + defer func() { + if err == nil { + err = f.Close() + } else { + f.Close() + } + }() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + fields := strings.Split(scanner.Text(), "\t") + + if _, found := fsTypeBlacklist[fields[2]]; found { + continue + } + + ret = append(ret, PartitionStat{ + // NOTE(seanc@): Device isn't exactly accurate: from mnttab(4): "The name + // of the resource that has been mounted." Ideally this value would come + // from Statvfs_t.Fsid but I'm leaving it to the caller to traverse + // unix.Statvfs(). + Device: fields[0], + Mountpoint: fields[1], + Fstype: fields[2], + Opts: strings.Split(fields[3], ","), + }) + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("unable to scan %q: %v", _MNTTAB, err) + } + + return ret, err +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + return nil, common.ErrNotImplementedError +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + statvfs := unix.Statvfs_t{} + if err := unix.Statvfs(path, &statvfs); err != nil { + return nil, fmt.Errorf("unable to call statvfs(2) on %q: %v", path, err) + } + + usageStat := &UsageStat{ + Path: path, + Fstype: common.IntToString(statvfs.Basetype[:]), + Total: statvfs.Blocks * statvfs.Frsize, + Free: statvfs.Bfree * statvfs.Frsize, + Used: (statvfs.Blocks - statvfs.Bfree) * statvfs.Frsize, + + // NOTE: ZFS (and FreeBZSD's UFS2) use dynamic inode/dnode allocation. + // Explicitly return a near-zero value for InodesUsedPercent so that nothing + // attempts to garbage collect based on a lack of available inodes/dnodes. + // Similarly, don't use the zero value to prevent divide-by-zero situations + // and inject a faux near-zero value. Filesystems evolve. Has your + // filesystem evolved? Probably not if you care about the number of + // available inodes. + InodesTotal: 1024.0 * 1024.0, + InodesUsed: 1024.0, + InodesFree: math.MaxUint64, + InodesUsedPercent: (1024.0 / (1024.0 * 1024.0)) * 100.0, + } + + usageStat.UsedPercent = (float64(usageStat.Used) / float64(usageStat.Total)) * 100.0 + + return usageStat, nil +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + out, err := invoke.CommandWithContext(ctx, "cfgadm", "-ls", "select=type(disk),cols=ap_id:info,cols2=,noheadings") + if err != nil { + return "", fmt.Errorf("exec cfgadm: %w", err) + } + + suf := "::" + strings.TrimPrefix(name, "/dev/") + s := bufio.NewScanner(bytes.NewReader(out)) + for s.Scan() { + flds := strings.Fields(s.Text()) + if strings.HasSuffix(flds[0], suf) { + flen := len(flds) + if flen >= 3 { + for i, f := range flds { + if i > 0 && i < flen-1 && f == "SN:" { + return flds[i+1], nil + } + } + } + return "", nil + } + } + if err := s.Err(); err != nil { + return "", err + } + return "", nil +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_unix.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_unix.go new file mode 100644 index 000000000..bdb62b24d --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_unix.go @@ -0,0 +1,62 @@ +//go:build freebsd || linux || darwin +// +build freebsd linux darwin + +package disk + +import ( + "context" + "strconv" + + "golang.org/x/sys/unix" +) + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + stat := unix.Statfs_t{} + err := unix.Statfs(path, &stat) + if err != nil { + return nil, err + } + bsize := stat.Bsize + + ret := &UsageStat{ + Path: unescapeFstab(path), + Fstype: getFsType(stat), + Total: (uint64(stat.Blocks) * uint64(bsize)), + Free: (uint64(stat.Bavail) * uint64(bsize)), + InodesTotal: (uint64(stat.Files)), + InodesFree: (uint64(stat.Ffree)), + } + + // if could not get InodesTotal, return empty + if ret.InodesTotal < ret.InodesFree { + return ret, nil + } + + ret.InodesUsed = (ret.InodesTotal - ret.InodesFree) + ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize) + + if ret.InodesTotal == 0 { + ret.InodesUsedPercent = 0 + } else { + ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0 + } + + if (ret.Used + ret.Free) == 0 { + ret.UsedPercent = 0 + } else { + // We don't use ret.Total to calculate percent. + // see https://github.com/shirou/gopsutil/issues/562 + ret.UsedPercent = (float64(ret.Used) / float64(ret.Used+ret.Free)) * 100.0 + } + + return ret, nil +} + +// Unescape escaped octal chars (like space 040, ampersand 046 and backslash 134) to their real value in fstab fields issue#555 +func unescapeFstab(path string) string { + escaped, err := strconv.Unquote(`"` + path + `"`) + if err != nil { + return path + } + return escaped +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/disk_windows.go b/vendor/github.com/shirou/gopsutil/v3/disk/disk_windows.go new file mode 100644 index 000000000..5fb9b5b42 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/disk_windows.go @@ -0,0 +1,201 @@ +//go:build windows +// +build windows + +package disk + +import ( + "bytes" + "context" + "fmt" + "syscall" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/windows" + "golang.org/x/sys/windows/registry" +) + +var ( + procGetDiskFreeSpaceExW = common.Modkernel32.NewProc("GetDiskFreeSpaceExW") + procGetLogicalDriveStringsW = common.Modkernel32.NewProc("GetLogicalDriveStringsW") + procGetDriveType = common.Modkernel32.NewProc("GetDriveTypeW") + procGetVolumeInformation = common.Modkernel32.NewProc("GetVolumeInformationW") +) + +var ( + fileFileCompression = int64(16) // 0x00000010 + fileReadOnlyVolume = int64(524288) // 0x00080000 +) + +// diskPerformance is an equivalent representation of DISK_PERFORMANCE in the Windows API. +// https://docs.microsoft.com/fr-fr/windows/win32/api/winioctl/ns-winioctl-disk_performance +type diskPerformance struct { + BytesRead int64 + BytesWritten int64 + ReadTime int64 + WriteTime int64 + IdleTime int64 + ReadCount uint32 + WriteCount uint32 + QueueDepth uint32 + SplitCount uint32 + QueryTime int64 + StorageDeviceNumber uint32 + StorageManagerName [8]uint16 + alignmentPadding uint32 // necessary for 32bit support, see https://github.com/elastic/beats/pull/16553 +} + +func init() { + // enable disk performance counters on Windows Server editions (needs to run as admin) + key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\PartMgr`, registry.SET_VALUE) + if err == nil { + key.SetDWordValue("EnableCounterForIoctl", 1) + } +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + lpFreeBytesAvailable := int64(0) + lpTotalNumberOfBytes := int64(0) + lpTotalNumberOfFreeBytes := int64(0) + diskret, _, err := procGetDiskFreeSpaceExW.Call( + uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(path))), + uintptr(unsafe.Pointer(&lpFreeBytesAvailable)), + uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)), + uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes))) + if diskret == 0 { + return nil, err + } + ret := &UsageStat{ + Path: path, + Total: uint64(lpTotalNumberOfBytes), + Free: uint64(lpTotalNumberOfFreeBytes), + Used: uint64(lpTotalNumberOfBytes) - uint64(lpTotalNumberOfFreeBytes), + UsedPercent: (float64(lpTotalNumberOfBytes) - float64(lpTotalNumberOfFreeBytes)) / float64(lpTotalNumberOfBytes) * 100, + // InodesTotal: 0, + // InodesFree: 0, + // InodesUsed: 0, + // InodesUsedPercent: 0, + } + return ret, nil +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + var ret []PartitionStat + lpBuffer := make([]byte, 254) + diskret, _, err := procGetLogicalDriveStringsW.Call( + uintptr(len(lpBuffer)), + uintptr(unsafe.Pointer(&lpBuffer[0]))) + if diskret == 0 { + return ret, err + } + for _, v := range lpBuffer { + if v >= 65 && v <= 90 { + path := string(v) + ":" + typepath, _ := windows.UTF16PtrFromString(path) + typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath))) + if typeret == 0 { + return ret, windows.GetLastError() + } + // 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM + + if typeret == 2 || typeret == 3 || typeret == 4 || typeret == 5 { + lpVolumeNameBuffer := make([]byte, 256) + lpVolumeSerialNumber := int64(0) + lpMaximumComponentLength := int64(0) + lpFileSystemFlags := int64(0) + lpFileSystemNameBuffer := make([]byte, 256) + volpath, _ := windows.UTF16PtrFromString(string(v) + ":/") + driveret, _, err := procGetVolumeInformation.Call( + uintptr(unsafe.Pointer(volpath)), + uintptr(unsafe.Pointer(&lpVolumeNameBuffer[0])), + uintptr(len(lpVolumeNameBuffer)), + uintptr(unsafe.Pointer(&lpVolumeSerialNumber)), + uintptr(unsafe.Pointer(&lpMaximumComponentLength)), + uintptr(unsafe.Pointer(&lpFileSystemFlags)), + uintptr(unsafe.Pointer(&lpFileSystemNameBuffer[0])), + uintptr(len(lpFileSystemNameBuffer))) + if driveret == 0 { + if typeret == 5 || typeret == 2 { + continue // device is not ready will happen if there is no disk in the drive + } + return ret, err + } + opts := []string{"rw"} + if lpFileSystemFlags&fileReadOnlyVolume != 0 { + opts = []string{"ro"} + } + if lpFileSystemFlags&fileFileCompression != 0 { + opts = append(opts, "compress") + } + + d := PartitionStat{ + Mountpoint: path, + Device: path, + Fstype: string(bytes.Replace(lpFileSystemNameBuffer, []byte("\x00"), []byte(""), -1)), + Opts: opts, + } + ret = append(ret, d) + } + } + } + return ret, nil +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + // https://github.com/giampaolo/psutil/blob/544e9daa4f66a9f80d7bf6c7886d693ee42f0a13/psutil/arch/windows/disk.c#L83 + drivemap := make(map[string]IOCountersStat, 0) + var diskPerformance diskPerformance + + lpBuffer := make([]uint16, 254) + lpBufferLen, err := windows.GetLogicalDriveStrings(uint32(len(lpBuffer)), &lpBuffer[0]) + if err != nil { + return drivemap, err + } + for _, v := range lpBuffer[:lpBufferLen] { + if 'A' <= v && v <= 'Z' { + path := string(rune(v)) + ":" + typepath, _ := windows.UTF16PtrFromString(path) + typeret := windows.GetDriveType(typepath) + if typeret == 0 { + return drivemap, windows.GetLastError() + } + if typeret != windows.DRIVE_FIXED { + continue + } + szDevice := fmt.Sprintf(`\\.\%s`, path) + const IOCTL_DISK_PERFORMANCE = 0x70020 + h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0) + if err != nil { + if err == windows.ERROR_FILE_NOT_FOUND { + continue + } + return drivemap, err + } + defer windows.CloseHandle(h) + + var diskPerformanceSize uint32 + err = windows.DeviceIoControl(h, IOCTL_DISK_PERFORMANCE, nil, 0, (*byte)(unsafe.Pointer(&diskPerformance)), uint32(unsafe.Sizeof(diskPerformance)), &diskPerformanceSize, nil) + if err != nil { + return drivemap, err + } + drivemap[path] = IOCountersStat{ + ReadBytes: uint64(diskPerformance.BytesRead), + WriteBytes: uint64(diskPerformance.BytesWritten), + ReadCount: uint64(diskPerformance.ReadCount), + WriteCount: uint64(diskPerformance.WriteCount), + ReadTime: uint64(diskPerformance.ReadTime / 10000 / 1000), // convert to ms: https://github.com/giampaolo/psutil/issues/1012 + WriteTime: uint64(diskPerformance.WriteTime / 10000 / 1000), + Name: path, + } + } + } + return drivemap, nil +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.c b/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.c new file mode 100644 index 000000000..8aab04f60 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.c @@ -0,0 +1,129 @@ +// https://github.com/lufia/iostat/blob/9f7362b77ad333b26c01c99de52a11bdb650ded2/iostat_darwin.c +#include +#include +#include "iostat_darwin.h" + +#define IOKIT 1 /* to get io_name_t in device_types.h */ + +#include +#include +#include +#include + +#include + +static int getdrivestat(io_registry_entry_t d, DriveStats *stat); +static int fillstat(io_registry_entry_t d, DriveStats *stat); + +int +gopsutil_v3_readdrivestat(DriveStats a[], int n) +{ + CFMutableDictionaryRef match; + io_iterator_t drives; + io_registry_entry_t d; + kern_return_t status; + int na, rv; + + match = IOServiceMatching("IOMedia"); + CFDictionaryAddValue(match, CFSTR(kIOMediaWholeKey), kCFBooleanTrue); + status = IOServiceGetMatchingServices(0, match, &drives); + if(status != KERN_SUCCESS) + return -1; + + na = 0; + while(na < n && (d=IOIteratorNext(drives)) > 0){ + rv = getdrivestat(d, &a[na]); + if(rv < 0) + return -1; + if(rv > 0) + na++; + IOObjectRelease(d); + } + IOObjectRelease(drives); + return na; +} + +static int +getdrivestat(io_registry_entry_t d, DriveStats *stat) +{ + io_registry_entry_t parent; + kern_return_t status; + CFDictionaryRef props; + CFStringRef name; + CFNumberRef num; + int rv; + + memset(stat, 0, sizeof *stat); + status = IORegistryEntryGetParentEntry(d, kIOServicePlane, &parent); + if(status != KERN_SUCCESS) + return -1; + if(!IOObjectConformsTo(parent, "IOBlockStorageDriver")){ + IOObjectRelease(parent); + return 0; + } + + status = IORegistryEntryCreateCFProperties(d, (CFMutableDictionaryRef *)&props, kCFAllocatorDefault, kNilOptions); + if(status != KERN_SUCCESS){ + IOObjectRelease(parent); + return -1; + } + name = (CFStringRef)CFDictionaryGetValue(props, CFSTR(kIOBSDNameKey)); + CFStringGetCString(name, stat->name, NAMELEN, CFStringGetSystemEncoding()); + num = (CFNumberRef)CFDictionaryGetValue(props, CFSTR(kIOMediaSizeKey)); + CFNumberGetValue(num, kCFNumberSInt64Type, &stat->size); + num = (CFNumberRef)CFDictionaryGetValue(props, CFSTR(kIOMediaPreferredBlockSizeKey)); + CFNumberGetValue(num, kCFNumberSInt64Type, &stat->blocksize); + CFRelease(props); + + rv = fillstat(parent, stat); + IOObjectRelease(parent); + if(rv < 0) + return -1; + return 1; +} + +static struct { + char *key; + size_t off; +} statstab[] = { + {kIOBlockStorageDriverStatisticsBytesReadKey, offsetof(DriveStats, read)}, + {kIOBlockStorageDriverStatisticsBytesWrittenKey, offsetof(DriveStats, written)}, + {kIOBlockStorageDriverStatisticsReadsKey, offsetof(DriveStats, nread)}, + {kIOBlockStorageDriverStatisticsWritesKey, offsetof(DriveStats, nwrite)}, + {kIOBlockStorageDriverStatisticsTotalReadTimeKey, offsetof(DriveStats, readtime)}, + {kIOBlockStorageDriverStatisticsTotalWriteTimeKey, offsetof(DriveStats, writetime)}, + {kIOBlockStorageDriverStatisticsLatentReadTimeKey, offsetof(DriveStats, readlat)}, + {kIOBlockStorageDriverStatisticsLatentWriteTimeKey, offsetof(DriveStats, writelat)}, +}; + +static int +fillstat(io_registry_entry_t d, DriveStats *stat) +{ + CFDictionaryRef props, v; + CFNumberRef num; + kern_return_t status; + typeof(statstab[0]) *bp, *ep; + + status = IORegistryEntryCreateCFProperties(d, (CFMutableDictionaryRef *)&props, kCFAllocatorDefault, kNilOptions); + if(status != KERN_SUCCESS) + return -1; + v = (CFDictionaryRef)CFDictionaryGetValue(props, CFSTR(kIOBlockStorageDriverStatisticsKey)); + if(v == NULL){ + CFRelease(props); + return -1; + } + + ep = &statstab[sizeof(statstab)/sizeof(statstab[0])]; + for(bp = &statstab[0]; bp < ep; bp++){ + CFStringRef s; + + s = CFStringCreateWithCString(kCFAllocatorDefault, bp->key, CFStringGetSystemEncoding()); + num = (CFNumberRef)CFDictionaryGetValue(v, s); + if(num) + CFNumberGetValue(num, kCFNumberSInt64Type, ((char*)stat)+bp->off); + CFRelease(s); + } + + CFRelease(props); + return 0; +} diff --git a/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.h b/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.h new file mode 100644 index 000000000..cb9ec7a0a --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/disk/iostat_darwin.h @@ -0,0 +1,32 @@ +// https://github.com/lufia/iostat/blob/9f7362b77ad333b26c01c99de52a11bdb650ded2/iostat_darwin.h +typedef struct DriveStats DriveStats; +typedef struct CPUStats CPUStats; + +enum { + NDRIVE = 16, + NAMELEN = 31 +}; + +struct DriveStats { + char name[NAMELEN+1]; + int64_t size; + int64_t blocksize; + + int64_t read; + int64_t written; + int64_t nread; + int64_t nwrite; + int64_t readtime; + int64_t writetime; + int64_t readlat; + int64_t writelat; +}; + +struct CPUStats { + natural_t user; + natural_t nice; + natural_t sys; + natural_t idle; +}; + +extern int gopsutil_v3_readdrivestat(DriveStats a[], int n); diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host.go b/vendor/github.com/shirou/gopsutil/v3/host/host.go new file mode 100644 index 000000000..7c53e2084 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host.go @@ -0,0 +1,157 @@ +package host + +import ( + "context" + "encoding/json" + "errors" + "os" + "runtime" + "time" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +var invoke common.Invoker = common.Invoke{} + +// A HostInfoStat describes the host status. +// This is not in the psutil but it useful. +type InfoStat struct { + Hostname string `json:"hostname"` + Uptime uint64 `json:"uptime"` + BootTime uint64 `json:"bootTime"` + Procs uint64 `json:"procs"` // number of processes + OS string `json:"os"` // ex: freebsd, linux + Platform string `json:"platform"` // ex: ubuntu, linuxmint + PlatformFamily string `json:"platformFamily"` // ex: debian, rhel + PlatformVersion string `json:"platformVersion"` // version of the complete OS + KernelVersion string `json:"kernelVersion"` // version of the OS kernel (if available) + KernelArch string `json:"kernelArch"` // native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error + VirtualizationSystem string `json:"virtualizationSystem"` + VirtualizationRole string `json:"virtualizationRole"` // guest or host + HostID string `json:"hostId"` // ex: uuid +} + +type UserStat struct { + User string `json:"user"` + Terminal string `json:"terminal"` + Host string `json:"host"` + Started int `json:"started"` +} + +type TemperatureStat struct { + SensorKey string `json:"sensorKey"` + Temperature float64 `json:"temperature"` + High float64 `json:"sensorHigh"` + Critical float64 `json:"sensorCritical"` +} + +func (h InfoStat) String() string { + s, _ := json.Marshal(h) + return string(s) +} + +func (u UserStat) String() string { + s, _ := json.Marshal(u) + return string(s) +} + +func (t TemperatureStat) String() string { + s, _ := json.Marshal(t) + return string(s) +} + +func Info() (*InfoStat, error) { + return InfoWithContext(context.Background()) +} + +func InfoWithContext(ctx context.Context) (*InfoStat, error) { + var err error + ret := &InfoStat{ + OS: runtime.GOOS, + } + + ret.Hostname, err = os.Hostname() + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.KernelVersion, err = KernelVersionWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.KernelArch, err = KernelArch() + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.BootTime, err = BootTimeWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.Uptime, err = UptimeWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.Procs, err = numProcs(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + ret.HostID, err = HostIDWithContext(ctx) + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { + return nil, err + } + + return ret, nil +} + +// BootTime returns the system boot time expressed in seconds since the epoch. +func BootTime() (uint64, error) { + return BootTimeWithContext(context.Background()) +} + +func Uptime() (uint64, error) { + return UptimeWithContext(context.Background()) +} + +func Users() ([]UserStat, error) { + return UsersWithContext(context.Background()) +} + +func PlatformInformation() (string, string, string, error) { + return PlatformInformationWithContext(context.Background()) +} + +// HostID returns the unique host ID provided by the OS. +func HostID() (string, error) { + return HostIDWithContext(context.Background()) +} + +func Virtualization() (string, string, error) { + return VirtualizationWithContext(context.Background()) +} + +func KernelVersion() (string, error) { + return KernelVersionWithContext(context.Background()) +} + +func SensorsTemperatures() ([]TemperatureStat, error) { + return SensorsTemperaturesWithContext(context.Background()) +} + +func timeSince(ts uint64) uint64 { + return uint64(time.Now().Unix()) - ts +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_bsd.go b/vendor/github.com/shirou/gopsutil/v3/host/host_bsd.go new file mode 100644 index 000000000..4dc2bba58 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_bsd.go @@ -0,0 +1,37 @@ +//go:build darwin || freebsd || openbsd +// +build darwin freebsd openbsd + +package host + +import ( + "context" + "sync/atomic" + + "golang.org/x/sys/unix" +) + +// cachedBootTime must be accessed via atomic.Load/StoreUint64 +var cachedBootTime uint64 + +func BootTimeWithContext(ctx context.Context) (uint64, error) { + t := atomic.LoadUint64(&cachedBootTime) + if t != 0 { + return t, nil + } + tv, err := unix.SysctlTimeval("kern.boottime") + if err != nil { + return 0, err + } + + atomic.StoreUint64(&cachedBootTime, uint64(tv.Sec)) + + return uint64(tv.Sec), nil +} + +func UptimeWithContext(ctx context.Context) (uint64, error) { + boot, err := BootTimeWithContext(ctx) + if err != nil { + return 0, err + } + return timeSince(boot), nil +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_darwin.go b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin.go new file mode 100644 index 000000000..2f20fc616 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin.go @@ -0,0 +1,129 @@ +//go:build darwin +// +build darwin + +package host + +import ( + "bytes" + "context" + "encoding/binary" + "errors" + "io/ioutil" + "os" + "strings" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "github.com/shirou/gopsutil/v3/process" + "golang.org/x/sys/unix" +) + +// from utmpx.h +const user_PROCESS = 7 + +func HostIDWithContext(ctx context.Context) (string, error) { + out, err := invoke.CommandWithContext(ctx, "ioreg", "-rd1", "-c", "IOPlatformExpertDevice") + if err != nil { + return "", err + } + + for _, line := range strings.Split(string(out), "\n") { + if strings.Contains(line, "IOPlatformUUID") { + parts := strings.SplitAfter(line, `" = "`) + if len(parts) == 2 { + uuid := strings.TrimRight(parts[1], `"`) + return strings.ToLower(uuid), nil + } + } + } + + return "", errors.New("cannot find host id") +} + +func numProcs(ctx context.Context) (uint64, error) { + procs, err := process.PidsWithContext(ctx) + if err != nil { + return 0, err + } + return uint64(len(procs)), nil +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + utmpfile := "/var/run/utmpx" + var ret []UserStat + + file, err := os.Open(utmpfile) + if err != nil { + return ret, err + } + defer file.Close() + + buf, err := ioutil.ReadAll(file) + if err != nil { + return ret, err + } + + u := Utmpx{} + entrySize := int(unsafe.Sizeof(u)) + count := len(buf) / entrySize + + for i := 0; i < count; i++ { + b := buf[i*entrySize : i*entrySize+entrySize] + + var u Utmpx + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) + if err != nil { + continue + } + if u.Type != user_PROCESS { + continue + } + user := UserStat{ + User: common.IntToString(u.User[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(u.Tv.Sec), + } + ret = append(ret, user) + } + + return ret, nil +} + +func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { + platform := "" + family := "" + pver := "" + + p, err := unix.Sysctl("kern.ostype") + if err == nil { + platform = strings.ToLower(p) + } + + out, err := invoke.CommandWithContext(ctx, "sw_vers", "-productVersion") + if err == nil { + pver = strings.ToLower(strings.TrimSpace(string(out))) + } + + // check if the macos server version file exists + _, err = os.Stat("/System/Library/CoreServices/ServerVersion.plist") + + // server file doesn't exist + if os.IsNotExist(err) { + family = "Standalone Workstation" + } else { + family = "Server" + } + + return platform, family, pver, nil +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + version, err := unix.Sysctl("kern.osrelease") + return strings.ToLower(version), err +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_amd64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_amd64.go new file mode 100644 index 000000000..8caeed2e8 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_amd64.go @@ -0,0 +1,20 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_darwin.go + +package host + +type Utmpx struct { + User [256]int8 + ID [4]int8 + Line [32]int8 + Pid int32 + Type int16 + Pad_cgo_0 [6]byte + Tv Timeval + Host [256]int8 + Pad [16]uint32 +} + +type Timeval struct { + Sec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_arm64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_arm64.go new file mode 100644 index 000000000..293bd4df8 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_arm64.go @@ -0,0 +1,23 @@ +//go:build darwin && arm64 +// +build darwin,arm64 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs host/types_darwin.go + +package host + +type Utmpx struct { + User [256]int8 + Id [4]int8 + Line [32]int8 + Pid int32 + Type int16 + Tv Timeval + Host [256]int8 + Pad [16]uint32 +} +type Timeval struct { + Sec int64 + Usec int32 + Pad_cgo_0 [4]byte +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_cgo.go new file mode 100644 index 000000000..ffdc7b78f --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_cgo.go @@ -0,0 +1,47 @@ +//go:build darwin && cgo +// +build darwin,cgo + +package host + +// #cgo LDFLAGS: -framework IOKit +// #include "smc_darwin.h" +import "C" +import "context" + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + temperatureKeys := []string{ + C.AMBIENT_AIR_0, + C.AMBIENT_AIR_1, + C.CPU_0_DIODE, + C.CPU_0_HEATSINK, + C.CPU_0_PROXIMITY, + C.ENCLOSURE_BASE_0, + C.ENCLOSURE_BASE_1, + C.ENCLOSURE_BASE_2, + C.ENCLOSURE_BASE_3, + C.GPU_0_DIODE, + C.GPU_0_HEATSINK, + C.GPU_0_PROXIMITY, + C.HARD_DRIVE_BAY, + C.MEMORY_SLOT_0, + C.MEMORY_SLOTS_PROXIMITY, + C.NORTHBRIDGE, + C.NORTHBRIDGE_DIODE, + C.NORTHBRIDGE_PROXIMITY, + C.THUNDERBOLT_0, + C.THUNDERBOLT_1, + C.WIRELESS_MODULE, + } + var temperatures []TemperatureStat + + C.gopsutil_v3_open_smc() + defer C.gopsutil_v3_close_smc() + + for _, key := range temperatureKeys { + temperatures = append(temperatures, TemperatureStat{ + SensorKey: key, + Temperature: float64(C.gopsutil_v3_get_temperature(C.CString(key))), + }) + } + return temperatures, nil +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_nocgo.go b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_nocgo.go new file mode 100644 index 000000000..6285ba94d --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_darwin_nocgo.go @@ -0,0 +1,14 @@ +//go:build darwin && !cgo +// +build darwin,!cgo + +package host + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + return []TemperatureStat{}, common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_fallback.go b/vendor/github.com/shirou/gopsutil/v3/host/host_fallback.go new file mode 100644 index 000000000..585250f9a --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_fallback.go @@ -0,0 +1,50 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows +// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows + +package host + +import ( + "context" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func HostIDWithContext(ctx context.Context) (string, error) { + return "", common.ErrNotImplementedError +} + +func numProcs(ctx context.Context) (uint64, error) { + return 0, common.ErrNotImplementedError +} + +func BootTimeWithContext(ctx context.Context) (uint64, error) { + return 0, common.ErrNotImplementedError +} + +func UptimeWithContext(ctx context.Context) (uint64, error) { + return 0, common.ErrNotImplementedError +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + return []UserStat{}, common.ErrNotImplementedError +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + return "", common.ErrNotImplementedError +} + +func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { + return "", "", "", common.ErrNotImplementedError +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + return []TemperatureStat{}, common.ErrNotImplementedError +} + +func KernelArch() (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd.go b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd.go new file mode 100644 index 000000000..2c9aa9d0d --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd.go @@ -0,0 +1,151 @@ +//go:build freebsd +// +build freebsd + +package host + +import ( + "bytes" + "context" + "encoding/binary" + "io/ioutil" + "math" + "os" + "strings" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "github.com/shirou/gopsutil/v3/process" + "golang.org/x/sys/unix" +) + +const ( + UTNameSize = 16 /* see MAXLOGNAME in */ + UTLineSize = 8 + UTHostSize = 16 +) + +func HostIDWithContext(ctx context.Context) (string, error) { + uuid, err := unix.Sysctl("kern.hostuuid") + if err != nil { + return "", err + } + return strings.ToLower(uuid), err +} + +func numProcs(ctx context.Context) (uint64, error) { + procs, err := process.PidsWithContext(ctx) + if err != nil { + return 0, err + } + return uint64(len(procs)), nil +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + utmpfile := "/var/run/utx.active" + if !common.PathExists(utmpfile) { + utmpfile = "/var/run/utmp" // before 9.0 + return getUsersFromUtmp(utmpfile) + } + + var ret []UserStat + file, err := os.Open(utmpfile) + if err != nil { + return ret, err + } + defer file.Close() + + buf, err := ioutil.ReadAll(file) + if err != nil { + return ret, err + } + + entrySize := sizeOfUtmpx + count := len(buf) / entrySize + + for i := 0; i < count; i++ { + b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx] + var u Utmpx + br := bytes.NewReader(b) + err := binary.Read(br, binary.BigEndian, &u) + if err != nil || u.Type != 4 { + continue + } + sec := math.Floor(float64(u.Tv) / 1000000) + user := UserStat{ + User: common.IntToString(u.User[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(sec), + } + + ret = append(ret, user) + } + + return ret, nil +} + +func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { + platform, err := unix.Sysctl("kern.ostype") + if err != nil { + return "", "", "", err + } + + version, err := unix.Sysctl("kern.osrelease") + if err != nil { + return "", "", "", err + } + + return strings.ToLower(platform), "", strings.ToLower(version), nil +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +// before 9.0 +func getUsersFromUtmp(utmpfile string) ([]UserStat, error) { + var ret []UserStat + file, err := os.Open(utmpfile) + if err != nil { + return ret, err + } + defer file.Close() + + buf, err := ioutil.ReadAll(file) + if err != nil { + return ret, err + } + + u := Utmp{} + entrySize := int(unsafe.Sizeof(u)) + count := len(buf) / entrySize + + for i := 0; i < count; i++ { + b := buf[i*entrySize : i*entrySize+entrySize] + var u Utmp + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) + if err != nil || u.Time == 0 { + continue + } + user := UserStat{ + User: common.IntToString(u.Name[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(u.Time), + } + + ret = append(ret, user) + } + + return ret, nil +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + return []TemperatureStat{}, common.ErrNotImplementedError +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + _, _, version, err := PlatformInformationWithContext(ctx) + return version, err +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_386.go b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_386.go new file mode 100644 index 000000000..88453d2a2 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_386.go @@ -0,0 +1,37 @@ +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs types_freebsd.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmpx = 0xc5 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [16]int8 + Host [16]int8 + Time int32 +} + +type Utmpx struct { + Type uint8 + Tv uint64 + Id [8]int8 + Pid uint32 + User [32]int8 + Line [16]int8 + Host [128]int8 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_amd64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_amd64.go new file mode 100644 index 000000000..8af74b0fe --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_amd64.go @@ -0,0 +1,37 @@ +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs types_freebsd.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmpx = 0xc5 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [16]int8 + Host [16]int8 + Time int32 +} + +type Utmpx struct { + Type uint8 + Tv uint64 + Id [8]int8 + Pid uint32 + User [32]int8 + Line [16]int8 + Host [128]int8 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm.go b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm.go new file mode 100644 index 000000000..f7d6ede55 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm.go @@ -0,0 +1,37 @@ +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs types_freebsd.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmpx = 0xc5 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [16]int8 + Host [16]int8 + Time int32 +} + +type Utmpx struct { + Type uint8 + Tv uint64 + Id [8]int8 + Pid uint32 + User [32]int8 + Line [16]int8 + Host [128]int8 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm64.go new file mode 100644 index 000000000..41bec3c11 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_freebsd_arm64.go @@ -0,0 +1,40 @@ +//go:build freebsd && arm64 +// +build freebsd,arm64 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs host/types_freebsd.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmpx = 0xc5 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [16]int8 + Host [16]int8 + Time int32 +} + +type Utmpx struct { + Type uint8 + Tv uint64 + Id [8]int8 + Pid uint32 + User [32]int8 + Line [16]int8 + Host [128]int8 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux.go new file mode 100644 index 000000000..940415c9c --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux.go @@ -0,0 +1,518 @@ +//go:build linux +// +build linux + +package host + +import ( + "bytes" + "context" + "encoding/binary" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/unix" +) + +type lsbStruct struct { + ID string + Release string + Codename string + Description string +} + +// from utmp.h +const ( + user_PROCESS = 7 + + hostTemperatureScale = 1000.0 +) + +func HostIDWithContext(ctx context.Context) (string, error) { + sysProductUUID := common.HostSys("class/dmi/id/product_uuid") + machineID := common.HostEtc("machine-id") + procSysKernelRandomBootID := common.HostProc("sys/kernel/random/boot_id") + switch { + // In order to read this file, needs to be supported by kernel/arch and run as root + // so having fallback is important + case common.PathExists(sysProductUUID): + lines, err := common.ReadLines(sysProductUUID) + if err == nil && len(lines) > 0 && lines[0] != "" { + return strings.ToLower(lines[0]), nil + } + fallthrough + // Fallback on GNU Linux systems with systemd, readable by everyone + case common.PathExists(machineID): + lines, err := common.ReadLines(machineID) + if err == nil && len(lines) > 0 && len(lines[0]) == 32 { + st := lines[0] + return fmt.Sprintf("%s-%s-%s-%s-%s", st[0:8], st[8:12], st[12:16], st[16:20], st[20:32]), nil + } + fallthrough + // Not stable between reboot, but better than nothing + default: + lines, err := common.ReadLines(procSysKernelRandomBootID) + if err == nil && len(lines) > 0 && lines[0] != "" { + return strings.ToLower(lines[0]), nil + } + } + + return "", nil +} + +func numProcs(ctx context.Context) (uint64, error) { + return common.NumProcs() +} + +func BootTimeWithContext(ctx context.Context) (uint64, error) { + return common.BootTimeWithContext(ctx) +} + +func UptimeWithContext(ctx context.Context) (uint64, error) { + sysinfo := &unix.Sysinfo_t{} + if err := unix.Sysinfo(sysinfo); err != nil { + return 0, err + } + return uint64(sysinfo.Uptime), nil +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + utmpfile := common.HostVar("run/utmp") + + file, err := os.Open(utmpfile) + if err != nil { + return nil, err + } + defer file.Close() + + buf, err := ioutil.ReadAll(file) + if err != nil { + return nil, err + } + + count := len(buf) / sizeOfUtmp + + ret := make([]UserStat, 0, count) + + for i := 0; i < count; i++ { + b := buf[i*sizeOfUtmp : (i+1)*sizeOfUtmp] + + var u utmp + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) + if err != nil { + continue + } + if u.Type != user_PROCESS { + continue + } + user := UserStat{ + User: common.IntToString(u.User[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(u.Tv.Sec), + } + ret = append(ret, user) + } + + return ret, nil +} + +func getlsbStruct() (*lsbStruct, error) { + ret := &lsbStruct{} + if common.PathExists(common.HostEtc("lsb-release")) { + contents, err := common.ReadLines(common.HostEtc("lsb-release")) + if err != nil { + return ret, err // return empty + } + for _, line := range contents { + field := strings.Split(line, "=") + if len(field) < 2 { + continue + } + switch field[0] { + case "DISTRIB_ID": + ret.ID = field[1] + case "DISTRIB_RELEASE": + ret.Release = field[1] + case "DISTRIB_CODENAME": + ret.Codename = field[1] + case "DISTRIB_DESCRIPTION": + ret.Description = field[1] + } + } + } else if common.PathExists("/usr/bin/lsb_release") { + out, err := invoke.Command("/usr/bin/lsb_release") + if err != nil { + return ret, err + } + for _, line := range strings.Split(string(out), "\n") { + field := strings.Split(line, ":") + if len(field) < 2 { + continue + } + switch field[0] { + case "Distributor ID": + ret.ID = field[1] + case "Release": + ret.Release = field[1] + case "Codename": + ret.Codename = field[1] + case "Description": + ret.Description = field[1] + } + } + + } + + return ret, nil +} + +func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) { + lsb, err := getlsbStruct() + if err != nil { + lsb = &lsbStruct{} + } + + if common.PathExistsWithContents(common.HostEtc("oracle-release")) { + platform = "oracle" + contents, err := common.ReadLines(common.HostEtc("oracle-release")) + if err == nil { + version = getRedhatishVersion(contents) + } + + } else if common.PathExistsWithContents(common.HostEtc("enterprise-release")) { + platform = "oracle" + contents, err := common.ReadLines(common.HostEtc("enterprise-release")) + if err == nil { + version = getRedhatishVersion(contents) + } + } else if common.PathExistsWithContents(common.HostEtc("slackware-version")) { + platform = "slackware" + contents, err := common.ReadLines(common.HostEtc("slackware-version")) + if err == nil { + version = getSlackwareVersion(contents) + } + } else if common.PathExistsWithContents(common.HostEtc("debian_version")) { + if lsb.ID == "Ubuntu" { + platform = "ubuntu" + version = lsb.Release + } else if lsb.ID == "LinuxMint" { + platform = "linuxmint" + version = lsb.Release + } else { + if common.PathExistsWithContents("/usr/bin/raspi-config") { + platform = "raspbian" + } else { + platform = "debian" + } + contents, err := common.ReadLines(common.HostEtc("debian_version")) + if err == nil && len(contents) > 0 && contents[0] != "" { + version = contents[0] + } + } + } else if common.PathExists(common.HostEtc("neokylin-release")) { + contents, err := common.ReadLines(common.HostEtc("neokylin-release")) + if err == nil { + version = getRedhatishVersion(contents) + platform = getRedhatishPlatform(contents) + } + } else if common.PathExists(common.HostEtc("redhat-release")) { + contents, err := common.ReadLines(common.HostEtc("redhat-release")) + if err == nil { + version = getRedhatishVersion(contents) + platform = getRedhatishPlatform(contents) + } + } else if common.PathExists(common.HostEtc("system-release")) { + contents, err := common.ReadLines(common.HostEtc("system-release")) + if err == nil { + version = getRedhatishVersion(contents) + platform = getRedhatishPlatform(contents) + } + } else if common.PathExists(common.HostEtc("gentoo-release")) { + platform = "gentoo" + contents, err := common.ReadLines(common.HostEtc("gentoo-release")) + if err == nil { + version = getRedhatishVersion(contents) + } + } else if common.PathExists(common.HostEtc("SuSE-release")) { + contents, err := common.ReadLines(common.HostEtc("SuSE-release")) + if err == nil { + version = getSuseVersion(contents) + platform = getSusePlatform(contents) + } + // TODO: slackware detecion + } else if common.PathExists(common.HostEtc("arch-release")) { + platform = "arch" + version = lsb.Release + } else if common.PathExists(common.HostEtc("alpine-release")) { + platform = "alpine" + contents, err := common.ReadLines(common.HostEtc("alpine-release")) + if err == nil && len(contents) > 0 && contents[0] != "" { + version = contents[0] + } + } else if common.PathExists(common.HostEtc("os-release")) { + p, v, err := common.GetOSRelease() + if err == nil { + platform = p + version = v + } + } else if lsb.ID == "RedHat" { + platform = "redhat" + version = lsb.Release + } else if lsb.ID == "Amazon" { + platform = "amazon" + version = lsb.Release + } else if lsb.ID == "ScientificSL" { + platform = "scientific" + version = lsb.Release + } else if lsb.ID == "XenServer" { + platform = "xenserver" + version = lsb.Release + } else if lsb.ID != "" { + platform = strings.ToLower(lsb.ID) + version = lsb.Release + } + + platform = strings.Trim(platform, `"`) + + switch platform { + case "debian", "ubuntu", "linuxmint", "raspbian": + family = "debian" + case "fedora": + family = "fedora" + case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky", "almalinux": + family = "rhel" + case "suse", "opensuse", "opensuse-leap", "opensuse-tumbleweed", "opensuse-tumbleweed-kubic", "sles", "sled", "caasp": + family = "suse" + case "gentoo": + family = "gentoo" + case "slackware": + family = "slackware" + case "arch": + family = "arch" + case "exherbo": + family = "exherbo" + case "alpine": + family = "alpine" + case "coreos": + family = "coreos" + case "solus": + family = "solus" + case "neokylin": + family = "neokylin" + } + + return platform, family, version, nil +} + +func KernelVersionWithContext(ctx context.Context) (version string, err error) { + var utsname unix.Utsname + err = unix.Uname(&utsname) + if err != nil { + return "", err + } + return string(utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)]), nil +} + +func getSlackwareVersion(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + c = strings.Replace(c, "slackware ", "", 1) + return c +} + +func getRedhatishVersion(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + + if strings.Contains(c, "rawhide") { + return "rawhide" + } + if matches := regexp.MustCompile(`release (\w[\d.]*)`).FindStringSubmatch(c); matches != nil { + return matches[1] + } + return "" +} + +func getRedhatishPlatform(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + + if strings.Contains(c, "red hat") { + return "redhat" + } + f := strings.Split(c, " ") + + return f[0] +} + +func getSuseVersion(contents []string) string { + version := "" + for _, line := range contents { + if matches := regexp.MustCompile(`VERSION = ([\d.]+)`).FindStringSubmatch(line); matches != nil { + version = matches[1] + } else if matches := regexp.MustCompile(`PATCHLEVEL = ([\d]+)`).FindStringSubmatch(line); matches != nil { + version = version + "." + matches[1] + } + } + return version +} + +func getSusePlatform(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + if strings.Contains(c, "opensuse") { + return "opensuse" + } + return "suse" +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return common.VirtualizationWithContext(ctx) +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + var err error + + var files []string + + temperatures := make([]TemperatureStat, 0) + + // Only the temp*_input file provides current temperature + // value in millidegree Celsius as reported by the temperature to the device: + // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface + if files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_input")); err != nil { + return temperatures, err + } + + if len(files) == 0 { + // CentOS has an intermediate /device directory: + // https://github.com/giampaolo/psutil/issues/971 + if files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/device/temp*_input")); err != nil { + return temperatures, err + } + } + + var warns Warnings + + if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files + files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/")) + if err != nil { + return temperatures, err + } + for _, file := range files { + // Get the name of the temperature you are reading + name, err := ioutil.ReadFile(filepath.Join(file, "type")) + if err != nil { + warns.Add(err) + continue + } + // Get the temperature reading + current, err := ioutil.ReadFile(filepath.Join(file, "temp")) + if err != nil { + warns.Add(err) + continue + } + temperature, err := strconv.ParseInt(strings.TrimSpace(string(current)), 10, 64) + if err != nil { + warns.Add(err) + continue + } + + temperatures = append(temperatures, TemperatureStat{ + SensorKey: strings.TrimSpace(string(name)), + Temperature: float64(temperature) / 1000.0, + }) + } + return temperatures, warns.Reference() + } + + temperatures = make([]TemperatureStat, 0, len(files)) + + // example directory + // device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm + // name temp1_input temp2_input temp3_input temp4_input temp5_input temp6_input temp7_input + // power/ temp1_label temp2_label temp3_label temp4_label temp5_label temp6_label temp7_label + // subsystem/ temp1_max temp2_max temp3_max temp4_max temp5_max temp6_max temp7_max + // temp1_crit temp2_crit temp3_crit temp4_crit temp5_crit temp6_crit temp7_crit uevent + for _, file := range files { + var raw []byte + + var temperature float64 + + // Get the base directory location + directory := filepath.Dir(file) + + // Get the base filename prefix like temp1 + basename := strings.Split(filepath.Base(file), "_")[0] + + // Get the base path like /temp1 + basepath := filepath.Join(directory, basename) + + // Get the label of the temperature you are reading + label := "" + + if raw, _ = ioutil.ReadFile(basepath + "_label"); len(raw) != 0 { + // Format the label from "Core 0" to "core_0" + label = strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(raw))), " "), "_") + } + + // Get the name of the temperature you are reading + if raw, err = ioutil.ReadFile(filepath.Join(directory, "name")); err != nil { + warns.Add(err) + continue + } + + name := strings.TrimSpace(string(raw)) + + if label != "" { + name = name + "_" + label + } + + // Get the temperature reading + if raw, err = ioutil.ReadFile(file); err != nil { + warns.Add(err) + continue + } + + if temperature, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil { + warns.Add(err) + continue + } + + // Add discovered temperature sensor to the list + temperatures = append(temperatures, TemperatureStat{ + SensorKey: name, + Temperature: temperature / hostTemperatureScale, + High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale, + Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale, + }) + } + + return temperatures, warns.Reference() +} + +func optionalValueReadFromFile(filename string) float64 { + var raw []byte + + var err error + + var value float64 + + // Check if file exists + if _, err := os.Stat(filename); os.IsNotExist(err) { + return 0 + } + + if raw, err = ioutil.ReadFile(filename); err != nil { + return 0 + } + + if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil { + return 0 + } + + return value +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_386.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_386.go new file mode 100644 index 000000000..46e0c5d5a --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_386.go @@ -0,0 +1,47 @@ +// ATTENTION - FILE MANUAL FIXED AFTER CGO. +// Fixed line: Tv _Ctype_struct_timeval -> Tv UtTv +// Created by cgo -godefs, MANUAL FIXED +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + ID [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv UtTv + Addr_v6 [4]int32 + X__unused [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type UtTv struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_amd64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_amd64.go new file mode 100644 index 000000000..1e574482f --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_amd64.go @@ -0,0 +1,50 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv _Ctype_struct___0 + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int64 + Usec int64 +} + +type _Ctype_struct___0 struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm.go new file mode 100644 index 000000000..7abbbb8a3 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go | sed "s/uint8/int8/g" + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm64.go new file mode 100644 index 000000000..eebef55cd --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_arm64.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int64 + Usec int64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips.go new file mode 100644 index 000000000..50207e5bc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__unused [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64.go new file mode 100644 index 000000000..50207e5bc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__unused [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64le.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64le.go new file mode 100644 index 000000000..50207e5bc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mips64le.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__unused [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mipsle.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mipsle.go new file mode 100644 index 000000000..50207e5bc --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_mipsle.go @@ -0,0 +1,45 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__unused [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_ppc64le.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_ppc64le.go new file mode 100644 index 000000000..51f5bee11 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_ppc64le.go @@ -0,0 +1,48 @@ +//go:build linux && ppc64le +// +build linux,ppc64le + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int64 + Usec int64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_riscv64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_riscv64.go new file mode 100644 index 000000000..bb03a0b39 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_riscv64.go @@ -0,0 +1,49 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv _Ctype_struct___0 + Addr_v6 [4]int32 + X__glibc_reserved [20]uint8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int64 + Usec int64 +} + +type _Ctype_struct___0 struct { + Sec int32 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_linux_s390x.go b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_s390x.go new file mode 100644 index 000000000..6ea432a61 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_linux_s390x.go @@ -0,0 +1,48 @@ +//go:build linux && s390x +// +build linux,s390x + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type utmp struct { + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 +} + +type exit_status struct { + Termination int16 + Exit int16 +} + +type timeval struct { + Sec int64 + Usec int64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd.go b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd.go new file mode 100644 index 000000000..569de4abd --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd.go @@ -0,0 +1,105 @@ +//go:build openbsd +// +build openbsd + +package host + +import ( + "bytes" + "context" + "encoding/binary" + "io/ioutil" + "os" + "strings" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "github.com/shirou/gopsutil/v3/process" + "golang.org/x/sys/unix" +) + +const ( + UTNameSize = 32 /* see MAXLOGNAME in */ + UTLineSize = 8 + UTHostSize = 16 +) + +func HostIDWithContext(ctx context.Context) (string, error) { + return "", common.ErrNotImplementedError +} + +func numProcs(ctx context.Context) (uint64, error) { + procs, err := process.PidsWithContext(ctx) + if err != nil { + return 0, err + } + return uint64(len(procs)), nil +} + +func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { + platform := "" + family := "" + version := "" + + p, err := unix.Sysctl("kern.ostype") + if err == nil { + platform = strings.ToLower(p) + } + v, err := unix.Sysctl("kern.osrelease") + if err == nil { + version = strings.ToLower(v) + } + + return platform, family, version, nil +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + var ret []UserStat + utmpfile := "/var/run/utmp" + file, err := os.Open(utmpfile) + if err != nil { + return ret, err + } + defer file.Close() + + buf, err := ioutil.ReadAll(file) + if err != nil { + return ret, err + } + + u := Utmp{} + entrySize := int(unsafe.Sizeof(u)) + count := len(buf) / entrySize + + for i := 0; i < count; i++ { + b := buf[i*entrySize : i*entrySize+entrySize] + var u Utmp + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) + if err != nil || u.Time == 0 || u.Name[0] == 0 { + continue + } + user := UserStat{ + User: common.IntToString(u.Name[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(u.Time), + } + + ret = append(ret, user) + } + + return ret, nil +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + return []TemperatureStat{}, common.ErrNotImplementedError +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + _, _, version, err := PlatformInformationWithContext(ctx) + return version, err +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_386.go b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_386.go new file mode 100644 index 000000000..b299d7ae4 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_386.go @@ -0,0 +1,34 @@ +//go:build openbsd && 386 +// +build openbsd,386 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs host/types_openbsd.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x130 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [32]int8 + Host [256]int8 + Time int64 +} +type Timeval struct { + Sec int64 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_amd64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_amd64.go new file mode 100644 index 000000000..2d23b9b71 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_amd64.go @@ -0,0 +1,32 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_openbsd.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x130 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [32]int8 + Host [256]int8 + Time int64 +} + +type Timeval struct { + Sec int64 + Usec int64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm.go b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm.go new file mode 100644 index 000000000..f0ac57d0b --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm.go @@ -0,0 +1,34 @@ +//go:build openbsd && arm +// +build openbsd,arm + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs host/types_openbsd.go + +package host + +const ( + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x130 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [32]int8 + Host [256]int8 + Time int64 +} +type Timeval struct { + Sec int64 + Usec int32 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm64.go b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm64.go new file mode 100644 index 000000000..20fb42dd7 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_openbsd_arm64.go @@ -0,0 +1,34 @@ +//go:build openbsd && arm64 +// +build openbsd,arm64 + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo -godefs host/types_openbsd.go + +package host + +const ( + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x130 +) + +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [32]int8 + Host [256]int8 + Time int64 +} +type Timeval struct { + Sec int64 + Usec int64 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_posix.go b/vendor/github.com/shirou/gopsutil/v3/host/host_posix.go new file mode 100644 index 000000000..89e63781e --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_posix.go @@ -0,0 +1,16 @@ +//go:build linux || freebsd || openbsd || darwin || solaris +// +build linux freebsd openbsd darwin solaris + +package host + +import ( + "bytes" + + "golang.org/x/sys/unix" +) + +func KernelArch() (string, error) { + var utsname unix.Utsname + err := unix.Uname(&utsname) + return string(utsname.Machine[:bytes.IndexByte(utsname.Machine[:], 0)]), err +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_solaris.go b/vendor/github.com/shirou/gopsutil/v3/host/host_solaris.go new file mode 100644 index 000000000..7d3625acb --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_solaris.go @@ -0,0 +1,202 @@ +package host + +import ( + "bufio" + "bytes" + "context" + "encoding/csv" + "fmt" + "io" + "io/ioutil" + "os" + "regexp" + "strconv" + "strings" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func HostIDWithContext(ctx context.Context) (string, error) { + platform, err := parseReleaseFile() + if err != nil { + return "", err + } + + if platform == "SmartOS" { + // If everything works, use the current zone ID as the HostID if present. + out, err := invoke.CommandWithContext(ctx, "zonename") + if err == nil { + sc := bufio.NewScanner(bytes.NewReader(out)) + for sc.Scan() { + line := sc.Text() + + // If we're in the global zone, rely on the hostname. + if line == "global" { + hostname, err := os.Hostname() + if err == nil { + return hostname, nil + } + } else { + return strings.TrimSpace(line), nil + } + } + } + } + + // If HostID is still unknown, use hostid(1), which can lie to callers but at + // this point there are no hardware facilities available. This behavior + // matches that of other supported OSes. + out, err := invoke.CommandWithContext(ctx, "hostid") + if err == nil { + sc := bufio.NewScanner(bytes.NewReader(out)) + for sc.Scan() { + line := sc.Text() + return strings.TrimSpace(line), nil + } + } + + return "", nil +} + +// Count number of processes based on the number of entries in /proc +func numProcs(ctx context.Context) (uint64, error) { + dirs, err := ioutil.ReadDir("/proc") + if err != nil { + return 0, err + } + return uint64(len(dirs)), nil +} + +var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`) + +func BootTimeWithContext(ctx context.Context) (uint64, error) { + out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:boot_time") + if err != nil { + return 0, err + } + + kstats := kstatMatch.FindAllStringSubmatch(string(out), -1) + if len(kstats) != 1 { + return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats)) + } + + return strconv.ParseUint(kstats[0][2], 10, 64) +} + +func UptimeWithContext(ctx context.Context) (uint64, error) { + bootTime, err := BootTime() + if err != nil { + return 0, err + } + return timeSince(bootTime), nil +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + return []UserStat{}, common.ErrNotImplementedError +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + var ret []TemperatureStat + + out, err := invoke.CommandWithContext(ctx, "ipmitool", "-c", "sdr", "list") + if err != nil { + return ret, err + } + + r := csv.NewReader(strings.NewReader(string(out))) + // Output may contain errors, e.g. "bmc_send_cmd: Permission denied", don't expect a consistent number of records + r.FieldsPerRecord = -1 + for { + record, err := r.Read() + if err == io.EOF { + break + } + if err != nil { + return ret, err + } + // CPU1 Temp,40,degrees C,ok + if len(record) < 3 || record[1] == "" || record[2] != "degrees C" { + continue + } + v, err := strconv.ParseFloat(record[1], 64) + if err != nil { + return ret, err + } + ts := TemperatureStat{ + SensorKey: strings.TrimSuffix(record[0], " Temp"), + Temperature: v, + } + ret = append(ret, ts) + } + + return ret, nil +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +// Find distribution name from /etc/release +func parseReleaseFile() (string, error) { + b, err := ioutil.ReadFile("/etc/release") + if err != nil { + return "", err + } + s := string(b) + s = strings.TrimSpace(s) + + var platform string + + switch { + case strings.HasPrefix(s, "SmartOS"): + platform = "SmartOS" + case strings.HasPrefix(s, "OpenIndiana"): + platform = "OpenIndiana" + case strings.HasPrefix(s, "OmniOS"): + platform = "OmniOS" + case strings.HasPrefix(s, "Open Storage"): + platform = "NexentaStor" + case strings.HasPrefix(s, "Solaris"): + platform = "Solaris" + case strings.HasPrefix(s, "Oracle Solaris"): + platform = "Solaris" + default: + platform = strings.Fields(s)[0] + } + + return platform, nil +} + +// parseUnameOutput returns platformFamily, kernelVersion and platformVersion +func parseUnameOutput(ctx context.Context) (string, string, string, error) { + out, err := invoke.CommandWithContext(ctx, "uname", "-srv") + if err != nil { + return "", "", "", err + } + + fields := strings.Fields(string(out)) + if len(fields) < 3 { + return "", "", "", fmt.Errorf("malformed `uname` output") + } + + return fields[0], fields[1], fields[2], nil +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + _, kernelVersion, _, err := parseUnameOutput(ctx) + return kernelVersion, err +} + +func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { + platform, err := parseReleaseFile() + if err != nil { + return "", "", "", err + } + + platformFamily, _, platformVersion, err := parseUnameOutput(ctx) + if err != nil { + return "", "", "", err + } + + return platform, platformFamily, platformVersion, nil +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/host_windows.go b/vendor/github.com/shirou/gopsutil/v3/host/host_windows.go new file mode 100644 index 000000000..fcd1d5908 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/host_windows.go @@ -0,0 +1,279 @@ +//go:build windows +// +build windows + +package host + +import ( + "context" + "fmt" + "math" + "strconv" + "strings" + "sync/atomic" + "syscall" + "time" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "github.com/shirou/gopsutil/v3/process" + "github.com/yusufpapurcu/wmi" + "golang.org/x/sys/windows" +) + +var ( + procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime") + procGetTickCount32 = common.Modkernel32.NewProc("GetTickCount") + procGetTickCount64 = common.Modkernel32.NewProc("GetTickCount64") + procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") + procRtlGetVersion = common.ModNt.NewProc("RtlGetVersion") +) + +// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/ns-wdm-_osversioninfoexw +type osVersionInfoExW struct { + dwOSVersionInfoSize uint32 + dwMajorVersion uint32 + dwMinorVersion uint32 + dwBuildNumber uint32 + dwPlatformId uint32 + szCSDVersion [128]uint16 + wServicePackMajor uint16 + wServicePackMinor uint16 + wSuiteMask uint16 + wProductType uint8 + wReserved uint8 +} + +type systemInfo struct { + wProcessorArchitecture uint16 + wReserved uint16 + dwPageSize uint32 + lpMinimumApplicationAddress uintptr + lpMaximumApplicationAddress uintptr + dwActiveProcessorMask uintptr + dwNumberOfProcessors uint32 + dwProcessorType uint32 + dwAllocationGranularity uint32 + wProcessorLevel uint16 + wProcessorRevision uint16 +} + +type msAcpi_ThermalZoneTemperature struct { + Active bool + CriticalTripPoint uint32 + CurrentTemperature uint32 + InstanceName string +} + +func HostIDWithContext(ctx context.Context) (string, error) { + // there has been reports of issues on 32bit using golang.org/x/sys/windows/registry, see https://github.com/shirou/gopsutil/pull/312#issuecomment-277422612 + // for rationale of using windows.RegOpenKeyEx/RegQueryValueEx instead of registry.OpenKey/GetStringValue + var h windows.Handle + err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h) + if err != nil { + return "", err + } + defer windows.RegCloseKey(h) + + const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16 + const uuidLen = 36 + + var regBuf [windowsRegBufLen]uint16 + bufLen := uint32(windowsRegBufLen) + var valType uint32 + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + if err != nil { + return "", err + } + + hostID := windows.UTF16ToString(regBuf[:]) + hostIDLen := len(hostID) + if hostIDLen != uuidLen { + return "", fmt.Errorf("HostID incorrect: %q\n", hostID) + } + + return strings.ToLower(hostID), nil +} + +func numProcs(ctx context.Context) (uint64, error) { + procs, err := process.PidsWithContext(ctx) + if err != nil { + return 0, err + } + return uint64(len(procs)), nil +} + +func UptimeWithContext(ctx context.Context) (uint64, error) { + procGetTickCount := procGetTickCount64 + err := procGetTickCount64.Find() + if err != nil { + procGetTickCount = procGetTickCount32 // handle WinXP, but keep in mind that "the time will wrap around to zero if the system is run continuously for 49.7 days." from MSDN + } + r1, _, lastErr := syscall.Syscall(procGetTickCount.Addr(), 0, 0, 0, 0) + if lastErr != 0 { + return 0, lastErr + } + return uint64((time.Duration(r1) * time.Millisecond).Seconds()), nil +} + +// cachedBootTime must be accessed via atomic.Load/StoreUint64 +var cachedBootTime uint64 + +func BootTimeWithContext(ctx context.Context) (uint64, error) { + t := atomic.LoadUint64(&cachedBootTime) + if t != 0 { + return t, nil + } + up, err := Uptime() + if err != nil { + return 0, err + } + t = timeSince(up) + atomic.StoreUint64(&cachedBootTime, t) + return t, nil +} + +func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) { + // GetVersionEx lies on Windows 8.1 and returns as Windows 8 if we don't declare compatibility in manifest + // RtlGetVersion bypasses this lying layer and returns the true Windows version + // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-rtlgetversion + // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/ns-wdm-_osversioninfoexw + var osInfo osVersionInfoExW + osInfo.dwOSVersionInfoSize = uint32(unsafe.Sizeof(osInfo)) + ret, _, err := procRtlGetVersion.Call(uintptr(unsafe.Pointer(&osInfo))) + if ret != 0 { + return + } + + // Platform + var h windows.Handle // like HostIDWithContext(), we query the registry using the raw windows.RegOpenKeyEx/RegQueryValueEx + err = windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Windows NT\CurrentVersion`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h) + if err != nil { + return + } + defer windows.RegCloseKey(h) + var bufLen uint32 + var valType uint32 + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`ProductName`), nil, &valType, nil, &bufLen) + if err != nil { + return + } + regBuf := make([]uint16, bufLen/2+1) + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`ProductName`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + if err != nil { + return + } + platform = windows.UTF16ToString(regBuf[:]) + if strings.Contains(platform, "Windows 10") { // check build number to determine whether it's actually Windows 11 + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, nil, &bufLen) + if err == nil { + regBuf = make([]uint16, bufLen/2+1) + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + if err == nil { + buildNumberStr := windows.UTF16ToString(regBuf[:]) + if buildNumber, err := strconv.Atoi(buildNumberStr); err == nil && buildNumber >= 22000 { + platform = strings.Replace(platform, "Windows 10", "Windows 11", 1) + } + } + } + } + if !strings.HasPrefix(platform, "Microsoft") { + platform = "Microsoft " + platform + } + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CSDVersion`), nil, &valType, nil, &bufLen) // append Service Pack number, only on success + if err == nil { // don't return an error if only the Service Pack retrieval fails + regBuf = make([]uint16, bufLen/2+1) + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CSDVersion`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + if err == nil { + platform += " " + windows.UTF16ToString(regBuf[:]) + } + } + + // PlatformFamily + switch osInfo.wProductType { + case 1: + family = "Standalone Workstation" + case 2: + family = "Server (Domain Controller)" + case 3: + family = "Server" + } + + // Platform Version + version = fmt.Sprintf("%d.%d.%d Build %d", osInfo.dwMajorVersion, osInfo.dwMinorVersion, osInfo.dwBuildNumber, osInfo.dwBuildNumber) + + return platform, family, version, nil +} + +func UsersWithContext(ctx context.Context) ([]UserStat, error) { + var ret []UserStat + + return ret, common.ErrNotImplementedError +} + +func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + var ret []TemperatureStat + var dst []msAcpi_ThermalZoneTemperature + q := wmi.CreateQuery(&dst, "") + if err := common.WMIQueryWithContext(ctx, q, &dst, nil, "root/wmi"); err != nil { + return ret, err + } + + for _, v := range dst { + ts := TemperatureStat{ + SensorKey: v.InstanceName, + Temperature: kelvinToCelsius(v.CurrentTemperature, 2), + } + ret = append(ret, ts) + } + + return ret, nil +} + +func kelvinToCelsius(temp uint32, n int) float64 { + // wmi return temperature Kelvin * 10, so need to divide the result by 10, + // and then minus 273.15 to get °Celsius. + t := float64(temp/10) - 273.15 + n10 := math.Pow10(n) + return math.Trunc((t+0.5/n10)*n10) / n10 +} + +func VirtualizationWithContext(ctx context.Context) (string, string, error) { + return "", "", common.ErrNotImplementedError +} + +func KernelVersionWithContext(ctx context.Context) (string, error) { + _, _, version, err := PlatformInformationWithContext(ctx) + return version, err +} + +func KernelArch() (string, error) { + var systemInfo systemInfo + procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo))) + + const ( + PROCESSOR_ARCHITECTURE_INTEL = 0 + PROCESSOR_ARCHITECTURE_ARM = 5 + PROCESSOR_ARCHITECTURE_ARM64 = 12 + PROCESSOR_ARCHITECTURE_IA64 = 6 + PROCESSOR_ARCHITECTURE_AMD64 = 9 + ) + switch systemInfo.wProcessorArchitecture { + case PROCESSOR_ARCHITECTURE_INTEL: + if systemInfo.wProcessorLevel < 3 { + return "i386", nil + } + if systemInfo.wProcessorLevel > 6 { + return "i686", nil + } + return fmt.Sprintf("i%d86", systemInfo.wProcessorLevel), nil + case PROCESSOR_ARCHITECTURE_ARM: + return "arm", nil + case PROCESSOR_ARCHITECTURE_ARM64: + return "aarch64", nil + case PROCESSOR_ARCHITECTURE_IA64: + return "ia64", nil + case PROCESSOR_ARCHITECTURE_AMD64: + return "x86_64", nil + } + return "", nil +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.c b/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.c new file mode 100644 index 000000000..0197d95b4 --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.c @@ -0,0 +1,169 @@ +#include +#include +#include "smc_darwin.h" + +#define IOSERVICE_SMC "AppleSMC" +#define IOSERVICE_MODEL "IOPlatformExpertDevice" + +#define DATA_TYPE_SP78 "sp78" + +typedef enum { + kSMCUserClientOpen = 0, + kSMCUserClientClose = 1, + kSMCHandleYPCEvent = 2, + kSMCReadKey = 5, + kSMCWriteKey = 6, + kSMCGetKeyCount = 7, + kSMCGetKeyFromIndex = 8, + kSMCGetKeyInfo = 9, +} selector_t; + +typedef struct { + unsigned char major; + unsigned char minor; + unsigned char build; + unsigned char reserved; + unsigned short release; +} SMCVersion; + +typedef struct { + uint16_t version; + uint16_t length; + uint32_t cpuPLimit; + uint32_t gpuPLimit; + uint32_t memPLimit; +} SMCPLimitData; + +typedef struct { + IOByteCount data_size; + uint32_t data_type; + uint8_t data_attributes; +} SMCKeyInfoData; + +typedef struct { + uint32_t key; + SMCVersion vers; + SMCPLimitData p_limit_data; + SMCKeyInfoData key_info; + uint8_t result; + uint8_t status; + uint8_t data8; + uint32_t data32; + uint8_t bytes[32]; +} SMCParamStruct; + +typedef enum { + kSMCSuccess = 0, + kSMCError = 1, + kSMCKeyNotFound = 0x84, +} kSMC_t; + +typedef struct { + uint8_t data[32]; + uint32_t data_type; + uint32_t data_size; + kSMC_t kSMC; +} smc_return_t; + +static const int SMC_KEY_SIZE = 4; // number of characters in an SMC key. +static io_connect_t conn; // our connection to the SMC. + +kern_return_t gopsutil_v3_open_smc(void) { + kern_return_t result; + io_service_t service; + + service = IOServiceGetMatchingService(0, IOServiceMatching(IOSERVICE_SMC)); + if (service == 0) { + // Note: IOServiceMatching documents 0 on failure + printf("ERROR: %s NOT FOUND\n", IOSERVICE_SMC); + return kIOReturnError; + } + + result = IOServiceOpen(service, mach_task_self(), 0, &conn); + IOObjectRelease(service); + + return result; +} + +kern_return_t gopsutil_v3_close_smc(void) { return IOServiceClose(conn); } + +static uint32_t to_uint32(char *key) { + uint32_t ans = 0; + uint32_t shift = 24; + + if (strlen(key) != SMC_KEY_SIZE) { + return 0; + } + + for (int i = 0; i < SMC_KEY_SIZE; i++) { + ans += key[i] << shift; + shift -= 8; + } + + return ans; +} + +static kern_return_t call_smc(SMCParamStruct *input, SMCParamStruct *output) { + kern_return_t result; + size_t input_cnt = sizeof(SMCParamStruct); + size_t output_cnt = sizeof(SMCParamStruct); + + result = IOConnectCallStructMethod(conn, kSMCHandleYPCEvent, input, input_cnt, + output, &output_cnt); + + if (result != kIOReturnSuccess) { + result = err_get_code(result); + } + return result; +} + +static kern_return_t read_smc(char *key, smc_return_t *result_smc) { + kern_return_t result; + SMCParamStruct input; + SMCParamStruct output; + + memset(&input, 0, sizeof(SMCParamStruct)); + memset(&output, 0, sizeof(SMCParamStruct)); + memset(result_smc, 0, sizeof(smc_return_t)); + + input.key = to_uint32(key); + input.data8 = kSMCGetKeyInfo; + + result = call_smc(&input, &output); + result_smc->kSMC = output.result; + + if (result != kIOReturnSuccess || output.result != kSMCSuccess) { + return result; + } + + result_smc->data_size = output.key_info.data_size; + result_smc->data_type = output.key_info.data_type; + + input.key_info.data_size = output.key_info.data_size; + input.data8 = kSMCReadKey; + + result = call_smc(&input, &output); + result_smc->kSMC = output.result; + + if (result != kIOReturnSuccess || output.result != kSMCSuccess) { + return result; + } + + memcpy(result_smc->data, output.bytes, sizeof(output.bytes)); + + return result; +} + +double gopsutil_v3_get_temperature(char *key) { + kern_return_t result; + smc_return_t result_smc; + + result = read_smc(key, &result_smc); + + if (!(result == kIOReturnSuccess) && result_smc.data_size == 2 && + result_smc.data_type == to_uint32(DATA_TYPE_SP78)) { + return 0.0; + } + + return (double)result_smc.data[0]; +} diff --git a/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.h b/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.h new file mode 100644 index 000000000..e3013abdb --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/smc_darwin.h @@ -0,0 +1,32 @@ +#ifndef __SMC_H__ +#define __SMC_H__ 1 + +#include + +#define AMBIENT_AIR_0 "TA0P" +#define AMBIENT_AIR_1 "TA1P" +#define CPU_0_DIODE "TC0D" +#define CPU_0_HEATSINK "TC0H" +#define CPU_0_PROXIMITY "TC0P" +#define ENCLOSURE_BASE_0 "TB0T" +#define ENCLOSURE_BASE_1 "TB1T" +#define ENCLOSURE_BASE_2 "TB2T" +#define ENCLOSURE_BASE_3 "TB3T" +#define GPU_0_DIODE "TG0D" +#define GPU_0_HEATSINK "TG0H" +#define GPU_0_PROXIMITY "TG0P" +#define HARD_DRIVE_BAY "TH0P" +#define MEMORY_SLOT_0 "TM0S" +#define MEMORY_SLOTS_PROXIMITY "TM0P" +#define NORTHBRIDGE "TN0H" +#define NORTHBRIDGE_DIODE "TN0D" +#define NORTHBRIDGE_PROXIMITY "TN0P" +#define THUNDERBOLT_0 "TI0P" +#define THUNDERBOLT_1 "TI1P" +#define WIRELESS_MODULE "TW0P" + +kern_return_t gopsutil_v3_open_smc(void); +kern_return_t gopsutil_v3_close_smc(void); +double gopsutil_v3_get_temperature(char *); + +#endif // __SMC_H__ diff --git a/vendor/github.com/shirou/gopsutil/v3/host/types.go b/vendor/github.com/shirou/gopsutil/v3/host/types.go new file mode 100644 index 000000000..c2e7c0bda --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/host/types.go @@ -0,0 +1,24 @@ +package host + +import ( + "fmt" +) + +type Warnings struct { + List []error +} + +func (w *Warnings) Add(err error) { + w.List = append(w.List, err) +} + +func (w *Warnings) Reference() error { + if len(w.List) > 0 { + return w + } + return nil +} + +func (w *Warnings) Error() string { + return fmt.Sprintf("Number of warnings: %v", len(w.List)) +} diff --git a/vendor/github.com/shirou/gopsutil/v3/internal/common/common.go b/vendor/github.com/shirou/gopsutil/v3/internal/common/common.go index 4f6df9a3a..adc4922bd 100644 --- a/vendor/github.com/shirou/gopsutil/v3/internal/common/common.go +++ b/vendor/github.com/shirou/gopsutil/v3/internal/common/common.go @@ -311,6 +311,15 @@ func PathExists(filename string) bool { return false } +// PathExistsWithContents returns the filename exists and it is not empty +func PathExistsWithContents(filename string) bool { + info, err := os.Stat(filename) + if err != nil { + return false + } + return info.Size() > 4 // at least 4 bytes +} + // GetEnv retrieves the environment variable key. If it does not exist it returns the default. func GetEnv(key string, dfault string, combineWith ...string) string { value := os.Getenv(key) diff --git a/vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go b/vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go index 326b342ed..da44c3f2b 100644 --- a/vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go +++ b/vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go @@ -274,6 +274,12 @@ func GetOSRelease() (platform string, version string, err error) { version = trimQuotes(field[1]) } } + + // cleanup amazon ID + if platform == "amzn" { + platform = "amazon" + } + return platform, version, nil } diff --git a/vendor/github.com/shirou/gopsutil/v3/internal/common/endian.go b/vendor/github.com/shirou/gopsutil/v3/internal/common/endian.go new file mode 100644 index 000000000..147cfdc4b --- /dev/null +++ b/vendor/github.com/shirou/gopsutil/v3/internal/common/endian.go @@ -0,0 +1,10 @@ +package common + +import "unsafe" + +// IsLittleEndian checks if the current platform uses little-endian. +// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License) +func IsLittleEndian() bool { + var x int16 = 0x0011 + return *(*byte)(unsafe.Pointer(&x)) == 0x11 +} diff --git a/vendor/github.com/shirou/gopsutil/v3/mem/mem_darwin_cgo.go b/vendor/github.com/shirou/gopsutil/v3/mem/mem_darwin_cgo.go index 82c38245b..e5da7dcdb 100644 --- a/vendor/github.com/shirou/gopsutil/v3/mem/mem_darwin_cgo.go +++ b/vendor/github.com/shirou/gopsutil/v3/mem/mem_darwin_cgo.go @@ -5,6 +5,7 @@ package mem /* #include +#include */ import "C" @@ -12,8 +13,6 @@ import ( "context" "fmt" "unsafe" - - "golang.org/x/sys/unix" ) // VirtualMemory returns VirtualmemoryStat. @@ -34,7 +33,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { return nil, fmt.Errorf("host_statistics error=%d", status) } - pageSize := uint64(unix.Getpagesize()) + pageSize := uint64(C.vm_kernel_page_size) total, err := getHwMemsize() if err != nil { return nil, err diff --git a/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go b/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go index 81cde8133..c08997196 100644 --- a/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go +++ b/vendor/github.com/shirou/gopsutil/v3/net/net_linux.go @@ -721,9 +721,13 @@ func decodeAddressWithContext(ctx context.Context, family uint32, src string) (A return Addr{}, fmt.Errorf("decode error, %w", err) } var ip net.IP - // Assumes this is little_endian + if family == syscall.AF_INET { - ip = net.IP(ReverseWithContext(ctx, decoded)) + if common.IsLittleEndian() { + ip = net.IP(ReverseWithContext(ctx, decoded)) + } else { + ip = net.IP(decoded) + } } else { // IPv6 ip, err = parseIPv6HexStringWithContext(ctx, decoded) if err != nil { diff --git a/vendor/github.com/shirou/gopsutil/v3/process/process_darwin_386.go b/vendor/github.com/shirou/gopsutil/v3/process/process_darwin_386.go deleted file mode 100644 index b353e5eac..000000000 --- a/vendor/github.com/shirou/gopsutil/v3/process/process_darwin_386.go +++ /dev/null @@ -1,236 +0,0 @@ -// Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_darwin.go - -package process - -const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 -) - -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 -) - -type Timespec struct { - Sec int64 - Nsec int64 -} - -type Timeval struct { - Sec int64 - Usec int32 - Pad_cgo_0 [4]byte -} - -type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 -} - -type Rlimit struct { - Cur uint64 - Max uint64 -} - -type UGid_t uint32 - -type KinfoProc struct { - Proc ExternProc - Eproc Eproc -} - -type Eproc struct { - Paddr *uint64 - Sess *Session - Pcred Upcred - Ucred Uucred - Pad_cgo_0 [4]byte - Vm Vmspace - Ppid int32 - Pgid int32 - Jobc int16 - Pad_cgo_1 [2]byte - Tdev int32 - Tpgid int32 - Pad_cgo_2 [4]byte - Tsess *Session - Wmesg [8]int8 - Xsize int32 - Xrssize int16 - Xccount int16 - Xswrss int16 - Pad_cgo_3 [2]byte - Flag int32 - Login [12]int8 - Spare [4]int32 - Pad_cgo_4 [4]byte -} - -type Proc struct{} - -type Session struct{} - -type ucred struct { - Link _Ctype_struct___0 - Ref uint64 - Posix Posix_cred - Label *Label - Audit Au_session -} - -type Uucred struct { - Ref int32 - UID uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 -} - -type Upcred struct { - Pc_lock [72]int8 - Pc_ucred *ucred - P_ruid uint32 - P_svuid uint32 - P_rgid uint32 - P_svgid uint32 - P_refcnt int32 - Pad_cgo_0 [4]byte -} - -type Vmspace struct { - Dummy int32 - Pad_cgo_0 [4]byte - Dummy2 *int8 - Dummy3 [5]int32 - Pad_cgo_1 [4]byte - Dummy4 [3]*int8 -} - -type Sigacts struct{} - -type ExternProc struct { - P_un [16]byte - P_vmspace uint64 - P_sigacts uint64 - Pad_cgo_0 [3]byte - P_flag int32 - P_stat int8 - P_pid int32 - P_oppid int32 - P_dupfd int32 - Pad_cgo_1 [4]byte - User_stack uint64 - Exit_thread uint64 - P_debugger int32 - Sigwait int32 - P_estcpu uint32 - P_cpticks int32 - P_pctcpu uint32 - Pad_cgo_2 [4]byte - P_wchan uint64 - P_wmesg uint64 - P_swtime uint32 - P_slptime uint32 - P_realtimer Itimerval - P_rtime Timeval - P_uticks uint64 - P_sticks uint64 - P_iticks uint64 - P_traceflag int32 - Pad_cgo_3 [4]byte - P_tracep uint64 - P_siglist int32 - Pad_cgo_4 [4]byte - P_textvp uint64 - P_holdcnt int32 - P_sigmask uint32 - P_sigignore uint32 - P_sigcatch uint32 - P_priority uint8 - P_usrpri uint8 - P_nice int8 - P_comm [17]int8 - Pad_cgo_5 [4]byte - P_pgrp uint64 - P_addr uint64 - P_xstat uint16 - P_acflag uint16 - Pad_cgo_6 [4]byte - P_ru uint64 -} - -type Itimerval struct { - Interval Timeval - Value Timeval -} - -type Vnode struct{} - -type Pgrp struct{} - -type UserStruct struct{} - -type Au_session struct { - Aia_p *AuditinfoAddr - Mask AuMask -} - -type Posix_cred struct { - UID uint32 - Ruid uint32 - Svuid uint32 - Ngroups int16 - Pad_cgo_0 [2]byte - Groups [16]uint32 - Rgid uint32 - Svgid uint32 - Gmuid uint32 - Flags int32 -} - -type Label struct{} - -type AuditinfoAddr struct { - Auid uint32 - Mask AuMask - Termid AuTidAddr - Asid int32 - Flags uint64 -} - -type AuMask struct { - Success uint32 - Failure uint32 -} - -type AuTidAddr struct { - Port int32 - Type uint32 - Addr [4]uint32 -} - -type UcredQueue struct { - Next *ucred - Prev **ucred -} diff --git a/vendor/github.com/shirou/gopsutil/v3/process/process_linux.go b/vendor/github.com/shirou/gopsutil/v3/process/process_linux.go index df6c1401f..d5b5bc329 100644 --- a/vendor/github.com/shirou/gopsutil/v3/process/process_linux.go +++ b/vendor/github.com/shirou/gopsutil/v3/process/process_linux.go @@ -688,9 +688,9 @@ func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string if len(cmdline) == 0 { return nil, nil } - if cmdline[len(cmdline)-1] == 0 { - cmdline = cmdline[:len(cmdline)-1] - } + + cmdline = bytes.TrimRight(cmdline, "\x00") + parts := bytes.Split(cmdline, []byte{0}) var strParts []string for _, p := range parts { diff --git a/vendor/github.com/simonnilsson/ask/.gitignore b/vendor/github.com/simonnilsson/ask/.gitignore new file mode 100644 index 000000000..78fe7aa8b --- /dev/null +++ b/vendor/github.com/simonnilsson/ask/.gitignore @@ -0,0 +1,18 @@ +# General +.vscode +.DS_Store + +# Compiled Executables, Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so +*.exe +*.dll +*.dylib +*.wasm + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool +*.out diff --git a/vendor/github.com/simonnilsson/ask/LICENSE b/vendor/github.com/simonnilsson/ask/LICENSE new file mode 100644 index 000000000..602486d1f --- /dev/null +++ b/vendor/github.com/simonnilsson/ask/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Simon Nilsson + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/github.com/simonnilsson/ask/README.md b/vendor/github.com/simonnilsson/ask/README.md new file mode 100644 index 000000000..2d658348a --- /dev/null +++ b/vendor/github.com/simonnilsson/ask/README.md @@ -0,0 +1,77 @@ +

+ASK +
+PkgGoDev +Go Report Card +
+and you shall receive. +

+Ask provides a simple way of accessing nested properties in maps and slices. Works great in combination with encoding/json and other packages that "Unmarshal" arbitrary data into Go data-types. Inspired by the get function in the lodash javascript library. + +## Use + +```go +package main + +import "json" +import "github.com/simonnilsson/ask" + +func main() { + + // Use parsed JSON as source data + var object map[string]interface{} + json.Unmarshal([]byte(`{ "a": [{ "b": { "c": 3 } }] }`), &object) + + // Extract the 3 + res, ok := ask.For(object, "a[0].b.c").Int(0) + + fmt.Println(res, ok) + // Output: 3 true + + // Attempt extracting a string at path .d that does not exist + res2, ok := ask.For(object, "a[0].b.d").String("nothing") + + fmt.Println(res2, ok) + // Output: nothing false + +} +``` + +## API + +Internally ask uses type assertions to traverse down the path supplied. Each invocation starts by calling For() with your data structure **source** and the **path** in this structure to extract. + +```go +For(source interface{}, path string) *Answer +``` +Additional paths can be traversed by calling Path() on the resulting answer. +```go +(a *Answer) Path(path string) *Answer +``` + + + +### Type assertion + +After receiving an `*Answer` from a call to For() it can be asserted to a type. The methods for this is seen below. Each function takes a default value as a parameter that will be returned in case the value can not be asserted from the answer. A second return value is used to indicate if the assertion was successful. + +* `String(d string) (string, bool)` +* `Bool(d bool) (bool, bool)` +* `Int(d int64) (int64, bool)` +* `Uint(d uint64) (uint64, bool)` +* `Float(d float64) (float64, bool)` +* `Slice(d []interface{}) ([]interface{}, bool)` +* `Map(d map[string]interface{}) (map[string]interface{}, bool)` + +If a number is found but it is of different type than requested it will be casted to desired type and return success. If the value would not fit within the valid range of requested type the operation will fail however and the default parameter will be returned instead. + +Two additional methods are available, one to check if the answer has a value (not nil) and one to return the raw value as a interface{}. + +* `Exists() bool` +* `Value() interface{}` + +For full documentation see [pkg.go.dev](https://pkg.go.dev/github.com/simonnilsson/ask). + +## License + +[MIT](LICENSE) \ No newline at end of file diff --git a/vendor/github.com/simonnilsson/ask/ask.go b/vendor/github.com/simonnilsson/ask/ask.go new file mode 100644 index 000000000..dc32d37b8 --- /dev/null +++ b/vendor/github.com/simonnilsson/ask/ask.go @@ -0,0 +1,174 @@ +// Package ask provides a simple way of accessing nested properties in maps and arrays. +// Works great in combination with encoding/json and other packages that "Unmarshal" arbitrary data into Go data-types. +// Inspired by the get function in the lodash javascript library. +package ask + +import ( + "math" + "reflect" + "regexp" + "strconv" + "strings" +) + +var tokenMatcher = regexp.MustCompile("([^[]+)?(?:\\[(\\d+)])?") + +// Answer holds result of call to For, use one of its methods to extract a value. +type Answer struct { + value interface{} +} + +// For is used to select a path from source to return as answer. +func For(source interface{}, path string) *Answer { + + parts := strings.Split(path, ".") + + current := source + + for _, part := range parts { + + match := tokenMatcher.FindStringSubmatch(strings.TrimSpace(part)) + + if len(match) == 3 { + + if match[1] != "" { + m, ok := current.(map[string]interface{}) + if !ok { + return &Answer{} + } + current = m[match[1]] + } + + if match[2] != "" { + index, _ := strconv.Atoi(match[2]) + a, ok := current.([]interface{}) + if !ok || index < 0 || len(a) <= index { + return &Answer{} + } + current = a[index] + } + + } + + } + + return &Answer{value: current} +} + +// Path does the same thing as For but uses existing answer as source. +func (a *Answer) Path(path string) *Answer { + return For(a.value, path) +} + +// Exists returns a boolean indicating if the answer exists (not nil). +func (a *Answer) Exists() bool { + return a.value != nil +} + +// Value returns the raw value as type interface{}, can be nil if no value is available. +func (a *Answer) Value() interface{} { + return a.value +} + +// Slice attempts asserting answer as a []interface{}. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Slice(d []interface{}) ([]interface{}, bool) { + res, ok := a.value.([]interface{}) + if ok { + return res, ok + } + return d, false +} + +// Map attempts asserting answer as a map[string]interface{}. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Map(d map[string]interface{}) (map[string]interface{}, bool) { + res, ok := a.value.(map[string]interface{}) + if ok { + return res, ok + } + return d, false +} + +// String attempts asserting answer as a string. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) String(d string) (string, bool) { + res, ok := a.value.(string) + if ok { + return res, ok + } + return d, false +} + +// Bool attempts asserting answer as a bool. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Bool(d bool) (bool, bool) { + res, ok := a.value.(bool) + if ok { + return res, ok + } + return d, false +} + +// Int attempts asserting answer as a int64. Casting from other number types will be done if necessary. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Int(d int64) (int64, bool) { + switch vt := a.value.(type) { + case int, int8, int16, int32, int64: + return reflect.ValueOf(vt).Int(), true + case uint, uint8, uint16, uint32, uint64: + val := reflect.ValueOf(vt).Uint() + if val <= math.MaxInt64 { + return int64(val), true + } + case float32, float64: + val := reflect.ValueOf(vt).Float() + if val >= 0 && val <= math.MaxInt64 { + return int64(val), true + } + } + return d, false +} + +// Uint attempts asserting answer as a uint64. Casting from other number types will be done if necessary. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Uint(d uint64) (uint64, bool) { + switch vt := a.value.(type) { + case int, int8, int16, int32, int64: + val := reflect.ValueOf(vt).Int() + if val >= 0 { + return uint64(val), true + } + case uint, uint8, uint16, uint32, uint64: + return reflect.ValueOf(vt).Uint(), true + case float32, float64: + val := reflect.ValueOf(vt).Float() + if val >= 0 && val <= math.MaxUint64 { + return uint64(val), true + } + } + return d, false +} + +// Float attempts asserting answer as a float64. Casting from other number types will be done if necessary. +// The first return value is the result, and the second indicates if the operation was successful. +// If not successful the first return value will be set to the d parameter. +func (a *Answer) Float(d float64) (float64, bool) { + switch vt := a.value.(type) { + case int, int8, int16, int32, int64: + return float64(reflect.ValueOf(vt).Int()), true + case uint, uint8, uint16, uint32, uint64: + return float64(reflect.ValueOf(vt).Uint()), true + case float32: + return float64(vt), true + case float64: + return vt, true + } + return d, false +} diff --git a/vendor/github.com/simonnilsson/ask/logo.eps b/vendor/github.com/simonnilsson/ask/logo.eps new file mode 100644 index 000000000..1a6f0db81 Binary files /dev/null and b/vendor/github.com/simonnilsson/ask/logo.eps differ diff --git a/vendor/github.com/simonnilsson/ask/logo.svg b/vendor/github.com/simonnilsson/ask/logo.svg new file mode 100644 index 000000000..1b41eacb6 --- /dev/null +++ b/vendor/github.com/simonnilsson/ask/logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/vendor/golang.org/x/sys/windows/registry/key.go b/vendor/golang.org/x/sys/windows/registry/key.go new file mode 100644 index 000000000..6c8d97b6a --- /dev/null +++ b/vendor/golang.org/x/sys/windows/registry/key.go @@ -0,0 +1,206 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build windows +// +build windows + +// Package registry provides access to the Windows registry. +// +// Here is a simple example, opening a registry key and reading a string value from it. +// +// k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) +// if err != nil { +// log.Fatal(err) +// } +// defer k.Close() +// +// s, _, err := k.GetStringValue("SystemRoot") +// if err != nil { +// log.Fatal(err) +// } +// fmt.Printf("Windows system root is %q\n", s) +package registry + +import ( + "io" + "runtime" + "syscall" + "time" +) + +const ( + // Registry key security and access rights. + // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724878.aspx + // for details. + ALL_ACCESS = 0xf003f + CREATE_LINK = 0x00020 + CREATE_SUB_KEY = 0x00004 + ENUMERATE_SUB_KEYS = 0x00008 + EXECUTE = 0x20019 + NOTIFY = 0x00010 + QUERY_VALUE = 0x00001 + READ = 0x20019 + SET_VALUE = 0x00002 + WOW64_32KEY = 0x00200 + WOW64_64KEY = 0x00100 + WRITE = 0x20006 +) + +// Key is a handle to an open Windows registry key. +// Keys can be obtained by calling OpenKey; there are +// also some predefined root keys such as CURRENT_USER. +// Keys can be used directly in the Windows API. +type Key syscall.Handle + +const ( + // Windows defines some predefined root keys that are always open. + // An application can use these keys as entry points to the registry. + // Normally these keys are used in OpenKey to open new keys, + // but they can also be used anywhere a Key is required. + CLASSES_ROOT = Key(syscall.HKEY_CLASSES_ROOT) + CURRENT_USER = Key(syscall.HKEY_CURRENT_USER) + LOCAL_MACHINE = Key(syscall.HKEY_LOCAL_MACHINE) + USERS = Key(syscall.HKEY_USERS) + CURRENT_CONFIG = Key(syscall.HKEY_CURRENT_CONFIG) + PERFORMANCE_DATA = Key(syscall.HKEY_PERFORMANCE_DATA) +) + +// Close closes open key k. +func (k Key) Close() error { + return syscall.RegCloseKey(syscall.Handle(k)) +} + +// OpenKey opens a new key with path name relative to key k. +// It accepts any open key, including CURRENT_USER and others, +// and returns the new key and an error. +// The access parameter specifies desired access rights to the +// key to be opened. +func OpenKey(k Key, path string, access uint32) (Key, error) { + p, err := syscall.UTF16PtrFromString(path) + if err != nil { + return 0, err + } + var subkey syscall.Handle + err = syscall.RegOpenKeyEx(syscall.Handle(k), p, 0, access, &subkey) + if err != nil { + return 0, err + } + return Key(subkey), nil +} + +// OpenRemoteKey opens a predefined registry key on another +// computer pcname. The key to be opened is specified by k, but +// can only be one of LOCAL_MACHINE, PERFORMANCE_DATA or USERS. +// If pcname is "", OpenRemoteKey returns local computer key. +func OpenRemoteKey(pcname string, k Key) (Key, error) { + var err error + var p *uint16 + if pcname != "" { + p, err = syscall.UTF16PtrFromString(`\\` + pcname) + if err != nil { + return 0, err + } + } + var remoteKey syscall.Handle + err = regConnectRegistry(p, syscall.Handle(k), &remoteKey) + if err != nil { + return 0, err + } + return Key(remoteKey), nil +} + +// ReadSubKeyNames returns the names of subkeys of key k. +// The parameter n controls the number of returned names, +// analogous to the way os.File.Readdirnames works. +func (k Key) ReadSubKeyNames(n int) ([]string, error) { + // RegEnumKeyEx must be called repeatedly and to completion. + // During this time, this goroutine cannot migrate away from + // its current thread. See https://golang.org/issue/49320 and + // https://golang.org/issue/49466. + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + names := make([]string, 0) + // Registry key size limit is 255 bytes and described there: + // https://msdn.microsoft.com/library/windows/desktop/ms724872.aspx + buf := make([]uint16, 256) //plus extra room for terminating zero byte +loopItems: + for i := uint32(0); ; i++ { + if n > 0 { + if len(names) == n { + return names, nil + } + } + l := uint32(len(buf)) + for { + err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil) + if err == nil { + break + } + if err == syscall.ERROR_MORE_DATA { + // Double buffer size and try again. + l = uint32(2 * len(buf)) + buf = make([]uint16, l) + continue + } + if err == _ERROR_NO_MORE_ITEMS { + break loopItems + } + return names, err + } + names = append(names, syscall.UTF16ToString(buf[:l])) + } + if n > len(names) { + return names, io.EOF + } + return names, nil +} + +// CreateKey creates a key named path under open key k. +// CreateKey returns the new key and a boolean flag that reports +// whether the key already existed. +// The access parameter specifies the access rights for the key +// to be created. +func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) { + var h syscall.Handle + var d uint32 + err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path), + 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d) + if err != nil { + return 0, false, err + } + return Key(h), d == _REG_OPENED_EXISTING_KEY, nil +} + +// DeleteKey deletes the subkey path of key k and its values. +func DeleteKey(k Key, path string) error { + return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path)) +} + +// A KeyInfo describes the statistics of a key. It is returned by Stat. +type KeyInfo struct { + SubKeyCount uint32 + MaxSubKeyLen uint32 // size of the key's subkey with the longest name, in Unicode characters, not including the terminating zero byte + ValueCount uint32 + MaxValueNameLen uint32 // size of the key's longest value name, in Unicode characters, not including the terminating zero byte + MaxValueLen uint32 // longest data component among the key's values, in bytes + lastWriteTime syscall.Filetime +} + +// ModTime returns the key's last write time. +func (ki *KeyInfo) ModTime() time.Time { + return time.Unix(0, ki.lastWriteTime.Nanoseconds()) +} + +// Stat retrieves information about the open key k. +func (k Key) Stat() (*KeyInfo, error) { + var ki KeyInfo + err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil, + &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount, + &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime) + if err != nil { + return nil, err + } + return &ki, nil +} diff --git a/vendor/golang.org/x/sys/windows/registry/mksyscall.go b/vendor/golang.org/x/sys/windows/registry/mksyscall.go new file mode 100644 index 000000000..ee74927d3 --- /dev/null +++ b/vendor/golang.org/x/sys/windows/registry/mksyscall.go @@ -0,0 +1,10 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build generate +// +build generate + +package registry + +//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go syscall.go diff --git a/vendor/golang.org/x/sys/windows/registry/syscall.go b/vendor/golang.org/x/sys/windows/registry/syscall.go new file mode 100644 index 000000000..417335123 --- /dev/null +++ b/vendor/golang.org/x/sys/windows/registry/syscall.go @@ -0,0 +1,33 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build windows +// +build windows + +package registry + +import "syscall" + +const ( + _REG_OPTION_NON_VOLATILE = 0 + + _REG_CREATED_NEW_KEY = 1 + _REG_OPENED_EXISTING_KEY = 2 + + _ERROR_NO_MORE_ITEMS syscall.Errno = 259 +) + +func LoadRegLoadMUIString() error { + return procRegLoadMUIStringW.Find() +} + +//sys regCreateKeyEx(key syscall.Handle, subkey *uint16, reserved uint32, class *uint16, options uint32, desired uint32, sa *syscall.SecurityAttributes, result *syscall.Handle, disposition *uint32) (regerrno error) = advapi32.RegCreateKeyExW +//sys regDeleteKey(key syscall.Handle, subkey *uint16) (regerrno error) = advapi32.RegDeleteKeyW +//sys regSetValueEx(key syscall.Handle, valueName *uint16, reserved uint32, vtype uint32, buf *byte, bufsize uint32) (regerrno error) = advapi32.RegSetValueExW +//sys regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegEnumValueW +//sys regDeleteValue(key syscall.Handle, name *uint16) (regerrno error) = advapi32.RegDeleteValueW +//sys regLoadMUIString(key syscall.Handle, name *uint16, buf *uint16, buflen uint32, buflenCopied *uint32, flags uint32, dir *uint16) (regerrno error) = advapi32.RegLoadMUIStringW +//sys regConnectRegistry(machinename *uint16, key syscall.Handle, result *syscall.Handle) (regerrno error) = advapi32.RegConnectRegistryW + +//sys expandEnvironmentStrings(src *uint16, dst *uint16, size uint32) (n uint32, err error) = kernel32.ExpandEnvironmentStringsW diff --git a/vendor/golang.org/x/sys/windows/registry/value.go b/vendor/golang.org/x/sys/windows/registry/value.go new file mode 100644 index 000000000..2789f6f18 --- /dev/null +++ b/vendor/golang.org/x/sys/windows/registry/value.go @@ -0,0 +1,387 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build windows +// +build windows + +package registry + +import ( + "errors" + "io" + "syscall" + "unicode/utf16" + "unsafe" +) + +const ( + // Registry value types. + NONE = 0 + SZ = 1 + EXPAND_SZ = 2 + BINARY = 3 + DWORD = 4 + DWORD_BIG_ENDIAN = 5 + LINK = 6 + MULTI_SZ = 7 + RESOURCE_LIST = 8 + FULL_RESOURCE_DESCRIPTOR = 9 + RESOURCE_REQUIREMENTS_LIST = 10 + QWORD = 11 +) + +var ( + // ErrShortBuffer is returned when the buffer was too short for the operation. + ErrShortBuffer = syscall.ERROR_MORE_DATA + + // ErrNotExist is returned when a registry key or value does not exist. + ErrNotExist = syscall.ERROR_FILE_NOT_FOUND + + // ErrUnexpectedType is returned by Get*Value when the value's type was unexpected. + ErrUnexpectedType = errors.New("unexpected key value type") +) + +// GetValue retrieves the type and data for the specified value associated +// with an open key k. It fills up buffer buf and returns the retrieved +// byte count n. If buf is too small to fit the stored value it returns +// ErrShortBuffer error along with the required buffer size n. +// If no buffer is provided, it returns true and actual buffer size n. +// If no buffer is provided, GetValue returns the value's type only. +// If the value does not exist, the error returned is ErrNotExist. +// +// GetValue is a low level function. If value's type is known, use the appropriate +// Get*Value function instead. +func (k Key) GetValue(name string, buf []byte) (n int, valtype uint32, err error) { + pname, err := syscall.UTF16PtrFromString(name) + if err != nil { + return 0, 0, err + } + var pbuf *byte + if len(buf) > 0 { + pbuf = (*byte)(unsafe.Pointer(&buf[0])) + } + l := uint32(len(buf)) + err = syscall.RegQueryValueEx(syscall.Handle(k), pname, nil, &valtype, pbuf, &l) + if err != nil { + return int(l), valtype, err + } + return int(l), valtype, nil +} + +func (k Key) getValue(name string, buf []byte) (data []byte, valtype uint32, err error) { + p, err := syscall.UTF16PtrFromString(name) + if err != nil { + return nil, 0, err + } + var t uint32 + n := uint32(len(buf)) + for { + err = syscall.RegQueryValueEx(syscall.Handle(k), p, nil, &t, (*byte)(unsafe.Pointer(&buf[0])), &n) + if err == nil { + return buf[:n], t, nil + } + if err != syscall.ERROR_MORE_DATA { + return nil, 0, err + } + if n <= uint32(len(buf)) { + return nil, 0, err + } + buf = make([]byte, n) + } +} + +// GetStringValue retrieves the string value for the specified +// value name associated with an open key k. It also returns the value's type. +// If value does not exist, GetStringValue returns ErrNotExist. +// If value is not SZ or EXPAND_SZ, it will return the correct value +// type and ErrUnexpectedType. +func (k Key) GetStringValue(name string) (val string, valtype uint32, err error) { + data, typ, err2 := k.getValue(name, make([]byte, 64)) + if err2 != nil { + return "", typ, err2 + } + switch typ { + case SZ, EXPAND_SZ: + default: + return "", typ, ErrUnexpectedType + } + if len(data) == 0 { + return "", typ, nil + } + u := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[: len(data)/2 : len(data)/2] + return syscall.UTF16ToString(u), typ, nil +} + +// GetMUIStringValue retrieves the localized string value for +// the specified value name associated with an open key k. +// If the value name doesn't exist or the localized string value +// can't be resolved, GetMUIStringValue returns ErrNotExist. +// GetMUIStringValue panics if the system doesn't support +// regLoadMUIString; use LoadRegLoadMUIString to check if +// regLoadMUIString is supported before calling this function. +func (k Key) GetMUIStringValue(name string) (string, error) { + pname, err := syscall.UTF16PtrFromString(name) + if err != nil { + return "", err + } + + buf := make([]uint16, 1024) + var buflen uint32 + var pdir *uint16 + + err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir) + if err == syscall.ERROR_FILE_NOT_FOUND { // Try fallback path + + // Try to resolve the string value using the system directory as + // a DLL search path; this assumes the string value is of the form + // @[path]\dllname,-strID but with no path given, e.g. @tzres.dll,-320. + + // This approach works with tzres.dll but may have to be revised + // in the future to allow callers to provide custom search paths. + + var s string + s, err = ExpandString("%SystemRoot%\\system32\\") + if err != nil { + return "", err + } + pdir, err = syscall.UTF16PtrFromString(s) + if err != nil { + return "", err + } + + err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir) + } + + for err == syscall.ERROR_MORE_DATA { // Grow buffer if needed + if buflen <= uint32(len(buf)) { + break // Buffer not growing, assume race; break + } + buf = make([]uint16, buflen) + err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir) + } + + if err != nil { + return "", err + } + + return syscall.UTF16ToString(buf), nil +} + +// ExpandString expands environment-variable strings and replaces +// them with the values defined for the current user. +// Use ExpandString to expand EXPAND_SZ strings. +func ExpandString(value string) (string, error) { + if value == "" { + return "", nil + } + p, err := syscall.UTF16PtrFromString(value) + if err != nil { + return "", err + } + r := make([]uint16, 100) + for { + n, err := expandEnvironmentStrings(p, &r[0], uint32(len(r))) + if err != nil { + return "", err + } + if n <= uint32(len(r)) { + return syscall.UTF16ToString(r[:n]), nil + } + r = make([]uint16, n) + } +} + +// GetStringsValue retrieves the []string value for the specified +// value name associated with an open key k. It also returns the value's type. +// If value does not exist, GetStringsValue returns ErrNotExist. +// If value is not MULTI_SZ, it will return the correct value +// type and ErrUnexpectedType. +func (k Key) GetStringsValue(name string) (val []string, valtype uint32, err error) { + data, typ, err2 := k.getValue(name, make([]byte, 64)) + if err2 != nil { + return nil, typ, err2 + } + if typ != MULTI_SZ { + return nil, typ, ErrUnexpectedType + } + if len(data) == 0 { + return nil, typ, nil + } + p := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[: len(data)/2 : len(data)/2] + if len(p) == 0 { + return nil, typ, nil + } + if p[len(p)-1] == 0 { + p = p[:len(p)-1] // remove terminating null + } + val = make([]string, 0, 5) + from := 0 + for i, c := range p { + if c == 0 { + val = append(val, string(utf16.Decode(p[from:i]))) + from = i + 1 + } + } + return val, typ, nil +} + +// GetIntegerValue retrieves the integer value for the specified +// value name associated with an open key k. It also returns the value's type. +// If value does not exist, GetIntegerValue returns ErrNotExist. +// If value is not DWORD or QWORD, it will return the correct value +// type and ErrUnexpectedType. +func (k Key) GetIntegerValue(name string) (val uint64, valtype uint32, err error) { + data, typ, err2 := k.getValue(name, make([]byte, 8)) + if err2 != nil { + return 0, typ, err2 + } + switch typ { + case DWORD: + if len(data) != 4 { + return 0, typ, errors.New("DWORD value is not 4 bytes long") + } + var val32 uint32 + copy((*[4]byte)(unsafe.Pointer(&val32))[:], data) + return uint64(val32), DWORD, nil + case QWORD: + if len(data) != 8 { + return 0, typ, errors.New("QWORD value is not 8 bytes long") + } + copy((*[8]byte)(unsafe.Pointer(&val))[:], data) + return val, QWORD, nil + default: + return 0, typ, ErrUnexpectedType + } +} + +// GetBinaryValue retrieves the binary value for the specified +// value name associated with an open key k. It also returns the value's type. +// If value does not exist, GetBinaryValue returns ErrNotExist. +// If value is not BINARY, it will return the correct value +// type and ErrUnexpectedType. +func (k Key) GetBinaryValue(name string) (val []byte, valtype uint32, err error) { + data, typ, err2 := k.getValue(name, make([]byte, 64)) + if err2 != nil { + return nil, typ, err2 + } + if typ != BINARY { + return nil, typ, ErrUnexpectedType + } + return data, typ, nil +} + +func (k Key) setValue(name string, valtype uint32, data []byte) error { + p, err := syscall.UTF16PtrFromString(name) + if err != nil { + return err + } + if len(data) == 0 { + return regSetValueEx(syscall.Handle(k), p, 0, valtype, nil, 0) + } + return regSetValueEx(syscall.Handle(k), p, 0, valtype, &data[0], uint32(len(data))) +} + +// SetDWordValue sets the data and type of a name value +// under key k to value and DWORD. +func (k Key) SetDWordValue(name string, value uint32) error { + return k.setValue(name, DWORD, (*[4]byte)(unsafe.Pointer(&value))[:]) +} + +// SetQWordValue sets the data and type of a name value +// under key k to value and QWORD. +func (k Key) SetQWordValue(name string, value uint64) error { + return k.setValue(name, QWORD, (*[8]byte)(unsafe.Pointer(&value))[:]) +} + +func (k Key) setStringValue(name string, valtype uint32, value string) error { + v, err := syscall.UTF16FromString(value) + if err != nil { + return err + } + buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[: len(v)*2 : len(v)*2] + return k.setValue(name, valtype, buf) +} + +// SetStringValue sets the data and type of a name value +// under key k to value and SZ. The value must not contain a zero byte. +func (k Key) SetStringValue(name, value string) error { + return k.setStringValue(name, SZ, value) +} + +// SetExpandStringValue sets the data and type of a name value +// under key k to value and EXPAND_SZ. The value must not contain a zero byte. +func (k Key) SetExpandStringValue(name, value string) error { + return k.setStringValue(name, EXPAND_SZ, value) +} + +// SetStringsValue sets the data and type of a name value +// under key k to value and MULTI_SZ. The value strings +// must not contain a zero byte. +func (k Key) SetStringsValue(name string, value []string) error { + ss := "" + for _, s := range value { + for i := 0; i < len(s); i++ { + if s[i] == 0 { + return errors.New("string cannot have 0 inside") + } + } + ss += s + "\x00" + } + v := utf16.Encode([]rune(ss + "\x00")) + buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[: len(v)*2 : len(v)*2] + return k.setValue(name, MULTI_SZ, buf) +} + +// SetBinaryValue sets the data and type of a name value +// under key k to value and BINARY. +func (k Key) SetBinaryValue(name string, value []byte) error { + return k.setValue(name, BINARY, value) +} + +// DeleteValue removes a named value from the key k. +func (k Key) DeleteValue(name string) error { + return regDeleteValue(syscall.Handle(k), syscall.StringToUTF16Ptr(name)) +} + +// ReadValueNames returns the value names of key k. +// The parameter n controls the number of returned names, +// analogous to the way os.File.Readdirnames works. +func (k Key) ReadValueNames(n int) ([]string, error) { + ki, err := k.Stat() + if err != nil { + return nil, err + } + names := make([]string, 0, ki.ValueCount) + buf := make([]uint16, ki.MaxValueNameLen+1) // extra room for terminating null character +loopItems: + for i := uint32(0); ; i++ { + if n > 0 { + if len(names) == n { + return names, nil + } + } + l := uint32(len(buf)) + for { + err := regEnumValue(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil) + if err == nil { + break + } + if err == syscall.ERROR_MORE_DATA { + // Double buffer size and try again. + l = uint32(2 * len(buf)) + buf = make([]uint16, l) + continue + } + if err == _ERROR_NO_MORE_ITEMS { + break loopItems + } + return names, err + } + names = append(names, syscall.UTF16ToString(buf[:l])) + } + if n > len(names) { + return names, io.EOF + } + return names, nil +} diff --git a/vendor/golang.org/x/sys/windows/registry/zsyscall_windows.go b/vendor/golang.org/x/sys/windows/registry/zsyscall_windows.go new file mode 100644 index 000000000..fc1835d8a --- /dev/null +++ b/vendor/golang.org/x/sys/windows/registry/zsyscall_windows.go @@ -0,0 +1,117 @@ +// Code generated by 'go generate'; DO NOT EDIT. + +package registry + +import ( + "syscall" + "unsafe" + + "golang.org/x/sys/windows" +) + +var _ unsafe.Pointer + +// Do the interface allocations only once for common +// Errno values. +const ( + errnoERROR_IO_PENDING = 997 +) + +var ( + errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) + errERROR_EINVAL error = syscall.EINVAL +) + +// errnoErr returns common boxed Errno values, to prevent +// allocations at runtime. +func errnoErr(e syscall.Errno) error { + switch e { + case 0: + return errERROR_EINVAL + case errnoERROR_IO_PENDING: + return errERROR_IO_PENDING + } + // TODO: add more here, after collecting data on the common + // error values see on Windows. (perhaps when running + // all.bat?) + return e +} + +var ( + modadvapi32 = windows.NewLazySystemDLL("advapi32.dll") + modkernel32 = windows.NewLazySystemDLL("kernel32.dll") + + procRegConnectRegistryW = modadvapi32.NewProc("RegConnectRegistryW") + procRegCreateKeyExW = modadvapi32.NewProc("RegCreateKeyExW") + procRegDeleteKeyW = modadvapi32.NewProc("RegDeleteKeyW") + procRegDeleteValueW = modadvapi32.NewProc("RegDeleteValueW") + procRegEnumValueW = modadvapi32.NewProc("RegEnumValueW") + procRegLoadMUIStringW = modadvapi32.NewProc("RegLoadMUIStringW") + procRegSetValueExW = modadvapi32.NewProc("RegSetValueExW") + procExpandEnvironmentStringsW = modkernel32.NewProc("ExpandEnvironmentStringsW") +) + +func regConnectRegistry(machinename *uint16, key syscall.Handle, result *syscall.Handle) (regerrno error) { + r0, _, _ := syscall.Syscall(procRegConnectRegistryW.Addr(), 3, uintptr(unsafe.Pointer(machinename)), uintptr(key), uintptr(unsafe.Pointer(result))) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regCreateKeyEx(key syscall.Handle, subkey *uint16, reserved uint32, class *uint16, options uint32, desired uint32, sa *syscall.SecurityAttributes, result *syscall.Handle, disposition *uint32) (regerrno error) { + r0, _, _ := syscall.Syscall9(procRegCreateKeyExW.Addr(), 9, uintptr(key), uintptr(unsafe.Pointer(subkey)), uintptr(reserved), uintptr(unsafe.Pointer(class)), uintptr(options), uintptr(desired), uintptr(unsafe.Pointer(sa)), uintptr(unsafe.Pointer(result)), uintptr(unsafe.Pointer(disposition))) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regDeleteKey(key syscall.Handle, subkey *uint16) (regerrno error) { + r0, _, _ := syscall.Syscall(procRegDeleteKeyW.Addr(), 2, uintptr(key), uintptr(unsafe.Pointer(subkey)), 0) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regDeleteValue(key syscall.Handle, name *uint16) (regerrno error) { + r0, _, _ := syscall.Syscall(procRegDeleteValueW.Addr(), 2, uintptr(key), uintptr(unsafe.Pointer(name)), 0) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) { + r0, _, _ := syscall.Syscall9(procRegEnumValueW.Addr(), 8, uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(valtype)), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(buflen)), 0) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regLoadMUIString(key syscall.Handle, name *uint16, buf *uint16, buflen uint32, buflenCopied *uint32, flags uint32, dir *uint16) (regerrno error) { + r0, _, _ := syscall.Syscall9(procRegLoadMUIStringW.Addr(), 7, uintptr(key), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(buf)), uintptr(buflen), uintptr(unsafe.Pointer(buflenCopied)), uintptr(flags), uintptr(unsafe.Pointer(dir)), 0, 0) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func regSetValueEx(key syscall.Handle, valueName *uint16, reserved uint32, vtype uint32, buf *byte, bufsize uint32) (regerrno error) { + r0, _, _ := syscall.Syscall6(procRegSetValueExW.Addr(), 6, uintptr(key), uintptr(unsafe.Pointer(valueName)), uintptr(reserved), uintptr(vtype), uintptr(unsafe.Pointer(buf)), uintptr(bufsize)) + if r0 != 0 { + regerrno = syscall.Errno(r0) + } + return +} + +func expandEnvironmentStrings(src *uint16, dst *uint16, size uint32) (n uint32, err error) { + r0, _, e1 := syscall.Syscall(procExpandEnvironmentStringsW.Addr(), 3, uintptr(unsafe.Pointer(src)), uintptr(unsafe.Pointer(dst)), uintptr(size)) + n = uint32(r0) + if n == 0 { + err = errnoErr(e1) + } + return +} diff --git a/vendor/modules.txt b/vendor/modules.txt index a3a9f17af..48d7797fa 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -938,9 +938,11 @@ github.com/satori/go.uuid # github.com/segmentio/ksuid v1.0.4 ## explicit; go 1.12 github.com/segmentio/ksuid -# github.com/shirou/gopsutil/v3 v3.22.6 +# github.com/shirou/gopsutil/v3 v3.22.7 ## explicit; go 1.15 github.com/shirou/gopsutil/v3/cpu +github.com/shirou/gopsutil/v3/disk +github.com/shirou/gopsutil/v3/host github.com/shirou/gopsutil/v3/internal/common github.com/shirou/gopsutil/v3/mem github.com/shirou/gopsutil/v3/net @@ -1226,6 +1228,7 @@ golang.org/x/sys/internal/unsafeheader golang.org/x/sys/plan9 golang.org/x/sys/unix golang.org/x/sys/windows +golang.org/x/sys/windows/registry # golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 ## explicit; go 1.17 golang.org/x/term