2021-12-23 05:54:41 +08:00
|
|
|
//go:build darwin
|
2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package host
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2014-08-08 22:09:28 +08:00
|
|
|
"encoding/binary"
|
2021-04-13 16:31:23 +08:00
|
|
|
"errors"
|
2023-09-09 01:05:14 +08:00
|
|
|
"io"
|
2014-08-08 22:09:28 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2023-06-04 05:17:16 +08:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
2016-02-02 22:23:34 +08:00
|
|
|
// from utmpx.h
|
2021-11-06 17:53:56 +08:00
|
|
|
const user_PROCESS = 7
|
2016-02-02 22:23:34 +08:00
|
|
|
|
2020-09-11 19:51:20 +08:00
|
|
|
func HostIDWithContext(ctx context.Context) (string, error) {
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
|
2021-04-13 16:31:23 +08:00
|
|
|
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")
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2014-08-08 22:09:28 +08:00
|
|
|
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()
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2023-09-09 01:05:14 +08:00
|
|
|
buf, err := io.ReadAll(file)
|
2014-08-08 22:09:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2023-10-10 22:05:52 +08:00
|
|
|
// Skip macOS utmpx header part
|
|
|
|
buf = buf[604:]
|
|
|
|
|
2015-02-14 22:04:17 +08:00
|
|
|
u := Utmpx{}
|
2014-08-08 22:09:28 +08:00
|
|
|
entrySize := int(unsafe.Sizeof(u))
|
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*entrySize : i*entrySize+entrySize]
|
|
|
|
|
2015-02-14 22:04:17 +08:00
|
|
|
var u Utmpx
|
2014-08-08 22:09:28 +08:00
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-06 17:53:56 +08:00
|
|
|
if u.Type != user_PROCESS {
|
2015-02-14 22:04:17 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
user := UserStat{
|
2015-02-14 22:04:17 +08:00
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(u.Tv.Sec),
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2017-12-31 14:25:49 +08:00
|
|
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
2014-08-08 22:09:28 +08:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
2016-09-12 07:07:12 +08:00
|
|
|
pver := ""
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2018-12-29 20:38:21 +08:00
|
|
|
p, err := unix.Sysctl("kern.ostype")
|
2014-08-08 22:09:28 +08:00
|
|
|
if err == nil {
|
2018-12-29 20:38:21 +08:00
|
|
|
platform = strings.ToLower(p)
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "sw_vers", "-productVersion")
|
2016-09-12 07:07:12 +08:00
|
|
|
if err == nil {
|
|
|
|
pver = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
2019-06-01 01:13:06 +08:00
|
|
|
// check if the macos server version file exists
|
2019-06-01 01:19:04 +08:00
|
|
|
_, err = os.Stat("/System/Library/CoreServices/ServerVersion.plist")
|
2019-06-01 01:13:06 +08:00
|
|
|
|
|
|
|
// server file doesn't exist
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
family = "Standalone Workstation"
|
|
|
|
} else {
|
|
|
|
family = "Server"
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:17:56 +08:00
|
|
|
return platform, family, pver, nil
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2017-12-31 14:25:49 +08:00
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
2018-12-29 20:38:21 +08:00
|
|
|
version, err := unix.Sysctl("kern.osrelease")
|
|
|
|
return strings.ToLower(version), err
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|