1// +build openbsd
2
3package host
4
5import (
6	"bytes"
7	"context"
8	"encoding/binary"
9	"io/ioutil"
10	"os"
11	"strings"
12	"unsafe"
13
14	"github.com/shirou/gopsutil/v3/internal/common"
15	"github.com/shirou/gopsutil/v3/process"
16	"golang.org/x/sys/unix"
17)
18
19const (
20	UTNameSize = 32 /* see MAXLOGNAME in <sys/param.h> */
21	UTLineSize = 8
22	UTHostSize = 16
23)
24
25func HostIDWithContext(ctx context.Context) (string, error) {
26	return "", common.ErrNotImplementedError
27}
28
29func numProcs(ctx context.Context) (uint64, error) {
30	procs, err := process.PidsWithContext(ctx)
31	if err != nil {
32		return 0, err
33	}
34	return uint64(len(procs)), nil
35}
36
37func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
38	platform := ""
39	family := ""
40	version := ""
41
42	p, err := unix.Sysctl("kern.ostype")
43	if err == nil {
44		platform = strings.ToLower(p)
45	}
46	v, err := unix.Sysctl("kern.osrelease")
47	if err == nil {
48		version = strings.ToLower(v)
49	}
50
51	return platform, family, version, nil
52}
53
54func VirtualizationWithContext(ctx context.Context) (string, string, error) {
55	return "", "", common.ErrNotImplementedError
56}
57
58func UsersWithContext(ctx context.Context) ([]UserStat, error) {
59	var ret []UserStat
60	utmpfile := "/var/run/utmp"
61	file, err := os.Open(utmpfile)
62	if err != nil {
63		return ret, err
64	}
65	defer file.Close()
66
67	buf, err := ioutil.ReadAll(file)
68	if err != nil {
69		return ret, err
70	}
71
72	u := Utmp{}
73	entrySize := int(unsafe.Sizeof(u))
74	count := len(buf) / entrySize
75
76	for i := 0; i < count; i++ {
77		b := buf[i*entrySize : i*entrySize+entrySize]
78		var u Utmp
79		br := bytes.NewReader(b)
80		err := binary.Read(br, binary.LittleEndian, &u)
81		if err != nil || u.Time == 0 || u.Name[0] == 0 {
82			continue
83		}
84		user := UserStat{
85			User:     common.IntToString(u.Name[:]),
86			Terminal: common.IntToString(u.Line[:]),
87			Host:     common.IntToString(u.Host[:]),
88			Started:  int(u.Time),
89		}
90
91		ret = append(ret, user)
92	}
93
94	return ret, nil
95}
96
97func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
98	return []TemperatureStat{}, common.ErrNotImplementedError
99}
100
101func KernelVersionWithContext(ctx context.Context) (string, error) {
102	_, _, version, err := PlatformInformationWithContext(ctx)
103	return version, err
104}
105