Skip to content

Commit

Permalink
host[freebsd]: change to use utmpx for 9.0 or later.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Aug 14, 2015
1 parent 1223e28 commit abb4086
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
1 change: 0 additions & 1 deletion docker/docker_notlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package docker
import (
"encoding/json"

"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/cpu"
)

Expand Down
60 changes: 51 additions & 9 deletions host/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ func BootTime() (int64, error) {
}

func Users() ([]UserStat, error) {
utmpfile := "/var/run/utmp"
var ret []UserStat
utmpfile := "/var/run/utx.active"
if !common.PathExists(utmpfile) {
utmpfile = "/var/run/utmp" // before 9.0
return getUsersFromUtmp(utmpfile)
}

var ret []UserStat
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
Expand All @@ -90,24 +94,25 @@ func Users() ([]UserStat, error) {
return ret, err
}

u := Utmp{}
entrySize := int(unsafe.Sizeof(u))
u := Utmpx{}
entrySize := int(unsafe.Sizeof(u)) - 3
entrySize = 197 // TODO: why should 197
count := len(buf) / entrySize

for i := 0; i < count; i++ {
b := buf[i*entrySize : i*entrySize+entrySize]

var u Utmp
var u Utmpx
br := bytes.NewReader(b)
err := binary.Read(br, binary.LittleEndian, &u)
if err != nil || u.Time == 0 {
if err != nil || u.Type != 4 {
continue
}
sec := (binary.LittleEndian.Uint32(u.Tv.Sec[:])) / 2 // TODO:
user := UserStat{
User: common.IntToString(u.Name[:]),
User: common.IntToString(u.User[:]),
Terminal: common.IntToString(u.Line[:]),
Host: common.IntToString(u.Host[:]),
Started: int(u.Time),
Started: int(sec),
}

ret = append(ret, user)
Expand Down Expand Up @@ -141,3 +146,40 @@ func GetVirtualization() (string, string, error) {

return system, role, nil
}

// before 9.0
func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
var ret []UserStat
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
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
}
19 changes: 16 additions & 3 deletions host/host_freebsd_amd64.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// +build freebsd
// +build amd64
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs host/types_freebsd.go
// cgo -godefs types_freebsd.go

package host

Expand All @@ -26,3 +24,18 @@ type Utmp struct {
Host [16]int8
Time int32
}
type Utmpx struct {
Type int16
Tv Timeval
Id [8]int8
Pid int32
User [32]int8
Line [16]int8
Host [125]int8
// Host [128]int8
// X__ut_spare [64]int8
}
type Timeval struct {
Sec [4]byte
Usec [3]byte
}
5 changes: 4 additions & 1 deletion host/types_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ package host
/*
#define KERNEL
#include <sys/types.h>
#include <utmp.h>
#include <sys/time.h>
#include <utmpx.h>
enum {
sizeofPtr = sizeof(void*),
Expand Down Expand Up @@ -38,3 +39,5 @@ type (
)

type Utmp C.struct_utmp
type Utmpx C.struct_utmpx
type Timeval C.struct_timeval

0 comments on commit abb4086

Please sign in to comment.