Merge pull request #712 from shirou/feature/fix_freebsd_utmpx_time

fix FreeBSD Users() time
pull/716/head
shirou 6 years ago committed by GitHub
commit 6c106d3395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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"
"encoding/binary"
"io/ioutil"
"math"
"os"
"runtime"
"strings"
@ -143,11 +144,11 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
var u Utmpx
br := bytes.NewReader(b)
err := binary.Read(br, binary.LittleEndian, &u)
err := binary.Read(br, binary.BigEndian, &u)
if err != nil || u.Type != 4 {
continue
}
sec := (binary.LittleEndian.Uint32(u.Tv.Sec[:])) / 2 // TODO:
sec := math.Floor(float64(u.Tv) / 1000000)
user := UserStat{
User: common.IntToString(u.User[:]),
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
package host
@ -9,7 +9,7 @@ const (
sizeofInt = 0x4
sizeofLong = 0x4
sizeofLongLong = 0x8
sizeOfUtmpx = 197 // TODO why should 197
sizeOfUtmpx = 0xc5
)
type (
@ -27,17 +27,11 @@ type Utmp struct {
}
type Utmpx struct {
Type int16
Tv Timeval
Type uint8
Tv uint64
Id [8]int8
Pid int32
Pid uint32
User [32]int8
Line [16]int8
Host [125]int8
// X__ut_spare [64]int8
}
type Timeval struct {
Sec [4]byte
Usec [3]byte
Host [128]int8
}

@ -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
package host
@ -9,7 +9,7 @@ const (
sizeofInt = 0x4
sizeofLong = 0x8
sizeofLongLong = 0x8
sizeOfUtmpx = 197 // TODO: why should 197, not 0x118
sizeOfUtmpx = 0xc5
)
type (
@ -27,18 +27,11 @@ type Utmp struct {
}
type Utmpx struct {
Type int16
Tv Timeval
Type uint8
Tv uint64
Id [8]int8
Pid int32
Pid uint32
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
Host [128]int8
}

@ -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
package host
@ -9,7 +9,7 @@ const (
sizeofInt = 0x4
sizeofLong = 0x8
sizeofLongLong = 0x8
sizeOfUtmpx = 197 // TODO: why should 197, not 0x118
sizeOfUtmpx = 0xc5
)
type (
@ -27,18 +27,11 @@ type Utmp struct {
}
type Utmpx struct {
Type int16
Tv Timeval
Type uint8
Tv uint64
Id [8]int8
Pid int32
Pid uint32
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
Host [128]int8
}

@ -11,6 +11,7 @@ package host
#include <sys/types.h>
#include <sys/time.h>
#include <utmpx.h>
#include "freebsd_headers/utxdb.h"
enum {
sizeofPtr = sizeof(void*),
@ -27,7 +28,7 @@ const (
sizeofInt = C.sizeof_int
sizeofLong = C.sizeof_long
sizeofLongLong = C.sizeof_longlong
sizeOfUtmpx = C.sizeof_struct_utmpx
sizeOfUtmpx = C.sizeof_struct_futx
)
// Basic types
@ -39,6 +40,5 @@ type (
_C_long_long C.longlong
)
type Utmp C.struct_utmp
type Utmpx C.struct_utmpx
type Timeval C.struct_timeval
type Utmp C.struct_utmp // for FreeBSD 9.0 compatibility
type Utmpx C.struct_futx

Loading…
Cancel
Save