Package utmp
The utmp package provides functionality to read and manipulate utmp entries in Go. The package includes structs and methods to work with the utmp file, which contains information about user login sessions and terminated processes.
Types
exit_status struct
type exit_status struct {
e_termination int16
e_exit int16
}
The exit_status struct represents the exit status of a terminated process. It has two fields: e_termination and e_exit, which store the termination and exit values, respectively.
Utmp struct
type Utmp struct {
Type int
Pid int
Device string
Id string
Username string
HostName string
Exit exit_status
Session int
Seconds int64
MicroSecs int64
Login time.Time
AddrV6 [4]int
}
The Utmp struct represents a utmp entry. It contains various fields to store information such as the entry type, process ID, device, user ID, username, hostname, exit status, session ID, timestamp, login time, and IPv6 address.
Users struct
type Users struct {
Users []Utmp
}
The Users struct holds a collection of Utmp entries. It has a single field Users, which is a slice of Utmp structs.
LiveUsers struct
type LiveUsers struct {
Users []*Utmp
}
The LiveUsers struct represents currently logged in users. It contains a field Users, which is a slice of pointers to Utmp structs.
Functions and Methods
Read function
func Read() *Users
The Read function reads the utmp entries and returns a pointer to a Users struct. It iterates over the entries using the C function getutent() and converts the C struct to Go struct (Utmp). The function populates the Users struct with the converted entries and returns a pointer to it.
Live method
func (u *Users) Live() *LiveUsers
The Live method filters the Users array and returns a pointer to a LiveUsers struct. It iterates over the Users array and checks if the entry type is 7 (representing a normal process). If the entry type is 7, it appends a pointer to the entry (Utmp) to the LiveUsers struct. The method returns a pointer to the filtered LiveUsers struct.
Please note that the code includes C imports and uses unsafe operations for converting the ut_addr_v6 field. Caution should be exercised when working with these parts of the code.
It is recommended to refer to the man utmp manual for a deeper understanding of the utmp entries and their fields.