The correlation function usage of operation Utmp file in C language _c language

Source: Internet
Author: User

C language Getutent () function: Get account login data from utmp file
header file:

#include <utmp.h>

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:

#include <utmp.h>

To define a function:

void setutent (void);

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:

#include <utmp.h>

To define a function:

void endutent (void);

Function Description: Endutent () is used to close the Utmp file that was opened by Getutent.

Example: Please refer to getutent ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.