1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/host/host_freebsd.go

149 lines
3.0 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2021-12-22 21:54:41 +00:00
//go:build freebsd
2014-04-22 12:04:16 +09:00
2014-12-30 22:09:05 +09:00
package host
2014-04-22 12:04:16 +09:00
import (
2014-04-22 17:45:39 +09:00
"bytes"
2017-12-31 15:25:49 +09:00
"context"
2014-04-22 17:45:39 +09:00
"encoding/binary"
"io"
"math"
2014-04-22 12:04:16 +09:00
"os"
2014-04-22 12:43:31 +09:00
"strings"
2014-04-22 17:45:39 +09:00
"unsafe"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/process"
2014-04-22 12:04:16 +09:00
)
2014-12-31 00:30:55 +09:00
const (
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
UTLineSize = 8
UTHostSize = 16
)
2020-09-11 14:51:20 +03:00
func HostIDWithContext(ctx context.Context) (string, error) {
uuid, err := unix.Sysctl("kern.hostuuid")
if err != nil {
return "", err
}
2020-09-11 14:51:20 +03:00
return strings.ToLower(uuid), err
2014-04-22 12:04:16 +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) {
utmpfile := "/var/run/utx.active"
if !common.PathExists(utmpfile) {
utmpfile = "/var/run/utmp" // before 9.0
return getUsersFromUtmp(utmpfile)
}
2014-04-22 17:45:39 +09:00
var ret []UserStat
2014-04-22 17:45:39 +09:00
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
2017-02-22 08:46:23 -05:00
defer file.Close()
2014-04-22 17:45:39 +09:00
buf, err := io.ReadAll(file)
2014-04-22 17:45:39 +09:00
if err != nil {
return ret, err
}
2016-04-23 23:10:23 +09:00
entrySize := sizeOfUtmpx
2014-04-22 17:45:39 +09:00
count := len(buf) / entrySize
for i := 0; i < count; i++ {
2016-04-23 23:10:23 +09:00
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
var u Utmpx
2014-04-22 17:45:39 +09:00
br := bytes.NewReader(b)
err := binary.Read(br, binary.BigEndian, &u)
if err != nil || u.Type != 4 {
2014-04-22 17:45:39 +09:00
continue
}
sec := math.Floor(float64(u.Tv) / 1000000)
2014-04-22 17:45:39 +09:00
user := UserStat{
User: common.IntToString(u.User[:]),
2014-12-31 00:30:55 +09:00
Terminal: common.IntToString(u.Line[:]),
Host: common.IntToString(u.Host[:]),
Started: int(sec),
2014-04-22 17:45:39 +09:00
}
2014-12-31 00:30:55 +09:00
2014-04-22 17:45:39 +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) {
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
}
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
}
// before 9.0
func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
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()
buf, err := io.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
}
2017-03-19 02:05:46 +01:00
2017-12-31 15:25:49 +09:00
func KernelVersionWithContext(ctx context.Context) (string, error) {
2020-09-11 14:51:20 +03:00
_, _, version, err := PlatformInformationWithContext(ctx)
2017-08-03 11:08:35 +09:00
return version, err
}