mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-29 13:49:21 +08:00
[freebsd][host]: fix Users() return start secs.
This commit is contained in:
parent
0e6ea68690
commit
2fd3f03f84
63
host/freebsd_headers/utxdb.h
Normal file
63
host/freebsd_headers/utxdb.h
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*-
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _UTXDB_H_
|
||||||
|
#define _UTXDB_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define _PATH_UTX_ACTIVE "/var/run/utx.active"
|
||||||
|
#define _PATH_UTX_LASTLOGIN "/var/log/utx.lastlogin"
|
||||||
|
#define _PATH_UTX_LOG "/var/log/utx.log"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Entries in struct futx are ordered by how often they are used. In
|
||||||
|
* utx.log only entries will be written until the last non-zero byte,
|
||||||
|
* which means we want to put the hostname at the end. Most primitive
|
||||||
|
* records only store a ut_type and ut_tv, which means we want to store
|
||||||
|
* those at the front.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct utmpx;
|
||||||
|
|
||||||
|
struct futx {
|
||||||
|
uint8_t fu_type;
|
||||||
|
uint64_t fu_tv;
|
||||||
|
char fu_id[8];
|
||||||
|
uint32_t fu_pid;
|
||||||
|
char fu_user[32];
|
||||||
|
char fu_line[16];
|
||||||
|
char fu_host[128];
|
||||||
|
} __packed;
|
||||||
|
|
||||||
|
void utx_to_futx(const struct utmpx *, struct futx *);
|
||||||
|
struct utmpx *futx_to_utx(const struct futx *);
|
||||||
|
|
||||||
|
#endif /* !_UTXDB_H_ */
|
@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -143,11 +144,11 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
|||||||
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
|
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
|
||||||
var u Utmpx
|
var u Utmpx
|
||||||
br := bytes.NewReader(b)
|
br := bytes.NewReader(b)
|
||||||
err := binary.Read(br, binary.LittleEndian, &u)
|
err := binary.Read(br, binary.BigEndian, &u)
|
||||||
if err != nil || u.Type != 4 {
|
if err != nil || u.Type != 4 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sec := (binary.LittleEndian.Uint32(u.Tv.Sec[:])) / 2 // TODO:
|
sec := math.Floor(float64(u.Tv) / 1000000)
|
||||||
user := UserStat{
|
user := UserStat{
|
||||||
User: common.IntToString(u.User[:]),
|
User: common.IntToString(u.User[:]),
|
||||||
Terminal: common.IntToString(u.Line[:]),
|
Terminal: common.IntToString(u.Line[:]),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Created by cgo -godefs - DO NOT EDIT
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
// cgo -godefs types_freebsd.go
|
// cgo -godefs types_freebsd.go
|
||||||
|
|
||||||
package host
|
package host
|
||||||
@ -9,7 +9,7 @@ const (
|
|||||||
sizeofInt = 0x4
|
sizeofInt = 0x4
|
||||||
sizeofLong = 0x8
|
sizeofLong = 0x8
|
||||||
sizeofLongLong = 0x8
|
sizeofLongLong = 0x8
|
||||||
sizeOfUtmpx = 197 // TODO: why should 197, not 0x118
|
sizeOfUtmpx = 0xc5
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -27,18 +27,11 @@ type Utmp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Utmpx struct {
|
type Utmpx struct {
|
||||||
Type int16
|
Type uint8
|
||||||
Tv Timeval
|
Tv uint64
|
||||||
Id [8]int8
|
Id [8]int8
|
||||||
Pid int32
|
Pid uint32
|
||||||
User [32]int8
|
User [32]int8
|
||||||
Line [16]int8
|
Line [16]int8
|
||||||
Host [125]int8
|
Host [128]int8
|
||||||
// Host [128]int8
|
|
||||||
// X__ut_spare [64]int8
|
|
||||||
}
|
|
||||||
|
|
||||||
type Timeval struct {
|
|
||||||
Sec [4]byte
|
|
||||||
Usec [3]byte
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ package host
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <utmpx.h>
|
#include <utmpx.h>
|
||||||
|
#include "freebsd_headers/utxdb.h"
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
sizeofPtr = sizeof(void*),
|
sizeofPtr = sizeof(void*),
|
||||||
@ -27,7 +28,7 @@ const (
|
|||||||
sizeofInt = C.sizeof_int
|
sizeofInt = C.sizeof_int
|
||||||
sizeofLong = C.sizeof_long
|
sizeofLong = C.sizeof_long
|
||||||
sizeofLongLong = C.sizeof_longlong
|
sizeofLongLong = C.sizeof_longlong
|
||||||
sizeOfUtmpx = C.sizeof_struct_utmpx
|
sizeOfUtmpx = C.sizeof_struct_futx
|
||||||
)
|
)
|
||||||
|
|
||||||
// Basic types
|
// Basic types
|
||||||
@ -39,6 +40,5 @@ type (
|
|||||||
_C_long_long C.longlong
|
_C_long_long C.longlong
|
||||||
)
|
)
|
||||||
|
|
||||||
type Utmp C.struct_utmp
|
type Utmp C.struct_utmp // for FreeBSD 9.0 compatibility
|
||||||
type Utmpx C.struct_utmpx
|
type Utmpx C.struct_futx
|
||||||
type Timeval C.struct_timeval
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user