Node:Manipulating the Database, Next:XPG Functions, Up:User Accounting Database
These functions and the corresponding data structures are declared in
the header file utmp.h
.
struct exit_status | Data Type |
The exit_status data structure is used to hold information about
the exit status of processes marked as DEAD_PROCESS in the user
accounting database.
|
struct utmp | Data Type |
The utmp data structure is used to hold information about entries
in the user accounting database. On the GNU system it has the following
members:
|
The ut_type
, ut_pid
, ut_id
, ut_tv
, and
ut_host
fields are not available on all systems. Portable
applications therefore should be prepared for these situations. To help
doing this the utmp.h
header provides macros
_HAVE_UT_TYPE
, _HAVE_UT_PID
, _HAVE_UT_ID
,
_HAVE_UT_TV
, and _HAVE_UT_HOST
if the respective field is
available. The programmer can handle the situations by using
#ifdef
in the program code.
The following macros are defined for use as values for the
ut_type
member of the utmp
structure. The values are
integer constants.
EMPTY
RUN_LVL
BOOT_TIME
OLD_TIME
NEW_TIME
INIT_PROCESS
LOGIN_PROCESS
USER_PROCESS
DEAD_PROCESS
ACCOUNTING
The size of the ut_line
, ut_id
, ut_user
and
ut_host
arrays can be found using the sizeof
operator.
Many older systems have, instead of an ut_tv
member, an
ut_time
member, usually of type time_t
, for representing
the time associated with the entry. Therefore, for backwards
compatibility only, utmp.h
defines ut_time
as an alias for
ut_tv.tv_sec
.
void setutent (void) | Function |
This function opens the user accounting database to begin scanning it.
You can then call getutent , getutid or getutline to
read entries and pututline to write entries.
If the database is already open, it resets the input to the beginning of the database. |
struct utmp * getutent (void) | Function |
The getutent function reads the next entry from the user
accounting database. It returns a pointer to the entry, which is
statically allocated and may be overwritten by subsequent calls to
getutent . You must copy the contents of the structure if you
wish to save the information or you can use the getutent_r
function which stores the data in a user-provided buffer.
A null pointer is returned in case no further entry is available. |
void endutent (void) | Function |
This function closes the user accounting database. |
struct utmp * getutid (const struct utmp *id) | Function |
This function searches forward from the current point in the database
for an entry that matches id. If the ut_type member of the
id structure is one of RUN_LVL , BOOT_TIME ,
OLD_TIME or NEW_TIME the entries match if the
ut_type members are identical. If the ut_type member of
the id structure is INIT_PROCESS , LOGIN_PROCESS ,
USER_PROCESS or DEAD_PROCESS , the entries match if the
ut_type member of the entry read from the database is one of
these four, and the ut_id members match. However if the
ut_id member of either the id structure or the entry read
from the database is empty it checks if the ut_line members match
instead. If a matching entry is found, getutid returns a pointer
to the entry, which is statically allocated, and may be overwritten by a
subsequent call to getutent , getutid or getutline .
You must copy the contents of the structure if you wish to save the
information.
A null pointer is returned in case the end of the database is reached without a match. The |
struct utmp * getutline (const struct utmp *line) | Function |
This function searches forward from the current point in the database
until it finds an entry whose ut_type value is
LOGIN_PROCESS or USER_PROCESS , and whose ut_line
member matches the ut_line member of the line structure.
If it finds such an entry, it returns a pointer to the entry which is
statically allocated, and may be overwritten by a subsequent call to
getutent , getutid or getutline . You must copy the
contents of the structure if you wish to save the information.
A null pointer is returned in case the end of the database is reached without a match. The |
struct utmp * pututline (const struct utmp *utmp) | Function |
The pututline function inserts the entry *utmp at
the appropriate place in the user accounting database. If it finds that
it is not already at the correct place in the database, it uses
getutid to search for the position to insert the entry, however
this will not modify the static structure returned by getutent ,
getutid and getutline . If this search fails, the entry
is appended to the database.
The
|
All the get*
functions mentioned before store the information
they return in a static buffer. This can be a problem in multi-threaded
programs since the data returned for the request is overwritten by the
return value data in another thread. Therefore the GNU C Library
provides as extensions three more functions which return the data in a
user-provided buffer.
int getutent_r (struct utmp *buffer, struct utmp **result) | Function |
The getutent_r is equivalent to the getutent function. It
returns the next entry from the database. But instead of storing the
information in a static buffer it stores it in the buffer pointed to by
the parameter buffer.
If the call was successful, the function returns This function is a GNU extension. |
int getutid_r (const struct utmp *id, struct utmp *buffer, struct utmp **result) | Function |
This function retrieves just like getutid the next entry matching
the information stored in id. But the result is stored in the
buffer pointed to by the parameter buffer.
If successful the function returns This function is a GNU extension. |
int getutline_r (const struct utmp *line, struct utmp *buffer, struct utmp **result) | Function |
This function retrieves just like getutline the next entry
matching the information stored in line. But the result is stored
in the buffer pointed to by the parameter buffer.
If successful the function returns This function is a GNU extension. |
In addition to the user accounting database, most systems keep a number
of similar databases. For example most systems keep a log file with all
previous logins (usually in /etc/wtmp
or /var/log/wtmp
).
For specifying which database to examine, the following function should be used.
int utmpname (const char *file) | Function |
The utmpname function changes the name of the database to be
examined to file, and closes any previously opened database. By
default getutent , getutid , getutline and
pututline read from and write to the user accounting database.
The following macros are defined for use as the file argument:
The |
Specially for maintaining log-like databases the GNU C Library provides the following function:
void updwtmp (const char *wtmp_file, const struct utmp *utmp) | Function |
The updwtmp function appends the entry *utmp to the
database specified by wtmp_file. For possible values for the
wtmp_file argument see the utmpname function.
|
Portability Note: Although many operating systems provide a
subset of these functions, they are not standardized. There are often
subtle differences in the return types, and there are considerable
differences between the various definitions of struct utmp
. When
programming for the GNU system, it is probably best to stick
with the functions described in this section. If however, you want your
program to be portable, consider using the XPG functions described in
XPG Functions, or take a look at the BSD compatible functions in
Logging In and Out.