C language Getutent () function: Get account login data from utmp file
header file:
To define a function:
struct utmp *getutent (void);
Function Description: Getutent () is used to read a login data from the Utmp file (/var/run/utmp), which is returned in utmp structure. The first user data is obtained on the first call, and the next data is returned every time it is invoked, and Null is returned until no data is already in place.
The UTMP structure is defined as follows:
struct utmp
{short
int ut_type;//Logon type
pid_t ut_pid;//login process pid
char ut_line[ut_linesize];//Login Appliance Name , omitting the "/dev/"
char ut_id[4];//inittab ID
char ut_user[ut_namesize];//Login account
char ut_host[ut_hostsize]; /login account of the remote host name
struxt exit_status ut_exit;//When the type is dead_process the end state of the process is
long int ut_session;//SESSIOC ID
struct Timeval ut_tv; Time record
int32_t ut_addr_v6[4]//remote host network address
char __unused[20];//reserved Unused
};
There are several types of Ut_type:
- EMPTY: This is an empty record.
- RUN_LVL: Recording System run-level changes
- Boot_time: Recording System boot time
- New_time: Records the time after the system time changes
- Old_tine: Record the time when the system time is changed.
- Init_process: Record a process derived from init.
- Login_process: Log login process.
- User_process: Document the general process.
- Dead_process: Record the end of the process.
- ACCOUNTING: not currently in use.
Exit_status Structure Definition:
struct Exit_status
{short
int e_termination;//Process end state short
int e_exit;//Process exit status
};
Refer to Gettimeofday () for the TIMEVAL structure definition.
The related constants are defined as follows:
- Ut_linesize 32
- Ut_namesize 32
- Ut_hostsize 256
Return value: Returns the UTMP structure data, if returned null indicates no data, or an error occurred.
Additional note: Getutent () opens the Utmp file on the first call, and Endutent () can be used to close the Utmp file after reading the data.
Example
#include <utmp.h>
Main ()
{
struct utmp *u;
while ((U = getutent ()))
{
if (U->ut_type = = user_process)
printf ("%d%s%s,%s \ n", U->ut_type, U->ut _user, U->ut_line, u->ut_host);
Endutent ();
}
Perform
Indicates that three root accounts are logged in/dev/pts/0,/DEV/PTS/1,/DEV/PTS/2
7 Root pts/0
7 Root PTS/1
7 Root PTS/2
C language Setutent () function: Read login data from utmp file from scratch
header file:
To define a function:
Function Description: Setutent () is used to refer the read-write address of getutent () to the beginning of utmp file.
C language Endutent () function: Close file (Close Utmp file)
header file:
To define a function:
Function Description: Endutent () is used to close the Utmp file that was opened by Getutent.
Example: Please refer to getutent ().