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