The Linux Programming Interface

(nextflipdebug5) #1

822 Chapter 40


The getutxent(), getutxid(), and getutxline() functions read a record from the utmp file
and return a pointer to a (statically allocated) utmpx structure.

The getutxent() function retrieves the next sequential record from the utmp file. The
getutxid() and getutxline() functions perform searches, starting from the current file
location, for a record matching the criteria specified in the utmpx structure pointed
to by the ut argument.
The getutxid() function searches the utmp file for a record based on the values
specified in the ut_type and ut_id fields of the ut argument:
z If the ut_type field is RUN_LVL, BOOT_TIME, NEW_TIME, or OLD_TIME, then getutxid() finds
the next record whose ut_type field matches the specified value. (Records of
these types don’t correspond to user logins.) This permits searches for records
of changes to the system time and run-level.
z If the ut_type field is one of the remaining valid values (INIT_PROCESS,
LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS), then getutxent() finds the next
record whose ut_type field matches any of these values and whose ut_id field
matches that specified in its ut argument. This permits scanning the file for
records corresponding to a particular terminal.
The getutxline() function searches forward for a record whose ut_type field is either
LOGIN_PROCESS or USER_PROCESS, and whose ut_line field matches that specified in the ut
argument. This is useful for finding records corresponding to user logins.
Both getutxid() and getutxline() return NULL if the search fails (i.e., end-of-file is
encountered without finding a matching record).
On some UNIX implementations, getutxline() and getutxid() treat the static area
used for returning the utmpx structure as a kind of cache. If they determine that the
record placed in this cache by a previous getutx*() call matches the criteria specified
in ut, then no file read is performed; the call simply returns the same record once more
(SUSv3 permits this behavior). Therefore, to prevent the same record from being
repeatedly returned when calling getutxline() and getutxid() within a loop, we must
zero out this static data structure, using code such as the following:

struct utmpx *res = NULL;

/* Other code omitted */

if (res != NULL) /* If 'res' was set via a previous call */
memset(res, 0, sizeof(struct utmpx));
res = getutxline(&ut);

#include <utmpx.h>

struct utmpx *getutxent(void);
struct utmpx *getutxid(const struct utmpx *ut);
struct utmpx *getutxline(const struct utmpx *ut);
All return a pointer to a statically allocated utmpx structure,
or NULL if no matching record or EOF was encountered
Free download pdf