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