GETUTXENT(3) | MidnightBSD Library Functions Manual | GETUTXENT(3) |
endutxent
,
getutxent
, getutxid
,
getutxline
, getutxuser
,
pututxline
, setutxdb
,
setutxent
— user accounting
database functions
Standard C Library (libc, -lc)
#include
<utmpx.h>
void
endutxent
(void);
struct utmpx *
getutxent
(void);
struct utmpx *
getutxid
(const
struct utmpx *id);
struct utmpx *
getutxline
(const
struct utmpx *line);
struct utmpx *
getutxuser
(const
char *user);
struct utmpx *
pututxline
(const
struct utmpx *utmpx);
int
setutxdb
(int
type, const char
*file);
void
setutxent
(void);
These functions operate on the user accounting database which stores records of various system activities, such as user login and logouts, but also system startups and shutdowns and modifications to the system's clock. The system stores these records in three databases, each having a different purpose:
Each entry in these databases is defined by the structure
utmpx found in the include file
<utmpx.h>
:
struct utmpx { short ut_type; /* Type of entry. */ struct timeval ut_tv; /* Time entry was made. */ char ut_id[]; /* Record identifier. */ pid_t ut_pid; /* Process ID. */ char ut_user[]; /* User login name. */ char ut_line[]; /* Device name. */ char ut_host[]; /* Remote hostname. */ };
The ut_type field indicates the type of the log entry, which can have one of the following values:
EMPTY
BOOT_TIME
SHUTDOWN_TIME
OLD_TIME
NEW_TIME
USER_PROCESS
INIT_PROCESS
LOGIN_PROCESS
DEAD_PROCESS
Entries of type INIT_PROCESS
and
LOGIN_PROCESS
are not processed by this
implementation.
Other fields inside the structure are:
EMPTY
.USER_PROCESS
,
INIT_PROCESS
,
LOGIN_PROCESS
and
DEAD_PROCESS
.USER_PROCESS
,
INIT_PROCESS
,
LOGIN_PROCESS
and
DEAD_PROCESS
.USER_PROCESS
and INIT_PROCESS
. For
INIT_PROCESS
entries this entry typically contains
the name of the login process.USER_PROCESS
and
LOGIN_PROCESS
.USER_PROCESS
.This implementation guarantees all inapplicable fields are discarded. The ut_user, ut_line and ut_host fields of the structure returned by the library functions are also guaranteed to be null-terminated in this implementation.
The
getutxent
()
function can be used to read the next entry from the user accounting
database.
The
getutxid
()
function searches for the next entry in the database of which the behaviour
is based on the ut_type field of
id. If ut_type has a value of
BOOT_TIME
, SHUTDOWN_TIME
,
OLD_TIME
or NEW_TIME
, it
will return the next entry whose ut_type has an equal
value. If ut_type has a value of
USER_PROCESS
, INIT_PROCESS
,
LOGIN_PROCESS
or
DEAD_PROCESS
, it will return the next entry whose
ut_type has one of the previously mentioned values and
whose ut_id is equal.
The
getutxline
()
function searches for the next entry in the database whose
ut_type has a value of
USER_PROCESS
or
LOGIN_PROCESS
and whose
ut_line is equal to the same field in
line.
The
getutxuser
()
function searches for the next entry in the database whose
ut_type has a value of
USER_PROCESS
and whose ut_user
is equal to user.
The previously mentioned functions will
automatically try to open the user accounting database if not already done
so. The
setutxdb
()
and setutxent
() functions allow the database to be
opened manually, causing the offset within the user accounting database to
be rewound. The
endutxent
()
function closes the database.
The
setutxent
()
database always opens the active sessions database. The
setutxdb
() function opens the database identified by
type, whose value is either
UTXDB_ACTIVE
,
UTXDB_LASTLOGIN
or
UTXDB_LOG
. It will open a custom file with filename
file instead of the system-default if
file is not null. Care must be taken that when using a
custom filename, type still has to match with the
actual format, since each database may use its own file format.
The
pututxline
()
function writes record utmpx to the system-default
user accounting databases. The value of ut_type
determines which databases are modified.
Entries of type SHUTDOWN_TIME
,
OLD_TIME
and NEW_TIME
will
only be written to /var/log/utx.log.
Entries of type USER_PROCESS
will also be
written to /var/run/utx.active and
/var/log/utx.lastlogin.
Entries of type DEAD_PROCESS
will only be
written to /var/log/utx.log and
/var/run/utx.active if a corresponding
USER_PROCESS
, INIT_PROCESS
or LOGIN_PROCESS
entry whose
ut_id is equal has been found in the latter.
In addition, entries of type BOOT_TIME
and
SHUTDOWN_TIME
will cause all existing entries in
/var/run/utx.active to be discarded.
All entries whose type has not been mentioned
previously, are discarded by this implementation of
pututxline
().
This implementation also ignores the value of
ut_tv.
The getutxent
(),
getutxid
(), getutxline
(),
and getutxuser
() functions return a pointer to an
utmpx structure that matches the mentioned constraints
on success or NULL
when reaching the end-of-file or
when an error occurs.
The pututxline
() function returns a
pointer to an utmpx structure containing a copy of the
structure written to disk upon success. It returns
NULL
when the provided utmpx
is invalid, or ut_type has a value of
DEAD_PROCESS
and an entry with an identifier with a
value equal to the field ut_id was not found; the
global variable errno is set to indicate the
error.
The setutxdb
() function returns 0 if the
user accounting database was opened successfully. Otherwise, -1 is returned
and the global variable errno is set to indicate the
error.
In addition to the error conditions described in
open(2),
fdopen(3),
fopen(3),
fseek(3), the
pututxline
() function can generate the following
errors:
ESRCH
]EINVAL
]setutxdb
() function can generate the following errors:
last(1), write(1), getpid(2), gettimeofday(2), tty(4), ac(8), newsyslog(8), utx(8)
The endutxent
(),
getutxent
(), getutxid
(),
getutxline
() and setutxent
()
functions are expected to conform to IEEE Std 1003.1-2008
(“POSIX.1”).
The pututxline
() function deviates from
the standard by writing its records to multiple database files, depending on
its ut_type. This prevents the need for special
utility functions to update the other databases, such as the
updlastlogx
() and updwtmpx
()
functions which are available in other implementations. It also tries to
replace DEAD_PROCESS
entries in the active sessions
database when storing USER_PROCESS
entries and no
entry with the same value for ut_id has been found.
The standard always requires a new entry to be allocated, which could cause
an unbounded growth of the database.
The getutxuser
() and
setutxdb
() functions, the
ut_host field of the utmpx
structure and SHUTDOWN_TIME
are extensions.
These functions appeared in FreeBSD 9.0.
They replaced the <utmp.h>
interface.
Ed Schouten <ed@FreeBSD.org>
October 27, 2011 | midnightbsd-3.1 |