What can the q1:who command do?
$ whoxxx : 0 yyyy-mm-dd hh:mm (: 0) xxx pts/0 yyyy-mm-dd hh:mm (: 0)
This is the result of the output on the CentOS7, who version is 8.22. Each row represents a user who has logged in, the first column is the user name, the second column is the terminal name, and the third column is the logon time.
The WHO command can be viewed in detail through the reading manual.
$ mans
How does the q2:who command work?
First, we'll read the online Help:
DESCRIPTION Print information about the users who is currently logged in. If FILE is not specified, use/var/run/utmp. /var/log/wtmp as FILE is common. If ARG1 ARG2 Given,-m presumed: ' Am I ' or ' Mom likes ' is usual.
Information about the logged-on user is generally described in/var/run/utmp or/var/log/wtmp.
The utmp file holds information about the users currently in the system. The wtmp file holds information about the user who has logged in to the system.
Now focus on the Utmp file, who obtains information by reading the file. Use the-k command to search online help based on keywords:
$ man-k utmp
Note the observation description, where a row contains information that may be useful:
UTMPX (5) -Login Records
Then use man to view online Help:
$ man 5 utmp
Browse the manual and find the file structure defined in the header file <utmp.h>, first go to the/usr/include/directory to find:
$ find/usr/include/utmp.h/usr/include/utmp.h
Then use the more or cat command to view the file:
$ more/usr/include/utmp.h
Browsing discovers that the file is not the header file that declares the original data structure:
/* Get system dependent values and data structures. * * #include <bits/utmp.h>
Then go to the next level of directory bits to view, here note in the C source file can not include<bits/utmp.h>, is still include<utmp.h>.
$ more/usr/include/bits/utmp.h
By browsing the code, get to the struct:
/* The structure describing an entry in the user accounting database. */struct utmp{Short int ut_type;/* type of login. */pid_t ut_pid;/* process ID of login process. */char ut_line[ut_linesize];/* devicename. */char ut_id[4];/* Inittab ID. */char ut_user[ut_namesize];/* Username. */char ut_host[ut_hostsize];/* Hostname for remote login. */struct Exit_status ut_exit;/* exit status of a process marked as dead_process. *//* the Ut_session and Ut_tv fields must is the same size when compiled 32-and 64-bit. This allows data files and the shared memory to be shared between 32-and 64-bit applications. */#ifdef __wordsize_time64_compat32 int32_t ut_session;/* session ID, used for windowing. */struct {int32_t tv_sec;/* Seconds. */int32_t tv_usec;/* microseconds. */} ut_tv;/* time entry was made. */#else Long int ut_session;/* session ID, used for windowing. */struct Timeval ut_tv;/* time entry was made. */#endif int32_t ut_addr_v6[4];/* InterNET address of remote host. */char __unused[20];/* Reserved for future use. */};
Observed variables can be obtained to the WHO used the member variable Ut_type (who used to filter blank and leave the current user), Ut_line (display device name, that is, the user's Terminal), Ut_user (username), ut_host (telnet user name, (: 0) display), Ut_time (Time).
So the WHO command actually opens the Utmp file, reads the record, displays the record, and then closes the utmp file.
Q3: How to write who?
According to the above analysis, the WHO command according to open files, read records, display records, close the file process work. Open the file, read with read, display printf, close close. Where printf does not have to be introduced. The other three are specific as follows:
Open Target opens a file header file #include <fcntl.h> function prototype int fd = open (char *name, int how) parameter name file name How o_rdonly, O_wronly, O_rdwr return value -1 encountered error int successfully returned
Linux practice-writing who commands