shirou_gopsutil/host/host_darwin.go

134 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-12-23 05:54:41 +08:00
//go:build darwin
// +build darwin
2014-12-30 21:09:05 +08:00
package host
import (
"bytes"
2017-12-31 14:25:49 +08:00
"context"
"encoding/binary"
"errors"
"io"
"os"
"strings"
"unsafe"
2023-06-04 05:17:16 +08:00
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/process"
)
// from utmpx.h
const user_PROCESS = 7
2020-09-11 19:51:20 +08:00
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")
}
2020-09-11 19:51:20 +08:00
func numProcs(ctx context.Context) (uint64, error) {
procs, err := process.PidsWithContext(ctx)
if err != nil {
return 0, err
}
return uint64(len(procs)), nil
2017-12-31 14:25:49 +08:00
}
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
}
2017-02-22 21:46:23 +08:00
defer file.Close()
buf, err := io.ReadAll(file)
if err != nil {
return ret, err
}
2023-10-10 22:05:52 +08:00
// Skip macOS utmpx header part
buf = buf[604:]
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
}
2017-12-31 14:25:49 +08:00
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
}
2017-12-31 14:25:49 +08:00
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
2017-08-03 10:08:35 +08:00
return "", "", common.ErrNotImplementedError
}
2017-12-31 14:25:49 +08:00
func KernelVersionWithContext(ctx context.Context) (string, error) {
version, err := unix.Sysctl("kern.osrelease")
return strings.ToLower(version), err
}